浏览代码

updated C# codegen to use restricted set of csharp options from descriptor.proto

Jan Tattermusch 10 年之前
父节点
当前提交
44664bb705

+ 1 - 1
src/google/protobuf/compiler/csharp/csharp_extension.cc

@@ -63,7 +63,7 @@ ExtensionGenerator::~ExtensionGenerator() {
 }
 
 void ExtensionGenerator::Generate(Writer* writer) {
-  if (descriptor_->file()->options().csharp_cls_compliance()
+  if (cls_compliance()
       && (GetFieldConstantName(descriptor_).substr(0, 1) == "_")) {
     writer->WriteLine("[global::System.CLSCompliant(false)]");
   }

+ 1 - 1
src/google/protobuf/compiler/csharp/csharp_field_base.cc

@@ -84,7 +84,7 @@ void FieldGeneratorBase::AddPublicMemberAttributes(Writer* writer) {
 }
 
 void FieldGeneratorBase::AddClsComplianceCheck(Writer* writer) {
-  if (!is_cls_compliant() && descriptor_->file()->options().csharp_cls_compliance()) {
+  if (cls_compliance() && !is_cls_compliant()) {
     writer->WriteLine("[global::System.CLSCompliant(false)]");
   }
 }

+ 4 - 2
src/google/protobuf/compiler/csharp/csharp_generator.cc

@@ -61,9 +61,11 @@ bool Generator::Generate(
     GeneratorContext* generator_context,
     string* error) const {
 
-  // TODO: parse generator parameters...
+  // TODO(jtattermusch): parse generator parameters:
+  // cls_compliance
+  // file_extension
 
-  // TODO: file output file naming logic
+  // TODO(jtattermusch): rework output file naming logic
   std::string filename =
       StripDotProto(file->name()) + ".cs";
   scoped_ptr<io::ZeroCopyOutputStream> output(

+ 4 - 15
src/google/protobuf/compiler/csharp/csharp_helpers.cc

@@ -122,16 +122,12 @@ std::string GetUmbrellaClassNameInternal(const std::string& proto_file) {
 }
 
 std::string GetFileUmbrellaClassname(const FileDescriptor* descriptor) {
-  if (descriptor->options().has_csharp_umbrella_classname()) {
-    return descriptor->options().csharp_umbrella_namespace();
-  } else {
-    return GetUmbrellaClassNameInternal(descriptor->name());
-  }
+  // umbrella_classname can no longer be set using message option.
+  return GetUmbrellaClassNameInternal(descriptor->name());
 }
 
 std::string GetFileUmbrellaNamespace(const FileDescriptor* descriptor) {
-  if (!descriptor->options().csharp_nest_classes()
-      && !descriptor->options().has_csharp_umbrella_namespace()) {
+  if (!descriptor->options().has_csharp_umbrella_namespace()) {
     bool collision = false;
     // TODO(jtattermusch): detect collisions!
 //      foreach (IDescriptor d in MessageTypes)
@@ -196,12 +192,6 @@ std::string UnderscoresToPascalCase(const std::string& input) {
 
 std::string ToCSharpName(const std::string& name, const FileDescriptor* file) {
   std::string result = GetFileNamespace(file);
-  if (file->options().csharp_nest_classes()) {
-    if (result != "") {
-      result += ".";
-    }
-    result += GetFileUmbrellaClassname(file);
-  }
   if (result != "") {
     result += '.';
   }
@@ -233,8 +223,7 @@ std::string GetQualifiedUmbrellaClassName(const FileDescriptor* descriptor) {
   std::string umbrellaClassname = GetFileUmbrellaClassname(descriptor);
 
   std::string fullName = umbrellaClassname;
-  if (!descriptor->options().csharp_nest_classes()
-      && !umbrellaNamespace.empty()) {
+  if (!umbrellaNamespace.empty()) {
     fullName = umbrellaNamespace + "." + umbrellaClassname;
   }
   return fullName;

+ 2 - 13
src/google/protobuf/compiler/csharp/csharp_message.cc

@@ -118,9 +118,7 @@ void MessageGenerator::GenerateStaticVariables(Writer* writer) {
 
   if (!use_lite_runtime()) {
     // The descriptor for this type.
-    std::string access =
-        descriptor_->file()->options().csharp_nest_classes() ?
-            "private" : "internal";
+    std::string access = "internal";
     writer->WriteLine(
         "$0$ static pbd::MessageDescriptor internal__$1$__Descriptor;", access,
         identifier);
@@ -175,9 +173,6 @@ void MessageGenerator::GenerateStaticVariableInitializers(Writer* writer) {
 }
 
 void MessageGenerator::Generate(Writer* writer) {
-  if (descriptor_->file()->options().csharp_add_serializable()) {
-    writer->WriteLine("[global::System.SerializableAttribute()]");
-  }
   writer->WriteLine(
       "[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]");
   WriteGeneratedCodeAttributes(writer);
@@ -187,9 +182,6 @@ void MessageGenerator::Generate(Writer* writer) {
       descriptor_->extension_range_count() > 0 ? "Extendable" : "Generated",
       runtime_suffix());
   writer->Indent();
-  if (descriptor_->file()->options().csharp_generate_private_ctor()) {
-    writer->WriteLine("private $0$() { }", class_name());
-  }
   // Must call MakeReadOnly() to make sure all lists are made read-only
   writer->WriteLine(
       "private static readonly $0$ defaultInstance = new $0$().MakeReadOnly();",
@@ -271,7 +263,7 @@ void MessageGenerator::Generate(Writer* writer) {
   for (int i = 0; i < descriptor_->field_count(); i++) {
     const FieldDescriptor* fieldDescriptor = descriptor_->field(i);
     // TODO(jtattermusch): same code for cls compliance is in csharp_extension
-    if (descriptor_->file()->options().csharp_cls_compliance()
+    if (cls_compliance()
         && GetFieldConstantName(fieldDescriptor)[0] == '_') {
       writer->WriteLine("[global::System.CLSCompliant(false)]");
     }
@@ -557,9 +549,6 @@ void MessageGenerator::GenerateBuilder(Writer* writer) {
   writer->WriteLine("  return new Builder(prototype);");
   writer->WriteLine("}");
   writer->WriteLine();
-  if (descriptor_->file()->options().csharp_add_serializable()) {
-    writer->WriteLine("[global::System.SerializableAttribute()]");
-  }
   writer->WriteLine(
       "[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]");
   WriteGeneratedCodeAttributes(writer);

+ 5 - 0
src/google/protobuf/compiler/csharp/csharp_source_generator_base.cc

@@ -76,6 +76,11 @@ std::string SourceGeneratorBase::class_access_level() {
   return "public";
 }
 
+bool SourceGeneratorBase::cls_compliance() {
+  // TODO(jtattermusch): implement this based on "cls_compliance" cmdline param.
+  return true;
+}
+
 }  // namespace csharp
 }  // namespace compiler
 }  // namespace protobuf

+ 1 - 0
src/google/protobuf/compiler/csharp/csharp_source_generator_base.h

@@ -48,6 +48,7 @@ class SourceGeneratorBase {
   virtual ~SourceGeneratorBase();
 
   std::string class_access_level();
+  bool cls_compliance();
 
   bool optimize_size() {
     return optimizeSize_;

+ 9 - 20
src/google/protobuf/compiler/csharp/csharp_umbrella_class.cc

@@ -86,17 +86,14 @@ void UmbrellaClassGenerator::Generate(Writer* writer) {
   } else {
     WriteLiteExtensions(writer);
   }
-  // The class declaration either gets closed before or after the children are written.
-  if (!file_->options().csharp_nest_classes()) {
+  // Close the class declaration.
+  writer->Outdent();
+  writer->WriteLine("}");
+
+  // Close the namespace around the umbrella class if defined
+  if (!umbrellaNamespace_.empty()) {
     writer->Outdent();
     writer->WriteLine("}");
-
-    // Close the namespace around the umbrella class if defined
-    if (!file_->options().csharp_nest_classes()
-        && !umbrellaNamespace_.empty()) {
-      writer->Outdent();
-      writer->WriteLine("}");
-    }
   }
 
   // write children: Enums
@@ -121,12 +118,8 @@ void UmbrellaClassGenerator::Generate(Writer* writer) {
     writer->WriteLine();
   }
 
-  // TODO(jtattermusch): add support for generating services.
-  //WriteChildren(writer, "Services", Descriptor.Services);
-  if (file_->options().csharp_nest_classes()) {
-    writer->Outdent();
-    writer->WriteLine("}");
-  }
+  // TODO(jtattermusch): add insertion point for services.
+
   if (!namespace_.empty()) {
     writer->Outdent();
     writer->WriteLine("}");
@@ -155,16 +148,12 @@ void UmbrellaClassGenerator::WriteIntroduction(Writer* writer) {
   }
 
   // Add the namespace around the umbrella class if defined
-  if (!file_->options().csharp_nest_classes() && !umbrellaNamespace_.empty()) {
+  if (!umbrellaNamespace_.empty()) {
     writer->WriteLine("namespace $0$ {", umbrellaNamespace_);
     writer->Indent();
     writer->WriteLine();
   }
 
-  if (file_->options().csharp_code_contracts()) {
-    writer->WriteLine(
-        "[global::System.Diagnostics.Contracts.ContractVerificationAttribute(false)]");
-  }
   writer->WriteLine(
       "[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]");
   WriteGeneratedCodeAttributes(writer);