Move DebuggerNonUserCodeAttribute to function members

I think this has caught everything.
I've left a stub for attributes to be applied to the types themselves, but we don't currently need anything.

Follow-up commit will include the changes to generated code itself.

Fixes #1671.
This commit is contained in:
Jon Skeet 2016-07-01 13:19:00 +01:00
parent cae3b0cbb6
commit ada0a81a74
13 changed files with 73 additions and 39 deletions

View File

@ -60,7 +60,7 @@ EnumGenerator::~EnumGenerator() {
void EnumGenerator::Generate(io::Printer* printer) { void EnumGenerator::Generate(io::Printer* printer) {
WriteEnumDocComment(printer, descriptor_); WriteEnumDocComment(printer, descriptor_);
WriteGeneratedCodeAttributes(printer); WriteGeneratedTypeAttributes(printer);
printer->Print("$access_level$ enum $name$ {\n", printer->Print("$access_level$ enum $name$ {\n",
"access_level", class_access_level(), "access_level", class_access_level(),
"name", descriptor_->name()); "name", descriptor_->name());

View File

@ -124,12 +124,13 @@ void FieldGeneratorBase::GenerateCodecCode(io::Printer* printer) {
void FieldGeneratorBase::AddDeprecatedFlag(io::Printer* printer) { void FieldGeneratorBase::AddDeprecatedFlag(io::Printer* printer) {
if (descriptor_->options().deprecated()) if (descriptor_->options().deprecated())
{ {
printer->Print("[global::System.ObsoleteAttribute()]\n"); printer->Print("[global::System.ObsoleteAttribute]\n");
} }
} }
void FieldGeneratorBase::AddPublicMemberAttributes(io::Printer* printer) { void FieldGeneratorBase::AddPublicMemberAttributes(io::Printer* printer) {
AddDeprecatedFlag(printer); AddDeprecatedFlag(printer);
WriteGeneratedCodeAttributes(printer);
} }
std::string FieldGeneratorBase::oneof_property_name() { std::string FieldGeneratorBase::oneof_property_name() {

View File

@ -80,7 +80,7 @@ void MapFieldGenerator::GenerateMembers(io::Printer* printer) {
", $tag$);\n" ", $tag$);\n"
"private readonly pbc::MapField<$key_type_name$, $value_type_name$> $name$_ = new pbc::MapField<$key_type_name$, $value_type_name$>();\n"); "private readonly pbc::MapField<$key_type_name$, $value_type_name$> $name$_ = new pbc::MapField<$key_type_name$, $value_type_name$>();\n");
WritePropertyDocComment(printer, descriptor_); WritePropertyDocComment(printer, descriptor_);
AddDeprecatedFlag(printer); AddPublicMemberAttributes(printer);
printer->Print( printer->Print(
variables_, variables_,
"$access_level$ pbc::MapField<$key_type_name$, $value_type_name$> $property_name$ {\n" "$access_level$ pbc::MapField<$key_type_name$, $value_type_name$> $property_name$ {\n"

View File

@ -104,9 +104,7 @@ void MessageGenerator::Generate(io::Printer* printer) {
vars["access_level"] = class_access_level(); vars["access_level"] = class_access_level();
WriteMessageDocComment(printer, descriptor_); WriteMessageDocComment(printer, descriptor_);
printer->Print( WriteGeneratedTypeAttributes(printer);
"[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]\n");
WriteGeneratedCodeAttributes(printer);
printer->Print( printer->Print(
vars, vars,
"$access_level$ sealed partial class $class_name$ : pb::IMessage<$class_name$> {\n"); "$access_level$ sealed partial class $class_name$ : pb::IMessage<$class_name$> {\n");
@ -115,7 +113,11 @@ void MessageGenerator::Generate(io::Printer* printer) {
// All static fields and properties // All static fields and properties
printer->Print( printer->Print(
vars, vars,
"private static readonly pb::MessageParser<$class_name$> _parser = new pb::MessageParser<$class_name$>(() => new $class_name$());\n" "private static readonly pb::MessageParser<$class_name$> _parser = new pb::MessageParser<$class_name$>(() => new $class_name$());\n");
WriteGeneratedCodeAttributes(printer);
printer->Print(
vars,
"public static pb::MessageParser<$class_name$> Parser { get { return _parser; } }\n\n"); "public static pb::MessageParser<$class_name$> Parser { get { return _parser; } }\n\n");
// Access the message descriptor via the relevant file descriptor or containing message descriptor. // Access the message descriptor via the relevant file descriptor or containing message descriptor.
@ -127,18 +129,23 @@ void MessageGenerator::Generate(io::Printer* printer) {
+ ".Descriptor.NestedTypes[" + SimpleItoa(descriptor_->index()) + "]"; + ".Descriptor.NestedTypes[" + SimpleItoa(descriptor_->index()) + "]";
} }
WriteGeneratedCodeAttributes(printer);
printer->Print( printer->Print(
vars, vars,
"public static pbr::MessageDescriptor Descriptor {\n" "public static pbr::MessageDescriptor Descriptor {\n"
" get { return $descriptor_accessor$; }\n" " get { return $descriptor_accessor$; }\n"
"}\n" "}\n"
"\n" "\n");
WriteGeneratedCodeAttributes(printer);
printer->Print(
vars,
"pbr::MessageDescriptor pb::IMessage.Descriptor {\n" "pbr::MessageDescriptor pb::IMessage.Descriptor {\n"
" get { return Descriptor; }\n" " get { return Descriptor; }\n"
"}\n" "}\n"
"\n"); "\n");
// Parameterless constructor and partial OnConstruction method. // Parameterless constructor and partial OnConstruction method.
WriteGeneratedCodeAttributes(printer);
printer->Print( printer->Print(
vars, vars,
"public $class_name$() {\n" "public $class_name$() {\n"
@ -190,10 +197,16 @@ void MessageGenerator::Generate(io::Printer* printer) {
// It's unclear exactly where they should go. // It's unclear exactly where they should go.
printer->Print( printer->Print(
vars, vars,
"private $property_name$OneofCase $name$Case_ = $property_name$OneofCase.None;\n" "private $property_name$OneofCase $name$Case_ = $property_name$OneofCase.None;\n");
WriteGeneratedCodeAttributes(printer);
printer->Print(
vars,
"public $property_name$OneofCase $property_name$Case {\n" "public $property_name$OneofCase $property_name$Case {\n"
" get { return $name$Case_; }\n" " get { return $name$Case_; }\n"
"}\n\n" "}\n\n");
WriteGeneratedCodeAttributes(printer);
printer->Print(
vars,
"public void Clear$property_name$() {\n" "public void Clear$property_name$() {\n"
" $name$Case_ = $property_name$OneofCase.None;\n" " $name$Case_ = $property_name$OneofCase.None;\n"
" $name$_ = null;\n" " $name$_ = null;\n"
@ -210,8 +223,7 @@ void MessageGenerator::Generate(io::Printer* printer) {
printer->Print( printer->Print(
vars, vars,
"#region Nested types\n" "#region Nested types\n"
"/// <summary>Container for nested types declared in the $class_name$ message type.</summary>\n" "/// <summary>Container for nested types declared in the $class_name$ message type.</summary>\n");
"[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]\n");
WriteGeneratedCodeAttributes(printer); WriteGeneratedCodeAttributes(printer);
printer->Print("public static partial class Types {\n"); printer->Print("public static partial class Types {\n");
printer->Indent(); printer->Indent();
@ -255,6 +267,7 @@ bool MessageGenerator::HasNestedGeneratedTypes()
void MessageGenerator::GenerateCloningCode(io::Printer* printer) { void MessageGenerator::GenerateCloningCode(io::Printer* printer) {
map<string, string> vars; map<string, string> vars;
WriteGeneratedCodeAttributes(printer);
vars["class_name"] = class_name(); vars["class_name"] = class_name();
printer->Print( printer->Print(
vars, vars,
@ -294,6 +307,7 @@ void MessageGenerator::GenerateCloningCode(io::Printer* printer) {
printer->Outdent(); printer->Outdent();
printer->Print("}\n\n"); printer->Print("}\n\n");
WriteGeneratedCodeAttributes(printer);
printer->Print( printer->Print(
vars, vars,
"public $class_name$ Clone() {\n" "public $class_name$ Clone() {\n"
@ -309,11 +323,15 @@ void MessageGenerator::GenerateFrameworkMethods(io::Printer* printer) {
vars["class_name"] = class_name(); vars["class_name"] = class_name();
// Equality // Equality
WriteGeneratedCodeAttributes(printer);
printer->Print( printer->Print(
vars, vars,
"public override bool Equals(object other) {\n" "public override bool Equals(object other) {\n"
" return Equals(other as $class_name$);\n" " return Equals(other as $class_name$);\n"
"}\n\n" "}\n\n");
WriteGeneratedCodeAttributes(printer);
printer->Print(
vars,
"public bool Equals($class_name$ other) {\n" "public bool Equals($class_name$ other) {\n"
" if (ReferenceEquals(other, null)) {\n" " if (ReferenceEquals(other, null)) {\n"
" return false;\n" " return false;\n"
@ -338,6 +356,7 @@ void MessageGenerator::GenerateFrameworkMethods(io::Printer* printer) {
// GetHashCode // GetHashCode
// Start with a non-zero value to easily distinguish between null and "empty" messages. // Start with a non-zero value to easily distinguish between null and "empty" messages.
WriteGeneratedCodeAttributes(printer);
printer->Print( printer->Print(
"public override int GetHashCode() {\n" "public override int GetHashCode() {\n"
" int hash = 1;\n"); " int hash = 1;\n");
@ -355,6 +374,7 @@ void MessageGenerator::GenerateFrameworkMethods(io::Printer* printer) {
printer->Outdent(); printer->Outdent();
printer->Print("}\n\n"); printer->Print("}\n\n");
WriteGeneratedCodeAttributes(printer);
printer->Print( printer->Print(
"public override string ToString() {\n" "public override string ToString() {\n"
" return pb::JsonFormatter.ToDiagnosticString(this);\n" " return pb::JsonFormatter.ToDiagnosticString(this);\n"
@ -362,6 +382,7 @@ void MessageGenerator::GenerateFrameworkMethods(io::Printer* printer) {
} }
void MessageGenerator::GenerateMessageSerializationMethods(io::Printer* printer) { void MessageGenerator::GenerateMessageSerializationMethods(io::Printer* printer) {
WriteGeneratedCodeAttributes(printer);
printer->Print( printer->Print(
"public void WriteTo(pb::CodedOutputStream output) {\n"); "public void WriteTo(pb::CodedOutputStream output) {\n");
printer->Indent(); printer->Indent();
@ -377,7 +398,9 @@ void MessageGenerator::GenerateMessageSerializationMethods(io::Printer* printer)
printer->Outdent(); printer->Outdent();
printer->Print( printer->Print(
"}\n" "}\n"
"\n" "\n");
WriteGeneratedCodeAttributes(printer);
printer->Print(
"public int CalculateSize() {\n"); "public int CalculateSize() {\n");
printer->Indent(); printer->Indent();
printer->Print("int size = 0;\n"); printer->Print("int size = 0;\n");
@ -398,6 +421,7 @@ void MessageGenerator::GenerateMergingMethods(io::Printer* printer) {
map<string, string> vars; map<string, string> vars;
vars["class_name"] = class_name(); vars["class_name"] = class_name();
WriteGeneratedCodeAttributes(printer);
printer->Print( printer->Print(
vars, vars,
"public void MergeFrom($class_name$ other) {\n"); "public void MergeFrom($class_name$ other) {\n");
@ -434,6 +458,7 @@ void MessageGenerator::GenerateMergingMethods(io::Printer* printer) {
} }
printer->Outdent(); printer->Outdent();
printer->Print("}\n\n"); printer->Print("}\n\n");
WriteGeneratedCodeAttributes(printer);
printer->Print("public void MergeFrom(pb::CodedInputStream input) {\n"); printer->Print("public void MergeFrom(pb::CodedInputStream input) {\n");
printer->Indent(); printer->Indent();
printer->Print( printer->Print(

View File

@ -65,7 +65,7 @@ void MessageFieldGenerator::GenerateMembers(io::Printer* printer) {
variables_, variables_,
"private $type_name$ $name$_;\n"); "private $type_name$ $name$_;\n");
WritePropertyDocComment(printer, descriptor_); WritePropertyDocComment(printer, descriptor_);
AddDeprecatedFlag(printer); AddPublicMemberAttributes(printer);
printer->Print( printer->Print(
variables_, variables_,
"$access_level$ $type_name$ $property_name$ {\n" "$access_level$ $type_name$ $property_name$ {\n"
@ -159,7 +159,7 @@ MessageOneofFieldGenerator::~MessageOneofFieldGenerator() {
void MessageOneofFieldGenerator::GenerateMembers(io::Printer* printer) { void MessageOneofFieldGenerator::GenerateMembers(io::Printer* printer) {
WritePropertyDocComment(printer, descriptor_); WritePropertyDocComment(printer, descriptor_);
AddDeprecatedFlag(printer); AddPublicMemberAttributes(printer);
printer->Print( printer->Print(
variables_, variables_,
"$access_level$ $type_name$ $property_name$ {\n" "$access_level$ $type_name$ $property_name$ {\n"

View File

@ -71,7 +71,7 @@ void PrimitiveFieldGenerator::GenerateMembers(io::Printer* printer) {
variables_, variables_,
"private $type_name$ $name_def_message$;\n"); "private $type_name$ $name_def_message$;\n");
WritePropertyDocComment(printer, descriptor_); WritePropertyDocComment(printer, descriptor_);
AddDeprecatedFlag(printer); AddPublicMemberAttributes(printer);
printer->Print( printer->Print(
variables_, variables_,
"$access_level$ $type_name$ $property_name$ {\n" "$access_level$ $type_name$ $property_name$ {\n"
@ -174,7 +174,7 @@ PrimitiveOneofFieldGenerator::~PrimitiveOneofFieldGenerator() {
void PrimitiveOneofFieldGenerator::GenerateMembers(io::Printer* printer) { void PrimitiveOneofFieldGenerator::GenerateMembers(io::Printer* printer) {
WritePropertyDocComment(printer, descriptor_); WritePropertyDocComment(printer, descriptor_);
AddDeprecatedFlag(printer); AddPublicMemberAttributes(printer);
printer->Print( printer->Print(
variables_, variables_,
"$access_level$ $type_name$ $property_name$ {\n" "$access_level$ $type_name$ $property_name$ {\n"

View File

@ -122,10 +122,9 @@ void ReflectionClassGenerator::WriteIntroduction(io::Printer* printer) {
} }
printer->Print( printer->Print(
"/// <summary>Holder for reflection information generated from $file_name$</summary>\n" "/// <summary>Holder for reflection information generated from $file_name$</summary>\n",
"[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]\n",
"file_name", file_->name()); "file_name", file_->name());
WriteGeneratedCodeAttributes(printer); WriteGeneratedTypeAttributes(printer);
printer->Print( printer->Print(
"$access_level$ static partial class $reflection_class_name$ {\n" "$access_level$ static partial class $reflection_class_name$ {\n"
"\n", "\n",

View File

@ -64,7 +64,7 @@ void RepeatedEnumFieldGenerator::GenerateMembers(io::Printer* printer) {
printer->Print(variables_, printer->Print(variables_,
"private readonly pbc::RepeatedField<$type_name$> $name$_ = new pbc::RepeatedField<$type_name$>();\n"); "private readonly pbc::RepeatedField<$type_name$> $name$_ = new pbc::RepeatedField<$type_name$>();\n");
WritePropertyDocComment(printer, descriptor_); WritePropertyDocComment(printer, descriptor_);
AddDeprecatedFlag(printer); AddPublicMemberAttributes(printer);
printer->Print( printer->Print(
variables_, variables_,
"$access_level$ pbc::RepeatedField<$type_name$> $property_name$ {\n" "$access_level$ pbc::RepeatedField<$type_name$> $property_name$ {\n"

View File

@ -79,7 +79,7 @@ void RepeatedMessageFieldGenerator::GenerateMembers(io::Printer* printer) {
variables_, variables_,
"private readonly pbc::RepeatedField<$type_name$> $name$_ = new pbc::RepeatedField<$type_name$>();\n"); "private readonly pbc::RepeatedField<$type_name$> $name$_ = new pbc::RepeatedField<$type_name$>();\n");
WritePropertyDocComment(printer, descriptor_); WritePropertyDocComment(printer, descriptor_);
AddDeprecatedFlag(printer); AddPublicMemberAttributes(printer);
printer->Print( printer->Print(
variables_, variables_,
"$access_level$ pbc::RepeatedField<$type_name$> $property_name$ {\n" "$access_level$ pbc::RepeatedField<$type_name$> $property_name$ {\n"

View File

@ -64,7 +64,7 @@ void RepeatedPrimitiveFieldGenerator::GenerateMembers(io::Printer* printer) {
printer->Print(variables_, printer->Print(variables_,
"private readonly pbc::RepeatedField<$type_name$> $name$_ = new pbc::RepeatedField<$type_name$>();\n"); "private readonly pbc::RepeatedField<$type_name$> $name$_ = new pbc::RepeatedField<$type_name$>();\n");
WritePropertyDocComment(printer, descriptor_); WritePropertyDocComment(printer, descriptor_);
AddDeprecatedFlag(printer); AddPublicMemberAttributes(printer);
printer->Print( printer->Print(
variables_, variables_,
"$access_level$ pbc::RepeatedField<$type_name$> $property_name$ {\n" "$access_level$ pbc::RepeatedField<$type_name$> $property_name$ {\n"

View File

@ -56,6 +56,10 @@ SourceGeneratorBase::~SourceGeneratorBase() {
} }
void SourceGeneratorBase::WriteGeneratedCodeAttributes(io::Printer* printer) { void SourceGeneratorBase::WriteGeneratedCodeAttributes(io::Printer* printer) {
printer->Print("[global::System.Diagnostics.DebuggerNonUserCodeAttribute]\n");
}
void SourceGeneratorBase::WriteGeneratedTypeAttributes(io::Printer* printer) {
// This hook can be used to reintroduce generated code attributes in the future. // This hook can be used to reintroduce generated code attributes in the future.
} }

View File

@ -50,7 +50,12 @@ class SourceGeneratorBase {
std::string class_access_level(); std::string class_access_level();
const Options* options(); const Options* options();
// Write any attributes used to decorate generated function members (methods and properties).
// Should not be used to decorate types.
void WriteGeneratedCodeAttributes(io::Printer* printer); void WriteGeneratedCodeAttributes(io::Printer* printer);
// Write any attributes used to decorate generated types.
// Should not be used to decorate function members.
void WriteGeneratedTypeAttributes(io::Printer* printer);
private: private:
const FileDescriptor* descriptor_; const FileDescriptor* descriptor_;

View File

@ -73,7 +73,7 @@ void WrapperFieldGenerator::GenerateMembers(io::Printer* printer) {
";\n" ";\n"
"private $type_name$ $name$_;\n"); "private $type_name$ $name$_;\n");
WritePropertyDocComment(printer, descriptor_); WritePropertyDocComment(printer, descriptor_);
AddDeprecatedFlag(printer); AddPublicMemberAttributes(printer);
printer->Print( printer->Print(
variables_, variables_,
"$access_level$ $type_name$ $property_name$ {\n" "$access_level$ $type_name$ $property_name$ {\n"
@ -169,7 +169,7 @@ void WrapperOneofFieldGenerator::GenerateMembers(io::Printer* printer) {
GenerateCodecCode(printer); GenerateCodecCode(printer);
printer->Print(";\n"); printer->Print(";\n");
WritePropertyDocComment(printer, descriptor_); WritePropertyDocComment(printer, descriptor_);
AddDeprecatedFlag(printer); AddPublicMemberAttributes(printer);
printer->Print( printer->Print(
variables_, variables_,
"$access_level$ $type_name$ $property_name$ {\n" "$access_level$ $type_name$ $property_name$ {\n"