Skip to content

Commit

Permalink
Authoring Support: Add WinRT Diagnostics (#615)
Browse files Browse the repository at this point in the history
* add diagnostics, and unittesting app
  • Loading branch information
j0shuams committed Dec 30, 2020
1 parent 88af9af commit d1b538e
Show file tree
Hide file tree
Showing 12 changed files with 6,035 additions and 235 deletions.
6 changes: 3 additions & 3 deletions src/Authoring/AuthoringSample/Program.cs
Expand Up @@ -3,12 +3,11 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Metadata;
using WinRT;
using System.Runtime.InteropServices.WindowsRuntime;

namespace AuthoringSample
{
Expand Down Expand Up @@ -181,7 +180,7 @@ public struct ComplexStruct
public BasicStruct BasicStruct;
}

public class CustomWWW : IWwwFormUrlDecoderEntry
public sealed class CustomWWW : IWwwFormUrlDecoderEntry
{
public string Name => "CustomWWW";

Expand Down Expand Up @@ -330,6 +329,7 @@ public int GetSum(CustomDictionary dictionary, string element)
return -1;
}


public void SetTypeToTestClass()
{
Type = typeof(TestClass);
Expand Down
19 changes: 19 additions & 0 deletions src/Authoring/DiagnosticTests/DiagnosticTests.csproj
@@ -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>
66 changes: 66 additions & 0 deletions src/Authoring/DiagnosticTests/Helpers.cs
@@ -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;
}
}
}

0 comments on commit d1b538e

Please sign in to comment.