Skip to content

Commit

Permalink
Review 2
Browse files Browse the repository at this point in the history
  • Loading branch information
zsolt-kolbay-sonarsource committed Feb 26, 2024
1 parent 1fea206 commit e72fa1c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,38 @@ internal static partial class SyntaxNodeExtensions
{
private static readonly ControlFlowGraphCache CfgCache = new();
private static readonly SyntaxKind[] ParenthesizedNodeKinds = [SyntaxKind.ParenthesizedExpression, SyntaxKindEx.ParenthesizedPattern];

private static readonly SyntaxKind[] EnclosingScopeSyntaxKinds = [
SyntaxKind.AddAccessorDeclaration,
SyntaxKind.AddAccessorDeclaration,
SyntaxKind.AnonymousMethodExpression,
SyntaxKind.BaseConstructorInitializer,
SyntaxKind.ConstructorDeclaration,
SyntaxKind.ConversionOperatorDeclaration,
SyntaxKind.DestructorDeclaration,
SyntaxKind.EqualsValueClause,
SyntaxKind.GetAccessorDeclaration,
SyntaxKind.GlobalStatement,
SyntaxKindEx.InitAccessorDeclaration,
SyntaxKindEx.LocalFunctionStatement,
SyntaxKind.MethodDeclaration,
SyntaxKind.OperatorDeclaration,
SyntaxKind.ParenthesizedLambdaExpression,
SyntaxKindEx.PrimaryConstructorBaseType,
SyntaxKind.RemoveAccessorDeclaration,
SyntaxKind.SetAccessorDeclaration,
SyntaxKind.SimpleLambdaExpression,
SyntaxKind.ThisConstructorInitializer];

private static readonly SyntaxKind[] NegationOrConditionEnclosingSyntaxKinds = [
SyntaxKind.AnonymousMethodExpression,
SyntaxKind.BitwiseNotExpression,
SyntaxKind.ConditionalExpression,
SyntaxKind.IfStatement,
SyntaxKind.MethodDeclaration,
SyntaxKind.ParenthesizedLambdaExpression,
SyntaxKind.SimpleLambdaExpression,
SyntaxKind.WhileStatement];

public static ControlFlowGraph CreateCfg(this SyntaxNode node, SemanticModel model, CancellationToken cancel) =>
CfgCache.FindOrCreate(node, model, cancel);

Expand Down Expand Up @@ -79,12 +91,8 @@ public static bool IsPartOfBinaryNegationOrCondition(this SyntaxNode node)
}

var current = topNode;
while (current.Parent != null && current.Parent?.Kind() is not (SyntaxKind.BitwiseNotExpression
or SyntaxKind.IfStatement
or SyntaxKind.WhileStatement
or SyntaxKind.ConditionalExpression
or SyntaxKind.MethodDeclaration
or SyntaxKind.SimpleLambdaExpression))
while (current.Parent != null
&& !NegationOrConditionEnclosingSyntaxKinds.Contains(current.Parent.Kind()))
{
current = current.Parent;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,7 @@ public class Derived(int i) {{baseType}} { }
[DataRow("""int Method() { Func<int, int, int> add = delegate (int a, int b) { return $$a + b$$; }; return add(1, 2); }""", SyntaxKind.AnonymousMethodExpression)]
[DataRow("""Derived(int arg) : base($$arg$$) { }""", SyntaxKind.BaseConstructorInitializer)]
[DataRow("""Derived() { $$var x = 42;$$ }""", SyntaxKind.ConstructorDeclaration)]
[DataRow("""public static implicit operator int(Derived d) => $$42$$;""", SyntaxKind.ConversionOperatorDeclaration)]
[DataRow("""~Derived() { $$var x = 42;$$ }""", SyntaxKind.DestructorDeclaration)]
[DataRow("""int field = $$int.Parse("42")$$;""", SyntaxKind.EqualsValueClause)]
[DataRow("""int Property { get; set; } = $$int.Parse("42")$$;""", SyntaxKind.EqualsValueClause)]
Expand All @@ -1214,6 +1215,7 @@ public class Derived(int i) {{baseType}} { }
[DataRow("""int Method() { return LocalFunction(); int LocalFunction() => $$42$$; }""", SyntaxKindEx.LocalFunctionStatement)]
[DataRow("""int Method() { $$return 42;$$ }""", SyntaxKind.MethodDeclaration)]
[DataRow("""int Method() => $$42$$;""", SyntaxKind.MethodDeclaration)]
[DataRow("""public static Derived operator +(Derived d) => $$d$$;""", SyntaxKind.OperatorDeclaration)]
[DataRow("""int Method() { var lambda = () => $$42$$; return lambda(); }""", SyntaxKind.ParenthesizedLambdaExpression)]
[DataRow("""int Method() { Func<int, int> lambda = x => $$x + 1$$; return lambda(42); }""", SyntaxKind.SimpleLambdaExpression)]
[DataRow("""event EventHandler SomeEvent { add { int x = 42; } remove { $$int x = 42;$$ } }""", SyntaxKind.RemoveAccessorDeclaration)]
Expand Down

0 comments on commit e72fa1c

Please sign in to comment.