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


C# SyntaxNodeAnalysisContext.IsGeneratedOrNonUserCode方法代码示例

本文整理汇总了C#中SyntaxNodeAnalysisContext.IsGeneratedOrNonUserCode方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxNodeAnalysisContext.IsGeneratedOrNonUserCode方法的具体用法?C# SyntaxNodeAnalysisContext.IsGeneratedOrNonUserCode怎么用?C# SyntaxNodeAnalysisContext.IsGeneratedOrNonUserCode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SyntaxNodeAnalysisContext的用法示例。


在下文中一共展示了SyntaxNodeAnalysisContext.IsGeneratedOrNonUserCode方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: AnalyzeIfOrElseStatement

        private void AnalyzeIfOrElseStatement(SyntaxNodeAnalysisContext context)
        {
            if (context.IsGeneratedOrNonUserCode())
            {
                return;
            }

            IfStatementSyntax ifStatement = context.Node as IfStatementSyntax;

            // Is this an if statement followed directly by a call with no braces?
            if ((ifStatement != null) &&
                (ifStatement.Statement != null) &&
                (ifStatement.Statement.IsKind(SyntaxKind.Block) == false))
            {
                Location loc = ifStatement.GetLocation();
                Diagnostic diagnostic = Diagnostic.Create(Rule, loc, "if");
                context.ReportDiagnostic(diagnostic);
            }

            // Is this an else clause followed by a call with no braces?
            ElseClauseSyntax elseSyntax = context.Node as ElseClauseSyntax;
            if ((elseSyntax != null) &&
                (elseSyntax.Statement != null ) &&
                (elseSyntax.Statement.IsKind(SyntaxKind.IfStatement) == false) &&
                (elseSyntax.Statement.IsKind(SyntaxKind.Block) == false))
            {
                Location loc = elseSyntax.GetLocation();
                Diagnostic diagnostic = Diagnostic.Create(Rule, loc, "else");
                context.ReportDiagnostic(diagnostic);
            }
        }
开发者ID:vuder,项目名称:Wintellect.Analyzers,代码行数:31,代码来源:IfAndElseMustHaveBracesAnalyzer.cs

示例2: AnalyzeFieldDeclaration

        private static void AnalyzeFieldDeclaration(SyntaxNodeAnalysisContext context)
        {
            if (context.IsGeneratedOrNonUserCode()) { return; }
            var fieldDeclaration = (FieldDeclarationSyntax)context.Node;
            if (IsStaticReadonlyField(fieldDeclaration)) { return; }

            SyntaxToken[] accessTokens = GetAccessTokenFor(fieldDeclaration, SyntaxKind.PrivateKeyword);
            if (accessTokens.Length != 1) {
                string fieldName = fieldDeclaration.DescendantTokens().FirstOrDefault(token => token.IsKind(SyntaxKind.IdentifierToken)).Value as string;
                Diagnostic diagnostic = Diagnostic.Create(Rule, fieldDeclaration.GetLocation(), fieldName);
                context.ReportDiagnostic(diagnostic);
            }
        }
开发者ID:vuder,项目名称:CodingStandardCodeAnalyzers,代码行数:13,代码来源:FieldAccessCodeAnalyzer.cs

示例3: AnalyzeDateTimeUsage

        private void AnalyzeDateTimeUsage(SyntaxNodeAnalysisContext context)
        {
            if (context.IsGeneratedOrNonUserCode()) { return; }
            var node = context.Node as IdentifierNameSyntax;

            var membersToSearch = new[] {
                new SearchMemberInfo("System", "DateTime", "Now"),
                new SearchMemberInfo("System", "DateTime", "Today")
            };
            var symbol = node.CheckNodeIsMemberOfType(context.SemanticModel, membersToSearch);
            if (symbol == null) { return; }
            Diagnostic diagnostic = Diagnostic.Create(Rule, node.GetLocation(), $"{symbol.ContainingType.Name}.{symbol.Name}");
            context.ReportDiagnostic(diagnostic);
        }
开发者ID:vuder,项目名称:CodingStandardCodeAnalyzers,代码行数:14,代码来源:DateTimeClassUsageCodeAnalyzer.cs

