本文整理汇总了C#中SyntaxTreeAnalysisContext.IsGenerated方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxTreeAnalysisContext.IsGenerated方法的具体用法?C# SyntaxTreeAnalysisContext.IsGenerated怎么用?C# SyntaxTreeAnalysisContext.IsGenerated使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SyntaxTreeAnalysisContext
的用法示例。
在下文中一共展示了SyntaxTreeAnalysisContext.IsGenerated方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AnalyzeTree
private static void AnalyzeTree(SyntaxTreeAnalysisContext context, Compilation compilation)
{
if (context.IsGenerated()) return;
if (!compilation.SyntaxTrees.Contains(context.Tree)) return;
var semanticModel = compilation.GetSemanticModel(context.Tree);
SyntaxNode root;
if (!context.Tree.TryGetRoot(out root)) return;
var types = GetTypesInRoot(root);
foreach (var type in types)
{
var fieldDeclarations = type.ChildNodes().OfType<FieldDeclarationSyntax>();
var variablesToMakeReadonly = GetCandidateVariables(semanticModel, fieldDeclarations);
var typeSymbol = semanticModel.GetDeclaredSymbol(type);
if (typeSymbol == null) continue;
var methods = typeSymbol.GetAllMethodsIncludingFromInnerTypes();
foreach (var method in methods)
{
foreach (var syntaxReference in method.DeclaringSyntaxReferences)
{
var syntaxRefSemanticModel = syntaxReference.SyntaxTree.Equals(context.Tree)
? semanticModel
: compilation.GetSemanticModel(syntaxReference.SyntaxTree);
var descendants = syntaxReference.GetSyntax().DescendantNodes().ToList();
var assignments = descendants.OfKind(SyntaxKind.SimpleAssignmentExpression,
SyntaxKind.AddAssignmentExpression, SyntaxKind.AndAssignmentExpression, SyntaxKind.DivideAssignmentExpression,
SyntaxKind.ExclusiveOrAssignmentExpression, SyntaxKind.LeftShiftAssignmentExpression, SyntaxKind.ModuloAssignmentExpression,
SyntaxKind.MultiplyAssignmentExpression, SyntaxKind.OrAssignmentExpression, SyntaxKind.RightShiftAssignmentExpression,
SyntaxKind.SubtractAssignmentExpression);
foreach (AssignmentExpressionSyntax assignment in assignments)
{
var fieldSymbol = syntaxRefSemanticModel.GetSymbolInfo(assignment.Left).Symbol as IFieldSymbol;
VerifyVariable(variablesToMakeReadonly, method, syntaxRefSemanticModel, assignment, fieldSymbol);
}
var postFixUnaries = descendants.OfKind(SyntaxKind.PostIncrementExpression, SyntaxKind.PostDecrementExpression);
foreach (PostfixUnaryExpressionSyntax postFixUnary in postFixUnaries)
{
var fieldSymbol = syntaxRefSemanticModel.GetSymbolInfo(postFixUnary.Operand).Symbol as IFieldSymbol;
VerifyVariable(variablesToMakeReadonly, method, syntaxRefSemanticModel, postFixUnary, fieldSymbol);
}
var preFixUnaries = descendants.OfKind(SyntaxKind.PreDecrementExpression, SyntaxKind.PreIncrementExpression);
foreach (PrefixUnaryExpressionSyntax preFixUnary in preFixUnaries)
{
var fieldSymbol = syntaxRefSemanticModel.GetSymbolInfo(preFixUnary.Operand).Symbol as IFieldSymbol;
VerifyVariable(variablesToMakeReadonly, method, syntaxRefSemanticModel, preFixUnary, fieldSymbol);
}
}
}
foreach (var readonlyVariable in variablesToMakeReadonly.Values)
{
var props = new Dictionary<string, string> { { "identifier", readonlyVariable.Identifier.Text } }.ToImmutableDictionary();
var diagnostic = Diagnostic.Create(Rule, readonlyVariable.GetLocation(), props, readonlyVariable.Identifier.Text);
context.ReportDiagnostic(diagnostic);
}
}
}
示例2: AnalyzeTree
private static void AnalyzeTree(SyntaxTreeAnalysisContext context, Compilation compilation)
{
if (context.IsGenerated()) return;
if (!compilation.SyntaxTrees.Contains(context.Tree)) return;
var semanticModel = compilation.GetSemanticModel(context.Tree);
SyntaxNode root;
if (!context.Tree.TryGetRoot(out root)) return;
var types = GetTypesInRoot(root);
foreach (var type in types)
{
var fieldDeclarations = type.ChildNodes().OfType<FieldDeclarationSyntax>();
var variablesToMakeReadonly = GetCandidateVariables(semanticModel, fieldDeclarations);
var typeSymbol = semanticModel.GetDeclaredSymbol(type);
if (typeSymbol == null) continue;
var methods = typeSymbol.GetAllMethodsIncludingFromInnerTypes();
foreach (var method in methods)
{
foreach (var syntaxReference in method.DeclaringSyntaxReferences)
{
var syntaxRefSemanticModel = syntaxReference.SyntaxTree.Equals(context.Tree)
? semanticModel
: compilation.GetSemanticModel(syntaxReference.SyntaxTree);
var assignments = syntaxReference.GetSyntax().DescendantNodes().OfType<AssignmentExpressionSyntax>();
foreach (var assignment in assignments)
{
var fieldSymbol = syntaxRefSemanticModel.GetSymbolInfo(assignment.Left).Symbol as IFieldSymbol;
if (fieldSymbol == null) continue;
if (method.MethodKind == MethodKind.StaticConstructor && fieldSymbol.IsStatic)
AddVariableThatWasSkippedBeforeBecauseItLackedAInitializer(variablesToMakeReadonly, fieldSymbol, assignment);
else if (method.MethodKind == MethodKind.Constructor && !fieldSymbol.IsStatic)
AddVariableThatWasSkippedBeforeBecauseItLackedAInitializer(variablesToMakeReadonly, fieldSymbol, assignment);
else
RemoveVariableThatHasAssignment(variablesToMakeReadonly, fieldSymbol);
}
}
}
foreach (var readonlyVariable in variablesToMakeReadonly.Values)
{
var props = new Dictionary<string, string> { { "identifier", readonlyVariable.Identifier.Text } }.ToImmutableDictionary();
var diagnostic = Diagnostic.Create(Rule, readonlyVariable.GetLocation(), props, readonlyVariable.Identifier.Text);
context.ReportDiagnostic(diagnostic);
}
}
}
示例3: AnalyzeTrailingTrivia
private static void AnalyzeTrailingTrivia(SyntaxTreeAnalysisContext context)
{
if (context.IsGenerated()) return;
SourceText text;
if (!context.Tree.TryGetText(out text)) return;
SyntaxNode root;
if (!context.Tree.TryGetRoot(out root)) return;
foreach (var line in text.Lines)
{
if (line.End == 0) continue;
var endSpan = TextSpan.FromBounds(line.End - 1, line.End);
var candidateWhiteSpace = line.Text.GetSubText(endSpan).ToString();
if (string.Compare(candidateWhiteSpace, "\n", StringComparison.Ordinal) == 0
|| !Regex.IsMatch(candidateWhiteSpace, @"\s")) continue;
var isLiteral = root.FindNode(endSpan) is LiteralExpressionSyntax;
if (isLiteral) return;
var diag = Diagnostic.Create(Rule, Location.Create(context.Tree, line.Span));
context.ReportDiagnostic(diag);
}
}
示例4: AnalyzeSingleLineCommentTrivia
private void AnalyzeSingleLineCommentTrivia(SyntaxTreeAnalysisContext context)
{
if (context.IsGenerated()) return;
var root = context.Tree.GetRoot();
var comments = root.DescendantTrivia()
.Where(trivia => trivia.IsKind(SyntaxKind.SingleLineCommentTrivia))
.ToArray();
for (var i = 0; i < comments.Length; i++)
{
var comment = comments[i];
var code = GetFullCommentedCode(root, comment);
if (!CouldBeSourceCode(code.Code)) continue;
i += code.NumberOfComments - 1;
Location.Create(context.Tree, new TextSpan(code.Start, code.End));
var diagnostic = Diagnostic.Create(Rule, comment.GetLocation());
context.ReportDiagnostic(diagnostic);
}
}