本文整理汇总了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);
}
}
示例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);
}
}
示例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);
}
示例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);
}
示例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);
}
示例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);
}
}
示例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);
}
}