-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add source generator to emit supporting attribute types (#181)
* Add blank dnne-analyzers project * Add Roslyn dependencies and basic setup * Add AttributesGenerator type to emit attributes * Add DNNE.Analyzers.targets file * Wire up source generator to build setup * Use .NET 7 SDK in the CI script * Add ExportAttribute to the generated attributes * Update unit tests, remove local attributes * Reference analyzer directly in test project if needed * Add [AttributeUsage] to generated attributes * Add [ExcludeFromCodeCoverage] to generated attributes * Fix pseudo package scenario. Import new targets file using DNNE.targets --------- Co-authored-by: Aaron Robinson <arobins@microsoft.com>
- Loading branch information
1 parent
0093cdc
commit 4ea06f6
Showing
13 changed files
with
259 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
; Shipped analyzer releases | ||
; https://github.com/dotnet/roslyn-analyzers/blob/master/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md | ||
|
||
## Release 1.0 | ||
|
||
### New Rules | ||
|
||
Rule ID | Category | Severity | Notes | ||
--------|----------|----------|------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
; Unshipped analyzer release | ||
; https://github.com/dotnet/roslyn-analyzers/blob/master/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace DNNE; | ||
|
||
/// <summary> | ||
/// A generator that generates all the necessary DNNE attributes. | ||
/// </summary> | ||
[Generator(LanguageNames.CSharp)] | ||
public sealed class AttributesGenerator : IIncrementalGenerator | ||
{ | ||
/// <inheritdoc/> | ||
public void Initialize(IncrementalGeneratorInitializationContext context) | ||
{ | ||
context.RegisterPostInitializationOutput(static context => | ||
{ | ||
context.AddSource("DnneAttributes.g.cs", """ | ||
// <auto-generated/> | ||
#pragma warning disable | ||
namespace DNNE | ||
{ | ||
/// <summary> | ||
/// Defines a C export. Can be used when updating to use <c>UnmanagedCallersOnlyAttribute</c> would take more time. | ||
/// </summary> | ||
[global::System.AttributeUsage(global::System.AttributeTargets.Method, Inherited = false)] | ||
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] | ||
internal sealed class ExportAttribute : global::System.Attribute | ||
{ | ||
/// <summary> | ||
/// Creates a new <see cref="ExportAttribute"/> instance. | ||
/// </summary> | ||
public ExportAttribute() | ||
{ | ||
} | ||
/// <summary> | ||
/// Gets or sets the entry point to use to produce the C export. | ||
/// </summary> | ||
public string EntryPoint { get; set; } | ||
} | ||
/// <summary> | ||
/// Provides C code to be defined early in the generated C header file. | ||
/// </summary> | ||
/// <remarks> | ||
/// This attribute is respected on an exported method declaration or on a parameter for the method. | ||
/// The following header files will be included prior to the code being defined. | ||
/// <list type="bullet"> | ||
/// <item><c>stddef.h</c></item> | ||
/// <item><c>stdint.h</c></item> | ||
/// <item><c>dnne.h</c></item> | ||
/// </list> | ||
/// </remarks> | ||
[global::System.AttributeUsage(global::System.AttributeTargets.Method | global::System.AttributeTargets.Parameter, Inherited = false)] | ||
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] | ||
internal sealed class C99DeclCodeAttribute : global::System.Attribute | ||
{ | ||
/// <summary> | ||
/// Creates a new <see cref="C99DeclCodeAttribute"/> instance with the specified parameters. | ||
/// </summary> | ||
/// <param name="code">The C code to be defined in the generated C header file.</param> | ||
public C99DeclCodeAttribute(string code) | ||
{ | ||
} | ||
} | ||
/// <summary> | ||
/// Defines the C type to be used. | ||
/// </summary> | ||
/// <remarks> | ||
/// The level of indirection should be included in the supplied string. | ||
/// </remarks> | ||
[global::System.AttributeUsage(global::System.AttributeTargets.Parameter | global::System.AttributeTargets.ReturnValue, Inherited = false)] | ||
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] | ||
internal sealed class C99TypeAttribute : global::System.Attribute | ||
{ | ||
/// <summary> | ||
/// Creates a new <see cref="C99TypeAttribute"/> instance with the specified parameters. | ||
/// </summary> | ||
/// <param name="code">The C type to be used.</param> | ||
public C99TypeAttribute(string code) | ||
{ | ||
} | ||
} | ||
} | ||
"""); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<Project> | ||
|
||
<!-- Get the analyzer from the DNNE NuGet package --> | ||
<Target Name="_DnneGatherAnalyzers"> | ||
<ItemGroup> | ||
<_DnneAnalyzer Include="@(Analyzer)" Condition="'%(Analyzer.NuGetPackageId)' == 'DNNE'" /> | ||
</ItemGroup> | ||
</Target> | ||
|
||
<!-- Remove the analyzer if using Roslyn < 4.0 (DNNE's generators require Roslyn 4.0) --> | ||
<Target Name="_DnneRemoveAnalyzersForRoslyn3" | ||
Condition="'$(CSharpCoreTargetsPath)' != ''" | ||
AfterTargets="ResolvePackageDependenciesForBuild;ResolveNuGetPackageAssets" | ||
DependsOnTargets="_DnneGatherAnalyzers"> | ||
|
||
<!-- | ||
Use the CSharpCoreTargetsPath property to find the version of the compiler we are using. This is the same mechanism | ||
MSBuild uses to find the compiler. We could check the assembly version for any compiler assembly (since they all have | ||
the same version) but Microsoft.Build.Tasks.CodeAnalysis.dll is where MSBuild loads the compiler tasks from so if | ||
someone is getting creative with msbuild tasks/targets this is the "most correct" assembly to check. | ||
--> | ||
<GetAssemblyIdentity AssemblyFiles="$([System.IO.Path]::Combine(`$([System.IO.Path]::GetDirectoryName($(CSharpCoreTargetsPath)))`,`Microsoft.Build.Tasks.CodeAnalysis.dll`))"> | ||
<Output TaskParameter="Assemblies" ItemName="DnneCurrentCompilerAssemblyIdentity"/> | ||
</GetAssemblyIdentity> | ||
|
||
<PropertyGroup> | ||
|
||
<!-- Transform the resulting item from GetAssemblyIdentity into a property representing its assembly version --> | ||
<DnneCurrentCompilerVersion>@(DnneCurrentCompilerAssemblyIdentity->'%(Version)')</DnneCurrentCompilerVersion> | ||
|
||
<!-- The CurrentCompilerVersionIsNotNewEnough property can now be defined based on the Roslyn assembly version --> | ||
<DnneCurrentCompilerVersionIsNotNewEnough Condition="$([MSBuild]::VersionLessThan($(DnneCurrentCompilerVersion), 4.0))">true</DnneCurrentCompilerVersionIsNotNewEnough> | ||
</PropertyGroup> | ||
|
||
<!-- If the Roslyn version is < 4.0, disable the source generators --> | ||
<ItemGroup Condition ="'$(DnneCurrentCompilerVersionIsNotNewEnough)' == 'true'"> | ||
<Analyzer Remove="@(_DnneAnalyzer)"/> | ||
</ItemGroup> | ||
|
||
<!-- | ||
If the source generators are disabled, also emit a warning. This would've been produced by MSBuild itself as well, but | ||
emitting this manually lets us customize the message to inform developers as to why exactly the generators have been | ||
disabled, and that DNNE will not work at all unless a more up to date IDE or compiler version are used. | ||
--> | ||
<Warning Condition ="'$(DnneCurrentCompilerVersionIsNotNewEnough)' == 'true'" | ||
Code="DNNECFG0001" | ||
Text="The DNNE source generators have been disabled on the current configuration, as they need Roslyn 4.0 in order to work. DNNE requires the source generators to generate the additional supporting attribute types. To use them, a more up to date IDE (eg. VS 2022 17.0 or greater) or .NET SDK version (.NET 6.0.400 SDK or greater) is needed."/> | ||
</Target> | ||
|
||
<!-- Remove the analyzer if Roslyn is missing --> | ||
<Target Name="_DnneRemoveAnalyzersForRosynNotFound" | ||
Condition="'$(CSharpCoreTargetsPath)' == ''" | ||
AfterTargets="ResolvePackageDependenciesForBuild;ResolveNuGetPackageAssets" | ||
DependsOnTargets="_DnneGatherAnalyzers"> | ||
|
||
<!-- If no Roslyn assembly could be found, just remove the analyzer without emitting a warning --> | ||
<ItemGroup> | ||
<Analyzer Remove="@(_DnneAnalyzer)"/> | ||
</ItemGroup> | ||
</Target> | ||
|
||
<!-- | ||
Inform the user if packages.config is used (as the analyzers and the source generators | ||
won't work at all). Since packages.config can only be used with legacy-style projects, | ||
the entire package can be skipped if an SDK-style project is used. | ||
--> | ||
<Target Name="_DnneWarnForPackagesConfigUse" | ||
AfterTargets="ResolvePackageDependenciesForBuild;ResolveNuGetPackageAssets" | ||
Condition="'$(UsingMicrosoftNetSDK)' != 'true'"> | ||
|
||
<!-- | ||
Check whether packages are being restored via packages.config, by reading the associated MSBuild property. | ||
This happens when either the project style is using packages.config, or when explicitly requested. | ||
See https://learn.microsoft.com/nuget/reference/msbuild-targets#restoring-packagereference-and-packagesconfig-projects-with-msbuild. | ||
--> | ||
<PropertyGroup> | ||
<DnneIsTargetProjectUsingPackagesConfig Condition ="'$(RestorePackagesConfig)' == 'true' OR '$(RestoreProjectStyle)' == 'PackagesConfig'">true</DnneIsTargetProjectUsingPackagesConfig> | ||
</PropertyGroup> | ||
|
||
<!-- | ||
If no packages.config properties are set, also try to manually find the packages.config file. | ||
This will be in the @(None) elements, if present. Doing so makes sure this works in builds as | ||
well, since the implicit targets populating the properties above only run when restoring. | ||
Since the packages.config file will always be in the root of the project, if present, we will | ||
match with the full item spec (see https://learn.microsoft.com/nuget/reference/packages-config). | ||
--> | ||
<FindInList ItemSpecToFind="packages.config" | ||
List="@(None)" | ||
MatchFileNameOnly="false" | ||
Condition="'$(DnneIsTargetProjectUsingPackagesConfig)' != 'true'"> | ||
<Output TaskParameter="ItemFound" PropertyName="DnnePackagesConfigFile"/> | ||
</FindInList> | ||
|
||
<!-- Make sure to update the MSBuild property if the above task did find something --> | ||
<PropertyGroup> | ||
<DnneIsTargetProjectUsingPackagesConfig Condition ="'$(DnnePackagesConfigFile)' == 'packages.config'">true</DnneIsTargetProjectUsingPackagesConfig> | ||
</PropertyGroup> | ||
|
||
<!-- Emit a warning in case packages.config is used --> | ||
<Warning Condition ="'$(DnneIsTargetProjectUsingPackagesConfig)' == 'true'" | ||
Code="DNNECFG0002" | ||
Text="The DNNE source generators might not be loaded correctly, as the current project is using the packages.config setup to restore NuGet packages. Source generators require PackageReference to be used (either in a legacy-style or SDK-style .csproj project, both are supported as long as PackageReference is used)."/> | ||
</Target> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<RootNamespace>DNNE</RootNamespace> | ||
<LangVersion>11.0</LangVersion> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.0.1" PrivateAssets="all" Pack="false" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<AdditionalFiles Include="AnalyzerReleases.Shipped.md" /> | ||
<AdditionalFiles Include="AnalyzerReleases.Unshipped.md" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.