Use the comments before syntax line as file comments.

pull/6181/head
yang-g 9 years ago
parent 8282b755a2
commit 2e08941a37
  1. 2
      src/compiler/config.h
  2. 6
      src/compiler/cpp_generator.cc
  3. 27
      src/compiler/generator_helpers.h
  4. 11
      src/proto/grpc/testing/compiler_test.proto
  5. 35
      test/cpp/codegen/compiler_test_golden

@ -42,6 +42,7 @@
#include <google/protobuf/descriptor.pb.h>
#define GRPC_CUSTOM_DESCRIPTOR ::google::protobuf::Descriptor
#define GRPC_CUSTOM_FILEDESCRIPTOR ::google::protobuf::FileDescriptor
#define GRPC_CUSTOM_FILEDESCRIPTORPROTO ::google::protobuf::FileDescriptorProto
#define GRPC_CUSTOM_METHODDESCRIPTOR ::google::protobuf::MethodDescriptor
#define GRPC_CUSTOM_SERVICEDESCRIPTOR ::google::protobuf::ServiceDescriptor
#define GRPC_CUSTOM_SOURCELOCATION ::google::protobuf::SourceLocation
@ -73,6 +74,7 @@ namespace grpc {
namespace protobuf {
typedef GRPC_CUSTOM_DESCRIPTOR Descriptor;
typedef GRPC_CUSTOM_FILEDESCRIPTOR FileDescriptor;
typedef GRPC_CUSTOM_FILEDESCRIPTORPROTO FileDescriptorProto;
typedef GRPC_CUSTOM_METHODDESCRIPTOR MethodDescriptor;
typedef GRPC_CUSTOM_SERVICEDESCRIPTOR ServiceDescriptor;
typedef GRPC_CUSTOM_SOURCELOCATION SourceLocation;

@ -101,7 +101,11 @@ grpc::string GetHeaderPrologue(File *file, const Parameters &params) {
printer->Print(vars,
"// If you make any local change, they will be lost.\n");
printer->Print(vars, "// source: $filename$\n");
printer->Print(file->GetLeadingComments().c_str());
grpc::string leading_comments = file->GetLeadingComments();
if (!leading_comments.empty()) {
printer->Print(vars, "// Original file comments:\n");
printer->Print(leading_comments.c_str());
}
printer->Print(vars, "#ifndef GRPC_$filename_identifier$__INCLUDED\n");
printer->Print(vars, "#define GRPC_$filename_identifier$__INCLUDED\n");
printer->Print(vars, "\n");

@ -207,6 +207,33 @@ inline void GetComment(const DescriptorType *desc, CommentType type,
}
}
// For file level leading and detached leading comments, we return comments
// above syntax line. Return nothing for trailing comments.
template <>
inline void GetComment(const grpc::protobuf::FileDescriptor *desc,
CommentType type, std::vector<grpc::string> *out) {
if (type == COMMENTTYPE_TRAILING) {
return;
}
grpc::protobuf::SourceLocation location;
std::vector<int> path;
path.push_back(grpc::protobuf::FileDescriptorProto::kSyntaxFieldNumber);
if (!desc->GetSourceLocation(path, &location)) {
return;
}
if (type == COMMENTTYPE_LEADING) {
Split(location.leading_comments, '\n', out);
} else if (type == COMMENTTYPE_LEADING_DETACHED) {
for (unsigned int i = 0; i < location.leading_detached_comments.size();
i++) {
Split(location.leading_detached_comments[i], '\n', out);
out->push_back("");
}
} else {
abort();
}
}
} // namespace grpc_generator
#endif // GRPC_INTERNAL_COMPILER_GENERATOR_HELPERS_H

@ -31,12 +31,12 @@
// File detached comment 2
// Syntax leading comment 1
// File leading comment 1
syntax = "proto3";
// File detached comment 3
// Ignored detached comment
// Package leading comment 1
// Ignored package leading comment
package grpc.testing;
message Request {
@ -60,6 +60,7 @@ service ServiceA {
rpc MethodA2(stream Request) returns (Response);
// MethodA2 trailing comment 1
}
// Ignored ServiceA trailing comment 1
// ServiceB leading comment 1
service ServiceB {
@ -69,6 +70,6 @@ service ServiceB {
rpc MethodB1(Request) returns (Response);
// MethodB1 trailing comment 1
}
// ServiceB trailing comment 2
// Ignored ServiceB trailing comment 2
// File trailing comment
// Ignored file trailing comment

@ -1,6 +1,41 @@
// Generated by the gRPC protobuf plugin.
// If you make any local change, they will be lost.
// source: src/proto/grpc/testing/compiler_test.proto
// Original file comments:
// Copyright 2016, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// File detached comment 1
//
// File detached comment 2
//
// File leading comment 1
#ifndef GRPC_src_2fproto_2fgrpc_2ftesting_2fcompiler_5ftest_2eproto__INCLUDED
#define GRPC_src_2fproto_2fgrpc_2ftesting_2fcompiler_5ftest_2eproto__INCLUDED

Loading…
Cancel
Save