提交 0d8b154f 编写于 作者: G Gen Lu

Make registering OperationKind.None not to return any operation.

上级 855a58b5
......@@ -71,7 +71,7 @@ public Collector(List<IOperation> nodes)
public override BoundNode Visit(BoundNode node)
{
IOperation operation = node as IOperation;
if (operation != null)
if (operation != null && operation.Kind != OperationKind.None)
{
this._nodes.Add(operation);
// Certain child-operation of following operation kinds do not occur in bound nodes,
......
......@@ -1154,5 +1154,25 @@ public void M1()
Diagnostic(ParamsArrayTestAnalyzer.LongParamsDescriptor.Id, "new int[] { 2, 3, 4, 5, 6 }").WithLocation(17, 15)
);
}
[Fact]
public void NoneOperationCSharp()
{
// BoundStatementList is OperationKind.None
const string source = @"
class C
{
public void M0()
{
int x = 0;
int y = x++;
int z = y++;
}
}
";
CreateCompilationWithMscorlib45(source)
.VerifyDiagnostics()
.VerifyAnalyzerDiagnostics(new DiagnosticAnalyzer[] { new NoneOperationTestAnalyzer() }, null, null, false);
}
}
}
......@@ -1066,4 +1066,41 @@ private static long IntegralValue(object value)
return 0;
}
}
/// <summary>Analyzer used to test None IOperations.</summary>
public class NoneOperationTestAnalyzer : DiagnosticAnalyzer
{
private const string ReliabilityCategory = "Reliability";
// We should not see this warning triggered by any code
public static readonly DiagnosticDescriptor NoneOperationDescriptor = new DiagnosticDescriptor(
"NoneOperation",
"None operation found",
"an IOperation of None kind is found",
ReliabilityCategory,
DiagnosticSeverity.Warning,
isEnabledByDefault: true);
public sealed override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics
{
get { return ImmutableArray.Create(NoneOperationDescriptor); }
}
public sealed override void Initialize(AnalysisContext context)
{
context.RegisterOperationAction(
(operationContext) =>
{
Report(operationContext, operationContext.Operation.Syntax, NoneOperationDescriptor);
},
// None kind is only supposed to be used internally and will not actually register actions.
OperationKind.None);
}
private static void Report(OperationAnalysisContext context, SyntaxNode syntax, DiagnosticDescriptor descriptor)
{
context.ReportDiagnostic(Diagnostic.Create(descriptor, syntax.GetLocation()));
}
}
}
\ No newline at end of file
......@@ -35,7 +35,7 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
Public Overrides Function Visit(node As BoundNode) As BoundNode
Dim operation = TryCast(node, IOperation)
If operation IsNot Nothing Then
If operation IsNot Nothing AndAlso operation.Kind <> OperationKind.None Then
Me._nodes.Add(operation)
' Certain child-operation of following operation kinds do not occur in bound nodes,
' and those child-operation nodes have to be added explicitly.
......
......@@ -1210,5 +1210,32 @@ End Class
Diagnostic(ParamsArrayTestAnalyzer.LongParamsDescriptor.Id, "New Integer() { 2, 3, 4, 5, 6 }").WithLocation(13, 15),
Diagnostic(ParamsArrayTestAnalyzer.LongParamsDescriptor.Id, "New Integer() { 2, 3, 4, 5, 6 }").WithLocation(13, 15))
End Sub
<Fact>
Public Sub NoneOperationVisualBasic()
' BoundCaseStatement is OperationKind.None
Dim source = <compilation>
<file name="c.vb">
<![CDATA[
Class C
Public Sub M1(x as Integer)
Select Case x
Case 1, 2
Exit Select
Case = 10
Exit Select
Case Else
Exit Select
End Select
End Sub
End Class
]]>
</file>
</compilation>
Dim comp = CompilationUtils.CreateCompilationWithMscorlibAndVBRuntime(source)
comp.VerifyDiagnostics()
comp.VerifyAnalyzerDiagnostics({New NoneOperationTestAnalyzer}, Nothing, Nothing, False)
End Sub
End Class
End Namespace
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册