Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/Compilers/CSharp/Portable/Compilation/CSharpCompilation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1997,6 +1997,7 @@ private void GetDiagnosticsForAllMethodBodies(DiagnosticBag diagnostics, Cancell
hasDeclarationErrors: false,
diagnostics: diagnostics,
filterOpt: null,
semanticModelOpt: null,
cancellationToken: cancellationToken);

DocumentationCommentCompiler.WriteDocumentationCommentXml(this, null, null, diagnostics, cancellationToken);
Expand Down Expand Up @@ -2028,9 +2029,10 @@ private static bool IsDefinedOrImplementedInSourceTree(Symbol symbol, SyntaxTree
return false;
}

private ImmutableArray<Diagnostic> GetDiagnosticsForMethodBodiesInTree(SyntaxTree tree, TextSpan? span, CancellationToken cancellationToken)
private ImmutableArray<Diagnostic> GetDiagnosticsForMethodBodiesInTree(SemanticModel semanticModel, TextSpan? span, CancellationToken cancellationToken)
{
DiagnosticBag diagnostics = DiagnosticBag.GetInstance();
var tree = semanticModel.SyntaxTree;

MethodCompiler.CompileMethodBodies(
compilation: this,
Expand All @@ -2039,6 +2041,7 @@ private ImmutableArray<Diagnostic> GetDiagnosticsForMethodBodiesInTree(SyntaxTre
hasDeclarationErrors: false,
diagnostics: diagnostics,
filterOpt: s => IsDefinedOrImplementedInSourceTree(s, tree, span),
semanticModelOpt: semanticModel,
cancellationToken: cancellationToken);

DocumentationCommentCompiler.WriteDocumentationCommentXml(this, null, null, diagnostics, cancellationToken, tree, span);
Expand Down Expand Up @@ -2155,15 +2158,16 @@ private static IEnumerable<Diagnostic> FilterDiagnosticsByLocation(IEnumerable<D
}
}

internal ImmutableArray<Diagnostic> GetDiagnosticsForSyntaxTree(
internal ImmutableArray<Diagnostic> GetDiagnosticsForSemanticModel(
CompilationStage stage,
SyntaxTree syntaxTree,
SemanticModel semanticModel,
TextSpan? filterSpanWithinTree,
bool includeEarlierStages,
CancellationToken cancellationToken = default(CancellationToken))
{
cancellationToken.ThrowIfCancellationRequested();

var syntaxTree = semanticModel.SyntaxTree;
var builder = DiagnosticBag.GetInstance();
if (stage == CompilationStage.Parse || (stage > CompilationStage.Parse && includeEarlierStages))
{
Expand Down Expand Up @@ -2192,7 +2196,7 @@ internal ImmutableArray<Diagnostic> GetDiagnosticsForSyntaxTree(
//initializers which can result in 'field is never initialized' warnings for fields in partial
//types when the field is in a different source file than the one for which we're getting diagnostics.
//For that reason the bag must be also filtered by tree.
IEnumerable<Diagnostic> methodBodyDiagnostics = GetDiagnosticsForMethodBodiesInTree(syntaxTree, filterSpanWithinTree, cancellationToken);
IEnumerable<Diagnostic> methodBodyDiagnostics = GetDiagnosticsForMethodBodiesInTree(semanticModel, filterSpanWithinTree, cancellationToken);

// TODO: Enable the below commented assert and remove the filtering code in the next line.
// GetDiagnosticsForMethodBodiesInTree seems to be returning diagnostics with locations that don't satisfy the filter tree/span, this must be fixed.
Expand Down Expand Up @@ -2361,6 +2365,7 @@ internal override bool CompileImpl(
hasDeclarationErrors,
diagnostics: methodBodyDiagnosticBag,
filterOpt: filterOpt,
semanticModelOpt: null,
cancellationToken: cancellationToken);

SetupWin32Resources(moduleBeingBuilt, win32Resources, methodBodyDiagnosticBag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,29 +102,29 @@ private void VerifySpanForGetDiagnostics(TextSpan? span)
public override ImmutableArray<Diagnostic> GetSyntaxDiagnostics(TextSpan? span = null, CancellationToken cancellationToken = default(CancellationToken))
{
VerifySpanForGetDiagnostics(span);
return Compilation.GetDiagnosticsForSyntaxTree(
CompilationStage.Parse, this.SyntaxTree, span, includeEarlierStages: false, cancellationToken: cancellationToken);
return Compilation.GetDiagnosticsForSemanticModel(
CompilationStage.Parse, this, span, includeEarlierStages: false, cancellationToken: cancellationToken);
}

public override ImmutableArray<Diagnostic> GetDeclarationDiagnostics(TextSpan? span = null, CancellationToken cancellationToken = default(CancellationToken))
{
VerifySpanForGetDiagnostics(span);
return Compilation.GetDiagnosticsForSyntaxTree(
CompilationStage.Declare, this.SyntaxTree, span, includeEarlierStages: false, cancellationToken: cancellationToken);
return Compilation.GetDiagnosticsForSemanticModel(
CompilationStage.Declare, this, span, includeEarlierStages: false, cancellationToken: cancellationToken);
}

public override ImmutableArray<Diagnostic> GetMethodBodyDiagnostics(TextSpan? span = null, CancellationToken cancellationToken = default(CancellationToken))
{
VerifySpanForGetDiagnostics(span);
return Compilation.GetDiagnosticsForSyntaxTree(
CompilationStage.Compile, this.SyntaxTree, span, includeEarlierStages: false, cancellationToken: cancellationToken);
return Compilation.GetDiagnosticsForSemanticModel(
CompilationStage.Compile, this, span, includeEarlierStages: false, cancellationToken: cancellationToken);
}

public override ImmutableArray<Diagnostic> GetDiagnostics(TextSpan? span = null, CancellationToken cancellationToken = default(CancellationToken))
{
VerifySpanForGetDiagnostics(span);
return Compilation.GetDiagnosticsForSyntaxTree(
CompilationStage.Compile, this.SyntaxTree, span, includeEarlierStages: true, cancellationToken: cancellationToken);
return Compilation.GetDiagnosticsForSemanticModel(
CompilationStage.Compile, this, span, includeEarlierStages: true, cancellationToken: cancellationToken);
}

/// <summary>
Expand Down
8 changes: 6 additions & 2 deletions src/Compilers/CSharp/Portable/Compiler/MethodCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ internal sealed class MethodCompiler : CSharpSymbolVisitor<TypeCompilationState,
private readonly bool _hasDeclarationErrors;
private readonly PEModuleBuilder _moduleBeingBuiltOpt; // Null if compiling for diagnostics
private readonly Predicate<Symbol> _filterOpt; // If not null, limit analysis to specific symbols
private readonly SemanticModel _semanticModelOpt; // Non-null if computing method body diagnostics for a specific semantic model.
private readonly DebugDocumentProvider _debugDocumentProvider;

//
Expand Down Expand Up @@ -74,7 +75,7 @@ private void SetGlobalErrorIfTrue(bool arg)

// Internal for testing only.
internal MethodCompiler(CSharpCompilation compilation, PEModuleBuilder moduleBeingBuiltOpt, bool emittingPdb, bool hasDeclarationErrors,
DiagnosticBag diagnostics, Predicate<Symbol> filterOpt, CancellationToken cancellationToken)
DiagnosticBag diagnostics, Predicate<Symbol> filterOpt, SemanticModel semanticModelOpt, CancellationToken cancellationToken)
{
Debug.Assert(compilation != null);
Debug.Assert(diagnostics != null);
Expand All @@ -85,6 +86,7 @@ internal MethodCompiler(CSharpCompilation compilation, PEModuleBuilder moduleBei
_cancellationToken = cancellationToken;
_diagnostics = diagnostics;
_filterOpt = filterOpt;
_semanticModelOpt = semanticModelOpt;

_hasDeclarationErrors = hasDeclarationErrors;
SetGlobalErrorIfTrue(hasDeclarationErrors);
Expand All @@ -102,6 +104,7 @@ public static void CompileMethodBodies(
bool hasDeclarationErrors,
DiagnosticBag diagnostics,
Predicate<Symbol> filterOpt,
SemanticModel semanticModelOpt,
CancellationToken cancellationToken)
{
Debug.Assert(compilation != null);
Expand All @@ -125,6 +128,7 @@ public static void CompileMethodBodies(
hasDeclarationErrors,
diagnostics,
filterOpt,
semanticModelOpt,
cancellationToken);

if (compilation.Options.ConcurrentBuild)
Expand Down Expand Up @@ -930,7 +934,7 @@ private void CompileMethod(
var lazySemanticModel = body == null ? null : new Lazy<SemanticModel>(() =>
{
var syntax = body.Syntax;
var semanticModel = (CSharpSemanticModel)_compilation.GetSemanticModel(syntax.SyntaxTree);
var semanticModel = (CSharpSemanticModel)(_semanticModelOpt ?? _compilation.GetSemanticModel(syntax.SyntaxTree));
var memberModel = semanticModel.GetMemberModel(syntax);
if (memberModel != null)
{
Expand Down
1 change: 1 addition & 0 deletions src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12817,6 +12817,7 @@ static System.Action M()
hasDeclarationErrors: false,
diagnostics: diagnostics,
filterOpt: null,
semanticModelOpt: null,
cancellationToken: CancellationToken.None);

// Add diagnostic to MethodBodyCompiler bag, as if
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3136,7 +3136,7 @@ public class D
var libRef = CreateCompilationWithMscorlib(libSource).EmitToImageReference();
var comp = CreateCompilationWithMscorlib(source, new[] { libRef });
var tree = comp.SyntaxTrees.Single();
comp.GetDiagnosticsForSyntaxTree(CompilationStage.Declare, tree, null, includeEarlierStages: false, cancellationToken: CancellationToken.None);
comp.GetDiagnosticsForSyntaxTree(CompilationStage.Declare, tree, null, includeEarlierStages: false);
}

[WorkItem(709317, "DevDiv")]
Expand Down Expand Up @@ -3231,7 +3231,7 @@ public MyAttribute(int[] i) {{ }}
// [assembly:CLSCompliant(true)]
Diagnostic(ErrorCode.ERR_DuplicateAttribute, "CLSCompliant").WithArguments("CLSCompliant"));

comp.GetDiagnosticsForSyntaxTree(CompilationStage.Declare, tree1, null, includeEarlierStages: false, cancellationToken: CancellationToken.None).Verify(
comp.GetDiagnosticsForSyntaxTree(CompilationStage.Declare, tree1, null, includeEarlierStages: false).Verify(
// a.cs(21,6): warning CS3016: Arrays as attribute arguments is not CLS-compliant
// [My(new int[] { 1 })]
Diagnostic(ErrorCode.WRN_CLS_ArrayArgumentToAttribute, "My(new int[] { 1 })"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -727,10 +727,7 @@ private void ProcessCompilationUnitCompleted(CompilationUnitCompletedEvent compl
// might want to ask the compiler for all the diagnostics in the source file, for example
// to get information about unnecessary usings.

var semanticModel = analysisStateOpt != null ?
GetOrCreateCachedSemanticModel(completedEvent.CompilationUnit, completedEvent.Compilation, cancellationToken) :
completedEvent.SemanticModel;

var semanticModel = completedEvent.SemanticModel;
if (!analysisScope.ShouldAnalyze(semanticModel.SyntaxTree))
{
return;
Expand Down Expand Up @@ -1208,9 +1205,7 @@ private void ExecuteDeclaringReferenceActions(
Debug.Assert(shouldExecuteSyntaxNodeActions || shouldExecuteCodeBlockActions);

var symbol = symbolEvent.Symbol;
SemanticModel semanticModel = analysisStateOpt != null ?
GetOrCreateCachedSemanticModel(decl.SyntaxTree, symbolEvent.Compilation, cancellationToken) :
symbolEvent.SemanticModel(decl);
SemanticModel semanticModel = symbolEvent.SemanticModel(decl);

var cacheAnalysisData = analysisScope.Analyzers.Length < analyzers.Length &&
(!analysisScope.FilterSpanOpt.HasValue || analysisScope.FilterSpanOpt.Value.Length >= decl.SyntaxTree.GetRoot(cancellationToken).Span.Length);
Expand Down
13 changes: 13 additions & 0 deletions src/Compilers/Test/Utilities/CSharp/DiagnosticExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis.Test.Utilities;
using Microsoft.CodeAnalysis.Text;
using System.Threading;

namespace Microsoft.CodeAnalysis.CSharp
{
Expand All @@ -23,5 +25,16 @@ public static string ToLocalizedString(this MessageID id)
{
return new LocalizableErrorArgument(id).ToString(null, null);
}

public static ImmutableArray<Diagnostic> GetDiagnosticsForSyntaxTree(
this CSharpCompilation compilation,
CompilationStage stage,
SyntaxTree tree,
TextSpan? filterSpanWithinTree,
bool includeEarlierStages = true)
{
var semanticModel = compilation.GetSemanticModel(tree);
return compilation.GetDiagnosticsForSemanticModel(stage, semanticModel, filterSpanWithinTree, includeEarlierStages);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ Namespace Global.Microsoft.CodeAnalysis.VisualBasic
End Function

<Extension>
Friend Function GetDiagnosticsForSyntaxTree(c As VisualBasicCompilation, stage As CompilationStage, tree As SyntaxTree, Optional filterSpan As TextSpan? = Nothing) As ImmutableArray(Of Diagnostic)
Return c.GetDiagnosticsForSyntaxTree(stage, tree, filterSpan, includeEarlierStages:=True, cancellationToken:=CancellationToken.None)
Friend Function GetDiagnosticsForSyntaxTree(c As VisualBasicCompilation, stage As CompilationStage, tree As SyntaxTree, Optional filterSpan As TextSpan? = Nothing, Optional includeEarlierStages As Boolean = True) As ImmutableArray(Of Diagnostic)
Dim model = c.GetSemanticModel(tree)
Return c.GetDiagnosticsForSemanticModel(stage, model, filterSpan, includeEarlierStages)
End Function

' TODO: Figure out how to return a localized message using VB
Expand Down
10 changes: 8 additions & 2 deletions src/Compilers/VisualBasic/Portable/Compilation/MethodCompiler.vb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Private ReadOnly _namespaceScopeBuilder As NamespaceScopeBuilder
Private ReadOnly _moduleBeingBuiltOpt As PEModuleBuilder ' Nothing if compiling for diagnostics
Private ReadOnly _filterOpt As Predicate(Of Symbol) ' If not Nothing, limit analysis to specific symbols
Private ReadOnly _semanticModelOpt As SemanticModel ' If not Nothing, limit analysis to specific symbols
Private ReadOnly _debugDocumentProvider As DebugDocumentProvider

' GetDiagnostics only needs to Bind. If we need to go further, _doEmitPhase needs to be set.
Expand Down Expand Up @@ -83,6 +84,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
hasDeclarationErrors As Boolean,
diagnostics As DiagnosticBag,
filter As Predicate(Of Symbol),
semanticModelOpt As SemanticModel,
cancellationToken As CancellationToken)

_compilation = compilation
Expand All @@ -93,6 +95,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
_doEmitPhase = doEmitPhase
_emittingPdb = emittingPdb
_filterOpt = filter
_semanticModelOpt = semanticModelOpt

If emittingPdb Then
_debugDocumentProvider = Function(path As String, basePath As String) moduleBeingBuiltOpt.GetOrAddDebugDocument(path, basePath, AddressOf CreateDebugDocumentForFile)
Expand Down Expand Up @@ -140,14 +143,15 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
''' </summary>
Public Shared Sub GetCompileDiagnostics(compilation As VisualBasicCompilation,
root As NamespaceSymbol,
tree As SyntaxTree,
semanticModelOpt As SemanticModel,
filterSpanWithinTree As TextSpan?,
hasDeclarationErrors As Boolean,
diagnostics As DiagnosticBag,
doEmitPhase As Boolean,
cancellationToken As CancellationToken)

Dim filter As Predicate(Of Symbol) = Nothing
Dim tree = semanticModelOpt?.SyntaxTree

If tree IsNot Nothing Then
filter = Function(sym) IsDefinedOrImplementedInSourceTree(sym, tree, filterSpanWithinTree)
Expand All @@ -160,6 +164,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
hasDeclarationErrors:=hasDeclarationErrors,
diagnostics:=diagnostics,
filter:=filter,
semanticModelOpt:=semanticModelOpt,
cancellationToken:=cancellationToken)

root.Accept(compiler)
Expand Down Expand Up @@ -214,6 +219,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
hasDeclarationErrors:=hasDeclarationErrors,
diagnostics:=diagnostics,
filter:=filter,
semanticModelOpt:=Nothing,
cancellationToken:=cancellationToken)

compilation.SourceModule.GlobalNamespace.Accept(compiler)
Expand Down Expand Up @@ -1186,7 +1192,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Dim lazySemanticModel = New Lazy(Of SemanticModel)(
Function()
Dim syntax = block.Syntax
Dim semanticModel = CType(compilation.GetSemanticModel(syntax.SyntaxTree), SyntaxTreeSemanticModel)
Dim semanticModel = CType(If(_semanticModelOpt, compilation.GetSemanticModel(syntax.SyntaxTree)), SyntaxTreeSemanticModel)
Dim memberModel = CType(semanticModel.GetMemberSemanticModel(syntax), MethodBodySemanticModel)
If memberModel IsNot Nothing Then
memberModel.CacheBoundNodes(block, syntax)
Expand Down
Loading