Skip to content

Commit ecf7471

Browse files
committed
Merge pull request #2102 from sharwell/roslyn-1.2
Update to Roslyn 1.2
2 parents 710a7e2 + cc7deaf commit ecf7471

175 files changed

Lines changed: 1001 additions & 1972 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/Helpers/FixAllContextHelper.cs

Lines changed: 2 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,18 @@
33

44
namespace StyleCop.Analyzers.Helpers
55
{
6-
using System;
76
using System.Collections.Concurrent;
87
using System.Collections.Generic;
98
using System.Collections.Immutable;
109
using System.Linq;
11-
using System.Reflection;
1210
using System.Threading;
1311
using System.Threading.Tasks;
1412
using Microsoft.CodeAnalysis;
1513
using Microsoft.CodeAnalysis.CodeFixes;
1614
using Microsoft.CodeAnalysis.Diagnostics;
17-
using Microsoft.CodeAnalysis.Text;
1815

1916
internal static class FixAllContextHelper
2017
{
21-
private static readonly ImmutableDictionary<string, ImmutableArray<Type>> DiagnosticAnalyzers = GetAllAnalyzers();
22-
23-
private static readonly Func<CompilationWithAnalyzers, SyntaxTree, CancellationToken, Task<ImmutableArray<Diagnostic>>> GetAnalyzerSyntaxDiagnosticsAsync;
24-
private static readonly Func<CompilationWithAnalyzers, SemanticModel, TextSpan?, CancellationToken, Task<ImmutableArray<Diagnostic>>> GetAnalyzerSemanticDiagnosticsAsync;
25-
private static readonly Func<CompilationWithAnalyzers, ImmutableArray<DiagnosticAnalyzer>, CancellationToken, Task<ImmutableArray<Diagnostic>>> GetAnalyzerCompilationDiagnosticsAsync;
26-
27-
static FixAllContextHelper()
28-
{
29-
Version roslynVersion = typeof(AdditionalText).GetTypeInfo().Assembly.GetName().Version;
30-
bool avoidGetAnalyzerDiagnosticsAsync = roslynVersion >= new Version(1, 1, 0, 0) && roslynVersion < new Version(1, 2, 0, 0);
31-
if (avoidGetAnalyzerDiagnosticsAsync)
32-
{
33-
var methodInfo = typeof(CompilationWithAnalyzers).GetRuntimeMethod(nameof(GetAnalyzerSyntaxDiagnosticsAsync), new[] { typeof(SyntaxTree), typeof(CancellationToken) });
34-
GetAnalyzerSyntaxDiagnosticsAsync = (Func<CompilationWithAnalyzers, SyntaxTree, CancellationToken, Task<ImmutableArray<Diagnostic>>>)methodInfo?.CreateDelegate(typeof(Func<CompilationWithAnalyzers, SyntaxTree, CancellationToken, Task<ImmutableArray<Diagnostic>>>));
35-
36-
methodInfo = typeof(CompilationWithAnalyzers).GetRuntimeMethod(nameof(GetAnalyzerSemanticDiagnosticsAsync), new[] { typeof(SemanticModel), typeof(TextSpan?), typeof(CancellationToken) });
37-
GetAnalyzerSemanticDiagnosticsAsync = (Func<CompilationWithAnalyzers, SemanticModel, TextSpan?, CancellationToken, Task<ImmutableArray<Diagnostic>>>)methodInfo?.CreateDelegate(typeof(Func<CompilationWithAnalyzers, SemanticModel, TextSpan?, CancellationToken, Task<ImmutableArray<Diagnostic>>>));
38-
39-
methodInfo = typeof(CompilationWithAnalyzers).GetRuntimeMethod(nameof(GetAnalyzerCompilationDiagnosticsAsync), new[] { typeof(ImmutableArray<DiagnosticAnalyzer>), typeof(CancellationToken) });
40-
GetAnalyzerCompilationDiagnosticsAsync = (Func<CompilationWithAnalyzers, ImmutableArray<DiagnosticAnalyzer>, CancellationToken, Task<ImmutableArray<Diagnostic>>>)methodInfo?.CreateDelegate(typeof(Func<CompilationWithAnalyzers, ImmutableArray<DiagnosticAnalyzer>, CancellationToken, Task<ImmutableArray<Diagnostic>>>));
41-
}
42-
}
43-
4418
public static async Task<ImmutableDictionary<Document, ImmutableArray<Diagnostic>>> GetDocumentDiagnosticsToFixAsync(FixAllContext fixAllContext)
4519
{
4620
var allDiagnostics = ImmutableArray<Diagnostic>.Empty;
@@ -132,87 +106,7 @@ public static async Task<ImmutableDictionary<Project, ImmutableArray<Diagnostic>
132106

133107
public static async Task<ImmutableArray<Diagnostic>> GetAllDiagnosticsAsync(Compilation compilation, CompilationWithAnalyzers compilationWithAnalyzers, ImmutableArray<DiagnosticAnalyzer> analyzers, IEnumerable<Document> documents, bool includeCompilerDiagnostics, CancellationToken cancellationToken)
134108
{
135-
if (GetAnalyzerSyntaxDiagnosticsAsync == null || GetAnalyzerSemanticDiagnosticsAsync == null)
136-
{
137-
// In everything except Roslyn 1.1, we use GetAllDiagnosticsAsync and return the result.
138-
var allDiagnostics = await compilationWithAnalyzers.GetAllDiagnosticsAsync().ConfigureAwait(false);
139-
return allDiagnostics;
140-
}
141-
142-
compilationWithAnalyzers.Compilation.GetDeclarationDiagnostics(cancellationToken);
143-
144-
// Note that the following loop to obtain syntax and semantic diagnostics for each document cannot operate
145-
// on parallel due to our use of a single CompilationWithAnalyzers instance.
146-
var diagnostics = ImmutableArray.CreateBuilder<Diagnostic>();
147-
foreach (var document in documents)
148-
{
149-
var syntaxTree = await document.GetSyntaxTreeAsync(cancellationToken).ConfigureAwait(false);
150-
var syntaxDiagnostics = await GetAnalyzerSyntaxDiagnosticsAsync(compilationWithAnalyzers, syntaxTree, cancellationToken).ConfigureAwait(false);
151-
diagnostics.AddRange(syntaxDiagnostics);
152-
153-
var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
154-
var semanticDiagnostics = await GetAnalyzerSemanticDiagnosticsAsync(compilationWithAnalyzers, semanticModel, default(TextSpan?), cancellationToken).ConfigureAwait(false);
155-
diagnostics.AddRange(semanticDiagnostics);
156-
}
157-
158-
foreach (var analyzer in analyzers)
159-
{
160-
diagnostics.AddRange(await GetAnalyzerCompilationDiagnosticsAsync(compilationWithAnalyzers, ImmutableArray.Create(analyzer), cancellationToken).ConfigureAwait(false));
161-
}
162-
163-
if (includeCompilerDiagnostics)
164-
{
165-
// This is the special handling for cases where code fixes operate on warnings produced by the C#
166-
// compiler, as opposed to being created by specific analyzers.
167-
var compilerDiagnostics = compilation.GetDiagnostics(cancellationToken);
168-
diagnostics.AddRange(compilerDiagnostics);
169-
}
170-
171-
return diagnostics.ToImmutable();
172-
}
173-
174-
private static ImmutableDictionary<string, ImmutableArray<Type>> GetAllAnalyzers()
175-
{
176-
Assembly assembly = typeof(NoCodeFixAttribute).GetTypeInfo().Assembly;
177-
178-
var diagnosticAnalyzerType = typeof(DiagnosticAnalyzer);
179-
180-
var analyzers = ImmutableDictionary.CreateBuilder<string, ImmutableArray<Type>>();
181-
182-
foreach (var type in assembly.DefinedTypes)
183-
{
184-
if (type.IsSubclassOf(diagnosticAnalyzerType) && !type.IsAbstract)
185-
{
186-
Type analyzerType = type.AsType();
187-
DiagnosticAnalyzer analyzer = (DiagnosticAnalyzer)Activator.CreateInstance(analyzerType);
188-
foreach (var descriptor in analyzer.SupportedDiagnostics)
189-
{
190-
ImmutableArray<Type> types;
191-
if (analyzers.TryGetValue(descriptor.Id, out types))
192-
{
193-
types = types.Add(analyzerType);
194-
}
195-
else
196-
{
197-
types = ImmutableArray.Create(analyzerType);
198-
}
199-
200-
analyzers[descriptor.Id] = types;
201-
}
202-
}
203-
}
204-
205-
return analyzers.ToImmutable();
206-
}
207-
208-
private static ImmutableArray<DiagnosticAnalyzer> GetDiagnosticAnalyzersForContext(FixAllContext fixAllContext)
209-
{
210-
return DiagnosticAnalyzers
211-
.Where(x => fixAllContext.DiagnosticIds.Contains(x.Key))
212-
.SelectMany(x => x.Value)
213-
.Distinct()
214-
.Select(type => (DiagnosticAnalyzer)Activator.CreateInstance(type))
215-
.ToImmutableArray();
109+
return await compilationWithAnalyzers.GetAllDiagnosticsAsync().ConfigureAwait(false);
216110
}
217111

218112
/// <summary>
@@ -225,32 +119,7 @@ private static ImmutableArray<DiagnosticAnalyzer> GetDiagnosticAnalyzersForConte
225119
/// successfully, the <see cref="Task{TResult}.Result"/> will contain the requested diagnostics.</returns>
226120
private static async Task<ImmutableArray<Diagnostic>> GetAllDiagnosticsAsync(FixAllContext fixAllContext, Project project)
227121
{
228-
if (GetAnalyzerSyntaxDiagnosticsAsync == null || GetAnalyzerSemanticDiagnosticsAsync == null)
229-
{
230-
return await fixAllContext.GetAllDiagnosticsAsync(project).ConfigureAwait(false);
231-
}
232-
233-
/*
234-
* The rest of this method is workaround code for issues with Roslyn 1.1...
235-
*/
236-
237-
var analyzers = GetDiagnosticAnalyzersForContext(fixAllContext);
238-
239-
// Most code fixes in this project operate on diagnostics reported by analyzers in this project. However, a
240-
// few code fixes also operate on standard warnings produced by the C# compiler. Special handling is
241-
// required for the latter case since these warnings are not considered "analyzer diagnostics".
242-
bool includeCompilerDiagnostics = fixAllContext.DiagnosticIds.Any(x => x.StartsWith("CS", StringComparison.Ordinal));
243-
244-
// Use a single CompilationWithAnalyzers for the entire operation. This allows us to use the
245-
// GetDeclarationDiagnostics workaround for dotnet/roslyn#7446 a single time, rather than once per document.
246-
var compilation = await project.GetCompilationAsync(fixAllContext.CancellationToken).ConfigureAwait(false);
247-
var compilationWithAnalyzers = compilation.WithAnalyzers(analyzers, project.AnalyzerOptions, fixAllContext.CancellationToken);
248-
ImmutableArray<Diagnostic> diagnostics = await GetAllDiagnosticsAsync(compilation, compilationWithAnalyzers, analyzers, project.Documents, includeCompilerDiagnostics, fixAllContext.CancellationToken).ConfigureAwait(false);
249-
250-
// Make sure to filter the results to the set requested for the Fix All operation, since analyzers can
251-
// report diagnostics with different IDs.
252-
diagnostics = diagnostics.RemoveAll(x => !fixAllContext.DiagnosticIds.Contains(x.Id));
253-
return diagnostics;
122+
return await fixAllContext.GetAllDiagnosticsAsync(project).ConfigureAwait(false);
254123
}
255124

256125
private static async Task<ImmutableDictionary<Document, ImmutableArray<Diagnostic>>> GetDocumentDiagnosticsToFixAsync(

StyleCop.Analyzers/StyleCop.Analyzers.CodeFixes/StyleCop.Analyzers.CodeFixes.csproj

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -166,28 +166,28 @@
166166
</AdditionalFiles>
167167
</ItemGroup>
168168
<ItemGroup>
169-
<Reference Include="Microsoft.CodeAnalysis, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
170-
<HintPath>..\..\packages\Microsoft.CodeAnalysis.Common.1.0.0\lib\portable-net45+win8\Microsoft.CodeAnalysis.dll</HintPath>
169+
<Reference Include="Microsoft.CodeAnalysis, Version=1.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
170+
<HintPath>..\..\packages\Microsoft.CodeAnalysis.Common.1.2.1\lib\portable-net45+win8\Microsoft.CodeAnalysis.dll</HintPath>
171171
<Private>True</Private>
172172
</Reference>
173-
<Reference Include="Microsoft.CodeAnalysis.CSharp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
174-
<HintPath>..\..\packages\Microsoft.CodeAnalysis.CSharp.1.0.0\lib\portable-net45+win8\Microsoft.CodeAnalysis.CSharp.dll</HintPath>
173+
<Reference Include="Microsoft.CodeAnalysis.CSharp, Version=1.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
174+
<HintPath>..\..\packages\Microsoft.CodeAnalysis.CSharp.1.2.1\lib\portable-net45+win8\Microsoft.CodeAnalysis.CSharp.dll</HintPath>
175175
<Private>True</Private>
176176
</Reference>
177-
<Reference Include="Microsoft.CodeAnalysis.CSharp.Workspaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
178-
<HintPath>..\..\packages\Microsoft.CodeAnalysis.CSharp.Workspaces.1.0.0\lib\portable-net45+win8\Microsoft.CodeAnalysis.CSharp.Workspaces.dll</HintPath>
177+
<Reference Include="Microsoft.CodeAnalysis.CSharp.Workspaces, Version=1.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
178+
<HintPath>..\..\packages\Microsoft.CodeAnalysis.CSharp.Workspaces.1.2.1\lib\portable-net45+win8\Microsoft.CodeAnalysis.CSharp.Workspaces.dll</HintPath>
179179
<Private>True</Private>
180180
</Reference>
181-
<Reference Include="Microsoft.CodeAnalysis.Workspaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
182-
<HintPath>..\..\packages\Microsoft.CodeAnalysis.Workspaces.Common.1.0.0\lib\portable-net45+win8\Microsoft.CodeAnalysis.Workspaces.dll</HintPath>
181+
<Reference Include="Microsoft.CodeAnalysis.Workspaces, Version=1.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
182+
<HintPath>..\..\packages\Microsoft.CodeAnalysis.Workspaces.Common.1.2.1\lib\portable-net45+win8\Microsoft.CodeAnalysis.Workspaces.dll</HintPath>
183183
<Private>True</Private>
184184
</Reference>
185185
<Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
186186
<HintPath>..\..\packages\Newtonsoft.Json.7.0.1\lib\portable-net40+sl5+wp80+win8+wpa81\Newtonsoft.Json.dll</HintPath>
187187
<Private>True</Private>
188188
</Reference>
189-
<Reference Include="System.Collections.Immutable, Version=1.1.36.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
190-
<HintPath>..\..\packages\System.Collections.Immutable.1.1.36\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll</HintPath>
189+
<Reference Include="System.Collections.Immutable, Version=1.1.37.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
190+
<HintPath>..\..\packages\System.Collections.Immutable.1.1.37\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll</HintPath>
191191
<Private>True</Private>
192192
</Reference>
193193
<Reference Include="System.Composition.AttributedModel">
@@ -205,15 +205,15 @@
205205
<Reference Include="System.Composition.TypedParts">
206206
<HintPath>..\..\packages\Microsoft.Composition.1.0.27\lib\portable-net45+win8+wp8+wpa81\System.Composition.TypedParts.dll</HintPath>
207207
</Reference>
208-
<Reference Include="System.Reflection.Metadata, Version=1.0.21.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
209-
<HintPath>..\..\packages\System.Reflection.Metadata.1.0.21\lib\portable-net45+win8\System.Reflection.Metadata.dll</HintPath>
208+
<Reference Include="System.Reflection.Metadata, Version=1.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
209+
<HintPath>..\..\packages\System.Reflection.Metadata.1.2.0\lib\portable-net45+win8\System.Reflection.Metadata.dll</HintPath>
210210
<Private>True</Private>
211211
</Reference>
212212
</ItemGroup>
213213
<ItemGroup>
214214
<Analyzer Include="..\..\packages\AsyncUsageAnalyzers.1.0.0-alpha003\analyzers\dotnet\AsyncUsageAnalyzers.dll" />
215-
<Analyzer Include="..\..\packages\Microsoft.CodeAnalysis.Analyzers.1.0.0\analyzers\dotnet\cs\Microsoft.CodeAnalysis.Analyzers.dll" />
216-
<Analyzer Include="..\..\packages\Microsoft.CodeAnalysis.Analyzers.1.0.0\analyzers\dotnet\cs\Microsoft.CodeAnalysis.CSharp.Analyzers.dll" />
215+
<Analyzer Include="..\..\packages\Microsoft.CodeAnalysis.Analyzers.1.1.0\analyzers\dotnet\cs\Microsoft.CodeAnalysis.Analyzers.dll" />
216+
<Analyzer Include="..\..\packages\Microsoft.CodeAnalysis.Analyzers.1.1.0\analyzers\dotnet\cs\Microsoft.CodeAnalysis.CSharp.Analyzers.dll" />
217217
<Analyzer Include="..\..\packages\StyleCop.Analyzers.1.0.0\analyzers\dotnet\cs\Newtonsoft.Json.dll" />
218218
<Analyzer Include="..\..\packages\StyleCop.Analyzers.1.0.0\analyzers\dotnet\cs\StyleCop.Analyzers.CodeFixes.dll" />
219219
<Analyzer Include="..\..\packages\StyleCop.Analyzers.1.0.0\analyzers\dotnet\cs\StyleCop.Analyzers.dll" />
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="AsyncUsageAnalyzers" version="1.0.0-alpha003" targetFramework="portable45-net45+win8" developmentDependency="true" />
4-
<package id="Microsoft.CodeAnalysis.Analyzers" version="1.0.0" targetFramework="portable45-net45+win8" />
5-
<package id="Microsoft.CodeAnalysis.Common" version="1.0.0" targetFramework="portable45-net45+win8" />
6-
<package id="Microsoft.CodeAnalysis.CSharp" version="1.0.0" targetFramework="portable45-net45+win8" />
7-
<package id="Microsoft.CodeAnalysis.CSharp.Workspaces" version="1.0.0" targetFramework="portable45-net45+win8" />
8-
<package id="Microsoft.CodeAnalysis.Workspaces.Common" version="1.0.0" targetFramework="portable45-net45+win8" />
4+
<package id="Microsoft.CodeAnalysis.Analyzers" version="1.1.0" targetFramework="portable45-net45+win8" />
5+
<package id="Microsoft.CodeAnalysis.Common" version="1.2.1" targetFramework="portable45-net45+win8" />
6+
<package id="Microsoft.CodeAnalysis.CSharp" version="1.2.1" targetFramework="portable45-net45+win8" />
7+
<package id="Microsoft.CodeAnalysis.CSharp.Workspaces" version="1.2.1" targetFramework="portable45-net45+win8" />
8+
<package id="Microsoft.CodeAnalysis.Workspaces.Common" version="1.2.1" targetFramework="portable45-net45+win8" />
99
<package id="Microsoft.Composition" version="1.0.27" targetFramework="portable-net45+win8" />
1010
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="portable45-net45+win8" />
1111
<package id="NuGet.CommandLine" version="2.8.3" targetFramework="portable45-net45+win8" />
1212
<package id="StyleCop.Analyzers" version="1.0.0" targetFramework="portable45-net45+win8" developmentDependency="true" />
13-
<package id="System.Collections.Immutable" version="1.1.36" targetFramework="portable45-net45+win8" />
14-
<package id="System.Reflection.Metadata" version="1.0.21" targetFramework="portable45-net45+win8" />
13+
<package id="System.Collections.Immutable" version="1.1.37" targetFramework="portable45-net45+win8" />
14+
<package id="System.Reflection.Metadata" version="1.2.0" targetFramework="portable45-net45+win8" />
1515
<package id="Tvl.NuGet.BuildTasks" version="1.0.0-alpha002" targetFramework="portable45-net45+win8" />
1616
</packages>

0 commit comments

Comments
 (0)