Skip to content

Commit

Permalink
refactored code to centralize and simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
elachlan committed Aug 4, 2022
1 parent 480b424 commit aa5fe0a
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions src/Microsoft.Windows.CsWin32/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2935,6 +2935,15 @@ private bool HasObsoleteAttribute(CustomAttributeHandleCollection attributes)
return null;
}

// add generated code attribute.
if (typeDeclaration is not null)
{
typeDeclaration = typeDeclaration
.WithLeadingTrivia()
.AddAttributeLists(AttributeList().AddAttributes(GeneratedCodeAttribute))
.WithLeadingTrivia(typeDeclaration.GetLeadingTrivia());
}

return typeDeclaration;
}
catch (Exception ex)
Expand Down Expand Up @@ -3190,8 +3199,7 @@ private FieldDeclarationSyntax DeclareConstant(FieldDefinition fieldDef)

FieldDeclarationSyntax? result = FieldDeclaration(VariableDeclaration(fieldType.Type).AddVariables(
VariableDeclarator(Identifier(name)).WithInitializer(EqualsValueClause(value))))
.WithModifiers(modifiers)
.AddAttributeLists(AttributeList().AddAttributes(GeneratedCodeAttribute));
.WithModifiers(modifiers);
result = fieldType.AddMarshalAs(result);
result = this.AddApiDocumentation(result.Declaration.Variables[0].Identifier.ValueText, result);

Expand Down Expand Up @@ -3376,9 +3384,6 @@ private TypeDeclarationSyntax DeclareInterfaceAsStruct(TypeDefinitionHandle type
iface = iface.AddAttributeLists(AttributeList().AddAttributes(supportedOSPlatformAttribute));
}

// add generated code attribute.
iface = iface.AddAttributeLists(AttributeList().AddAttributes(GeneratedCodeAttribute));

return iface;
}

Expand Down Expand Up @@ -3540,9 +3545,6 @@ private TypeDeclarationSyntax DeclareInterfaceAsStruct(TypeDefinitionHandle type
ifaceDeclaration = ifaceDeclaration.AddAttributeLists(AttributeList().AddAttributes(supportedOSPlatformAttribute));
}

// add generated code attribute
ifaceDeclaration = ifaceDeclaration.AddAttributeLists(AttributeList().AddAttributes(GeneratedCodeAttribute));

// Only add overloads to instance collections after everything else is done,
// so we don't leave extension methods behind if we fail to generate the target interface.
this.volatileCode.AddComInterfaceExtension(friendlyOverloads);
Expand Down Expand Up @@ -3774,9 +3776,6 @@ private StructDeclarationSyntax DeclareStruct(TypeDefinitionHandle typeDefHandle
result = result.AddAttributeLists(AttributeList().AddAttributes(GUID(guid)));
}

// add generated code attribute.
result = result.AddAttributeLists(AttributeList().AddAttributes(GeneratedCodeAttribute));

result = this.AddApiDocumentation(name.Identifier.ValueText, result);

return result;
Expand All @@ -3794,8 +3793,7 @@ private ClassDeclarationSyntax DeclareCocreatableClass(TypeDefinition typeDef)
classModifiers = classModifiers.Add(TokenWithSpace(SyntaxKind.PartialKeyword));
ClassDeclarationSyntax result = ClassDeclaration(name.Identifier)
.WithModifiers(classModifiers)
.AddAttributeLists(AttributeList().AddAttributes(GUID(guid), ComImportAttribute))
.AddAttributeLists(AttributeList().AddAttributes(GeneratedCodeAttribute));
.AddAttributeLists(AttributeList().AddAttributes(GUID(guid), ComImportAttribute));

result = this.AddApiDocumentation(name.Identifier.ValueText, result);
return result;
Expand Down Expand Up @@ -3942,8 +3940,7 @@ private StructDeclarationSyntax DeclareTypeDefStruct(TypeDefinition typeDef, Typ
.WithBaseList(BaseList(SingletonSeparatedList<BaseTypeSyntax>(SimpleBaseType(GenericName(nameof(IEquatable<int>), TypeArgumentList().WithGreaterThanToken(TokenWithLineFeed(SyntaxKind.GreaterThanToken))).AddTypeArgumentListArguments(name)))).WithColonToken(TokenWithSpace(SyntaxKind.ColonToken)))
.WithMembers(members)
.WithModifiers(structModifiers)
.AddAttributeLists(AttributeList().WithCloseBracketToken(TokenWithLineFeed(SyntaxKind.CloseBracketToken)).AddAttributes(DebuggerDisplay("{" + fieldName + "}")))
.AddAttributeLists(AttributeList().AddAttributes(GeneratedCodeAttribute));
.AddAttributeLists(AttributeList().WithCloseBracketToken(TokenWithLineFeed(SyntaxKind.CloseBracketToken)).AddAttributes(DebuggerDisplay("{" + fieldName + "}")));

result = this.AddApiDocumentation(name.Identifier.ValueText, result);
return result;
Expand Down Expand Up @@ -4257,7 +4254,6 @@ private EnumDeclarationSyntax DeclareEnum(TypeDefinition typeDef)
AttributeList().WithCloseBracketToken(TokenWithLineFeed(SyntaxKind.CloseBracketToken)).AddAttributes(FlagsAttributeSyntax));
}

result = result.AddAttributeLists(AttributeList().AddAttributes(GeneratedCodeAttribute));
result = this.AddApiDocumentation(name, result);

return result;
Expand Down

0 comments on commit aa5fe0a

Please sign in to comment.