compiler: add std:: qualifications to all references to std::string

This commit is contained in:
Jihun Cho 2020-02-24 11:34:19 -08:00 committed by GitHub
parent a98db126e2
commit 3b1af27073
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 65 additions and 65 deletions

View File

@ -52,7 +52,7 @@ using google::protobuf::SourceLocation;
using std::to_string; using std::to_string;
// java keywords from: https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.9 // java keywords from: https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.9
static std::set<string> java_keywords = { static std::set<std::string> java_keywords = {
"abstract", "abstract",
"assert", "assert",
"boolean", "boolean",
@ -112,8 +112,8 @@ static std::set<string> java_keywords = {
// - decapitalize the first letter // - decapitalize the first letter
// - remove embedded underscores & capitalize the following letter // - remove embedded underscores & capitalize the following letter
// Finally, if the result is a reserved java keyword, append an underscore. // Finally, if the result is a reserved java keyword, append an underscore.
static string MixedLower(const string& word) { static std::string MixedLower(const std::string& word) {
string w; std::string w;
w += tolower(word[0]); w += tolower(word[0]);
bool after_underscore = false; bool after_underscore = false;
for (size_t i = 1; i < word.length(); ++i) { for (size_t i = 1; i < word.length(); ++i) {
@ -134,8 +134,8 @@ static string MixedLower(const string& word) {
// - An underscore is inserted where a lower case letter is followed by an // - An underscore is inserted where a lower case letter is followed by an
// upper case letter. // upper case letter.
// - All letters are converted to upper case // - All letters are converted to upper case
static string ToAllUpperCase(const string& word) { static std::string ToAllUpperCase(const std::string& word) {
string w; std::string w;
for (size_t i = 0; i < word.length(); ++i) { for (size_t i = 0; i < word.length(); ++i) {
w += toupper(word[i]); w += toupper(word[i]);
if ((i < word.length() - 1) && islower(word[i]) && isupper(word[i + 1])) { if ((i < word.length() - 1) && islower(word[i]) && isupper(word[i + 1])) {
@ -145,19 +145,19 @@ static string ToAllUpperCase(const string& word) {
return w; return w;
} }
static inline string LowerMethodName(const MethodDescriptor* method) { static inline std::string LowerMethodName(const MethodDescriptor* method) {
return MixedLower(method->name()); return MixedLower(method->name());
} }
static inline string MethodPropertiesFieldName(const MethodDescriptor* method) { static inline std::string MethodPropertiesFieldName(const MethodDescriptor* method) {
return "METHOD_" + ToAllUpperCase(method->name()); return "METHOD_" + ToAllUpperCase(method->name());
} }
static inline string MethodPropertiesGetterName(const MethodDescriptor* method) { static inline std::string MethodPropertiesGetterName(const MethodDescriptor* method) {
return MixedLower("get_" + method->name() + "_method"); return MixedLower("get_" + method->name() + "_method");
} }
static inline string MethodIdFieldName(const MethodDescriptor* method) { static inline std::string MethodIdFieldName(const MethodDescriptor* method) {
return "METHODID_" + ToAllUpperCase(method->name()); return "METHODID_" + ToAllUpperCase(method->name());
} }
@ -165,13 +165,13 @@ static inline bool ShouldGenerateAsLite(const Descriptor* desc) {
return false; return false;
} }
static inline string MessageFullJavaName(const Descriptor* desc) { static inline std::string MessageFullJavaName(const Descriptor* desc) {
return google::protobuf::compiler::java::ClassName(desc); return google::protobuf::compiler::java::ClassName(desc);
} }
// TODO(nmittler): Remove once protobuf includes javadoc methods in distribution. // TODO(nmittler): Remove once protobuf includes javadoc methods in distribution.
template <typename ITR> template <typename ITR>
static void GrpcSplitStringToIteratorUsing(const string& full, static void GrpcSplitStringToIteratorUsing(const std::string& full,
const char* delim, const char* delim,
ITR& result) { ITR& result) {
// Optimize the common case where delim is a single character. // Optimize the common case where delim is a single character.
@ -185,17 +185,17 @@ static void GrpcSplitStringToIteratorUsing(const string& full,
} else { } else {
const char* start = p; const char* start = p;
while (++p != end && *p != c); while (++p != end && *p != c);
*result++ = string(start, p - start); *result++ = std::string(start, p - start);
} }
} }
return; return;
} }
string::size_type begin_index, end_index; std::string::size_type begin_index, end_index;
begin_index = full.find_first_not_of(delim); begin_index = full.find_first_not_of(delim);
while (begin_index != string::npos) { while (begin_index != std::string::npos) {
end_index = full.find_first_of(delim, begin_index); end_index = full.find_first_of(delim, begin_index);
if (end_index == string::npos) { if (end_index == std::string::npos) {
*result++ = full.substr(begin_index); *result++ = full.substr(begin_index);
return; return;
} }
@ -205,28 +205,28 @@ static void GrpcSplitStringToIteratorUsing(const string& full,
} }
// TODO(nmittler): Remove once protobuf includes javadoc methods in distribution. // TODO(nmittler): Remove once protobuf includes javadoc methods in distribution.
static void GrpcSplitStringUsing(const string& full, static void GrpcSplitStringUsing(const std::string& full,
const char* delim, const char* delim,
std::vector<string>* result) { std::vector<std::string>* result) {
std::back_insert_iterator< std::vector<string> > it(*result); std::back_insert_iterator< std::vector<std::string> > it(*result);
GrpcSplitStringToIteratorUsing(full, delim, it); GrpcSplitStringToIteratorUsing(full, delim, it);
} }
// TODO(nmittler): Remove once protobuf includes javadoc methods in distribution. // TODO(nmittler): Remove once protobuf includes javadoc methods in distribution.
static std::vector<string> GrpcSplit(const string& full, const char* delim) { static std::vector<std::string> GrpcSplit(const std::string& full, const char* delim) {
std::vector<string> result; std::vector<std::string> result;
GrpcSplitStringUsing(full, delim, &result); GrpcSplitStringUsing(full, delim, &result);
return result; return result;
} }
// TODO(nmittler): Remove once protobuf includes javadoc methods in distribution. // TODO(nmittler): Remove once protobuf includes javadoc methods in distribution.
static string GrpcEscapeJavadoc(const string& input) { static std::string GrpcEscapeJavadoc(const std::string& input) {
string result; std::string result;
result.reserve(input.size() * 2); result.reserve(input.size() * 2);
char prev = '*'; char prev = '*';
for (string::size_type i = 0; i < input.size(); i++) { for (std::string::size_type i = 0; i < input.size(); i++) {
char c = input[i]; char c = input[i];
switch (c) { switch (c) {
case '*': case '*':
@ -280,17 +280,17 @@ static string GrpcEscapeJavadoc(const string& input) {
// TODO(nmittler): Remove once protobuf includes javadoc methods in distribution. // TODO(nmittler): Remove once protobuf includes javadoc methods in distribution.
template <typename DescriptorType> template <typename DescriptorType>
static string GrpcGetCommentsForDescriptor(const DescriptorType* descriptor) { static std::string GrpcGetCommentsForDescriptor(const DescriptorType* descriptor) {
SourceLocation location; SourceLocation location;
if (descriptor->GetSourceLocation(&location)) { if (descriptor->GetSourceLocation(&location)) {
return location.leading_comments.empty() ? return location.leading_comments.empty() ?
location.trailing_comments : location.leading_comments; location.trailing_comments : location.leading_comments;
} }
return string(); return std::string();
} }
// TODO(nmittler): Remove once protobuf includes javadoc methods in distribution. // TODO(nmittler): Remove once protobuf includes javadoc methods in distribution.
static std::vector<string> GrpcGetDocLines(const string& comments) { static std::vector<std::string> GrpcGetDocLines(const std::string& comments) {
if (!comments.empty()) { if (!comments.empty()) {
// TODO(kenton): Ideally we should parse the comment text as Markdown and // TODO(kenton): Ideally we should parse the comment text as Markdown and
// write it back as HTML, but this requires a Markdown parser. For now // write it back as HTML, but this requires a Markdown parser. For now
@ -298,26 +298,26 @@ static std::vector<string> GrpcGetDocLines(const string& comments) {
// If the comment itself contains block comment start or end markers, // If the comment itself contains block comment start or end markers,
// HTML-escape them so that they don't accidentally close the doc comment. // HTML-escape them so that they don't accidentally close the doc comment.
string escapedComments = GrpcEscapeJavadoc(comments); std::string escapedComments = GrpcEscapeJavadoc(comments);
std::vector<string> lines = GrpcSplit(escapedComments, "\n"); std::vector<std::string> lines = GrpcSplit(escapedComments, "\n");
while (!lines.empty() && lines.back().empty()) { while (!lines.empty() && lines.back().empty()) {
lines.pop_back(); lines.pop_back();
} }
return lines; return lines;
} }
return std::vector<string>(); return std::vector<std::string>();
} }
// TODO(nmittler): Remove once protobuf includes javadoc methods in distribution. // TODO(nmittler): Remove once protobuf includes javadoc methods in distribution.
template <typename DescriptorType> template <typename DescriptorType>
static std::vector<string> GrpcGetDocLinesForDescriptor(const DescriptorType* descriptor) { static std::vector<std::string> GrpcGetDocLinesForDescriptor(const DescriptorType* descriptor) {
return GrpcGetDocLines(GrpcGetCommentsForDescriptor(descriptor)); return GrpcGetDocLines(GrpcGetCommentsForDescriptor(descriptor));
} }
// TODO(nmittler): Remove once protobuf includes javadoc methods in distribution. // TODO(nmittler): Remove once protobuf includes javadoc methods in distribution.
static void GrpcWriteDocCommentBody(Printer* printer, static void GrpcWriteDocCommentBody(Printer* printer,
const std::vector<string>& lines, const std::vector<std::string>& lines,
bool surroundWithPreTag) { bool surroundWithPreTag) {
if (!lines.empty()) { if (!lines.empty()) {
if (surroundWithPreTag) { if (surroundWithPreTag) {
@ -342,9 +342,9 @@ static void GrpcWriteDocCommentBody(Printer* printer,
} }
// TODO(nmittler): Remove once protobuf includes javadoc methods in distribution. // TODO(nmittler): Remove once protobuf includes javadoc methods in distribution.
static void GrpcWriteDocComment(Printer* printer, const string& comments) { static void GrpcWriteDocComment(Printer* printer, const std::string& comments) {
printer->Print("/**\n"); printer->Print("/**\n");
std::vector<string> lines = GrpcGetDocLines(comments); std::vector<std::string> lines = GrpcGetDocLines(comments);
GrpcWriteDocCommentBody(printer, lines, false); GrpcWriteDocCommentBody(printer, lines, false);
printer->Print(" */\n"); printer->Print(" */\n");
} }
@ -355,7 +355,7 @@ static void GrpcWriteServiceDocComment(Printer* printer,
// Deviating from protobuf to avoid extraneous docs // Deviating from protobuf to avoid extraneous docs
// (see https://github.com/google/protobuf/issues/1406); // (see https://github.com/google/protobuf/issues/1406);
printer->Print("/**\n"); printer->Print("/**\n");
std::vector<string> lines = GrpcGetDocLinesForDescriptor(service); std::vector<std::string> lines = GrpcGetDocLinesForDescriptor(service);
GrpcWriteDocCommentBody(printer, lines, true); GrpcWriteDocCommentBody(printer, lines, true);
printer->Print(" */\n"); printer->Print(" */\n");
} }
@ -366,13 +366,13 @@ void GrpcWriteMethodDocComment(Printer* printer,
// Deviating from protobuf to avoid extraneous docs // Deviating from protobuf to avoid extraneous docs
// (see https://github.com/google/protobuf/issues/1406); // (see https://github.com/google/protobuf/issues/1406);
printer->Print("/**\n"); printer->Print("/**\n");
std::vector<string> lines = GrpcGetDocLinesForDescriptor(method); std::vector<std::string> lines = GrpcGetDocLinesForDescriptor(method);
GrpcWriteDocCommentBody(printer, lines, true); GrpcWriteDocCommentBody(printer, lines, true);
printer->Print(" */\n"); printer->Print(" */\n");
} }
static void PrintMethodFields( static void PrintMethodFields(
const ServiceDescriptor* service, std::map<string, string>* vars, const ServiceDescriptor* service, std::map<std::string, std::string>* vars,
Printer* p, ProtoFlavor flavor) { Printer* p, ProtoFlavor flavor) {
p->Print("// Static method descriptors that strictly reflect the proto.\n"); p->Print("// Static method descriptors that strictly reflect the proto.\n");
(*vars)["service_name"] = service->name(); (*vars)["service_name"] = service->name();
@ -486,15 +486,15 @@ enum CallType {
}; };
static void PrintBindServiceMethodBody(const ServiceDescriptor* service, static void PrintBindServiceMethodBody(const ServiceDescriptor* service,
std::map<string, string>* vars, std::map<std::string, std::string>* vars,
Printer* p); Printer* p);
// Prints a StubFactory for given service / stub type. // Prints a StubFactory for given service / stub type.
static void PrintStubFactory( static void PrintStubFactory(
const ServiceDescriptor* service, const ServiceDescriptor* service,
std::map<string, string>* vars, std::map<std::string, std::string>* vars,
Printer* p, StubType type) { Printer* p, StubType type) {
string stub_type_name; std::string stub_type_name;
switch (type) { switch (type) {
case ASYNC_CLIENT_IMPL: case ASYNC_CLIENT_IMPL:
stub_type_name = ""; stub_type_name = "";
@ -523,14 +523,14 @@ static void PrintStubFactory(
// Prints a client interface or implementation class, or a server interface. // Prints a client interface or implementation class, or a server interface.
static void PrintStub( static void PrintStub(
const ServiceDescriptor* service, const ServiceDescriptor* service,
std::map<string, string>* vars, std::map<std::string, std::string>* vars,
Printer* p, StubType type) { Printer* p, StubType type) {
const string service_name = service->name(); const std::string service_name = service->name();
(*vars)["service_name"] = service_name; (*vars)["service_name"] = service_name;
(*vars)["abstract_name"] = service_name + "ImplBase"; (*vars)["abstract_name"] = service_name + "ImplBase";
string stub_name = service_name; std::string stub_name = service_name;
string client_name = service_name; std::string client_name = service_name;
string stub_base_class_name = "AbstractStub"; std::string stub_base_class_name = "AbstractStub";
CallType call_type; CallType call_type;
bool impl_base = false; bool impl_base = false;
bool interface = false; bool interface = false;
@ -804,7 +804,7 @@ static bool CompareMethodClientStreaming(const MethodDescriptor* method1,
// Place all method invocations into a single class to reduce memory footprint // Place all method invocations into a single class to reduce memory footprint
// on Android. // on Android.
static void PrintMethodHandlerClass(const ServiceDescriptor* service, static void PrintMethodHandlerClass(const ServiceDescriptor* service,
std::map<string, string>* vars, std::map<std::string, std::string>* vars,
Printer* p) { Printer* p) {
// Sort method ids based on client_streaming() so switch tables are compact. // Sort method ids based on client_streaming() so switch tables are compact.
std::vector<const MethodDescriptor*> sorted_methods(service->method_count()); std::vector<const MethodDescriptor*> sorted_methods(service->method_count());
@ -910,7 +910,7 @@ static void PrintMethodHandlerClass(const ServiceDescriptor* service,
} }
static void PrintGetServiceDescriptorMethod(const ServiceDescriptor* service, static void PrintGetServiceDescriptorMethod(const ServiceDescriptor* service,
std::map<string, string>* vars, std::map<std::string, std::string>* vars,
Printer* p, Printer* p,
ProtoFlavor flavor) { ProtoFlavor flavor) {
(*vars)["service_name"] = service->name(); (*vars)["service_name"] = service->name();
@ -1011,7 +1011,7 @@ static void PrintGetServiceDescriptorMethod(const ServiceDescriptor* service,
} }
static void PrintBindServiceMethodBody(const ServiceDescriptor* service, static void PrintBindServiceMethodBody(const ServiceDescriptor* service,
std::map<string, string>* vars, std::map<std::string, std::string>* vars,
Printer* p) { Printer* p) {
(*vars)["service_name"] = service->name(); (*vars)["service_name"] = service->name();
p->Indent(); p->Indent();
@ -1065,7 +1065,7 @@ static void PrintBindServiceMethodBody(const ServiceDescriptor* service,
} }
static void PrintService(const ServiceDescriptor* service, static void PrintService(const ServiceDescriptor* service,
std::map<string, string>* vars, std::map<std::string, std::string>* vars,
Printer* p, Printer* p,
ProtoFlavor flavor, ProtoFlavor flavor,
bool disable_version) { bool disable_version) {
@ -1195,7 +1195,7 @@ void GenerateService(const ServiceDescriptor* service,
bool disable_version) { bool disable_version) {
// All non-generated classes must be referred by fully qualified names to // All non-generated classes must be referred by fully qualified names to
// avoid collision with generated classes. // avoid collision with generated classes.
std::map<string, string> vars; std::map<std::string, std::string> vars;
vars["String"] = "java.lang.String"; vars["String"] = "java.lang.String";
vars["Deprecated"] = "java.lang.Deprecated"; vars["Deprecated"] = "java.lang.Deprecated";
vars["Override"] = "java.lang.Override"; vars["Override"] = "java.lang.Override";
@ -1229,7 +1229,7 @@ void GenerateService(const ServiceDescriptor* service,
"com.google.common.util.concurrent.ListenableFuture"; "com.google.common.util.concurrent.ListenableFuture";
Printer printer(out, '$'); Printer printer(out, '$');
string package_name = ServiceJavaPackage(service->file()); std::string package_name = ServiceJavaPackage(service->file());
if (!package_name.empty()) { if (!package_name.empty()) {
printer.Print( printer.Print(
"package $package_name$;\n\n", "package $package_name$;\n\n",
@ -1237,7 +1237,7 @@ void GenerateService(const ServiceDescriptor* service,
} }
PrintImports(&printer); PrintImports(&printer);
// Package string is used to fully qualify method names. // Package std::string is used to fully qualify method names.
vars["Package"] = service->file()->package(); vars["Package"] = service->file()->package();
if (!vars["Package"].empty()) { if (!vars["Package"].empty()) {
vars["Package"].append("."); vars["Package"].append(".");
@ -1245,10 +1245,10 @@ void GenerateService(const ServiceDescriptor* service,
PrintService(service, &vars, &printer, flavor, disable_version); PrintService(service, &vars, &printer, flavor, disable_version);
} }
string ServiceJavaPackage(const FileDescriptor* file) { std::string ServiceJavaPackage(const FileDescriptor* file) {
string result = google::protobuf::compiler::java::ClassName(file); std::string result = google::protobuf::compiler::java::ClassName(file);
size_t last_dot_pos = result.find_last_of('.'); size_t last_dot_pos = result.find_last_of('.');
if (last_dot_pos != string::npos) { if (last_dot_pos != std::string::npos) {
result.resize(last_dot_pos); result.resize(last_dot_pos);
} else { } else {
result = ""; result = "";
@ -1256,7 +1256,7 @@ string ServiceJavaPackage(const FileDescriptor* file) {
return result; return result;
} }
string ServiceClassName(const google::protobuf::ServiceDescriptor* service) { std::string ServiceClassName(const google::protobuf::ServiceDescriptor* service) {
return service->name() + "Grpc"; return service->name() + "Grpc";
} }

View File

@ -56,11 +56,11 @@ enum ProtoFlavor {
}; };
// Returns the package name of the gRPC services defined in the given file. // Returns the package name of the gRPC services defined in the given file.
string ServiceJavaPackage(const google::protobuf::FileDescriptor* file); std::string ServiceJavaPackage(const google::protobuf::FileDescriptor* file);
// Returns the name of the outer class that wraps in all the generated code for // Returns the name of the outer class that wraps in all the generated code for
// the given service. // the given service.
string ServiceClassName(const google::protobuf::ServiceDescriptor* service); std::string ServiceClassName(const google::protobuf::ServiceDescriptor* service);
// Writes the generated service interface into the given ZeroCopyOutputStream // Writes the generated service interface into the given ZeroCopyOutputStream
void GenerateService(const google::protobuf::ServiceDescriptor* service, void GenerateService(const google::protobuf::ServiceDescriptor* service,

View File

@ -27,8 +27,8 @@
#include <google/protobuf/descriptor.h> #include <google/protobuf/descriptor.h>
#include <google/protobuf/io/zero_copy_stream.h> #include <google/protobuf/io/zero_copy_stream.h>
static string JavaPackageToDir(const string& package_name) { static std::string JavaPackageToDir(const std::string& package_name) {
string package_dir = package_name; std::string package_dir = package_name;
for (size_t i = 0; i < package_dir.size(); ++i) { for (size_t i = 0; i < package_dir.size(); ++i) {
if (package_dir[i] == '.') { if (package_dir[i] == '.') {
package_dir[i] = '/'; package_dir[i] = '/';
@ -44,10 +44,10 @@ class JavaGrpcGenerator : public google::protobuf::compiler::CodeGenerator {
virtual ~JavaGrpcGenerator() {} virtual ~JavaGrpcGenerator() {}
virtual bool Generate(const google::protobuf::FileDescriptor* file, virtual bool Generate(const google::protobuf::FileDescriptor* file,
const string& parameter, const std::string& parameter,
google::protobuf::compiler::GeneratorContext* context, google::protobuf::compiler::GeneratorContext* context,
string* error) const { std::string* error) const {
std::vector<std::pair<string, string> > options; std::vector<std::pair<std::string, std::string> > options;
google::protobuf::compiler::ParseGeneratorParameter(parameter, &options); google::protobuf::compiler::ParseGeneratorParameter(parameter, &options);
java_grpc_generator::ProtoFlavor flavor = java_grpc_generator::ProtoFlavor flavor =
@ -62,11 +62,11 @@ class JavaGrpcGenerator : public google::protobuf::compiler::CodeGenerator {
} }
} }
string package_name = java_grpc_generator::ServiceJavaPackage(file); std::string package_name = java_grpc_generator::ServiceJavaPackage(file);
string package_filename = JavaPackageToDir(package_name); std::string package_filename = JavaPackageToDir(package_name);
for (int i = 0; i < file->service_count(); ++i) { for (int i = 0; i < file->service_count(); ++i) {
const google::protobuf::ServiceDescriptor* service = file->service(i); const google::protobuf::ServiceDescriptor* service = file->service(i);
string filename = package_filename std::string filename = package_filename
+ java_grpc_generator::ServiceClassName(service) + ".java"; + java_grpc_generator::ServiceClassName(service) + ".java";
std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> output( std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> output(
context->Open(filename)); context->Open(filename));