提交 28791bf5 编写于 作者: J John Hamby

Eliminated ILabeledStatement and OperationKind.LabeledStatement.

上级 cfcfdefb
......@@ -642,6 +642,9 @@ public override void Accept(OperationVisitor visitor)
internal partial class BoundLabelStatement : ILabelStatement
{
// These represent synthesized labels, and do not have an attached statement.
IOperation ILabelStatement.LabeledStatement => null;
ILabelSymbol ILabelStatement.Label => this.Label;
protected override OperationKind StatementKind => OperationKind.LabelStatement;
......@@ -657,22 +660,22 @@ public override void Accept(OperationVisitor visitor)
}
}
internal partial class BoundLabeledStatement : ILabeledStatement
internal partial class BoundLabeledStatement : ILabelStatement
{
IOperation ILabeledStatement.Labeled => this.Body;
IOperation ILabelStatement.LabeledStatement => this.Body;
ILabelSymbol ILabelStatement.Label => this.Label;
protected override OperationKind StatementKind => OperationKind.LabeledStatement;
protected override OperationKind StatementKind => OperationKind.LabelStatement;
public override void Accept(OperationVisitor visitor)
{
visitor.VisitLabeledStatement(this);
visitor.VisitLabelStatement(this);
}
public override TResult Accept<TArgument, TResult>(OperationVisitor<TArgument, TResult> visitor, TArgument argument)
{
return visitor.VisitLabeledStatement(this, argument);
return visitor.VisitLabelStatement(this, argument);
}
}
......
......@@ -1331,6 +1331,29 @@ void Foo()
);
}
[Fact]
public void LabelOperatorsCSharp()
{
const string source = @"
public class A
{
public void Fred()
{
Wilma: goto Betty;
Betty: goto Wilma;
}
}
";
CreateCompilationWithMscorlib45(source)
.VerifyDiagnostics()
.VerifyAnalyzerDiagnostics(new DiagnosticAnalyzer[] { new LabelOperationsTestAnalyzer() }, null, null, false,
Diagnostic(LabelOperationsTestAnalyzer.LabelDescriptor.Id, "Wilma: goto Betty;").WithLocation(6, 9),
Diagnostic(LabelOperationsTestAnalyzer.GotoDescriptor.Id, "goto Betty;").WithLocation(6, 16),
Diagnostic(LabelOperationsTestAnalyzer.LabelDescriptor.Id, "Betty: goto Wilma;").WithLocation(7, 9),
Diagnostic(LabelOperationsTestAnalyzer.GotoDescriptor.Id, "goto Wilma;").WithLocation(7, 16)
);
}
[Fact]
public void UnaryBinaryOperatorsCSharp()
{
......
......@@ -1315,6 +1315,56 @@ public sealed override void Initialize(AnalysisContext context)
}
}
public class LabelOperationsTestAnalyzer : DiagnosticAnalyzer
{
public static readonly DiagnosticDescriptor LabelDescriptor = new DiagnosticDescriptor(
"Label",
"Label found",
"A label was was found",
"Testing",
DiagnosticSeverity.Warning,
isEnabledByDefault: true);
public static readonly DiagnosticDescriptor GotoDescriptor = new DiagnosticDescriptor(
"Goto",
"Goto found",
"A goto was was found",
"Testing",
DiagnosticSeverity.Warning,
isEnabledByDefault: true);
public sealed override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(LabelDescriptor, GotoDescriptor);
public sealed override void Initialize(AnalysisContext context)
{
context.RegisterOperationAction(
(operationContext) =>
{
ILabelSymbol label = ((ILabelStatement)operationContext.Operation).Label;
if (label.Name == "Wilma" || label.Name == "Betty")
{
operationContext.ReportDiagnostic(Diagnostic.Create(LabelDescriptor, operationContext.Operation.Syntax.GetLocation()));
}
},
OperationKind.LabelStatement);
context.RegisterOperationAction(
(operationContext) =>
{
IBranchStatement branch = (IBranchStatement)operationContext.Operation;
if (branch.BranchKind == BranchKind.GoTo)
{
ILabelSymbol label = branch.Target;
if (label.Name == "Wilma" || label.Name == "Betty")
{
operationContext.ReportDiagnostic(Diagnostic.Create(GotoDescriptor, branch.Syntax.GetLocation()));
}
}
},
OperationKind.BranchStatement);
}
}
public class UnaryAndBinaryOperationsTestAnalyzer : DiagnosticAnalyzer
{
public static readonly DiagnosticDescriptor OperatorAddMethodDescriptor = new DiagnosticDescriptor(
......
......@@ -95,19 +95,19 @@ public enum ArgumentKind
/// <summary>
/// Argument is specified positionally and matches the parameter of the same ordinality.
/// </summary>
Positional,
Positional = 0x0,
/// <summary>
/// Argument is specified by name and matches the parameter of the same name.
/// </summary>
Named,
Named = 0x1,
/// <summary>
/// Argument becomes an element of an array that matches a trailing C# params or VB ParamArray parameter.
/// </summary>
ParamArray,
ParamArray = 0x2,
/// <summary>
/// Argument was omitted in source but has a default value supplied automatically.
/// </summary>
DefaultValue
DefaultValue = 0x3
}
/// <summary>
......@@ -185,16 +185,16 @@ public interface ISyntheticLocalReferenceExpression : IReferenceExpression
/// </summary>
public enum SyntheticLocalKind
{
None,
None = 0x0,
/// <summary>
/// Created to capture the step value of a VB for loop.
/// </summary>
ForLoopStepValue,
ForLoopStepValue = 0x1,
/// <summary>
/// Created to capture the limit value of a VB for loop.
/// </summary>
ForLoopLimitValue
ForLoopLimitValue = 0x2
}
/// <summary>
......
......@@ -50,8 +50,8 @@ public enum OperationKind
LoopStatement = 0x6,
/// <summary>Indicates an <see cref="IReturnStatement"/>.</summary>
YieldBreakStatement = 0x9,
/// <summary>Indicates an <see cref="ILabelStatement"/>.</summary>
LabelStatement = 0xa,
LabeledStatement = 0xb, // Why do both of these exist?
/// <summary>Indicates an <see cref="IBranchStatement"/>.</summary>
BranchStatement = 0xc,
/// <summary>Indicates an <see cref="IEmptyStatement"/>.</summary>
......
......@@ -94,19 +94,19 @@ public enum CaseKind
/// <summary>
/// Indicates case x in C# or Case x in VB.
/// </summary>
SingleValue,
SingleValue = 0x0,
/// <summary>
/// Indicates Case Is op x in VB.
/// </summary>
Relational,
Relational = 0x1,
/// <summary>
/// Indicates Case x To Y in VB.
/// </summary>
Range,
Range = 0x2,
/// <summary>
/// Indicates default in C# or Case Else in VB.
/// </summary>
Default
Default = 0x3
}
/// <summary>
......@@ -276,17 +276,10 @@ public interface ILabelStatement : IOperation
/// Label that can be the target of branches.
/// </summary>
ILabelSymbol Label { get; }
}
/// <summary>
/// Represents a C# label statement.
/// </summary>
public interface ILabeledStatement : ILabelStatement
{
/// <summary>
/// Statement that has been labeled.
/// </summary>
IOperation Labeled { get; }
IOperation LabeledStatement { get; }
}
/// <summary>
......
......@@ -89,12 +89,7 @@ public virtual void VisitLabelStatement(ILabelStatement operation)
{
DefaultVisit(operation);
}
public virtual void VisitLabeledStatement(ILabeledStatement operation)
{
DefaultVisit(operation);
}
public virtual void VisitBranchStatement(IBranchStatement operation)
{
DefaultVisit(operation);
......@@ -480,11 +475,6 @@ public virtual TResult VisitLabelStatement(ILabelStatement operation, TArgument
return DefaultVisit(operation, argument);
}
public virtual TResult VisitLabeledStatement(ILabeledStatement operation, TArgument argument)
{
return DefaultVisit(operation, argument);
}
public virtual TResult VisitBranchStatement(IBranchStatement operation, TArgument argument)
{
return DefaultVisit(operation, argument);
......
......@@ -119,11 +119,8 @@ public override void VisitForEachLoopStatement(IForEachLoopStatement operation)
}
public override void VisitLabelStatement(ILabelStatement operation)
{ }
public override void VisitLabeledStatement(ILabeledStatement operation)
{
Visit(operation.Labeled);
Visit(operation.LabeledStatement);
}
public override void VisitBranchStatement(IBranchStatement operation)
......
......@@ -101,7 +101,6 @@ Microsoft.CodeAnalysis.OperationKind.InvalidStatement = 1 -> Microsoft.CodeAnaly
Microsoft.CodeAnalysis.OperationKind.InvocationExpression = 28 -> Microsoft.CodeAnalysis.OperationKind
Microsoft.CodeAnalysis.OperationKind.IsExpression = 51 -> Microsoft.CodeAnalysis.OperationKind
Microsoft.CodeAnalysis.OperationKind.LabelStatement = 10 -> Microsoft.CodeAnalysis.OperationKind
Microsoft.CodeAnalysis.OperationKind.LabeledStatement = 11 -> Microsoft.CodeAnalysis.OperationKind
Microsoft.CodeAnalysis.OperationKind.LambdaExpression = 43 -> Microsoft.CodeAnalysis.OperationKind
Microsoft.CodeAnalysis.OperationKind.LateBoundMemberReferenceExpression = 38 -> Microsoft.CodeAnalysis.OperationKind
Microsoft.CodeAnalysis.OperationKind.LiteralExpression = 26 -> Microsoft.CodeAnalysis.OperationKind
......@@ -434,8 +433,7 @@ Microsoft.CodeAnalysis.Semantics.IIsExpression.IsType.get -> Microsoft.CodeAnaly
Microsoft.CodeAnalysis.Semantics.IIsExpression.Operand.get -> Microsoft.CodeAnalysis.Semantics.IExpression
Microsoft.CodeAnalysis.Semantics.ILabelStatement
Microsoft.CodeAnalysis.Semantics.ILabelStatement.Label.get -> Microsoft.CodeAnalysis.ILabelSymbol
Microsoft.CodeAnalysis.Semantics.ILabeledStatement
Microsoft.CodeAnalysis.Semantics.ILabeledStatement.Labeled.get -> Microsoft.CodeAnalysis.IOperation
Microsoft.CodeAnalysis.Semantics.ILabelStatement.LabeledStatement.get -> Microsoft.CodeAnalysis.IOperation
Microsoft.CodeAnalysis.Semantics.ILambdaExpression
Microsoft.CodeAnalysis.Semantics.ILambdaExpression.Body.get -> Microsoft.CodeAnalysis.Semantics.IBlockStatement
Microsoft.CodeAnalysis.Semantics.ILambdaExpression.Signature.get -> Microsoft.CodeAnalysis.IMethodSymbol
......@@ -692,7 +690,6 @@ override Microsoft.CodeAnalysis.Semantics.OperationWalker.VisitInvalidStatement(
override Microsoft.CodeAnalysis.Semantics.OperationWalker.VisitInvocationExpression(Microsoft.CodeAnalysis.Semantics.IInvocationExpression operation) -> void
override Microsoft.CodeAnalysis.Semantics.OperationWalker.VisitIsExpression(Microsoft.CodeAnalysis.Semantics.IIsExpression operation) -> void
override Microsoft.CodeAnalysis.Semantics.OperationWalker.VisitLabelStatement(Microsoft.CodeAnalysis.Semantics.ILabelStatement operation) -> void
override Microsoft.CodeAnalysis.Semantics.OperationWalker.VisitLabeledStatement(Microsoft.CodeAnalysis.Semantics.ILabeledStatement operation) -> void
override Microsoft.CodeAnalysis.Semantics.OperationWalker.VisitLambdaExpression(Microsoft.CodeAnalysis.Semantics.ILambdaExpression operation) -> void
override Microsoft.CodeAnalysis.Semantics.OperationWalker.VisitLateBoundMemberReferenceExpression(Microsoft.CodeAnalysis.Semantics.ILateBoundMemberReferenceExpression operation) -> void
override Microsoft.CodeAnalysis.Semantics.OperationWalker.VisitLiteralExpression(Microsoft.CodeAnalysis.Semantics.ILiteralExpression operation) -> void
......@@ -785,7 +782,6 @@ virtual Microsoft.CodeAnalysis.Semantics.OperationVisitor.VisitInvalidStatement(
virtual Microsoft.CodeAnalysis.Semantics.OperationVisitor.VisitInvocationExpression(Microsoft.CodeAnalysis.Semantics.IInvocationExpression operation) -> void
virtual Microsoft.CodeAnalysis.Semantics.OperationVisitor.VisitIsExpression(Microsoft.CodeAnalysis.Semantics.IIsExpression operation) -> void
virtual Microsoft.CodeAnalysis.Semantics.OperationVisitor.VisitLabelStatement(Microsoft.CodeAnalysis.Semantics.ILabelStatement operation) -> void
virtual Microsoft.CodeAnalysis.Semantics.OperationVisitor.VisitLabeledStatement(Microsoft.CodeAnalysis.Semantics.ILabeledStatement operation) -> void
virtual Microsoft.CodeAnalysis.Semantics.OperationVisitor.VisitLambdaExpression(Microsoft.CodeAnalysis.Semantics.ILambdaExpression operation) -> void
virtual Microsoft.CodeAnalysis.Semantics.OperationVisitor.VisitLateBoundMemberReferenceExpression(Microsoft.CodeAnalysis.Semantics.ILateBoundMemberReferenceExpression operation) -> void
virtual Microsoft.CodeAnalysis.Semantics.OperationVisitor.VisitLiteralExpression(Microsoft.CodeAnalysis.Semantics.ILiteralExpression operation) -> void
......@@ -859,7 +855,6 @@ virtual Microsoft.CodeAnalysis.Semantics.OperationVisitor<TArgument, TResult>.Vi
virtual Microsoft.CodeAnalysis.Semantics.OperationVisitor<TArgument, TResult>.VisitInvocationExpression(Microsoft.CodeAnalysis.Semantics.IInvocationExpression operation, TArgument argument) -> TResult
virtual Microsoft.CodeAnalysis.Semantics.OperationVisitor<TArgument, TResult>.VisitIsExpression(Microsoft.CodeAnalysis.Semantics.IIsExpression operation, TArgument argument) -> TResult
virtual Microsoft.CodeAnalysis.Semantics.OperationVisitor<TArgument, TResult>.VisitLabelStatement(Microsoft.CodeAnalysis.Semantics.ILabelStatement operation, TArgument argument) -> TResult
virtual Microsoft.CodeAnalysis.Semantics.OperationVisitor<TArgument, TResult>.VisitLabeledStatement(Microsoft.CodeAnalysis.Semantics.ILabeledStatement operation, TArgument argument) -> TResult
virtual Microsoft.CodeAnalysis.Semantics.OperationVisitor<TArgument, TResult>.VisitLambdaExpression(Microsoft.CodeAnalysis.Semantics.ILambdaExpression operation, TArgument argument) -> TResult
virtual Microsoft.CodeAnalysis.Semantics.OperationVisitor<TArgument, TResult>.VisitLateBoundMemberReferenceExpression(Microsoft.CodeAnalysis.Semantics.ILateBoundMemberReferenceExpression operation, TArgument argument) -> TResult
virtual Microsoft.CodeAnalysis.Semantics.OperationVisitor<TArgument, TResult>.VisitLiteralExpression(Microsoft.CodeAnalysis.Semantics.ILiteralExpression operation, TArgument argument) -> TResult
......
......@@ -1050,6 +1050,13 @@ Namespace Microsoft.CodeAnalysis.VisualBasic
End Get
End Property
Private ReadOnly Property ILabeled As IOperation Implements ILabelStatement.LabeledStatement
Get
' The VB bound trees do not encode the statement to which the label is attached.
Return Nothing
End Get
End Property
Protected Overrides Function StatementKind() As OperationKind
Return OperationKind.LabelStatement
End Function
......
......@@ -1432,6 +1432,32 @@ End Class
Diagnostic(StaticMemberTestAnalyzer.StaticMemberDescriptor.Id, "D.Method()").WithLocation(27, 9))
End Sub
<Fact>
Public Sub LabelOperatorsVisualBasic()
Dim source = <compilation>
<file name="c.vb">
<![CDATA[
Public Class A
Public Sub Fred()
Wilma:
GoTo Betty
Betty:
GoTo Wilma
End Sub
End Class
]]>
</file>
</compilation>
Dim comp = CompilationUtils.CreateCompilationWithMscorlibAndVBRuntime(source)
comp.VerifyDiagnostics()
comp.VerifyAnalyzerDiagnostics({New LabelOperationsTestAnalyzer}, Nothing, Nothing, False,
Diagnostic(LabelOperationsTestAnalyzer.LabelDescriptor.Id, "Wilma:").WithLocation(3, 9),
Diagnostic(LabelOperationsTestAnalyzer.GotoDescriptor.Id, "GoTo Betty").WithLocation(4, 9),
Diagnostic(LabelOperationsTestAnalyzer.LabelDescriptor.Id, "Betty:").WithLocation(5, 9),
Diagnostic(LabelOperationsTestAnalyzer.GotoDescriptor.Id, "GoTo Wilma").WithLocation(6, 9))
End Sub
<Fact>
Public Sub UnaryBinaryOperatorsVisualBasic()
Dim source = <compilation>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册