Skip to content

Commit

Permalink
Add GeneratedCodeAttribute to Generated code and Fix Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
elachlan committed Aug 9, 2022
1 parent 10caefc commit d60d410
Show file tree
Hide file tree
Showing 2 changed files with 332 additions and 231 deletions.
64 changes: 52 additions & 12 deletions src/Microsoft.Windows.CsWin32/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ public class Generator : IDisposable
.WithCloseBracketToken(TokenWithLineFeed(SyntaxKind.CloseBracketToken))
.AddAttributes(Attribute(IdentifierName("DefaultDllImportSearchPaths")).AddArgumentListArguments(AttributeArgument(MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, IdentifierName(nameof(DllImportSearchPath)), IdentifierName(nameof(DllImportSearchPath.System32))))));

private static readonly AttributeSyntax GeneratedCodeAttribute = Attribute(IdentifierName("global::System.CodeDom.Compiler.GeneratedCode"))
.WithArgumentList(FixTrivia(AttributeArgumentList().AddArguments(
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(ThisAssembly.AssemblyName))),
AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(ThisAssembly.AssemblyInformationalVersion))))));

private static readonly HashSet<string> ImplicitConversionTypeDefs = new HashSet<string>(StringComparer.Ordinal)
{
"PWSTR",
Expand Down Expand Up @@ -465,13 +470,36 @@ private IEnumerable<MemberDeclarationSyntax> NamespaceMembers
{
get
{
IEnumerable<MemberDeclarationSyntax> result =
from entry in this.committedCode.MembersByModule
select ClassDeclaration(Identifier(this.options.ClassName))
.AddModifiers(TokenWithSpace(this.Visibility), TokenWithSpace(SyntaxKind.StaticKeyword), TokenWithSpace(SyntaxKind.PartialKeyword))
.AddMembers(entry.ToArray())
.WithLeadingTrivia(ParseLeadingTrivia(string.Format(CultureInfo.InvariantCulture, PartialPInvokeContentComment, entry.Key)))
.WithAdditionalAnnotations(new SyntaxAnnotation(SimpleFileNameAnnotation, $"{this.options.ClassName}.{entry.Key}"));
IEnumerable<IGrouping<string, MemberDeclarationSyntax>> members = this.committedCode.MembersByModule;
IEnumerable<MemberDeclarationSyntax> result = Enumerable.Empty<MemberDeclarationSyntax>();
for (int i = 0; i < members.Count(); i++)
{
IGrouping<string, MemberDeclarationSyntax> entry = members.ElementAt(i);
if (i == 0)
{
result = result.Concat(new MemberDeclarationSyntax[]
{
ClassDeclaration(Identifier(this.options.ClassName))
.AddModifiers(TokenWithSpace(this.Visibility), TokenWithSpace(SyntaxKind.StaticKeyword), TokenWithSpace(SyntaxKind.PartialKeyword))
.AddMembers(entry.ToArray())
.AddAttributeLists(AttributeList().AddAttributes(GeneratedCodeAttribute))
.WithLeadingTrivia(ParseLeadingTrivia(string.Format(CultureInfo.InvariantCulture, PartialPInvokeContentComment, entry.Key)))
.WithAdditionalAnnotations(new SyntaxAnnotation(SimpleFileNameAnnotation, $"{this.options.ClassName}.{entry.Key}")),
});
}
else
{
result = result.Concat(new MemberDeclarationSyntax[]
{
ClassDeclaration(Identifier(this.options.ClassName))
.AddModifiers(TokenWithSpace(this.Visibility), TokenWithSpace(SyntaxKind.StaticKeyword), TokenWithSpace(SyntaxKind.PartialKeyword))
.AddMembers(entry.ToArray())
.WithLeadingTrivia(ParseLeadingTrivia(string.Format(CultureInfo.InvariantCulture, PartialPInvokeContentComment, entry.Key)))
.WithAdditionalAnnotations(new SyntaxAnnotation(SimpleFileNameAnnotation, $"{this.options.ClassName}.{entry.Key}")),
});
}
}

result = result.Concat(this.committedCode.GeneratedTypes);

ClassDeclarationSyntax inlineArrayIndexerExtensionsClass = this.DeclareInlineArrayIndexerExtensionsClass();
Expand Down Expand Up @@ -1673,10 +1701,11 @@ internal void RequestConstant(FieldDefinitionHandle fieldDefHandle)
.AddModifiers(TokenWithSpace(this.Visibility))
.WithBaseList(BaseList(SingletonSeparatedList<BaseTypeSyntax>(SimpleBaseType(SafeHandleTypeSyntax))))
.AddMembers(members.ToArray())
.AddAttributeLists(AttributeList().AddAttributes(GeneratedCodeAttribute))
.WithLeadingTrivia(ParseLeadingTrivia($@"
/// <summary>
/// Represents a Win32 handle that can be closed with <see cref=""{this.options.ClassName}.{renamedReleaseMethod ?? releaseMethod}""/>.
/// </summary>
/// <summary>
/// Represents a Win32 handle that can be closed with <see cref=""{this.options.ClassName}.{renamedReleaseMethod ?? releaseMethod}""/>.
/// </summary>
"));

this.volatileCode.AddSafeHandleType(safeHandleDeclaration);
Expand Down Expand Up @@ -2861,6 +2890,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 @@ -3142,14 +3180,16 @@ private ClassDeclarationSyntax DeclareInlineArrayIndexerExtensionsClass()
{
return ClassDeclaration(InlineArrayIndexerExtensionsClassName.Identifier)
.AddMembers(this.committedCode.InlineArrayIndexerExtensions.ToArray())
.WithModifiers(TokenList(TokenWithSpace(this.Visibility), TokenWithSpace(SyntaxKind.StaticKeyword), TokenWithSpace(SyntaxKind.PartialKeyword)));
.WithModifiers(TokenList(TokenWithSpace(this.Visibility), TokenWithSpace(SyntaxKind.StaticKeyword), TokenWithSpace(SyntaxKind.PartialKeyword)))
.AddAttributeLists(AttributeList().AddAttributes(GeneratedCodeAttribute));
}

private ClassDeclarationSyntax DeclareComInterfaceFriendlyExtensionsClass()
{
return ClassDeclaration(ComInterfaceFriendlyExtensionsClassName.Identifier)
.AddMembers(this.committedCode.ComInterfaceExtensions.ToArray())
.WithModifiers(TokenList(TokenWithSpace(this.Visibility), TokenWithSpace(SyntaxKind.StaticKeyword), TokenWithSpace(SyntaxKind.PartialKeyword)));
.WithModifiers(TokenList(TokenWithSpace(this.Visibility), TokenWithSpace(SyntaxKind.StaticKeyword), TokenWithSpace(SyntaxKind.PartialKeyword)))
.AddAttributeLists(AttributeList().AddAttributes(GeneratedCodeAttribute));
}

/// <summary>
Expand Down
Loading

0 comments on commit d60d410

Please sign in to comment.