From f7c511957f527d1bcbab4af6020497452b65d688 Mon Sep 17 00:00:00 2001 From: Paul Harris Date: Wed, 17 Aug 2022 01:48:41 +0800 Subject: Audit and fixups for GCC and Clang (#7212) Added (for compiler versions that support it): -Wmissing-declarations -Wzero-as-null-pointer-constant Then, fixes to problems identified by the extra warnings Tested only on GCC 9.4.0 Adjusted the CPP code generator to output nullptr where appropriate, to satisfy -Wzero-as-null-pointer-constant Added a lot of 'static' declarations in front of functions, to satisfy -Wmissing-declarations, and wrap static function defs in anonymous namespaces. There are advantages to both anonymous namespaces and static, it seems that marking a function as static will not publish the name in the symbol table at all, thus giving the linker less work to do. --- grpc/src/compiler/cpp_generator.cc | 64 +++++++++++++++++++++++------------ grpc/src/compiler/go_generator.cc | 18 +++++----- grpc/src/compiler/java_generator.cc | 10 +++--- grpc/src/compiler/python_generator.cc | 4 ++- grpc/src/compiler/swift_generator.cc | 10 +++--- grpc/src/compiler/ts_generator.cc | 45 +++++++++++++----------- 6 files changed, 93 insertions(+), 58 deletions(-) (limited to 'grpc') diff --git a/grpc/src/compiler/cpp_generator.cc b/grpc/src/compiler/cpp_generator.cc index 16e3373b..fd635f2f 100644 --- a/grpc/src/compiler/cpp_generator.cc +++ b/grpc/src/compiler/cpp_generator.cc @@ -8,23 +8,24 @@ namespace grpc_cpp_generator { namespace { -grpc::string service_header_ext() { return ".grpc.fb.h"; } +static grpc::string service_header_ext() { return ".grpc.fb.h"; } -template grpc::string as_string(T x) { +template +static grpc::string as_string(T x) { std::ostringstream out; out << x; return out.str(); } -inline bool ClientOnlyStreaming(const grpc_generator::Method *method) { +static inline bool ClientOnlyStreaming(const grpc_generator::Method *method) { return method->ClientStreaming() && !method->ServerStreaming(); } -inline bool ServerOnlyStreaming(const grpc_generator::Method *method) { +static inline bool ServerOnlyStreaming(const grpc_generator::Method *method) { return !method->ClientStreaming() && method->ServerStreaming(); } -grpc::string FilenameIdentifier(const grpc::string &filename) { +static grpc::string FilenameIdentifier(const grpc::string &filename) { grpc::string result; for (unsigned i = 0; i < filename.size(); i++) { char c = filename[i]; @@ -39,11 +40,11 @@ grpc::string FilenameIdentifier(const grpc::string &filename) { } return result; } -} // namespace -template T *array_end(T (&array)[N]) { return array + N; } +template +static T *array_end(T (&array)[N]) { return array + N; } -void PrintIncludes(grpc_generator::Printer *printer, +static void PrintIncludes(grpc_generator::Printer *printer, const std::vector &headers, const Parameters ¶ms) { std::map vars; @@ -63,6 +64,8 @@ void PrintIncludes(grpc_generator::Printer *printer, } } +} // namespace + grpc::string GetHeaderPrologue(grpc_generator::File *file, const Parameters ¶ms) { grpc::string output; @@ -137,7 +140,10 @@ grpc::string GetHeaderIncludes(grpc_generator::File *file, return output; } -void PrintHeaderClientMethodInterfaces( + +namespace { + +static void PrintHeaderClientMethodInterfaces( grpc_generator::Printer *printer, const grpc_generator::Method *method, std::map *vars, bool is_public) { (*vars)["Method"] = method->name(); @@ -351,7 +357,9 @@ void PrintHeaderClientMethodInterfaces( } } -void PrintHeaderClientMethod(grpc_generator::Printer *printer, + + +static void PrintHeaderClientMethod(grpc_generator::Printer *printer, const grpc_generator::Method *method, std::map *vars, bool is_public) { @@ -554,7 +562,7 @@ void PrintHeaderClientMethod(grpc_generator::Printer *printer, } } -void PrintHeaderClientMethodData(grpc_generator::Printer *printer, +static void PrintHeaderClientMethodData(grpc_generator::Printer *printer, const grpc_generator::Method *method, std::map *vars) { (*vars)["Method"] = method->name(); @@ -562,7 +570,7 @@ void PrintHeaderClientMethodData(grpc_generator::Printer *printer, "const ::grpc::internal::RpcMethod rpcmethod_$Method$_;\n"); } -void PrintHeaderServerMethodSync(grpc_generator::Printer *printer, +static void PrintHeaderServerMethodSync(grpc_generator::Printer *printer, const grpc_generator::Method *method, std::map *vars) { (*vars)["Method"] = method->name(); @@ -596,7 +604,7 @@ void PrintHeaderServerMethodSync(grpc_generator::Printer *printer, printer->Print(method->GetTrailingComments("//").c_str()); } -void PrintHeaderServerMethodAsync(grpc_generator::Printer *printer, +static void PrintHeaderServerMethodAsync(grpc_generator::Printer *printer, const grpc_generator::Method *method, std::map *vars) { (*vars)["Method"] = method->name(); @@ -712,7 +720,7 @@ void PrintHeaderServerMethodAsync(grpc_generator::Printer *printer, printer->Print(*vars, "};\n"); } -void PrintHeaderServerMethodStreamedUnary( +static void PrintHeaderServerMethodStreamedUnary( grpc_generator::Printer *printer, const grpc_generator::Method *method, std::map *vars) { (*vars)["Method"] = method->name(); @@ -763,7 +771,7 @@ void PrintHeaderServerMethodStreamedUnary( } } -void PrintHeaderServerMethodSplitStreaming( +static void PrintHeaderServerMethodSplitStreaming( grpc_generator::Printer *printer, const grpc_generator::Method *method, std::map *vars) { (*vars)["Method"] = method->name(); @@ -816,7 +824,7 @@ void PrintHeaderServerMethodSplitStreaming( } } -void PrintHeaderServerMethodGeneric( +static void PrintHeaderServerMethodGeneric( grpc_generator::Printer *printer, const grpc_generator::Method *method, std::map *vars) { (*vars)["Method"] = method->name(); @@ -887,7 +895,7 @@ void PrintHeaderServerMethodGeneric( printer->Print(*vars, "};\n"); } -void PrintHeaderService(grpc_generator::Printer *printer, +static void PrintHeaderService(grpc_generator::Printer *printer, const grpc_generator::Service *service, std::map *vars) { (*vars)["Service"] = service->name(); @@ -1058,6 +1066,8 @@ void PrintHeaderService(grpc_generator::Printer *printer, printer->Print(service->GetTrailingComments("//").c_str()); } +} // namespace + grpc::string GetHeaderServices(grpc_generator::File *file, const Parameters ¶ms) { grpc::string output; @@ -1176,7 +1186,10 @@ grpc::string GetSourceIncludes(grpc_generator::File *file, return output; } -void PrintSourceClientMethod(grpc_generator::Printer *printer, + +namespace { + +static void PrintSourceClientMethod(grpc_generator::Printer *printer, const grpc_generator::Method *method, std::map *vars) { (*vars)["Method"] = method->name(); @@ -1320,7 +1333,7 @@ void PrintSourceClientMethod(grpc_generator::Printer *printer, } } -void PrintSourceServerMethod(grpc_generator::Printer *printer, +static void PrintSourceServerMethod(grpc_generator::Printer *printer, const grpc_generator::Method *method, std::map *vars) { (*vars)["Method"] = method->name(); @@ -1369,7 +1382,7 @@ void PrintSourceServerMethod(grpc_generator::Printer *printer, } } -void PrintSourceService(grpc_generator::Printer *printer, +static void PrintSourceService(grpc_generator::Printer *printer, const grpc_generator::Service *service, std::map *vars) { (*vars)["Service"] = service->name(); @@ -1486,6 +1499,8 @@ void PrintSourceService(grpc_generator::Printer *printer, } } +} // namespace + grpc::string GetSourceServices(grpc_generator::File *file, const Parameters ¶ms) { grpc::string output; @@ -1588,7 +1603,10 @@ grpc::string GetMockIncludes(grpc_generator::File *file, return output; } -void PrintMockClientMethods(grpc_generator::Printer *printer, + +namespace { + +static void PrintMockClientMethods(grpc_generator::Printer *printer, const grpc_generator::Method *method, std::map *vars) { (*vars)["Method"] = method->name(); @@ -1680,7 +1698,7 @@ void PrintMockClientMethods(grpc_generator::Printer *printer, } } -void PrintMockService(grpc_generator::Printer *printer, +static void PrintMockService(grpc_generator::Printer *printer, const grpc_generator::Service *service, std::map *vars) { (*vars)["Service"] = service->name(); @@ -1696,6 +1714,8 @@ void PrintMockService(grpc_generator::Printer *printer, printer->Print("};\n"); } +} // namespace + grpc::string GetMockServices(grpc_generator::File *file, const Parameters ¶ms) { grpc::string output; diff --git a/grpc/src/compiler/go_generator.cc b/grpc/src/compiler/go_generator.cc index 9ba2e65f..ad4694b1 100644 --- a/grpc/src/compiler/go_generator.cc +++ b/grpc/src/compiler/go_generator.cc @@ -19,22 +19,23 @@ inline bool ServerOnlyStreaming(const grpc_generator::Method *method) { } namespace grpc_go_generator { +namespace { // Returns string with first letter to lowerCase -grpc::string unexportName(grpc::string s) { +static grpc::string unexportName(grpc::string s) { if (s.empty()) return s; s[0] = static_cast(std::tolower(s[0])); return s; } // Returns string with first letter to uppercase -grpc::string exportName(grpc::string s) { +static grpc::string exportName(grpc::string s) { if (s.empty()) return s; s[0] = static_cast(std::toupper(s[0])); return s; } -void GenerateError(grpc_generator::Printer *printer, +static void GenerateError(grpc_generator::Printer *printer, std::map vars, const bool multiple_return = true) { printer->Print(vars, "if $Error_Check$ {\n"); @@ -46,7 +47,7 @@ void GenerateError(grpc_generator::Printer *printer, } // Generates imports for the service -void GenerateImports(grpc_generator::File *file, +static void GenerateImports(grpc_generator::File *file, grpc_generator::Printer *printer, std::map vars) { vars["filename"] = file->filename(); @@ -66,7 +67,7 @@ void GenerateImports(grpc_generator::File *file, } // Generates Server method signature source -void GenerateServerMethodSignature(const grpc_generator::Method *method, +static void GenerateServerMethodSignature(const grpc_generator::Method *method, grpc_generator::Printer *printer, std::map vars) { vars["Method"] = exportName(method->name()); @@ -86,7 +87,7 @@ void GenerateServerMethodSignature(const grpc_generator::Method *method, } } -void GenerateServerMethod(const grpc_generator::Method *method, +static void GenerateServerMethod(const grpc_generator::Method *method, grpc_generator::Printer *printer, std::map vars) { vars["Method"] = exportName(method->name()); @@ -204,7 +205,7 @@ void GenerateServerMethod(const grpc_generator::Method *method, } // Generates Client method signature source -void GenerateClientMethodSignature(const grpc_generator::Method *method, +static void GenerateClientMethodSignature(const grpc_generator::Method *method, grpc_generator::Printer *printer, std::map vars) { vars["Method"] = exportName(method->name()); @@ -225,7 +226,7 @@ void GenerateClientMethodSignature(const grpc_generator::Method *method, } // Generates Client method source -void GenerateClientMethod(const grpc_generator::Method *method, +static void GenerateClientMethod(const grpc_generator::Method *method, grpc_generator::Printer *printer, std::map vars) { printer->Print(vars, "func (c *$ServiceUnexported$Client) "); @@ -480,6 +481,7 @@ void GenerateService(const grpc_generator::Service *service, printer->Outdent(); printer->Print("}\n"); } +} // namespace // Returns source for the service grpc::string GenerateServiceSource(grpc_generator::File *file, diff --git a/grpc/src/compiler/java_generator.cc b/grpc/src/compiler/java_generator.cc index 98f8788d..bfe2b111 100644 --- a/grpc/src/compiler/java_generator.cc +++ b/grpc/src/compiler/java_generator.cc @@ -44,8 +44,9 @@ typedef grpc_generator::Method MethodDescriptor; namespace grpc_java_generator { typedef std::string string; +namespace { // Generates imports for the service -void GenerateImports(grpc_generator::File *file, +static void GenerateImports(grpc_generator::File *file, grpc_generator::Printer *printer, VARS &vars) { vars["filename"] = file->filename(); printer->Print(vars, @@ -287,7 +288,7 @@ static void GrpcWriteServiceDocComment(Printer *printer, VARS &vars, printer->Print(" */\n"); } -void GrpcWriteMethodDocComment(Printer *printer, VARS &vars, +static void GrpcWriteMethodDocComment(Printer *printer, VARS &vars, const MethodDescriptor *method) { printer->Print("/**\n"); std::vector lines = GrpcGetDocLinesForDescriptor(method); @@ -1029,7 +1030,7 @@ static void PrintService(Printer *p, VARS &vars, p->Print("}\n"); } -void PrintStaticImports(Printer *p) { +static void PrintStaticImports(Printer *p) { p->Print( "import java.nio.ByteBuffer;\n" "import static " @@ -1062,7 +1063,7 @@ void PrintStaticImports(Printer *p) { "io.grpc.stub.ServerCalls.asyncUnimplementedUnaryCall;\n\n"); } -void GenerateService(const grpc_generator::Service *service, +static void GenerateService(const grpc_generator::Service *service, grpc_generator::Printer *printer, VARS &vars, bool disable_version) { // All non-generated classes must be referred by fully qualified names to @@ -1097,6 +1098,7 @@ void GenerateService(const grpc_generator::Service *service, PrintService(printer, vars, service, disable_version); } +} // namespace grpc::string GenerateServiceSource( grpc_generator::File *file, const grpc_generator::Service *service, diff --git a/grpc/src/compiler/python_generator.cc b/grpc/src/compiler/python_generator.cc index 8108db45..d5f69e20 100644 --- a/grpc/src/compiler/python_generator.cc +++ b/grpc/src/compiler/python_generator.cc @@ -23,8 +23,9 @@ #include "src/compiler/python_generator.h" namespace grpc_python_generator { +namespace { -grpc::string GenerateMethodType(const grpc_generator::Method *method) { +static grpc::string GenerateMethodType(const grpc_generator::Method *method) { if (method->NoStreaming()) return "unary_unary"; @@ -131,6 +132,7 @@ void GenerateRegister(const grpc_generator::Service *service, printer->Outdent(); printer->Print("\n"); } +} // namespace grpc::string Generate(grpc_generator::File *file, const grpc_generator::Service *service) { diff --git a/grpc/src/compiler/swift_generator.cc b/grpc/src/compiler/swift_generator.cc index 403a803e..b0a96d86 100644 --- a/grpc/src/compiler/swift_generator.cc +++ b/grpc/src/compiler/swift_generator.cc @@ -28,8 +28,9 @@ #include "src/compiler/swift_generator.h" namespace grpc_swift_generator { +namespace { -std::string WrapInNameSpace(const std::vector &components, +static std::string WrapInNameSpace(const std::vector &components, const grpc::string &name) { std::string qualified_name; for (auto it = components.begin(); it != components.end(); ++it) @@ -37,14 +38,14 @@ std::string WrapInNameSpace(const std::vector &components, return qualified_name + name; } -grpc::string GenerateMessage(const std::vector &components, +static grpc::string GenerateMessage(const std::vector &components, const grpc::string &name) { return "Message<" + WrapInNameSpace(components, name) + ">"; } // MARK: - Client -void GenerateClientFuncName(const grpc_generator::Method *method, +static void GenerateClientFuncName(const grpc_generator::Method *method, grpc_generator::Printer *printer, std::map *dictonary) { auto vars = *dictonary; @@ -82,7 +83,7 @@ void GenerateClientFuncName(const grpc_generator::Method *method, " ) -> BidirectionalStreamingCall<$Input$, $Output$>"); } -void GenerateClientFuncBody(const grpc_generator::Method *method, +static void GenerateClientFuncBody(const grpc_generator::Method *method, grpc_generator::Printer *printer, std::map *dictonary) { auto vars = *dictonary; @@ -372,6 +373,7 @@ void GenerateServerProtocol(const grpc_generator::Service *service, } printer->Print("}"); } +} // namespace grpc::string Generate(grpc_generator::File *file, const grpc_generator::Service *service) { diff --git a/grpc/src/compiler/ts_generator.cc b/grpc/src/compiler/ts_generator.cc index 3c3daf0d..ff362b77 100644 --- a/grpc/src/compiler/ts_generator.cc +++ b/grpc/src/compiler/ts_generator.cc @@ -30,8 +30,9 @@ #include "src/compiler/schema_interface.h" namespace grpc_ts_generator { +namespace { -grpc::string GenerateNamespace(const std::vector ns, +static grpc::string GenerateNamespace(const std::vector ns, const std::string filename, const bool include_separator) { grpc::string path = ""; @@ -55,7 +56,7 @@ grpc::string GenerateNamespace(const std::vector ns, // MARK: - Shared code -void GenerateImports(const grpc_generator::Service *service, +static void GenerateImports(const grpc_generator::Service *service, grpc_generator::Printer *printer, std::map *dictonary, const bool grpc_var_import) { @@ -104,7 +105,7 @@ void GenerateImports(const grpc_generator::Service *service, // MARK: - Generate Main GRPC Code -void GetStreamType(grpc_generator::Printer *printer, +static void GetStreamType(grpc_generator::Printer *printer, const grpc_generator::Method *method, std::map *dictonary) { auto vars = *dictonary; @@ -116,7 +117,7 @@ void GetStreamType(grpc_generator::Printer *printer, printer->Print(vars, "responseStream: $ServerStreaming$,\n"); } -void GenerateSerializeMethod(grpc_generator::Printer *printer, +static void GenerateSerializeMethod(grpc_generator::Printer *printer, std::map *dictonary) { auto vars = *dictonary; printer->Print(vars, "function serialize_$Type$(buffer_args) {\n"); @@ -132,7 +133,7 @@ void GenerateSerializeMethod(grpc_generator::Printer *printer, printer->Print("}\n\n"); } -void GenerateDeserializeMethod( +static void GenerateDeserializeMethod( grpc_generator::Printer *printer, std::map *dictonary) { auto vars = *dictonary; @@ -145,7 +146,7 @@ void GenerateDeserializeMethod( printer->Print("}\n\n"); } -void GenerateMethods(const grpc_generator::Service *service, +static void GenerateMethods(const grpc_generator::Service *service, grpc_generator::Printer *printer, std::map *dictonary) { auto vars = *dictonary; @@ -177,7 +178,7 @@ void GenerateMethods(const grpc_generator::Service *service, } } -void GenerateService(const grpc_generator::Service *service, +static void GenerateService(const grpc_generator::Service *service, grpc_generator::Printer *printer, std::map *dictonary) { auto vars = *dictonary; @@ -212,6 +213,8 @@ void GenerateService(const grpc_generator::Service *service, "grpc.makeGenericClientConstructor($NAME$);"); } +} // namespace + grpc::string Generate(grpc_generator::File *file, const grpc_generator::Service *service, const grpc::string &filename) { @@ -233,9 +236,11 @@ grpc::string Generate(grpc_generator::File *file, return output; } +namespace { + // MARK: - Generate Interface -void FillInterface(grpc_generator::Printer *printer, +static void FillInterface(grpc_generator::Printer *printer, std::map *dictonary) { auto vars = *dictonary; printer->Print(vars, @@ -253,7 +258,7 @@ void FillInterface(grpc_generator::Printer *printer, printer->Print("}\n"); } -void GenerateInterfaces(const grpc_generator::Service *service, +static void GenerateInterfaces(const grpc_generator::Service *service, grpc_generator::Printer *printer, std::map *dictonary) { auto vars = *dictonary; @@ -275,7 +280,7 @@ void GenerateInterfaces(const grpc_generator::Service *service, } } -void GenerateExportedInterface( +static void GenerateExportedInterface( const grpc_generator::Service *service, grpc_generator::Printer *printer, std::map *dictonary) { auto vars = *dictonary; @@ -319,7 +324,7 @@ void GenerateExportedInterface( printer->Print("}\n"); } -void GenerateMainInterface(const grpc_generator::Service *service, +static void GenerateMainInterface(const grpc_generator::Service *service, grpc_generator::Printer *printer, std::map *dictonary) { auto vars = *dictonary; @@ -344,11 +349,11 @@ void GenerateMainInterface(const grpc_generator::Service *service, GenerateExportedInterface(service, printer, &vars); } -grpc::string GenerateMetaData() { return "metadata: grpc.Metadata"; } +static grpc::string GenerateMetaData() { return "metadata: grpc.Metadata"; } -grpc::string GenerateOptions() { return "options: Partial"; } +static grpc::string GenerateOptions() { return "options: Partial"; } -void GenerateUnaryClientInterface( +static void GenerateUnaryClientInterface( grpc_generator::Printer *printer, std::map *dictonary) { auto vars = *dictonary; @@ -363,7 +368,7 @@ void GenerateUnaryClientInterface( printer->Print(vars, (main + meta_data + options + callback).c_str()); } -void GenerateClientWriteStreamInterface( +static void GenerateClientWriteStreamInterface( grpc_generator::Printer *printer, std::map *dictonary) { auto vars = *dictonary; @@ -380,7 +385,7 @@ void GenerateClientWriteStreamInterface( printer->Print(vars, (main + meta_data + options + callback).c_str()); } -void GenerateClientReadableStreamInterface( +static void GenerateClientReadableStreamInterface( grpc_generator::Printer *printer, std::map *dictonary) { auto vars = *dictonary; @@ -392,7 +397,7 @@ void GenerateClientReadableStreamInterface( printer->Print(vars, (main + options + end_function).c_str()); } -void GenerateDepluxStreamInterface( +static void GenerateDepluxStreamInterface( grpc_generator::Printer *printer, std::map *dictonary) { auto vars = *dictonary; @@ -408,7 +413,7 @@ void GenerateDepluxStreamInterface( .c_str()); } -void GenerateClientInterface(const grpc_generator::Service *service, +static void GenerateClientInterface(const grpc_generator::Service *service, grpc_generator::Printer *printer, std::map *dictonary) { auto vars = *dictonary; @@ -446,7 +451,7 @@ void GenerateClientInterface(const grpc_generator::Service *service, printer->Print("}\n"); } -void GenerateClientClassInterface( +static void GenerateClientClassInterface( const grpc_generator::Service *service, grpc_generator::Printer *printer, std::map *dictonary) { auto vars = *dictonary; @@ -487,6 +492,8 @@ void GenerateClientClassInterface( printer->Outdent(); printer->Print("}\n"); } +} // namespace + grpc::string GenerateInterface(grpc_generator::File *file, const grpc_generator::Service *service, -- cgit v1.2.3