本文整理汇总了C#中AnalysisContext类的典型用法代码示例。如果您正苦于以下问题:C# AnalysisContext类的具体用法?C# AnalysisContext怎么用?C# AnalysisContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
AnalysisContext类属于命名空间,在下文中一共展示了AnalysisContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Initialize
public override void Initialize(AnalysisContext analysisContext)
{
analysisContext.EnableConcurrentExecution();
analysisContext.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
analysisContext.RegisterCompilationAction(AnalyzeCompilation);
}
示例2: Initialize
public override void Initialize(AnalysisContext context)
{
context.RegisterSyntaxNodeAction((syntaxContext) =>
{
syntaxContext.ReportDiagnostic(Diagnostic.Create(Rule, syntaxContext.Node.GetLocation()));
}, SyntaxKind.ParamsKeyword);
}
示例3: Initialize
public override void Initialize(AnalysisContext context)
{
context.RegisterSyntaxNodeAction(
(nodeContext) =>
{
Diagnostic diagnostic;
if (TryGetDiagnostic(nodeContext, out diagnostic))
{
nodeContext.ReportDiagnostic(diagnostic);
}
},
new SyntaxKind[] {
SyntaxKind.MethodDeclaration,
SyntaxKind.FieldDeclaration,
SyntaxKind.PropertyDeclaration,
SyntaxKind.IndexerDeclaration,
SyntaxKind.EventDeclaration,
SyntaxKind.ConstructorDeclaration,
SyntaxKind.OperatorDeclaration,
SyntaxKind.ClassDeclaration,
SyntaxKind.InterfaceDeclaration,
SyntaxKind.StructDeclaration,
SyntaxKind.EnumDeclaration,
SyntaxKind.DelegateDeclaration
}
);
}
示例4: Initialize
public override void Initialize(AnalysisContext context)
{
Arg.IsNotNull(() => context);
// TODO: Consider registering other actions that act on syntax instead of or in addition to symbols
context.RegisterSymbolAction(AnalyzeSymbol, SymbolKind.NamedType);
}
示例5: Initialize
public override void Initialize(AnalysisContext context)
{
context.RegisterSyntaxNodeActionInNonGenerated(
c =>
{
if (c.SemanticModel.Compilation.IsTest() ||
c.SemanticModel.Compilation.Options.OutputKind != OutputKind.DynamicallyLinkedLibrary)
{
//this rule only makes sense in libraries
return;
}
var awaitExpression = (AwaitExpressionSyntax)c.Node;
var expression = awaitExpression.Expression;
if (expression == null)
{
return;
}
var taskType = c.SemanticModel.Compilation.GetTypeByMetadataName("System.Threading.Tasks.Task");
if (taskType == null)
{
return;
}
var type = c.SemanticModel.GetTypeInfo(expression).Type;
if (type != null &&
taskType.Equals(type))
{
c.ReportDiagnostic(Diagnostic.Create(Rule, expression.GetLocation()));
}
},
SyntaxKind.AwaitExpression);
}
示例6: Initialize
public override void Initialize(AnalysisContext context)
{
context.RegisterSyntaxNodeActionInNonGenerated(
c =>
{
var methodCall = (InvocationExpressionSyntax) c.Node;
var methodParameterLookup = new MethodParameterLookup(methodCall, c.SemanticModel);
var argumentMappings = methodCall.ArgumentList.Arguments.Select(argument =>
new ArgumentParameterMapping(argument,
methodParameterLookup.GetParameterSymbol(argument)))
.ToList();
var methodSymbol = methodParameterLookup.MethodSymbol;
if (methodSymbol == null)
{
return;
}
foreach (var argumentMapping in argumentMappings)
{
if (ArgumentHasDefaultValue(argumentMapping, c.SemanticModel))
{
var argument = argumentMapping.Argument;
var parameter = argumentMapping.Parameter;
c.ReportDiagnostic(Diagnostic.Create(Rule, argument.GetLocation(), parameter.Name));
}
}
},
SyntaxKind.InvocationExpression);
}
示例7: Initialize
public override void Initialize(AnalysisContext context)
{
context.EnableConcurrentExecution();
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.RegisterCompilationStartAction(compilationContext =>
{
var exportAttribute = compilationContext.Compilation.GetTypeByMetadataName("System.Composition.ExportAttribute");
if (exportAttribute == null)
{
// We don't need to check assemblies unless they're referencing both MEFv2, so we're done
return;
}
compilationContext.RegisterSymbolAction(symbolContext =>
{
var namedType = (INamedTypeSymbol)symbolContext.Symbol;
var namedTypeAttributes = namedType.GetApplicableAttributes();
var exportAttributeApplication = namedTypeAttributes.FirstOrDefault(ad => ad.AttributeClass.DerivesFrom(exportAttribute));
if (exportAttributeApplication != null)
{
if (!namedTypeAttributes.Any(ad => ad.AttributeClass.Name == "SharedAttribute" &&
ad.AttributeClass.ContainingNamespace.Equals(exportAttribute.ContainingNamespace)))
{
// '{0}' is exported with MEFv2 and hence must be marked as Shared
symbolContext.ReportDiagnostic(Diagnostic.Create(Rule, exportAttributeApplication.ApplicationSyntaxReference.GetSyntax().GetLocation(), namedType.Name));
}
}
}, SymbolKind.NamedType);
});
}
示例8: Initialize
public override void Initialize(AnalysisContext analysisContext)
{
analysisContext.RegisterOperationAction(saContext =>
{
var fieldInitializer = saContext.Operation as IFieldInitializer;
// Diagnostics are reported on the last initialized field to retain the previous FxCop behavior
var lastField = fieldInitializer.InitializedFields.LastOrDefault();
var fieldInitializerValue = fieldInitializer.Value;
if (fieldInitializerValue == null||
lastField.IsConst ||
lastField.GetResultantVisibility() == SymbolVisibility.Public ||!lastField.IsStatic || !lastField.IsReadOnly ||
!fieldInitializerValue.ConstantValue.HasValue)
{
return;
}
var initializerValue = fieldInitializerValue.ConstantValue.Value;
// Though null is const we dont fire the diagnostic to be FxCop Compact
if (initializerValue != null)
{
if (fieldInitializerValue.Type == saContext.Compilation.GetSpecialType(SpecialType.System_String) &&
((string)initializerValue)?.Length == 0)
{
saContext.ReportDiagnostic(lastField.CreateDiagnostic(EmptyStringRule, lastField.Name));
return;
}
saContext.ReportDiagnostic(lastField.CreateDiagnostic(DefaultRule, lastField.Name, initializerValue));
}
},
OperationKind.FieldInitializerAtDeclaration);
}
示例9: Initialize
public override void Initialize(AnalysisContext context)
{
context.RegisterSymbolAction(
c =>
{
var symbol = c.Symbol as INamedTypeSymbol;
if (symbol == null ||
!symbol.IsAbstract ||
symbol.TypeKind != TypeKind.Class)
{
return;
}
if (AbstractClassShouldBeInterface(symbol))
{
ReportClass(symbol, MessageToInterface, c);
return;
}
if (AbstractClassShouldBeConcreteClass(symbol))
{
ReportClass(symbol, MessageToConcreteClass, c);
return;
}
},
SymbolKind.NamedType);
}
示例10: Initialize
public override void Initialize(AnalysisContext context)
{
context.RegisterSyntaxNodeActionInNonGenerated(
c =>
{
var field = (FieldDeclarationSyntax)c.Node;
foreach (var variable in field.Declaration.Variables
.Where(v => v.Initializer != null))
{
var variableSymbol = c.SemanticModel.GetDeclaredSymbol(variable) as IFieldSymbol;
if (variableSymbol == null ||
variableSymbol.IsConst)
{
continue;
}
if (CheckDefaultExpressionInitializer(variable) ||
CheckReferenceTypeNullInitializer(variable, variableSymbol) ||
CheckValueTypeDefaultValueInitializer(variable, variableSymbol))
{
c.ReportDiagnostic(Diagnostic.Create(Rule, variable.Initializer.GetLocation(), variableSymbol.Name));
return;
}
}
},
SyntaxKind.FieldDeclaration);
}
示例11: Initialize
public override void Initialize(AnalysisContext context)
{
context.RegisterSyntaxNodeActionInNonGenerated(
c =>
{
var assignment = (AssignmentExpressionSyntax) c.Node;
if (IsInSubExpression(assignment) ||
IsInCondition(assignment))
{
c.ReportDiagnostic(Diagnostic.Create(Rule, assignment.OperatorToken.GetLocation(),
assignment.Left.ToString()));
}
},
SyntaxKind.SimpleAssignmentExpression,
SyntaxKind.AddAssignmentExpression,
SyntaxKind.SubtractAssignmentExpression,
SyntaxKind.MultiplyAssignmentExpression,
SyntaxKind.DivideAssignmentExpression,
SyntaxKind.ModuloAssignmentExpression,
SyntaxKind.AndAssignmentExpression,
SyntaxKind.ExclusiveOrAssignmentExpression,
SyntaxKind.OrAssignmentExpression,
SyntaxKind.LeftShiftAssignmentExpression,
SyntaxKind.RightShiftAssignmentExpression);
}
示例12: Initialize
public override void Initialize(AnalysisContext context)
{
context.RegisterSyntaxNodeActionInNonGenerated(
c =>
{
var lessThan = (BinaryExpressionSyntax) c.Node;
int constValue;
if (SillyBitwiseOperation.TryGetConstantIntValue(lessThan.Left, out constValue) &&
constValue == 0 &&
IsIndexOfCall(lessThan.Right, c.SemanticModel))
{
c.ReportDiagnostic(Diagnostic.Create(Rule, Location.Create(lessThan.SyntaxTree,
TextSpan.FromBounds(lessThan.Left.SpanStart, lessThan.OperatorToken.Span.End))));
}
},
SyntaxKind.LessThanExpression);
context.RegisterSyntaxNodeActionInNonGenerated(
c =>
{
var greaterThan = (BinaryExpressionSyntax)c.Node;
int constValue;
if (SillyBitwiseOperation.TryGetConstantIntValue(greaterThan.Right, out constValue) &&
constValue == 0 &&
IsIndexOfCall(greaterThan.Left, c.SemanticModel))
{
c.ReportDiagnostic(Diagnostic.Create(Rule, Location.Create(greaterThan.SyntaxTree,
TextSpan.FromBounds(greaterThan.OperatorToken.SpanStart, greaterThan.Right.Span.End))));
}
},
SyntaxKind.GreaterThanExpression);
}
示例13: Initialize
public override void Initialize(AnalysisContext context)
{
context.RegisterCodeBlockStartAction<SyntaxKind>(startCodeBlockContext =>
{
// We only care about method bodies.
if (startCodeBlockContext.OwningSymbol.Kind != SymbolKind.Method)
{
return;
}
// We only care about methods with parameters.
var method = (IMethodSymbol)startCodeBlockContext.OwningSymbol;
if (method.Parameters.IsEmpty)
{
return;
}
// Initialize local mutable state in the start action.
var analyzer = new UnusedParametersAnalyzer(method);
// Register an intermediate non-end action that accesses and modifies the state.
startCodeBlockContext.RegisterSyntaxNodeAction(analyzer.AnalyzeSyntaxNode, SyntaxKind.IdentifierName);
// Register an end action to report diagnostics based on the final state.
startCodeBlockContext.RegisterCodeBlockEndAction(analyzer.CodeBlockEndAction);
});
}
示例14: Initialize
public override void Initialize(AnalysisContext context)
{
context.RegisterSyntaxNodeAction(
AnalyzeInvocation,
new SyntaxKind[] { SyntaxKind.InvocationExpression }
);
}
示例15: Initialize
/// <inheritdoc/>
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();
context.RegisterSyntaxNodeAction(DocumentationTriviaAction, SyntaxKind.SingleLineDocumentationCommentTrivia);
}
开发者ID:Romanx,项目名称:StyleCopAnalyzers,代码行数:8,代码来源:SA1612ElementParameterDocumentationMustMatchElementParameters.cs