Skip to content

Commit

Permalink
Merge pull request #2967 from dotpaul/cherrypick
Browse files Browse the repository at this point in the history
Cherry picking Compilation.GetTypeByMetadataName replacement
  • Loading branch information
mavasani authored Oct 23, 2019
2 parents 56e1420 + f21d049 commit aa3797e
Show file tree
Hide file tree
Showing 119 changed files with 410 additions and 387 deletions.
1 change: 1 addition & 0 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<!-- Roslyn -->
<MicrosoftCodeAnalysisVersion>2.9.0</MicrosoftCodeAnalysisVersion>
<MicrosoftNetCompilersVersion>3.3.1-beta3-final</MicrosoftNetCompilersVersion>
<MicrosoftCodeAnalysisBannedApiAnalyzersVersion>2.9.6</MicrosoftCodeAnalysisBannedApiAnalyzersVersion>
<MicrosoftCodeAnalysisFXCopAnalyersVersion>2.9.5</MicrosoftCodeAnalysisFXCopAnalyersVersion>
<MicrosoftCodeAnalysisAnalyersVersion>3.0.0-beta2.19218.3+e96bad97</MicrosoftCodeAnalysisAnalyersVersion>
<CodeStyleAnalyersVersion>3.3.0-beta2-19376-02</CodeStyleAnalyersVersion>
Expand Down
1 change: 1 addition & 0 deletions src/BannedSymbols.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
M:Microsoft.CodeAnalysis.Compilation.GetTypeByMetadataName(System.String); Use WellKnownTypeProvider instead
5 changes: 5 additions & 0 deletions src/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
<UpToDateCheckInput Include="$(MSBuildThisFileDirectory)..\eng\Analyzers_ShippingRules.ruleset" Condition="'$(CodeAnalysisRuleSet)' == '$(MSBuildThisFileDirectory)..\build\Analyzers_NonShippingRules.ruleset'" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.BannedApiAnalyzers" Version="$(MicrosoftCodeAnalysisBannedApiAnalyzersVersion)" />
<AdditionalFiles Include="$(MSBuildThisFileDirectory)\BannedSymbols.txt" Condition="'$(BannedSymbolsOptOut)' != 'true'" />
</ItemGroup>

<PropertyGroup>
<!-- Workaround for https://github.com/dotnet/roslyn/issues/25041 -->
<MSBuildAllProjects Condition="'$(CodeAnalysisRuleSet)' == '$(MSBuildThisFileDirectory)..\eng\Analyzers_NonShippingRules.ruleset'">$(MSBuildAllProjects);$(MSBuildThisFileDirectory)..\build\Analyzers_ShippingRules.ruleset</MSBuildAllProjects>
Expand Down
10 changes: 6 additions & 4 deletions src/MetaCompilation.Analyzers/Core/CodeFixProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Analyzer.Utilities;
using Analyzer.Utilities.Extensions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
Expand Down Expand Up @@ -1183,7 +1185,7 @@ private async Task<Document> MissingInitAsync(Document document, ClassDeclaratio

SemanticModel semanticModel = await document.GetSemanticModelAsync().ConfigureAwait(false);

INamedTypeSymbol notImplementedException = semanticModel.Compilation.GetTypeByMetadataName("System.NotImplementedException");
INamedTypeSymbol notImplementedException = semanticModel.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemNotImplementedException);
SyntaxList<StatementSyntax> statements = new SyntaxList<StatementSyntax>();
string name = "context";
SyntaxNode initializeDeclaration = CodeFixHelper.BuildInitialize(generator, notImplementedException, statements, name);
Expand Down Expand Up @@ -1553,7 +1555,7 @@ private async Task<Document> MissingAccessorAsync(Document document, PropertyDec

SemanticModel semanticModel = await document.GetSemanticModelAsync().ConfigureAwait(false);

INamedTypeSymbol notImplementedException = semanticModel.Compilation.GetTypeByMetadataName("System.NotImplementedException");
INamedTypeSymbol notImplementedException = semanticModel.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemNotImplementedException);
SyntaxNode[] throwStatement = new[] { generator.ThrowStatement(generator.ObjectCreationExpression(notImplementedException)) };
SyntaxNode type = generator.GetType(declaration);
PropertyDeclarationSyntax newPropertyDeclaration = generator.PropertyDeclaration("SupportedDiagnostics", type, Accessibility.Public, DeclarationModifiers.Override, throwStatement) as PropertyDeclarationSyntax;
Expand Down Expand Up @@ -1726,7 +1728,7 @@ private async Task<Document> AddSuppDiagAsync(Document document, ClassDeclaratio
SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);

SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
INamedTypeSymbol notImplementedException = semanticModel.Compilation.GetTypeByMetadataName("System.NotImplementedException");
INamedTypeSymbol notImplementedException = semanticModel.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemNotImplementedException);
PropertyDeclarationSyntax propertyDeclaration = CodeFixHelper.CreateSupportedDiagnostics(generator, notImplementedException);

