当前位置: 首页>>代码示例>>C#>>正文


C# SyntaxTreeAnalysisContext.IsGenerated方法代码示例

本文整理汇总了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);
         }
     }
 }
开发者ID:jhancock93,项目名称:code-cracker,代码行数:55,代码来源:ReadonlyFieldAnalyzer.cs

示例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);
         }
     }
 }
开发者ID:Cadums01,项目名称:code-cracker,代码行数:44,代码来源:ReadonlyFieldAnalyzer.cs

示例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);
     }
 }
开发者ID:haroldhues,项目名称:code-cracker,代码行数:20,代码来源:RemoveTrailingWhitespaceAnalyzer.cs

示例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);
            }
        }
开发者ID:JeanLLopes,项目名称:code-cracker,代码行数:23,代码来源:RemoveCommentedCodeAnalyzer.cs


注:本文中的SyntaxTreeAnalysisContext.IsGenerated方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。