Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Authoring Support: Add WinRT Diagnostics (#615)
* add diagnostics, and unittesting app
- Loading branch information
Showing
12 changed files
with
6,035 additions
and
235 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net5.0-windows10.0.19041.0</TargetFramework> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="NUnit" Version="3.12.0" /> | ||
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.8.0-4.20472.6" PrivateAssets="all" /> | ||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.0" PrivateAssets="all" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\WinRT.SourceGenerator\WinRT.SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true" /> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using System.Collections.Immutable; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace DiagnosticTests | ||
{ | ||
public sealed partial class UnitTesting | ||
{ | ||
/// <summary> | ||
/// CreateCompilation creates a CSharpCompilation | ||
/// </summary> | ||
/// <param name="source">string of source code</param> | ||
/// <returns></returns> | ||
private Compilation CreateCompilation(string source) | ||
=> CSharpCompilation.Create( | ||
assemblyName: "compilation", | ||
syntaxTrees: new[] { CSharpSyntaxTree.ParseText(source, new CSharpParseOptions(LanguageVersion.Preview)) }, | ||
references: new[] { MetadataReference.CreateFromFile(typeof(Binder).GetTypeInfo().Assembly.Location) }, | ||
options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); | ||
|
||
/// <summary> | ||
/// CreateDriver makes a CSharpGeneratorDriver | ||
/// </summary> | ||
/// <param name="compilation"></param> | ||
/// <param name="generators"></param> | ||
/// <returns></returns> | ||
private static GeneratorDriver CreateDriver(Compilation compilation, params ISourceGenerator[] generators) | ||
=> CSharpGeneratorDriver.Create( | ||
generators: ImmutableArray.Create(generators), | ||
additionalTexts: ImmutableArray<AdditionalText>.Empty, | ||
parseOptions: (CSharpParseOptions)compilation.SyntaxTrees.First().Options, | ||
optionsProvider: null); | ||
// todo: pass the CsWinRTComponent config option here so we don't have to comment out the check in the source generator | ||
|
||
/// <summary> | ||
/// RunGenerators makes a driver and applies the given generators to the compilation, storing diagnostics in an out param | ||
/// </summary> | ||
/// <param name="compilation"></param> | ||
/// <param name="diagnostics"></param> | ||
/// <param name="generators"></param> | ||
/// <returns></returns> | ||
private static Compilation RunGenerators(Compilation compilation, out ImmutableArray<Diagnostic> diagnostics, params ISourceGenerator[] generators) | ||
{ | ||
CreateDriver(compilation, generators).RunGeneratorsAndUpdateCompilation(compilation, out var updatedCompilation, out diagnostics); | ||
return updatedCompilation; | ||
} | ||
|
||
/// <summary> | ||
/// Create a HashSet of DiagnosticDescriptor from the Array of Diagnostic | ||
/// </summary> | ||
/// <param name="arr"></param> | ||
/// <returns></returns> | ||
private HashSet<DiagnosticDescriptor> MakeDiagnosticSet(ImmutableArray<Diagnostic> arr) | ||
{ | ||
HashSet<DiagnosticDescriptor> setSoFar = new HashSet<DiagnosticDescriptor>(); | ||
foreach (var d in arr) | ||
{ | ||
setSoFar.Add(d.Descriptor); | ||
} | ||
return setSoFar; | ||
} | ||
} | ||
} |
Oops, something went wrong.