From 44664bb705fc704ac3cc5a745d200145a4897ed2 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Mon, 13 Apr 2015 12:40:48 -0700 Subject: updated C# codegen to use restricted set of csharp options from descriptor.proto --- .../protobuf/compiler/csharp/csharp_extension.cc | 2 +- .../protobuf/compiler/csharp/csharp_field_base.cc | 2 +- .../protobuf/compiler/csharp/csharp_generator.cc | 6 +++-- .../protobuf/compiler/csharp/csharp_helpers.cc | 19 +++----------- .../protobuf/compiler/csharp/csharp_message.cc | 15 ++--------- .../csharp/csharp_source_generator_base.cc | 5 ++++ .../compiler/csharp/csharp_source_generator_base.h | 1 + .../compiler/csharp/csharp_umbrella_class.cc | 29 +++++++--------------- 8 files changed, 27 insertions(+), 52 deletions(-) (limited to 'src/google') diff --git a/src/google/protobuf/compiler/csharp/csharp_extension.cc b/src/google/protobuf/compiler/csharp/csharp_extension.cc index f929f49f..b089aed1 100644 --- a/src/google/protobuf/compiler/csharp/csharp_extension.cc +++ b/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)]"); } diff --git a/src/google/protobuf/compiler/csharp/csharp_field_base.cc b/src/google/protobuf/compiler/csharp/csharp_field_base.cc index 48af999c..d7eb5d34 100644 --- a/src/google/protobuf/compiler/csharp/csharp_field_base.cc +++ b/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)]"); } } diff --git a/src/google/protobuf/compiler/csharp/csharp_generator.cc b/src/google/protobuf/compiler/csharp/csharp_generator.cc index 0cac22df..deb3d205 100644 --- a/src/google/protobuf/compiler/csharp/csharp_generator.cc +++ b/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 output( diff --git a/src/google/protobuf/compiler/csharp/csharp_helpers.cc b/src/google/protobuf/compiler/csharp/csharp_helpers.cc index 49ae2c94..ac258f04 100644 --- a/src/google/protobuf/compiler/csharp/csharp_helpers.cc +++ b/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; diff --git a/src/google/protobuf/compiler/csharp/csharp_message.cc b/src/google/protobuf/compiler/csharp/csharp_message.cc index 2df3bd2d..126ed22d 100644 --- a/src/google/protobuf/compiler/csharp/csharp_message.cc +++ b/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); diff --git a/src/google/protobuf/compiler/csharp/csharp_source_generator_base.cc b/src/google/protobuf/compiler/csharp/csharp_source_generator_base.cc index 35c28141..ed016b46 100644 --- a/src/google/protobuf/compiler/csharp/csharp_source_generator_base.cc +++ b/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 diff --git a/src/google/protobuf/compiler/csharp/csharp_source_generator_base.h b/src/google/protobuf/compiler/csharp/csharp_source_generator_base.h index 1955394e..dbc65d04 100644 --- a/src/google/protobuf/compiler/csharp/csharp_source_generator_base.h +++ b/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_; diff --git a/src/google/protobuf/compiler/csharp/csharp_umbrella_class.cc b/src/google/protobuf/compiler/csharp/csharp_umbrella_class.cc index ead6c1a9..358cfa4f 100644 --- a/src/google/protobuf/compiler/csharp/csharp_umbrella_class.cc +++ b/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); -- cgit v1.2.3