aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/google/protobuf/compiler/csharp
diff options
context:
space:
mode:
authorGravatar Jon Skeet <jonskeet@google.com>2015-11-05 09:13:25 +0000
committerGravatar Jon Skeet <jonskeet@google.com>2015-11-09 09:22:43 +0000
commit61a50b9e6b3c2898eb1446230f3ed74f9c8be7f9 (patch)
tree3d13e72cd9acdbe542a42e3a0bb34e455e75450c /src/google/protobuf/compiler/csharp
parent64aa954daec2f6f2df460f9bcc1e2ca6ba42b640 (diff)
Change how the reflection descriptor class is generated.
Instead of having a Proto nested namespace to avoid conflicts between the descriptor-holding static class and message classes, just append "Reflection" to the name. Generated code changes (and corresponding manual changes) in following commit.
Diffstat (limited to 'src/google/protobuf/compiler/csharp')
-rw-r--r--src/google/protobuf/compiler/csharp/csharp_helpers.cc52
-rw-r--r--src/google/protobuf/compiler/csharp/csharp_umbrella_class.cc15
-rw-r--r--src/google/protobuf/compiler/csharp/csharp_umbrella_class.h1
3 files changed, 12 insertions, 56 deletions
diff --git a/src/google/protobuf/compiler/csharp/csharp_helpers.cc b/src/google/protobuf/compiler/csharp/csharp_helpers.cc
index 2264b88d..803e96b7 100644
--- a/src/google/protobuf/compiler/csharp/csharp_helpers.cc
+++ b/src/google/protobuf/compiler/csharp/csharp_helpers.cc
@@ -117,43 +117,19 @@ std::string GetFileNamespace(const FileDescriptor* descriptor) {
return UnderscoresToCamelCase(descriptor->package(), true, true);
}
-std::string GetUmbrellaClassUnqualifiedName(const FileDescriptor* descriptor) {
- // We manually rename Descriptor to DescriptorProtoFile to avoid collisions with
- // the static Descriptor property. It would be nice to be able to do this with an
- // option, but it would be rarely used.
- if (IsDescriptorProto(descriptor)) {
- return "DescriptorProtoFile";
- }
- // umbrella_classname can no longer be set using message option.
- std::string proto_file = descriptor->name();
- int lastslash = proto_file.find_last_of("/");
- std::string base = proto_file.substr(lastslash + 1);
- return UnderscoresToPascalCase(StripDotProto(base));
+// Returns the Pascal-cased last part of the proto file. For example,
+// input of "google/protobuf/foo_bar.proto" would result in "FooBar".
+std::string GetFileNameBase(const FileDescriptor* descriptor) {
+ std::string proto_file = descriptor->name();
+ int lastslash = proto_file.find_last_of("/");
+ std::string base = proto_file.substr(lastslash + 1);
+ return UnderscoresToPascalCase(StripDotProto(base));
}
-std::string GetUmbrellaClassNestedNamespace(const FileDescriptor* descriptor) {
- // TODO(jtattermusch): reintroduce csharp_umbrella_namespace option
- bool collision = false;
- std::string umbrella_classname = GetUmbrellaClassUnqualifiedName(descriptor);
- for(int i = 0; i < descriptor->message_type_count(); i++) {
- if (descriptor->message_type(i)->name() == umbrella_classname) {
- collision = true;
- break;
- }
- }
- for (int i = 0; i < descriptor->service_count(); i++) {
- if (descriptor->service(i)->name() == umbrella_classname) {
- collision = true;
- break;
- }
- }
- for (int i = 0; i < descriptor->enum_type_count(); i++) {
- if (descriptor->enum_type(i)->name() == umbrella_classname) {
- collision = true;
- break;
- }
- }
- return collision ? "Proto" : "";
+std::string GetUmbrellaClassUnqualifiedName(const FileDescriptor* descriptor) {
+ // umbrella_classname can no longer be set using message option.
+ // TODO: Detect collisions with existing messages, and append an underscore if necessary.
+ return GetFileNameBase(descriptor) + "Reflection";
}
// TODO(jtattermusch): can we reuse a utility function?
@@ -223,10 +199,6 @@ std::string GetUmbrellaClassName(const FileDescriptor* descriptor) {
if (!result.empty()) {
result += '.';
}
- std::string umbrellaNamespace = GetUmbrellaClassNestedNamespace(descriptor);
- if (!umbrellaNamespace.empty()) {
- result += umbrellaNamespace + ".";
- }
result += GetUmbrellaClassUnqualifiedName(descriptor);
return "global::" + result;
}
@@ -275,7 +247,7 @@ std::string GetOutputFile(
const bool generate_directories,
const std::string base_namespace,
string* error) {
- string relative_filename = GetUmbrellaClassUnqualifiedName(descriptor) + file_extension;
+ string relative_filename = GetFileNameBase(descriptor) + file_extension;
if (!generate_directories) {
return relative_filename;
}
diff --git a/src/google/protobuf/compiler/csharp/csharp_umbrella_class.cc b/src/google/protobuf/compiler/csharp/csharp_umbrella_class.cc
index 7cf101b0..8059fd2a 100644
--- a/src/google/protobuf/compiler/csharp/csharp_umbrella_class.cc
+++ b/src/google/protobuf/compiler/csharp/csharp_umbrella_class.cc
@@ -55,7 +55,6 @@ UmbrellaClassGenerator::UmbrellaClassGenerator(const FileDescriptor* file)
file_(file) {
namespace_ = GetFileNamespace(file);
umbrellaClassname_ = GetUmbrellaClassUnqualifiedName(file);
- umbrellaNamespace_ = GetUmbrellaClassNestedNamespace(file);
}
UmbrellaClassGenerator::~UmbrellaClassGenerator() {
@@ -69,12 +68,6 @@ void UmbrellaClassGenerator::Generate(io::Printer* printer) {
printer->Outdent();
printer->Print("}\n");
- // Close the namespace around the umbrella class if defined
- if (!umbrellaNamespace_.empty()) {
- printer->Outdent();
- printer->Print("}\n");
- }
-
// write children: Enums
if (file_->enum_type_count() > 0) {
printer->Print("#region Enums\n");
@@ -126,14 +119,6 @@ void UmbrellaClassGenerator::WriteIntroduction(io::Printer* printer) {
printer->Print("\n");
}
- // Add the namespace around the umbrella class if defined
- if (!umbrellaNamespace_.empty()) {
- printer->Print("namespace $umbrella_namespace$ {\n",
- "umbrella_namespace", umbrellaNamespace_);
- printer->Indent();
- printer->Print("\n");
- }
-
printer->Print(
"/// <summary>Holder for reflection information generated from $file_name$</summary>\n"
"[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]\n",
diff --git a/src/google/protobuf/compiler/csharp/csharp_umbrella_class.h b/src/google/protobuf/compiler/csharp/csharp_umbrella_class.h
index b8bd2133..45a5b717 100644
--- a/src/google/protobuf/compiler/csharp/csharp_umbrella_class.h
+++ b/src/google/protobuf/compiler/csharp/csharp_umbrella_class.h
@@ -53,7 +53,6 @@ class UmbrellaClassGenerator : public SourceGeneratorBase {
std::string namespace_;
std::string umbrellaClassname_;
- std::string umbrellaNamespace_;
void WriteIntroduction(io::Printer* printer);
void WriteDescriptor(io::Printer* printer);