本文整理汇总了C#中OperationAnalysisContext类的典型用法代码示例。如果您正苦于以下问题:C# OperationAnalysisContext类的具体用法?C# OperationAnalysisContext怎么用?C# OperationAnalysisContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
OperationAnalysisContext类属于命名空间,在下文中一共展示了OperationAnalysisContext类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: AnalyzeOperation
private void AnalyzeOperation(OperationAnalysisContext context)
{
IArrayCreationExpression arrayCreationExpression = context.Operation as IArrayCreationExpression;
// We can't replace array allocations in attributes, as they're persisted to metadata
// TODO: Once we have operation walkers, we can replace this syntactic check with an operation-based check.
if (arrayCreationExpression.Syntax.Ancestors().Any(IsAttributeSyntax))
{
return;
}
if (arrayCreationExpression.DimensionSizes.Length == 1)
{
IOperation dimensionSize = arrayCreationExpression.DimensionSizes[0];
if (dimensionSize.HasConstantValue(0))
{
// Workaround for https://github.com/dotnet/roslyn/issues/10214
// Bail out for compiler generated param array creation.
if (IsCompilerGeneratedParamsArray(arrayCreationExpression, context))
{
return;
}
// pointers can't be used as generic arguments
if (arrayCreationExpression.ElementType.TypeKind != TypeKind.Pointer)
{
context.ReportDiagnostic(context.Operation.Syntax.CreateDiagnostic(UseArrayEmptyDescriptor));
}
}
}
}
开发者ID:Anniepoh,项目名称:roslyn-analyzers,代码行数:32,代码来源:AvoidZeroLengthArrayAllocations.cs
示例2: AnalyzeOperation
private void AnalyzeOperation(OperationAnalysisContext context)
{
IArrayCreationExpression arrayCreationExpression = context.Operation as IArrayCreationExpression;
// We can't replace array allocations in attributes, as they're persisted to metadata
// TODO: Once we have operation walkers, we can replace this syntactic check with an operation-based check.
if (arrayCreationExpression.Syntax.Ancestors().Any(IsAttributeSyntax))
{
return;
}
if (arrayCreationExpression.DimensionSizes.Length == 1)
{
var dimensionSize = arrayCreationExpression.DimensionSizes[0];
if (dimensionSize.ConstantValue.HasValue && (int)dimensionSize.ConstantValue.Value == 0)
{
// pointers can't be used as generic arguments
if (arrayCreationExpression.ElementType.TypeKind != TypeKind.Pointer)
{
context.ReportDiagnostic(context.Operation.Syntax.CreateDiagnostic(UseArrayEmptyDescriptor));
}
}
}
}
开发者ID:maggiemsft,项目名称:roslyn-analyzers,代码行数:25,代码来源:AvoidZeroLengthArrayAllocations.cs
示例3: AnalyzeOperation
private static void AnalyzeOperation(OperationAnalysisContext context, ImmutableArray<INamedTypeSymbol> taskTypes)
{
IAwaitExpression awaitExpression = context.Operation as IAwaitExpression;
// Get the type of the expression being awaited and check it's a task type.
ITypeSymbol typeOfAwaitedExpression = awaitExpression?.AwaitedValue?.Type;
if (typeOfAwaitedExpression != null && taskTypes.Contains(typeOfAwaitedExpression.OriginalDefinition))
{
context.ReportDiagnostic(awaitExpression.AwaitedValue.Syntax.CreateDiagnostic(Rule));
}
}
开发者ID:duracellko,项目名称:roslyn-analyzers,代码行数:11,代码来源:DoNotDirectlyAwaitATask.cs
示例4: AnalyzeOperation
private void AnalyzeOperation(OperationAnalysisContext context, INamedTypeSymbol containingType)
{
var operation = context.Operation as IInvocationExpression;
var method = operation.TargetMethod;
if (method != null &&
(method.IsAbstract || method.IsVirtual) &&
method.ContainingType == containingType)
{
context.ReportDiagnostic(operation.Syntax.CreateDiagnostic(Rule));
}
}
开发者ID:maggiemsft,项目名称:roslyn-analyzers,代码行数:11,代码来源:DoNotCallOverridableMethodsInConstructors.cs
示例5: AnalyzeOperation
private void AnalyzeOperation(OperationAnalysisContext context, INamedTypeSymbol stringComparisonType)
{
OperationKind kind = context.Operation.Kind;
if (kind == OperationKind.InvocationExpression)
{
AnalyzeInvocationExpression((IInvocationExpression)context.Operation, stringComparisonType, context.ReportDiagnostic);
}
else
{
AnalyzeBinaryExpression((IBinaryOperatorExpression)context.Operation, context.ReportDiagnostic);
}
}
开发者ID:Anniepoh,项目名称:roslyn-analyzers,代码行数:12,代码来源:UseOrdinalStringComparison.cs
示例6: AnalyzeNode
private static void AnalyzeNode(OperationAnalysisContext context)
{
switch (context.Operation.Kind)
{
case OperationKind.InvocationExpression:
AnalyzeInvocationExpression(context);
break;
default:
AnalyzeBinaryExpression(context);
break;
}
}
开发者ID:Anniepoh,项目名称:roslyn-analyzers,代码行数:13,代码来源:TestForEmptyStringsUsingStringLength.cs
示例7: AnalyzeInvocationExpression
/// <summary>
/// Check to see if we have an invocation to string.Equals that has an empty string as an argument.
/// </summary>
private static void AnalyzeInvocationExpression(OperationAnalysisContext context)
{
var invocationOperation = (IInvocationExpression)context.Operation;
if (invocationOperation.ArgumentsInSourceOrder.Length > 0)
{
IMethodSymbol methodSymbol = invocationOperation.TargetMethod;
if (methodSymbol != null &&
IsStringEqualsMethod(methodSymbol) &&
HasAnEmptyStringArgument(invocationOperation))
{
context.ReportDiagnostic(invocationOperation.Syntax.CreateDiagnostic(s_rule));
}
}
}
开发者ID:Anniepoh,项目名称:roslyn-analyzers,代码行数:17,代码来源:TestForEmptyStringsUsingStringLength.cs
示例8: AnalyzeBinaryExpression
/// <summary>
/// Check to see if we have a equals or not equals expression where an empty string is being
/// compared.
/// </summary>
private static void AnalyzeBinaryExpression(OperationAnalysisContext context)
{
var binaryOperation = (IBinaryOperatorExpression)context.Operation;
if (binaryOperation.BinaryOperationKind != BinaryOperationKind.StringEquals &&
binaryOperation.BinaryOperationKind != BinaryOperationKind.StringNotEquals)
{
return;
}
if (IsEmptyString(binaryOperation.LeftOperand) || IsEmptyString(binaryOperation.RightOperand))
{
context.ReportDiagnostic(binaryOperation.Syntax.CreateDiagnostic(s_rule));
}
}
开发者ID:Anniepoh,项目名称:roslyn-analyzers,代码行数:19,代码来源:TestForEmptyStringsUsingStringLength.cs
示例9: AnalyzeOperation
private void AnalyzeOperation(OperationAnalysisContext context)
{
var switchOperation = (ISwitchStatement)context.Operation;
var switchBlock = switchOperation.Syntax;
var tree = switchBlock.SyntaxTree;
if (SwitchIsIncomplete(switchOperation, out var missingCases, out var missingDefaultCase) &&
!tree.OverlapsHiddenPosition(switchBlock.Span, context.CancellationToken))
{
Debug.Assert(missingCases || missingDefaultCase);
var properties = ImmutableDictionary<string, string>.Empty
.Add(PopulateSwitchHelpers.MissingCases, missingCases.ToString())
.Add(PopulateSwitchHelpers.MissingDefaultCase, missingDefaultCase.ToString());
var diagnostic = Diagnostic.Create(
HiddenDescriptor, switchBlock.GetLocation(), properties: properties);
context.ReportDiagnostic(diagnostic);
}
}
开发者ID:TyOverby,项目名称:roslyn,代码行数:19,代码来源:PopulateSwitchDiagnosticAnalyzer.cs
示例10: AnalyzeOperation
private void AnalyzeOperation(OperationAnalysisContext context)
{
var syntaxTree = context.Operation.Syntax.SyntaxTree;
var cancellationToken = context.CancellationToken;
var optionSet = context.Options.GetDocumentOptionSetAsync(syntaxTree, cancellationToken).GetAwaiter().GetResult();
if (optionSet == null)
{
return;
}
var option = optionSet.GetOption(CodeStyleOptions.PreferExplicitTupleNames, context.Compilation.Language);
var severity = option.Notification.Value;
if (severity == DiagnosticSeverity.Hidden)
{
return;
}
var fieldReferenceOperation = (IFieldReferenceExpression)context.Operation;
var field = fieldReferenceOperation.Field;
if (field.ContainingType.IsTupleType)
{
if (field.CorrespondingTupleField.Equals(field))
{
var namedField = GetNamedField(field.ContainingType, field, cancellationToken);
if (namedField != null)
{
var memberAccessSyntax = fieldReferenceOperation.Syntax;
var nameNode = memberAccessSyntax.ChildNodesAndTokens().Reverse().FirstOrDefault();
if (nameNode != null)
{
var properties = ImmutableDictionary<string, string>.Empty.Add(
nameof(ElementName), namedField.Name);
context.ReportDiagnostic(Diagnostic.Create(
GetDescriptorWithSeverity(severity),
nameNode.GetLocation(),
properties));
}
}
}
}
}
开发者ID:TyOverby,项目名称:roslyn,代码行数:42,代码来源:UseExplicitTupleNameDiagnosticAnalyzer.cs
示例11: AnalyzeObjectCreation
private static void AnalyzeObjectCreation(
OperationAnalysisContext context,
ISymbol owningSymbol,
ITypeSymbol argumentExceptionType)
{
var creation = (IObjectCreationExpression)context.Operation;
if (!creation.Type.Inherits(argumentExceptionType))
{
return;
}
if (creation.ArgumentsInParameterOrder.Length == 0)
{
if (HasMessageOrParameterNameConstructor(creation.Type))
{
// Call the {0} constructor that contains a message and/ or paramName parameter
ReportDiagnostic(context, s_localizableMessageNoArguments, creation.Type.Name);
}
}
else
{
foreach (IArgument argument in creation.ArgumentsInParameterOrder)
{
if (argument.Parameter.Type.SpecialType != SpecialType.System_String)
{
continue;
}
string value = argument.Value.ConstantValue.HasValue ? argument.Value.ConstantValue.Value as string : null;
if (value == null)
{
continue;
}
CheckArgument(owningSymbol, creation.Type, argument.Parameter, value, context);
}
}
}
开发者ID:bkoelman,项目名称:roslyn-analyzers,代码行数:38,代码来源:InstantiateArgumentExceptionsCorrectly.cs
示例12: TestAscendingArgument
private static void TestAscendingArgument(OperationAnalysisContext operationContext, IOperation argument, ref long priorArgumentValue)
{
Optional<object> argumentValue = argument.ConstantValue;
if (argumentValue.HasValue && argument.Type.SpecialType == SpecialType.System_Int32)
{
int integerArgument = (int)argumentValue.Value;
if (integerArgument < priorArgumentValue)
{
Report(operationContext, argument.Syntax, OutOfNumericalOrderArgumentsDescriptor);
}
priorArgumentValue = integerArgument;
}
}
开发者ID:rgani,项目名称:roslyn,代码行数:14,代码来源:OperationTestAnalyzer.cs
示例13: Report
private static void Report(OperationAnalysisContext context, SyntaxNode syntax, DiagnosticDescriptor descriptor)
{
context.ReportDiagnostic(Diagnostic.Create(descriptor, syntax.GetLocation()));
}
开发者ID:rgani,项目名称:roslyn,代码行数:4,代码来源:OperationTestAnalyzer.cs
示例14: ReportDiagnostic
private static void ReportDiagnostic(
OperationAnalysisContext oaContext,
IInvocationExpression invocationExpression,
IMethodSymbol targetMethod,
IMethodSymbol correctOverload)
{
oaContext.ReportDiagnostic(
invocationExpression.Syntax.CreateDiagnostic(
Rule,
targetMethod.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat),
oaContext.ContainingSymbol.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat),
correctOverload.ToDisplayString(SymbolDisplayFormat.CSharpErrorMessageFormat)));
}
开发者ID:bkoelman,项目名称:roslyn-analyzers,代码行数:13,代码来源:SpecifyStringComparison.cs
示例15: AnalyzeObjectCreationOperation
private void AnalyzeObjectCreationOperation(OperationAnalysisContext context)
{
AnalyzeObjectCreationInternal(context, null, context.Operation);
}
开发者ID:bkoelman,项目名称:roslyn-analyzers,代码行数:4,代码来源:DoNotUseInsecureDtdProcessing.cs
示例16: AnalyzeMethodOverloads
private void AnalyzeMethodOverloads(OperationAnalysisContext context, IMethodSymbol method, IHasArgumentsExpression expression)
{
if (method.MatchMethodDerivedByName(_xmlTypes.XmlDocument, SecurityMemberNames.Load) || //FxCop CA3056
method.MatchMethodDerivedByName(_xmlTypes.XmlDocument, SecurityMemberNames.LoadXml) || //FxCop CA3057
method.MatchMethodDerivedByName(_xmlTypes.XPathDocument, WellKnownMemberNames.InstanceConstructorName) || //FxCop CA3059
method.MatchMethodDerivedByName(_xmlTypes.XmlSchema, SecurityMemberNames.Read) || //FxCop CA3060
method.MatchMethodDerivedByName(_xmlTypes.DataSet, SecurityMemberNames.ReadXml) || //FxCop CA3063
method.MatchMethodDerivedByName(_xmlTypes.DataSet, SecurityMemberNames.ReadXmlSchema) || //FxCop CA3064
method.MatchMethodDerivedByName(_xmlTypes.XmlSerializer, SecurityMemberNames.Deserialize) || //FxCop CA3070
method.MatchMethodDerivedByName(_xmlTypes.DataTable, SecurityMemberNames.ReadXml) || //FxCop CA3071
method.MatchMethodDerivedByName(_xmlTypes.DataTable, SecurityMemberNames.ReadXmlSchema)) //FxCop CA3072
{
if (SecurityDiagnosticHelpers.HasXmlReaderParameter(method, _xmlTypes) < 0)
{
DiagnosticDescriptor rule = RuleDoNotUseInsecureDtdProcessing;
context.ReportDiagnostic(
Diagnostic.Create(
rule,
expression.Syntax.GetLocation(),
SecurityDiagnosticHelpers.GetLocalizableResourceString(
nameof(DesktopAnalyzersResources.DoNotUseDtdProcessingOverloadsMessage),
method.Name
)
)
);
}
}
else if (method.MatchMethodDerivedByName(_xmlTypes.XmlReader, SecurityMemberNames.Create))
{
int xmlReaderSettingsIndex = SecurityDiagnosticHelpers.GetXmlReaderSettingsParameterIndex(method, _xmlTypes);
if (xmlReaderSettingsIndex < 0)
{
DiagnosticDescriptor rule = RuleDoNotUseInsecureDtdProcessing;
Diagnostic diag = Diagnostic.Create(
RuleDoNotUseInsecureDtdProcessing,
expression.Syntax.GetLocation(),
SecurityDiagnosticHelpers.GetLocalizableResourceString(
nameof(DesktopAnalyzersResources.XmlReaderCreateWrongOverloadMessage)
)
);
context.ReportDiagnostic(diag);
}
else
{
SemanticModel model = context.Compilation.GetSemanticModel(context.Operation.Syntax.SyntaxTree);
IArgument arg = expression.ArgumentsInParameterOrder[xmlReaderSettingsIndex];
ISymbol settingsSymbol = arg.Value.Syntax.GetDeclaredOrReferencedSymbol(model);
if(settingsSymbol == null)
{
return;
}
XmlReaderSettingsEnvironment env;
if (!_xmlReaderSettingsEnvironments.TryGetValue(settingsSymbol, out env))
{
// symbol for settings is not found => passed in without any change => assume insecure
Diagnostic diag = Diagnostic.Create(
RuleDoNotUseInsecureDtdProcessing,
expression.Syntax.GetLocation(),
SecurityDiagnosticHelpers.GetLocalizableResourceString(
nameof(DesktopAnalyzersResources.XmlReaderCreateInsecureInputMessage)
)
);
context.ReportDiagnostic(diag);
}
else if (!env.IsDtdProcessingDisabled && !(env.IsSecureResolver && env.IsMaxCharactersFromEntitiesLimited))
{
Diagnostic diag;
if (env.IsConstructedInCodeBlock)
{
diag = Diagnostic.Create(
RuleDoNotUseInsecureDtdProcessing,
expression.Syntax.GetLocation(),
SecurityDiagnosticHelpers.GetLocalizableResourceString(
nameof(DesktopAnalyzersResources.XmlReaderCreateInsecureConstructedMessage)
)
);
}
else
{
diag = Diagnostic.Create(
RuleDoNotUseInsecureDtdProcessing,
expression.Syntax.GetLocation(),
SecurityDiagnosticHelpers.GetLocalizableResourceString(
nameof(DesktopAnalyzersResources.XmlReaderCreateInsecureInputMessage)
)
);
}
context.ReportDiagnostic(diag);
}
}
}
}
开发者ID:bkoelman,项目名称:roslyn-analyzers,代码行数:96,代码来源:DoNotUseInsecureDtdProcessing.cs
示例17: AnalyzeXmlTextReaderProperties
private void AnalyzeXmlTextReaderProperties(OperationAnalysisContext context, ISymbol assignedSymbol, IAssignmentExpression expression, bool isXmlTextReaderXmlResolverProperty, bool isXmlTextReaderDtdProcessingProperty)
{
XmlTextReaderEnvironment env;
if (!_xmlTextReaderEnvironments.TryGetValue(assignedSymbol, out env))
{
env = new XmlTextReaderEnvironment(_isFrameworkSecure);
}
if (isXmlTextReaderXmlResolverProperty)
{
env.IsXmlResolverSet = true;
}
else
{
env.IsDtdProcessingSet = true;
}
IConversionExpression conv = expression.Value as IConversionExpression;
if (isXmlTextReaderXmlResolverProperty && conv != null && SecurityDiagnosticHelpers.IsXmlSecureResolverType(conv.Operand.Type, _xmlTypes))
{
env.IsSecureResolver = true;
}
else if (isXmlTextReaderXmlResolverProperty && conv != null && SecurityDiagnosticHelpers.IsExpressionEqualsNull(conv.Operand))
{
env.IsSecureResolver = true;
}
else if (isXmlTextReaderDtdProcessingProperty && conv == null && !SecurityDiagnosticHelpers.IsExpressionEqualsDtdProcessingParse(expression.Value))
{
env.IsDtdProcessingDisabled = !SecurityDiagnosticHelpers.IsExpressionEqualsDtdProcessingParse(expression.Value);
}
else
{
// Generate a warning whenever the XmlResolver or DtdProcessing property is set to an insecure value
Diagnostic diag = Diagnostic.Create(
RuleDoNotUseInsecureDtdProcessing,
expression.Syntax.GetLocation(),
SecurityDiagnosticHelpers.GetLocalizableResourceString(
nameof(DesktopAnalyzersResources.XmlTextReaderSetInsecureResolutionMessage)
)
);
context.ReportDiagnostic(diag);
}
}
开发者ID:bkoelman,项目名称:roslyn-analyzers,代码行数:45,代码来源:DoNotUseInsecureDtdProcessing.cs
示例18: AnalyzeOperation
public void AnalyzeOperation(OperationAnalysisContext context)
{
switch (context.Operation.Kind)
{
case OperationKind.ObjectCreationExpression:
AnalyzeObjectCreationOperation(context);
break;
case OperationKind.AssignmentExpression:
AnalyzeAssignment(context);
break;
case OperationKind.FieldInitializerAtDeclaration:
AnalyzeFieldDeclaration(context);
break;
case OperationKind.VariableDeclaration:
AnalyzeVariableDeclaration(context);
break;
case OperationKind.InvocationExpression:
AnalyzeInvocation(context);
break;
}
}
开发者ID:bkoelman,项目名称:roslyn-analyzers,代码行数:21,代码来源:DoNotUseInsecureDtdProcessing.cs
示例19: AnalyzeOperation
public void AnalyzeOperation(OperationAnalysisContext context)
{
// Check if we have any pending unreferenced parameters.
if (_unusedParameters.Count == 0)
{
return;
}
// Mark this parameter as used.
var parameter = ((IParameterReferenceExpression)context.Operation).Parameter;
_unusedParameters.Remove(parameter);
}
开发者ID:hanin,项目名称:roslyn-analyzers,代码行数:12,代码来源:ReviewUnusedParameters.cs
示例20: AnalyzeObjectCreationForXmlTextReader
private void AnalyzeObjectCreationForXmlTextReader(OperationAnalysisContext context, ISymbol variable, IObjectCreationExpression objCreation)
{
XmlTextReaderEnvironment env;
if (variable == null || !_xmlTextReaderEnvironments.TryGetValue(variable, out env))
{
env = new XmlTextReaderEnvironment(_isFrameworkSecure)
{
XmlTextReaderDefinition = objCreation.Syntax
};
}
if (objCreation.Constructor.ContainingType != _xmlTypes.XmlTextReader)
{
env.IsDtdProcessingDisabled = true;
env.IsSecureResolver = true;
}
foreach (ISymbolInitializer init in objCreation.MemberInitializers)
{
var prop = init as IPropertyInitializer;
if (prop != null)
{
IConversionExpression operation = prop.Value as IConversionExpression;
if (operation != null && SecurityDiagnosticHelpers.IsXmlTextReaderXmlResolverPropertyDerived(prop.InitializedProperty, _xmlTypes))
{
env.IsXmlResolverSet = true;
if (SecurityDiagnosticHelpers.IsXmlSecureResolverType(operation.Operand.Type, _xmlTypes))
{
env.IsSecureResolver = true;
}
else if (SecurityDiagnosticHelpers.IsExpressionEqualsNull(operation.Operand))
{
env.IsSecureResolver = true;
}
else
{
env.IsSecureResolver = false;
}
}
else if (SecurityDiagnosticHelpers.IsXmlTextReaderDtdProcessingPropertyDerived(prop.InitializedProperty, _xmlTypes))
{
env.IsDtdProcessingSet = true;
env.IsDtdProcessingDisabled = !SecurityDiagnosticHelpers.IsExpressionEqualsDtdProcessingParse(prop.Value);
}
}
}
// if the XmlResolver or Dtdprocessing property is explicitly set when created, and is to an insecure value, generate a warning
if ((env.IsXmlResolverSet && !env.IsSecureResolver) ||
(env.IsDtdProcessingSet && !env.IsDtdProcessingDisabled))
{
Diagnostic diag = Diagnostic.Create(
RuleDoNotUseInsecureDtdProcessing,
env.XmlTextReaderDefinition.GetLocation(),
SecurityDiagnosticHelpers.GetLocalizableResourceString(
nameof(DesktopAnalyzersResources.XmlTextReaderSetInsecureResolutionMessage)
)
);
context.ReportDiagnostic(diag);
}
// if the XmlResolver or Dtdprocessing property is not explicitly set when constructed for a non-temp XmlTextReader object, add env to the dictionary.
else if (variable != null && !(env.IsDtdProcessingSet && env.IsXmlResolverSet))
{
_xmlTextReaderEnvironments[variable] = env;
}
// if the is not set or set to Parse for a temporary object, report right now.
else if (variable == null && !(env.IsDtdProcessingSet && env.IsXmlResolverSet && env.IsDtdProcessingDisabled && env.IsSecureResolver))
{
Diagnostic diag = Diagnostic.Create(
RuleDoNotUseInsecureDtdProcessing,
env.XmlTextReaderDefinition.GetLocation(),
SecurityDiagnosticHelpers.GetLocalizableResourceString(
nameof(DesktopAnalyzersResources.XmlTextReaderConstructedWithNoSecureResolutionMessage)
)
);
context.ReportDiagnostic(diag);
}
}
开发者ID:bkoelman,项目名称:roslyn-analyzers,代码行数:82,代码来源:DoNotUseInsecureDtdProcessing.cs
注:本文中的OperationAnalysisContext类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论