本文整理汇总了C#中Microsoft.CodeAnalysis.SemanticModel.GetDiagnostics方法的典型用法代码示例。如果您正苦于以下问题:C# SemanticModel.GetDiagnostics方法的具体用法?C# SemanticModel.GetDiagnostics怎么用?C# SemanticModel.GetDiagnostics使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.SemanticModel
的用法示例。
在下文中一共展示了SemanticModel.GetDiagnostics方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetUnnecessaryImports
public static IEnumerable<SyntaxNode> GetUnnecessaryImports(SemanticModel semanticModel, SyntaxNode root, CancellationToken cancellationToken)
{
var diagnostics = semanticModel.GetDiagnostics(cancellationToken: cancellationToken);
if (!diagnostics.Any())
{
return null;
}
var unnecessaryImports = new HashSet<UsingDirectiveSyntax>();
foreach (var diagnostic in diagnostics)
{
if (diagnostic.Id == "CS8019")
{
var node = root.FindNode(diagnostic.Location.SourceSpan) as UsingDirectiveSyntax;
if (node != null)
{
unnecessaryImports.Add(node);
}
}
}
if (cancellationToken.IsCancellationRequested || !unnecessaryImports.Any())
{
return null;
}
return unnecessaryImports;
}
示例2: GetUnusedImportDirectives
private static HashSet<SyntaxNode> GetUnusedImportDirectives(SemanticModel model, CancellationToken cancellationToken)
{
HashSet<SyntaxNode> unusedImportDirectives = new HashSet<SyntaxNode>();
SyntaxNode root = model.SyntaxTree.GetRoot(cancellationToken);
foreach (Diagnostic diagnostic in model.GetDiagnostics(null, cancellationToken).Where(d => d.Id == "CS8019" || d.Id == "CS0105"))
{
UsingDirectiveSyntax usingDirectiveSyntax = root.FindNode(diagnostic.Location.SourceSpan, false, false) as UsingDirectiveSyntax;
if (usingDirectiveSyntax != null)
{
unusedImportDirectives.Add(usingDirectiveSyntax);
}
}
return unusedImportDirectives;
}
示例3: Visitor
public Visitor(SemanticModel model)
: base(SyntaxWalkerDepth.Trivia)
{
_model = model;
_modelDiagnostic = _model.GetDiagnostics();
}
示例4: GetPointDiagnostics
private async Task<ImmutableArray<Diagnostic>> GetPointDiagnostics(TextSpan location, List<string> diagnosticIds)
{
_document = _workspace.GetDocument(_document.FilePath);
_semanticModel = await _document.GetSemanticModelAsync();
var diagnostics = _semanticModel.GetDiagnostics();
//Restrict diagnostics only to missing usings
return diagnostics.Where(d => d.Location.SourceSpan.Contains(location) &&
diagnosticIds.Contains(d.Id)).ToImmutableArray();
}
示例5: HasErrors
private static bool HasErrors(SemanticModel semanticModel, SyntaxNode node)
{
var diags = semanticModel.GetDiagnostics(node.Span);
if (diags.Any(d => d.Id.StartsWith("CS"))) return true;
return false;
}
示例6: GetSemantic
private void GetSemantic(string code, out SemanticModel model, out int pos)
{
var tree = CSharpSyntaxTree.ParseText(code);
//var writer = new ConsoleDumpWalker();
//writer.Visit(tree.GetRoot());
var refs = AssemblyUtility.AllReferencedAssemblyLocations
.Distinct()
.Select(x => MetadataReference.CreateFromFile(x));
var compilation = CSharpCompilation.Create(
"MyCompilation",
syntaxTrees: new[] { tree },
references: refs);
model = compilation.GetSemanticModel(tree);
var diags = model.GetDiagnostics();
foreach (var diag in diags)
{
if (diag.Id == "CS8019") continue;
Console.WriteLine(diag);
Assert.Fail();
}
var ns = tree.GetRoot().DescendantNodes().OfType<NamespaceDeclarationSyntax>().First();
pos = ns.OpenBraceToken.SpanStart;
// if we use that as a "container" then we get what's in the container *only*
// not what we want here, then we have to use the position to determine *scope*
//var ss = model.GetDeclaredSymbol(ns);
}