1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Diagnostics;
namespace ILLink.RoslynAnalyzer.Tests
{
internal static class TestCaseCompilation
{
private static readonly ImmutableArray<DiagnosticAnalyzer> SupportedDiagnosticAnalyzers =
ImmutableArray.Create<DiagnosticAnalyzer> (
new RequiresDynamicCodeAnalyzer (),
new COMAnalyzer (),
new RequiresAssemblyFilesAnalyzer (),
new RequiresUnreferencedCodeAnalyzer (),
new DynamicallyAccessedMembersAnalyzer ());
public static Task<(CompilationWithAnalyzers Compilation, SemanticModel SemanticModel, List<Diagnostic> ExceptionDiagnostics)> CreateCompilation (
string src,
(string, string)[]? globalAnalyzerOptions = null,
IEnumerable<MetadataReference>? additionalReferences = null,
IEnumerable<SyntaxTree>? additionalSources = null,
IEnumerable<AdditionalText>? additionalFiles = null)
=> CreateCompilation (CSharpSyntaxTree.ParseText (src), globalAnalyzerOptions, additionalReferences, additionalSources, additionalFiles);
public static async Task<(CompilationWithAnalyzers Compilation, SemanticModel SemanticModel, List<Diagnostic> ExceptionDiagnostics)> CreateCompilation (
SyntaxTree src,
(string, string)[]? globalAnalyzerOptions = null,
IEnumerable<MetadataReference>? additionalReferences = null,
IEnumerable<SyntaxTree>? additionalSources = null,
IEnumerable<AdditionalText>? additionalFiles = null)
{
var mdRef = MetadataReference.CreateFromFile (typeof (Mono.Linker.Tests.Cases.Expectations.Metadata.BaseMetadataAttribute).Assembly.Location);
additionalReferences ??= Array.Empty<MetadataReference> ();
var sources = new List<SyntaxTree> () { src };
sources.AddRange (additionalSources ?? Array.Empty<SyntaxTree> ());
var comp = CSharpCompilation.Create (
assemblyName: Guid.NewGuid ().ToString ("N"),
syntaxTrees: sources,
references: (await TestCaseUtils.GetNet6References ()).Add (mdRef).AddRange (additionalReferences),
new CSharpCompilationOptions (OutputKind.DynamicallyLinkedLibrary));
var analyzerOptions = new AnalyzerOptions (
additionalFiles: additionalFiles?.ToImmutableArray () ?? ImmutableArray<AdditionalText>.Empty,
new SimpleAnalyzerOptions (globalAnalyzerOptions));
var exceptionDiagnostics = new List<Diagnostic> ();
var compWithAnalyzerOptions = new CompilationWithAnalyzersOptions (
analyzerOptions,
(Exception exception, DiagnosticAnalyzer diagnosticAnalyzer, Diagnostic diagnostic) => {
exceptionDiagnostics.Add (diagnostic);
},
concurrentAnalysis: true,
logAnalyzerExecutionTime: false);
return (new CompilationWithAnalyzers (comp, SupportedDiagnosticAnalyzers, compWithAnalyzerOptions), comp.GetSemanticModel (src), exceptionDiagnostics);
}
sealed class SimpleAnalyzerOptions : AnalyzerConfigOptionsProvider
{
public SimpleAnalyzerOptions ((string, string)[]? globalOptions)
{
globalOptions ??= Array.Empty<(string, string)> ();
GlobalOptions = new SimpleAnalyzerConfigOptions (ImmutableDictionary.CreateRange (
StringComparer.OrdinalIgnoreCase,
globalOptions.Select (x => new KeyValuePair<string, string> (x.Item1, x.Item2))));
}
public override AnalyzerConfigOptions GlobalOptions { get; }
public override AnalyzerConfigOptions GetOptions (SyntaxTree tree)
=> SimpleAnalyzerConfigOptions.Empty;
public override AnalyzerConfigOptions GetOptions (AdditionalText textFile)
=> SimpleAnalyzerConfigOptions.Empty;
sealed class SimpleAnalyzerConfigOptions : AnalyzerConfigOptions
{
public static readonly SimpleAnalyzerConfigOptions Empty = new SimpleAnalyzerConfigOptions (ImmutableDictionary<string, string>.Empty);
private readonly ImmutableDictionary<string, string> _dict;
public SimpleAnalyzerConfigOptions (ImmutableDictionary<string, string> dict)
{
_dict = dict;
}
// Suppress warning about missing nullable attributes
#pragma warning disable 8765
public override bool TryGetValue (string key, out string? value)
=> _dict.TryGetValue (key, out value);
#pragma warning restore 8765
}
}
}
}
|