Skip to content

Commit fe3c85a

Browse files
committed
C#: Swap left and right child of assignment expressions.
1 parent 876c56e commit fe3c85a

File tree

7 files changed

+31
-39
lines changed

7 files changed

+31
-39
lines changed

csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Assignment.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,11 @@ public static Assignment Create(ExpressionNodeInfo info)
2222

2323
protected override void PopulateExpression(TextWriter trapFile)
2424
{
25-
if (Kind == ExprKind.SIMPLE_ASSIGN || Kind == ExprKind.REMOVE_EVENT || Kind == ExprKind.ADD_EVENT)
26-
{
27-
Create(Context, Syntax.Left, this, 1);
28-
Create(Context, Syntax.Right, this, 0);
25+
Create(Context, Syntax.Left, this, 0);
26+
Create(Context, Syntax.Right, this, 1);
2927

30-
if (Kind == ExprKind.ADD_EVENT || Kind == ExprKind.REMOVE_EVENT)
31-
{
32-
OperatorCall(trapFile, Syntax);
33-
}
34-
}
35-
else
28+
if (Kind != ExprKind.SIMPLE_ASSIGN)
3629
{
37-
Create(Context, Syntax.Left, this, 0);
38-
Create(Context, Syntax.Right, this, 1);
3930
OperatorCall(trapFile, Syntax);
4031
}
4132
}

csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Initializer.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,30 +83,31 @@ protected override void PopulateExpression(TextWriter trapFile)
8383
{
8484
var assignmentInfo = new ExpressionNodeInfo(Context, init, this, child++).SetKind(ExprKind.SIMPLE_ASSIGN);
8585
var assignmentEntity = new Expression(assignmentInfo);
86-
var typeInfoRight = Context.GetTypeInfo(assignment.Right);
87-
if (typeInfoRight.Type is null)
88-
// The type may be null for nested initializers such as
89-
// ```csharp
90-
// new ClassWithArrayField() { As = { [0] = a } }
91-
// ```
92-
// In this case we take the type from the assignment
93-
// `As = { [0] = a }` instead
94-
typeInfoRight = assignmentInfo.TypeInfo;
95-
CreateFromNode(new ExpressionNodeInfo(Context, assignment.Right, assignmentEntity, 0, typeInfoRight));
96-
9786
var target = Context.GetSymbolInfo(assignment.Left);
9887

9988
// If the target is null, then assume that this is an array initializer (of the form `[...] = ...`)
100-
10189
var access = target.Symbol is null ?
102-
new Expression(new ExpressionNodeInfo(Context, assignment.Left, assignmentEntity, 1).SetKind(ExprKind.ARRAY_ACCESS)) :
103-
Access.Create(new ExpressionNodeInfo(Context, assignment.Left, assignmentEntity, 1), target.Symbol, false, Context.CreateEntity(target.Symbol));
90+
new Expression(new ExpressionNodeInfo(Context, assignment.Left, assignmentEntity, 0).SetKind(ExprKind.ARRAY_ACCESS)) :
91+
Access.Create(new ExpressionNodeInfo(Context, assignment.Left, assignmentEntity, 0), target.Symbol, false, Context.CreateEntity(target.Symbol));
10492

10593
if (assignment.Left is ImplicitElementAccessSyntax iea)
10694
{
10795
// An array/indexer initializer of the form `[...] = ...`
10896
access.PopulateArguments(trapFile, iea.ArgumentList.Arguments, 0);
10997
}
98+
99+
var typeInfoRight = Context.GetTypeInfo(assignment.Right);
100+
if (typeInfoRight.Type is null)
101+
{
102+
// The type may be null for nested initializers such as
103+
// ```csharp
104+
// new ClassWithArrayField() { As = { [0] = a } }
105+
// ```
106+
// In this case we take the type from the assignment
107+
// `As = { [0] = a }` instead
108+
typeInfoRight = assignmentInfo.TypeInfo;
109+
}
110+
CreateFromNode(new ExpressionNodeInfo(Context, assignment.Right, assignmentEntity, 1, typeInfoRight));
110111
}
111112
else
112113
{

csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/ObjectCreation/AnonymousObjectCreation.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ protected override void PopulateExpression(TextWriter trapFile)
4141
var loc = Context.CreateLocation(init.GetLocation());
4242

4343
var assignment = new Expression(new ExpressionInfo(Context, type, loc, ExprKind.SIMPLE_ASSIGN, objectInitializer, child++, isCompilerGenerated: false, null));
44-
Create(Context, init.Expression, assignment, 0);
4544
Property.Create(Context, property);
46-
47-
var access = new Expression(new ExpressionInfo(Context, type, loc, ExprKind.PROPERTY_ACCESS, assignment, 1, isCompilerGenerated: false, null));
45+
var access = new Expression(new ExpressionInfo(Context, type, loc, ExprKind.PROPERTY_ACCESS, assignment, 0, isCompilerGenerated: false, null));
4846
trapFile.expr_access(access, propEntity);
47+
48+
Create(Context, init.Expression, assignment, 1);
4949
}
5050
}
5151
}

csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Query.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ protected Expression DeclareRangeVariable(Context cx, IExpressionParentEntity pa
9494
child
9595
);
9696

97-
Expression.Create(cx, Expr, decl, 0);
98-
9997
var nameLoc = cx.CreateLocation(name.GetLocation());
100-
var access = new Expression(new ExpressionInfo(cx, type, nameLoc, ExprKind.LOCAL_VARIABLE_ACCESS, decl, 1, isCompilerGenerated: false, null));
98+
var access = new Expression(new ExpressionInfo(cx, type, nameLoc, ExprKind.LOCAL_VARIABLE_ACCESS, decl, 0, isCompilerGenerated: false, null));
10199
cx.TrapWriter.Writer.expr_access(access, LocalVariable.Create(cx, variableSymbol));
102100

101+
Expression.Create(cx, Expr, decl, 1);
102+
103103
return decl;
104104
}
105105

csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/VariableDeclaration.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,11 @@ public static VariableDeclaration CreateDeclarator(Context cx, VariableDeclarato
176176

177177
if (d.Initializer is not null)
178178
{
179-
Create(cx, d.Initializer.Value, ret, 0);
180-
181179
// Create an access
182-
var access = new Expression(new ExpressionInfo(cx, type, localVar.Location, ExprKind.LOCAL_VARIABLE_ACCESS, ret, 1, isCompilerGenerated: false, null));
180+
var access = new Expression(new ExpressionInfo(cx, type, localVar.Location, ExprKind.LOCAL_VARIABLE_ACCESS, ret, 0, isCompilerGenerated: false, null));
183181
cx.TrapWriter.Writer.expr_access(access, localVar);
182+
183+
Create(cx, d.Initializer.Value, ret, 1);
184184
}
185185

186186
if (d.Parent is VariableDeclarationSyntax decl)

csharp/extractor/Semmle.Extraction.CSharp/Entities/Field.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ private Expression AddInitializerAssignment(TextWriter trapFile, ExpressionSynta
116116
{
117117
var type = Symbol.GetAnnotatedType();
118118
var simpleAssignExpr = new Expression(new ExpressionInfo(Context, type, loc, ExprKind.SIMPLE_ASSIGN, this, child++, isCompilerGenerated: true, constValue));
119-
Expression.CreateFromNode(new ExpressionNodeInfo(Context, initializer, simpleAssignExpr, 0));
120-
var access = new Expression(new ExpressionInfo(Context, type, Location, ExprKind.FIELD_ACCESS, simpleAssignExpr, 1, isCompilerGenerated: true, constValue));
119+
var access = new Expression(new ExpressionInfo(Context, type, Location, ExprKind.FIELD_ACCESS, simpleAssignExpr, 0, isCompilerGenerated: true, constValue));
121120
trapFile.expr_access(access, this);
121+
Expression.CreateFromNode(new ExpressionNodeInfo(Context, initializer, simpleAssignExpr, 1));
122122
return access;
123123
}
124124

csharp/extractor/Semmle.Extraction.CSharp/Entities/Property.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ public override void Populate(TextWriter trapFile)
9494
var loc = Context.CreateLocation(initializer!.GetLocation());
9595
var annotatedType = AnnotatedTypeSymbol.CreateNotAnnotated(Symbol.Type);
9696
var simpleAssignExpr = new Expression(new ExpressionInfo(Context, annotatedType, loc, ExprKind.SIMPLE_ASSIGN, this, child++, isCompilerGenerated: true, null));
97-
Expression.CreateFromNode(new ExpressionNodeInfo(Context, initializer.Value, simpleAssignExpr, 0));
98-
var access = new Expression(new ExpressionInfo(Context, annotatedType, Location, ExprKind.PROPERTY_ACCESS, simpleAssignExpr, 1, isCompilerGenerated: true, null));
97+
var access = new Expression(new ExpressionInfo(Context, annotatedType, Location, ExprKind.PROPERTY_ACCESS, simpleAssignExpr, 0, isCompilerGenerated: true, null));
9998
trapFile.expr_access(access, this);
99+
Expression.CreateFromNode(new ExpressionNodeInfo(Context, initializer.Value, simpleAssignExpr, 1));
100100
if (!Symbol.IsStatic)
101101
{
102102
This.CreateImplicit(Context, Symbol.ContainingType, Location, access, -1);

0 commit comments

Comments
 (0)