From 2e08941a37450b42dd21e8755e07091ea444f545 Mon Sep 17 00:00:00 2001 From: yang-g Date: Fri, 15 Apr 2016 10:46:41 -0700 Subject: [PATCH] Use the comments before syntax line as file comments. --- src/compiler/config.h | 2 ++ src/compiler/cpp_generator.cc | 6 +++- src/compiler/generator_helpers.h | 27 +++++++++++++++++ src/proto/grpc/testing/compiler_test.proto | 11 +++---- test/cpp/codegen/compiler_test_golden | 35 ++++++++++++++++++++++ 5 files changed, 75 insertions(+), 6 deletions(-) diff --git a/src/compiler/config.h b/src/compiler/config.h index a826dd9744d..a534b119d2f 100644 --- a/src/compiler/config.h +++ b/src/compiler/config.h @@ -42,6 +42,7 @@ #include #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; diff --git a/src/compiler/cpp_generator.cc b/src/compiler/cpp_generator.cc index 97455cdbfd0..b03b7fd8569 100644 --- a/src/compiler/cpp_generator.cc +++ b/src/compiler/cpp_generator.cc @@ -101,7 +101,11 @@ grpc::string GetHeaderPrologue(File *file, const Parameters ¶ms) { 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"); diff --git a/src/compiler/generator_helpers.h b/src/compiler/generator_helpers.h index 16f0ca32dfc..9ba73568571 100644 --- a/src/compiler/generator_helpers.h +++ b/src/compiler/generator_helpers.h @@ -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 *out) { + if (type == COMMENTTYPE_TRAILING) { + return; + } + grpc::protobuf::SourceLocation location; + std::vector 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 diff --git a/src/proto/grpc/testing/compiler_test.proto b/src/proto/grpc/testing/compiler_test.proto index 22674974eda..085e8ae59f7 100644 --- a/src/proto/grpc/testing/compiler_test.proto +++ b/src/proto/grpc/testing/compiler_test.proto @@ -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 diff --git a/test/cpp/codegen/compiler_test_golden b/test/cpp/codegen/compiler_test_golden index 9a2303902b3..ef3d1aaa510 100644 --- a/test/cpp/codegen/compiler_test_golden +++ b/test/cpp/codegen/compiler_test_golden @@ -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