var newNodes = new SyntaxList<SyntaxNode>();
Expand Down Expand Up @@ -2379,7 +2381,7 @@ internal static SyntaxNode CreateAnalysisMethod(SyntaxGenerator generator, strin
TypeSyntax type = SyntaxFactory.ParseTypeName("SyntaxNodeAnalysisContext");
SyntaxNode[] parameters = new[] { generator.ParameterDeclaration("context", type) };
SyntaxList<SyntaxNode> statements = new SyntaxList<SyntaxNode>();
INamedTypeSymbol notImplementedException = semanticModel.Compilation.GetTypeByMetadataName("System.NotImplementedException");
INamedTypeSymbol notImplementedException = semanticModel.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemNotImplementedException);
statements = statements.Add(generator.ThrowStatement(generator.ObjectCreationExpression(notImplementedException)));

SyntaxNode newMethodDeclaration = generator.MethodDeclaration(methodName, parameters: parameters, accessibility: Accessibility.Private, statements: statements);
Expand Down
21 changes: 11 additions & 10 deletions src/MetaCompilation.Analyzers/Core/DiagnosticAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Analyzer.Utilities.Extensions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand Down Expand Up @@ -2607,7 +2608,7 @@ private bool CheckMethods(InvocationExpressionSyntax invocationExpression, Compi
ReportDiagnostic(context, IncorrectAnalysisReturnTypeRule, analysisMethodSyntax.Identifier.GetLocation(), analysisMethodSyntax.Identifier.ValueText);
return false;
}
else if (analysisMethod.Parameters.Length != 1 || !Equals(analysisMethod.Parameters.First().Type, context.Compilation.GetTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.SyntaxNodeAnalysisContext")))
else if (analysisMethod.Parameters.Length != 1 || !Equals(analysisMethod.Parameters.First().Type, context.Compilation.GetOrCreateTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.SyntaxNodeAnalysisContext")))
{
ReportDiagnostic(context, IncorrectAnalysisParameterRule, analysisMethodSyntax.ParameterList.GetLocation(), analysisMethodSyntax.Identifier.ValueText);
return false;
Expand Down Expand Up @@ -2766,7 +2767,7 @@ private CheckInitializeInfo CheckInitialize(CompilationAnalysisContext context)
private BlockSyntax InitializeOverview(CompilationAnalysisContext context)
{
ImmutableArray<IParameterSymbol> parameters = _initializeSymbol.Parameters;
if (parameters.Length != 1 || !Equals(parameters[0].Type, context.Compilation.GetTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.AnalysisContext"))
if (parameters.Length != 1 || !Equals(parameters[0].Type, context.Compilation.GetOrCreateTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.AnalysisContext"))
|| _initializeSymbol.DeclaredAccessibility != Accessibility.Public || !_initializeSymbol.IsOverride || !_initializeSymbol.ReturnsVoid)
{
ReportDiagnostic(context, IncorrectInitSigRule, _initializeSymbol.Locations[0], _initializeSymbol.Name);
Expand Down Expand Up @@ -2866,9 +2867,9 @@ internal protected void AddMethod(SymbolAnalysisContext context)
return;
}

if (!Equals(sym.ContainingType.BaseType, context.Compilation.GetTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer")))
if (!Equals(sym.ContainingType.BaseType, context.Compilation.GetOrCreateTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer")))
{
if (!Equals(sym.ContainingType.BaseType, context.Compilation.GetTypeByMetadataName("Microsoft.CodeAnalysis.CodeFixes.CodeFixProvider")))
if (!Equals(sym.ContainingType.BaseType, context.Compilation.GetOrCreateTypeByMetadataName("Microsoft.CodeAnalysis.CodeFixes.CodeFixProvider")))
{
return;
}
Expand Down Expand Up @@ -2918,9 +2919,9 @@ internal protected void AddProperty(SymbolAnalysisContext context)
return;
}

if (!Equals(sym.ContainingType.BaseType, context.Compilation.GetTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer")))
if (!Equals(sym.ContainingType.BaseType, context.Compilation.GetOrCreateTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer")))
{
if (!Equals(sym.ContainingType.BaseType, context.Compilation.GetTypeByMetadataName("Microsoft.CodeAnalysis.CodeFixes.CodeFixProvider")))
if (!Equals(sym.ContainingType.BaseType, context.Compilation.GetOrCreateTypeByMetadataName("Microsoft.CodeAnalysis.CodeFixes.CodeFixProvider")))
{
return;
}
Expand Down Expand Up @@ -2967,7 +2968,7 @@ internal protected void AddField(SymbolAnalysisContext context)
return;
}

if (!Equals(sym.ContainingType.BaseType, context.Compilation.GetTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer")))
if (!Equals(sym.ContainingType.BaseType, context.Compilation.GetOrCreateTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer")))
{
return;
}
Expand Down Expand Up @@ -2995,7 +2996,7 @@ internal protected void AddClass(SymbolAnalysisContext context)
return;
}

if (!Equals(sym.BaseType, context.Compilation.GetTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer")))
if (!Equals(sym.BaseType, context.Compilation.GetOrCreateTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer")))
{
if (sym.ContainingType == null)
{
Expand All @@ -3007,7 +3008,7 @@ internal protected void AddClass(SymbolAnalysisContext context)
return;
}

if (Equals(sym.ContainingType.BaseType, context.Compilation.GetTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer")))
if (Equals(sym.ContainingType.BaseType, context.Compilation.GetOrCreateTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer")))
{
if (_otherAnalyzerClassSymbols.Contains(sym))
{
Expand All @@ -3021,7 +3022,7 @@ internal protected void AddClass(SymbolAnalysisContext context)
}
}

if (Equals(sym.BaseType, context.Compilation.GetTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer")))
if (Equals(sym.BaseType, context.Compilation.GetOrCreateTypeByMetadataName("Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer")))
{
_analyzerClassSymbol = sym;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis" Version="$(MicrosoftCodeAnalysisVersion)" />
</ItemGroup>
<Import Project="..\..\Utilities\Compiler\Analyzer.Utilities.projitems" Label="Shared" />
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ public override void Initialize(AnalysisContext context)

context.RegisterCompilationStartAction(compilationContext =>
{
INamedTypeSymbol solutionSymbol = compilationContext.Compilation.GetTypeByMetadataName(SolutionFullName);
INamedTypeSymbol projectSymbol = compilationContext.Compilation.GetTypeByMetadataName(ProjectFullName);
INamedTypeSymbol documentSymbol = compilationContext.Compilation.GetTypeByMetadataName(DocumentFullName);
INamedTypeSymbol syntaxNodeSymbol = compilationContext.Compilation.GetTypeByMetadataName(SyntaxNodeFullName);
INamedTypeSymbol compilationSymbol = compilationContext.Compilation.GetTypeByMetadataName(CompilationFullName);
INamedTypeSymbol solutionSymbol = compilationContext.Compilation.GetOrCreateTypeByMetadataName(SolutionFullName);
INamedTypeSymbol projectSymbol = compilationContext.Compilation.GetOrCreateTypeByMetadataName(ProjectFullName);
INamedTypeSymbol documentSymbol = compilationContext.Compilation.GetOrCreateTypeByMetadataName(DocumentFullName);
INamedTypeSymbol syntaxNodeSymbol = compilationContext.Compilation.GetOrCreateTypeByMetadataName(SyntaxNodeFullName);
INamedTypeSymbol compilationSymbol = compilationContext.Compilation.GetOrCreateTypeByMetadataName(CompilationFullName);
ImmutableArray<INamedTypeSymbol> immutableSymbols = ImmutableArray.Create(solutionSymbol, projectSymbol, documentSymbol, syntaxNodeSymbol, compilationSymbol);
//Only register our node action if we can find the symbols for our immutable types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System.Collections.Generic;
using System.Linq;
using Analyzer.Utilities.Extensions;
using Microsoft.CodeAnalysis.Analyzers.MetaAnalyzers;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
Expand All @@ -22,8 +23,8 @@ protected override RegisterActionCodeBlockAnalyzer GetCodeBlockAnalyzer(
INamedTypeSymbol operationBlockStartAnalysisContext,
INamedTypeSymbol symbolKind)
{
INamedTypeSymbol csharpSyntaxKind = compilation.GetTypeByMetadataName(CSharpSyntaxKindName);
INamedTypeSymbol basicSyntaxKind = compilation.GetTypeByMetadataName(BasicSyntaxKindName);
INamedTypeSymbol csharpSyntaxKind = compilation.GetOrCreateTypeByMetadataName(CSharpSyntaxKindName);
INamedTypeSymbol basicSyntaxKind = compilation.GetOrCreateTypeByMetadataName(BasicSyntaxKindName);
return new CSharpRegisterActionCodeBlockAnalyzer(csharpSyntaxKind, basicSyntaxKind, analysisContext, compilationStartAnalysisContext,
codeBlockStartAnalysisContext, operationBlockStartAnalysisContext, symbolKind);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Analyzer.Utilities.Extensions;
using Microsoft.CodeAnalysis.Analyzers.FixAnalyzers;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
Expand Down Expand Up @@ -54,7 +55,7 @@ private static async Task<Document> AddMethodAsync(Document document, SyntaxNode
var model = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
var typeIsSealed = ((INamedTypeSymbol)model.GetDeclaredSymbol(classDecl)).IsSealed;

INamedTypeSymbol codeFixProviderSymbol = model.Compilation.GetTypeByMetadataName(FixerWithFixAllAnalyzer.CodeFixProviderMetadataName);
INamedTypeSymbol codeFixProviderSymbol = model.Compilation.GetOrCreateTypeByMetadataName(FixerWithFixAllAnalyzer.CodeFixProviderMetadataName);
IMethodSymbol getFixAllProviderMethod = codeFixProviderSymbol.GetMembers(FixerWithFixAllAnalyzer.GetFixAllProviderMethodName).OfType<IMethodSymbol>().First();
var returnStatement = generator.ReturnStatement(generator.MemberAccessExpression(
generator.IdentifierName("WellKnownFixAllProviders"), "BatchFixer"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ private void CreateAnalyzerWithinCompilation(CompilationStartAnalysisContext con
{
context.CancellationToken.ThrowIfCancellationRequested();

INamedTypeSymbol codeFixProviderSymbol = context.Compilation.GetTypeByMetadataName(CodeFixProviderMetadataName);
INamedTypeSymbol codeFixProviderSymbol = context.Compilation.GetOrCreateTypeByMetadataName(CodeFixProviderMetadataName);
if (codeFixProviderSymbol == null)
{
return;
Expand All @@ -98,7 +98,7 @@ private void CreateAnalyzerWithinCompilation(CompilationStartAnalysisContext con
return;
}

INamedTypeSymbol codeActionSymbol = context.Compilation.GetTypeByMetadataName(CodeActionMetadataName);
INamedTypeSymbol codeActionSymbol = context.Compilation.GetOrCreateTypeByMetadataName(CodeActionMetadataName);
if (codeActionSymbol == null)
{
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public override void Initialize(AnalysisContext context)
context.RegisterCompilationStartAction(context =>
{
var compilation = context.Compilation;
var symbolType = compilation.GetTypeByMetadataName(s_symbolTypeFullName);
var symbolType = compilation.GetOrCreateTypeByMetadataName(s_symbolTypeFullName);
if (symbolType is null)
{
return;
Expand Down Expand Up @@ -203,6 +203,6 @@ private static bool IsExplicitCastToObject(IOperation operation)
}

public static bool UseSymbolEqualityComparer(Compilation compilation)
=> compilation.GetTypeByMetadataName(SymbolEqualityComparerName) is object;
=> compilation.GetOrCreateTypeByMetadataName(SymbolEqualityComparerName) is object;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Analyzer.Utilities;
using Analyzer.Utilities.Extensions;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Operations;

Expand Down Expand Up @@ -42,7 +43,7 @@ protected override DiagnosticAnalyzerSymbolAnalyzer GetDiagnosticAnalyzerSymbolA
{
var compilation = compilationContext.Compilation;

var analysisContext = compilation.GetTypeByMetadataName(AnalysisContextFullName);
var analysisContext = compilation.GetOrCreateTypeByMetadataName(AnalysisContextFullName);
if (analysisContext is null)
{
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ public override void Initialize(AnalysisContext context)

context.RegisterCompilationStartAction(compilationStartContext =>
{
if (compilationStartContext.Compilation.GetTypeByMetadataName(CodeActionMetadataName) == null)
if (compilationStartContext.Compilation.GetOrCreateTypeByMetadataName(CodeActionMetadataName) == null)
{
// No reference to core Workspaces assembly.
return;
}
INamedTypeSymbol diagnosticAnalyzer = compilationStartContext.Compilation.GetTypeByMetadataName(DiagnosticAnalyzerCorrectnessAnalyzer.DiagnosticAnalyzerTypeFullName);
INamedTypeSymbol diagnosticAnalyzer = compilationStartContext.Compilation.GetOrCreateTypeByMetadataName(DiagnosticAnalyzerCorrectnessAnalyzer.DiagnosticAnalyzerTypeFullName);
if (diagnosticAnalyzer == null)
{
// Does not contain any diagnostic analyzers.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ protected override void AnalyzeDiagnosticAnalyzer(SymbolAnalysisContext symbolCo
// then the analyzer is pretty likely a language-agnostic analyzer.
Compilation compilation = symbolContext.Compilation;
string compilationTypeNameToCheck = supportsCSharp ? CSharpCompilationFullName : BasicCompilationFullName;
INamedTypeSymbol compilationType = compilation.GetTypeByMetadataName(compilationTypeNameToCheck);
INamedTypeSymbol compilationType = compilation.GetOrCreateTypeByMetadataName(compilationTypeNameToCheck);
if (compilationType == null)
{
string missingLanguage = supportsCSharp ? LanguageNames.VisualBasic : LanguageNames.CSharp;
Expand Down
Loading

0 comments on commit aa3797e

Please sign in to comment.