提交 ddfc3eaa 编写于 作者: S Sam Harwell

Check for generated code with a walker instead of literal text

上级 6cb854a5
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.CodeDom.Compiler;
namespace Microsoft.CodeAnalysis.Diagnostics
{
internal abstract partial class AnalyzerDriver
{
private sealed class GeneratedCodeTokenWalker : SyntaxWalker
{
public GeneratedCodeTokenWalker()
: base(SyntaxWalkerDepth.Token)
{
}
public bool HasGeneratedCodeIdentifier { get; private set; }
public override void Visit(SyntaxNode node)
{
if (HasGeneratedCodeIdentifier)
return;
base.Visit(node);
}
protected override void VisitToken(SyntaxToken token)
{
HasGeneratedCodeIdentifier |= string.Equals(token.ValueText, "GeneratedCode", StringComparison.Ordinal)
|| string.Equals(token.ValueText, nameof(GeneratedCodeAttribute), StringComparison.Ordinal);
}
}
}
}
......@@ -944,8 +944,9 @@ private ImmutableHashSet<ISymbol> GetOrComputeGeneratedCodeSymbolsInTree(SyntaxT
private ImmutableHashSet<ISymbol> ComputeGeneratedCodeSymbolsInTree(SyntaxTree tree, Compilation compilation, CancellationToken cancellationToken)
{
// PERF: Bail out early if file doesn't have "GeneratedCode" text.
var text = tree.GetText(cancellationToken).ToString();
if (!text.Contains("GeneratedCode"))
var walker = new GeneratedCodeTokenWalker();
walker.Visit(tree.GetRoot(cancellationToken));
if (!walker.HasGeneratedCodeIdentifier)
{
return ImmutableHashSet<ISymbol>.Empty;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册