示例4: AnalyzeNode

        private void AnalyzeNode(SyntaxNodeAnalysisContext context)
        {
            if (context.IsGeneratedOrNonUserCode())
            {
                return;
            }

            // Look for calls to Debug.Assert.
            // Ensure it's the
            // Look at the parameters. 
            // If only one, trigger the error.

            // Take a quick peek and see if this is a call to an "Debug.Assert" method.
            InvocationExpressionSyntax invocationExpr = context.Node as InvocationExpressionSyntax;
            if (invocationExpr?.Expression.ToString() != "Debug.Assert")
            {
                return;
            }

            // Let's get serious and double check that we are dealing with System.Diagnostic.System.Assert.
            // In some scenarios, such as unit tests the module will be null because the whole tree hasn't been 
            // built. In that case, I'll just have to assume it's the real method.
            IMethodSymbol memberSymbol = context.SemanticModel.GetSymbolInfo(invocationExpr).Symbol as IMethodSymbol;
            if (memberSymbol != null)
            {
                INamedTypeSymbol classSymbol = memberSymbol.ContainingSymbol as INamedTypeSymbol;
                if (classSymbol != null)
                {
                    if (!classSymbol.IsType(typeof(Debug)))
                    {
                        return;
                    }
                }
            }

            // How many parameters are there?
            ArgumentListSyntax argumentList = invocationExpr.ArgumentList as ArgumentListSyntax;
            if ((argumentList?.Arguments.Count ?? 0) > 1)
            {
                return;
            }

            // We got us a problem here, boss.
            var diagnostic = Diagnostic.Create(Rule, invocationExpr.GetLocation());
            context.ReportDiagnostic(diagnostic);
        }
开发者ID:vuder,项目名称:Wintellect.Analyzers,代码行数:46,代码来源:CallAssertMethodsWithMessageParameterAnalyzer.cs

示例5: AnalyzeIfStatementDeclaration

 private void AnalyzeIfStatementDeclaration(SyntaxNodeAnalysisContext context)
 {
     if (context.IsGeneratedOrNonUserCode()) { return; }
     var ifStatement = context.Node as IfStatementSyntax;
     if (ifStatement == null) { return; }
     CSharpSyntaxNode errorLocation = null;
     StatementSyntax thenClause = ifStatement.Statement;
     if (thenClause != null && !(thenClause is BlockSyntax)) {
         errorLocation = thenClause;
     }
     ElseClauseSyntax elseClause = ifStatement.Else;
     if (elseClause != null) {
         if (elseClause.Statement is BlockSyntax == false && elseClause.Statement is IfStatementSyntax == false) {
             errorLocation = errorLocation == null ? elseClause : (CSharpSyntaxNode)ifStatement;
         }
     }
     if (errorLocation == null) { return; }
     Diagnostic diagnostic = Diagnostic.Create(Rule, errorLocation.GetLocation(), errorLocation.ToString());
     context.ReportDiagnostic(diagnostic);
 }
开发者ID:vuder,项目名称:CodingStandardCodeAnalyzers,代码行数:20,代码来源:IfStatementCodeAnalyzer.cs

示例6: AnalyzePredefinedType

        private void AnalyzePredefinedType(SyntaxNodeAnalysisContext context)
        {
            if (context.IsGeneratedOrNonUserCode())
            {
                return;
            }

            PredefinedTypeSyntax predefinedType = context.Node as PredefinedTypeSyntax;

            // Don't touch the void. :)
            if (!predefinedType.ToString().Equals("void", StringComparison.OrdinalIgnoreCase))
            {
                String typeString = predefinedType.ToString();
                String realString = TypeMap[typeString];
                var diagnostic = Diagnostic.Create(Rule,
                                                   predefinedType.GetLocation(),
                                                   typeString,
                                                   realString);
                context.ReportDiagnostic(diagnostic);
            }
        }
开发者ID:vuder,项目名称:Wintellect.Analyzers,代码行数:21,代码来源:AvoidPreDefinedTypesAnalyzer.cs

示例7: AnalyzeCatchBlock

        private void AnalyzeCatchBlock(SyntaxNodeAnalysisContext context)
        {
            // As always skip generated code.
            if (context.IsGeneratedOrNonUserCode())
            {
                return;
            }

            CatchClauseSyntax theCatch = (CatchClauseSyntax)context.Node;

            // I want to be smart about how I look at the catch blocks as the control flow could
            // be slow on very large blocks. Consequently, I only want to look at those blocks
            // that don't have any diagnostics (errors) in them.
            TextSpan span = theCatch.GetLocation().SourceSpan;
            var allDiagnostics = context.SemanticModel.Compilation.GetDiagnostics();
            for (Int32 i = 0; i < allDiagnostics.Length; i++)
            {
                if (allDiagnostics[i].Location.SourceSpan.IntersectsWith(span))
                {
                    return;
                }
            }

            // Take a look at the control flow.
            ControlFlowAnalysis flowAnalysis = context.SemanticModel.AnalyzeControlFlow(theCatch.Block);

            if (flowAnalysis.Succeeded)
            {
                if ((flowAnalysis.ReturnStatements.Length == 0) &&
                    (flowAnalysis.EndPointIsReachable == false) &&
                    (flowAnalysis.EntryPoints.Length == 0))
                {
                    return;
                }

                // Now we know this catch block either eats the exception or has a return.
                var diagnostic = Diagnostic.Create(Rule, theCatch.GetLocation());
                context.ReportDiagnostic(diagnostic);
            }
        }
开发者ID:vuder,项目名称:Wintellect.Analyzers,代码行数:40,代码来源:CatchBlocksShouldRethrowAnalyzer.cs


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