Skip to content

Commit 813f5b9

Browse files
committed
C#: Update PrintAST query
1 parent d93d658 commit 813f5b9

File tree

18 files changed

+135
-257
lines changed

18 files changed

+135
-257
lines changed

csharp/ql/lib/semmle/code/csharp/PrintAst.qll

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ private predicate shouldPrint(Element e, Location l) {
3232
}
3333

3434
private predicate isImplicitExpression(ControlFlowElement element) {
35-
element.(Expr).isImplicit() and not exists(element.getAChild())
35+
element.(Expr).isImplicit() and
36+
not element instanceof CastExpr and
37+
not element.(OperatorCall).getTarget() instanceof ImplicitConversionOperator and
38+
not element instanceof ElementInitializer
3639
}
3740

3841
private predicate isFilteredCompilerGenerated(Declaration d) {
@@ -291,18 +294,6 @@ class ControlFlowElementNode extends ElementNode {
291294
controlFlowElement = element and
292295
// Removing implicit expressions
293296
not isImplicitExpression(element) and
294-
// Removing extra nodes that are generated for an `AssignOperation`
295-
not exists(AssignOperation ao |
296-
ao.hasExpandedAssignment() and
297-
(
298-
ao.getExpandedAssignment() = controlFlowElement or
299-
ao.getExpandedAssignment().getRValue() = controlFlowElement or
300-
ao.getExpandedAssignment().getRValue().(BinaryOperation).getLeftOperand() =
301-
controlFlowElement.getParent*() or
302-
ao.getExpandedAssignment().getRValue().(OperatorCall).getChild(0) =
303-
controlFlowElement.getParent*()
304-
)
305-
) and
306297
not isNotNeeded(element.getParent+()) and
307298
// LambdaExpr is both a Callable and a ControlFlowElement,
308299
// print it with the more specific CallableNode
@@ -429,7 +420,7 @@ final class DeclarationWithAccessorsNode extends ElementNode {
429420
result.(ParametersNode).getParameterizable() = declaration
430421
or
431422
childIndex = 2 and
432-
result.(ElementNode).getElement() = declaration.(Property).getInitializer().getParent()
423+
result.(ElementNode).getElement() = declaration.(Property).getInitializer()
433424
or
434425
result.(ElementNode).getElement() =
435426
rank[childIndex - 2](Element a, string file, int line, int column, string name |
@@ -462,12 +453,7 @@ final class FieldNode extends ElementNode {
462453
result.(AttributesNode).getAttributable() = field
463454
or
464455
childIndex = 1 and
465-
field.hasInitializer() and
466-
(
467-
if field.getDeclaringType() instanceof Enum
468-
then result.(ElementNode).getElement() = field.getInitializer()
469-
else result.(ElementNode).getElement() = field.getInitializer().getParent()
470-
)
456+
result.(ElementNode).getElement() = field.getInitializer()
471457
}
472458
}
473459

csharp/ql/lib/semmle/code/csharp/Variable.qll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ class Field extends Variable, AssignableMember, Attributable, TopLevelExprParent
408408
* }
409409
* ```
410410
*/
411-
override Expr getInitializer() { result = this.getChildExpr(0).getChildExpr(0) }
411+
final override Expr getInitializer() { result = this.getChildExpr(0).getChildExpr(0) }
412412

413413
/**
414414
* Holds if this field has an initial value. For example, the initial
@@ -515,6 +515,4 @@ class EnumConstant extends MemberConstant {
515515
* ```
516516
*/
517517
predicate hasExplicitValue() { exists(this.getInitializer()) }
518-
519-
override Expr getInitializer() { result = this.getChildExpr(0) }
520518
}

csharp/ql/lib/semmle/code/csharp/exprs/Expr.qll

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ class Expr extends ControlFlowElement, @expr {
6767
* Holds if this expression is generated by the compiler and does not appear
6868
* explicitly in the source code.
6969
*/
70-
predicate isImplicit() { compiler_generated(this) }
70+
final predicate isImplicit() {
71+
compiler_generated(this) or
72+
this =
73+
any(AssignOperation op).getExpandedAssignment().getRValue().getChildExpr(0).getAChildExpr+()
74+
}
7175

7276
/**
7377
* Gets an expression that is the result of stripping (recursively) all

csharp/ql/test/library-tests/attributes/PrintAst.expected

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,7 @@ attributes.cs:
163163
# 67| 4: [BlockStmt] {...}
164164
# 70| [Enum] E
165165
# 70| 5: [Field] A
166-
# 70| 1: [AssignExpr] ... = ...
167-
# 70| 0: [MemberConstantAccess] access to constant A
168-
# 70| 1: [IntLiteral] 42
166+
# 70| 1: [IntLiteral] 42
169167
# 72| [Class] ArgsAttribute
170168
#-----| 3: (Base types)
171169
# 72| 0: [TypeMention] Attribute

csharp/ql/test/library-tests/comments/PrintAst.expected

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,10 @@ trivia.cs:
186186
# 89| 4: [BlockStmt] {...}
187187
# 92| 7: [Field] F1
188188
# 92| -1: [TypeMention] int
189-
# 92| 1: [AssignExpr] ... = ...
190-
# 92| 0: [FieldAccess] access to field F1
191-
# 94| 1: [IntLiteral] 10
189+
# 94| 1: [IntLiteral] 10
192190
# 98| 8: [Field] F2
193191
# 98| -1: [TypeMention] int
194-
# 98| 1: [AssignExpr] ... = ...
195-
# 98| 0: [FieldAccess] access to field F2
196-
# 98| 1: [IntLiteral] 0
192+
# 98| 1: [IntLiteral] 0
197193
# 100| 9: [Property] P1
198194
# 100| -1: [TypeMention] int
199195
# 102| 3: [Getter] get_P1

csharp/ql/test/library-tests/csharp11/PrintAst.expected

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,18 +1111,14 @@ StaticInterfaceMembers.cs:
11111111
#-----| 3: (Base types)
11121112
# 28| 4: [Property] Real
11131113
# 28| -1: [TypeMention] double
1114-
# 28| 2: [AssignExpr] ... = ...
1115-
# 28| 0: [PropertyCall] access to property Real
1116-
# 28| 1: [DoubleLiteral] 0
1114+
# 28| 2: [DoubleLiteral] 0
11171115
# 28| 3: [Getter] get_Real
11181116
# 28| 4: [Setter] set_Real
11191117
#-----| 2: (Parameters)
11201118
# 28| 0: [Parameter] value
11211119
# 29| 5: [Property] Imaginary
11221120
# 29| -1: [TypeMention] double
1123-
# 29| 2: [AssignExpr] ... = ...
1124-
# 29| 0: [PropertyCall] access to property Imaginary
1125-
# 29| 1: [DoubleLiteral] 0
1121+
# 29| 2: [DoubleLiteral] 0
11261122
# 29| 3: [Getter] get_Imaginary
11271123
# 29| 4: [Setter] set_Imaginary
11281124
#-----| 2: (Parameters)

csharp/ql/test/library-tests/csharp6/PrintAst.expected

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ csharp6.cs:
22
# 10| [Class] TestCSharp6
33
# 12| 6: [Property] Value
44
# 12| -1: [TypeMention] int
5-
# 15| 2: [AssignExpr] ... = ...
6-
# 12| 0: [PropertyCall] access to property Value
7-
# 15| 1: [IntLiteral] 20
5+
# 15| 2: [IntLiteral] 20
86
# 14| 3: [Getter] get_Value
97
# 17| 7: [Method] Fn
108
# 17| -1: [TypeMention] Void

csharp/ql/test/library-tests/csharp7.2/PrintAst.expected

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,11 @@ csharp72.cs:
3535
# 44| [Class] NumericLiterals
3636
# 46| 5: [Field] binaryValue
3737
# 46| -1: [TypeMention] int
38-
# 46| 1: [AssignExpr] ... = ...
39-
# 46| 0: [FieldAccess] access to field binaryValue
40-
# 46| 1: [IntLiteral] 85
38+
# 46| 1: [IntLiteral] 85
4139
# 49| [Class] PrivateProtected
4240
# 51| 5: [Field] X
4341
# 51| -1: [TypeMention] int
44-
# 51| 1: [AssignExpr] ... = ...
45-
# 51| 0: [FieldAccess] access to field X
46-
# 51| 1: [IntLiteral] 1
42+
# 51| 1: [IntLiteral] 1
4743
# 53| 6: [Method] F
4844
# 53| -1: [TypeMention] Void
4945
# 53| 4: [BlockStmt] {...}

csharp/ql/test/library-tests/csharp7/PrintAst.expected

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,17 @@ CSharp7.cs:
22
# 5| [Class] Literals
33
# 7| 5: [Field] x
44
# 7| -1: [TypeMention] int
5-
# 7| 1: [AssignExpr] ... = ...
6-
# 7| 0: [FieldAccess] access to field x
7-
# 7| 1: [IntLiteral] 11
5+
# 7| 1: [IntLiteral] 11
86
# 8| 6: [Field] y
97
# 8| -1: [TypeMention] int
10-
# 8| 1: [AssignExpr] ... = ...
11-
# 8| 0: [FieldAccess] access to field y
12-
# 8| 1: [IntLiteral] 123456
8+
# 8| 1: [IntLiteral] 123456
139
# 9| 7: [Field] z
1410
# 9| -1: [TypeMention] int
15-
# 9| 1: [AssignExpr] ... = ...
16-
# 9| 0: [FieldAccess] access to field z
17-
# 9| 1: [IntLiteral] 128
11+
# 9| 1: [IntLiteral] 128
1812
# 12| [Class] ExpressionBodiedMembers
1913
# 14| 4: [Field] field
2014
# 14| -1: [TypeMention] int
21-
# 14| 1: [AssignExpr] ... = ...
22-
# 14| 0: [FieldAccess] access to field field
23-
# 14| 1: [IntLiteral] 0
15+
# 14| 1: [IntLiteral] 0
2416
# 15| 5: [Method] Foo
2517
# 15| -1: [TypeMention] int
2618
# 15| 4: [FieldAccess] access to field field

csharp/ql/test/library-tests/csharp8/PrintAst.expected

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,14 @@ AlternateInterpolatedStrings.cs:
22
# 3| [Class] AlternateInterpolatedStrings
33
# 5| 5: [Field] s1
44
# 5| -1: [TypeMention] string
5-
# 5| 1: [AssignExpr] ... = ...
6-
# 5| 0: [FieldAccess] access to field s1
7-
# 5| 1: [InterpolatedStringExpr] $"..."
8-
# 5| 0: [StringLiteralUtf16] "C:"
9-
# 5| 1: [IntLiteral] 12
5+
# 5| 1: [InterpolatedStringExpr] $"..."
6+
# 5| 0: [StringLiteralUtf16] "C:"
7+
# 5| 1: [IntLiteral] 12
108
# 6| 6: [Field] s2
119
# 6| -1: [TypeMention] string
12-
# 6| 1: [AssignExpr] ... = ...
13-
# 6| 0: [FieldAccess] access to field s2
14-
# 6| 1: [InterpolatedStringExpr] $"..."
15-
# 6| 0: [StringLiteralUtf16] "C:"
16-
# 6| 1: [IntLiteral] 12
10+
# 6| 1: [InterpolatedStringExpr] $"..."
11+
# 6| 0: [StringLiteralUtf16] "C:"
12+
# 6| 1: [IntLiteral] 12
1713
AsyncStreams.cs:
1814
# 6| [Class] AsyncStreams
1915
# 8| 5: [Method] Items

0 commit comments

Comments
 (0)