Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7066568

Browse files
committed
C#: Change type access and expression order in casts for AST printing
1 parent 6c48eb8 commit 7066568

20 files changed

Lines changed: 207 additions & 189 deletions

File tree

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,24 @@ final class AssignmentNode extends ControlFlowElementNode {
356356
}
357357
}
358358

359+
/**
360+
* A node representing a `CastExpr`.
361+
* Type access and casted expression children are shown in this order.
362+
*/
363+
final class CastExprNode extends ControlFlowElementNode {
364+
CastExpr cast;
365+
366+
CastExprNode() { cast = element }
367+
368+
override ElementNode getChild(int childIndex) {
369+
childIndex = 0 and
370+
result.getElement() = cast.getTypeAccess()
371+
or
372+
childIndex = 1 and
373+
result.getElement() = cast.getExpr()
374+
}
375+
}
376+
359377
/**
360378
* A node representing a `Callable`, such as method declaration.
361379
*/

csharp/ql/test/experimental/ir/ir/PrintAst.expected

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,9 @@ casts.cs:
185185
# 14| -1: [TypeMention] Casts_B
186186
# 14| 0: [LocalVariableAccess] access to local variable bobjCE
187187
# 14| 1: [CastExpr] (...) ...
188-
# 14| 0: [LocalVariableAccess] access to local variable Aobj
189-
# 14| 1: [TypeAccess] access to type Casts_B
188+
# 14| 0: [TypeAccess] access to type Casts_B
190189
# 14| 0: [TypeMention] Casts_B
190+
# 14| 1: [LocalVariableAccess] access to local variable Aobj
191191
# 15| 2: [LocalVariableDeclStmt] ... ...;
192192
# 15| 0: [LocalVariableDeclAndInitExpr] Casts_B bobjAS = ...
193193
# 15| -1: [TypeMention] Casts_B
@@ -912,7 +912,7 @@ pointers.cs:
912912
# 6| 1: [TypeMention] Int32
913913
# 6| 0: [LocalVariableAccess] access to local variable b
914914
# 6| 1: [CastExpr] (...) ...
915-
# 6| 0: [ParameterAccess] access to parameter arr
915+
# 6| 1: [ParameterAccess] access to parameter arr
916916
# 7| 0: [BlockStmt] {...}
917917
# 8| 0: [LocalVariableDeclStmt] ... ...;
918918
# 8| 0: [LocalVariableDeclAndInitExpr] Int32* p = ...

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Assignments.cs:
1717
# 8| -1: [TypeMention] dynamic
1818
# 8| 0: [LocalVariableAccess] access to local variable d
1919
# 8| 1: [CastExpr] (...) ...
20-
# 8| 0: [IntLiteral] 0
20+
# 8| 1: [IntLiteral] 0
2121
# 9| 3: [ExprStmt] ...;
2222
# 9| 0: [AssignSubExpr] ... -= ...
2323
# 9| 0: [LocalVariableAccess] access to local variable d

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ csharp6.cs:
7979
# 30| 0: [Parameter] c
8080
# 30| 4: [EQExpr] ... == ...
8181
# 30| 0: [CastExpr] (...) ...
82-
# 30| 0: [ParameterAccess] access to parameter c
82+
# 30| 1: [ParameterAccess] access to parameter c
8383
# 30| 1: [CastExpr] (...) ...
84-
# 30| 0: [CharLiteral] T
84+
# 30| 1: [CharLiteral] T
8585
# 32| 5: [LocalVariableDeclStmt] ... ...;
8686
# 32| 0: [LocalVariableDeclAndInitExpr] Nullable<Char> testElementBinding = ...
8787
# 32| -1: [TypeMention] Nullable<Char>

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ csharp71.cs:
88
# 7| -1: [TypeMention] Int32
99
# 7| 0: [LocalVariableAccess] access to local variable x
1010
# 7| 1: [CastExpr] (...) ...
11-
# 7| 0: [DefaultValueExpr] default
11+
# 7| 1: [DefaultValueExpr] default
1212
# 7| 1: [LocalVariableDeclAndInitExpr] Int32 y = ...
1313
# 7| -1: [TypeMention] Int32
1414
# 7| 0: [LocalVariableAccess] access to local variable y
@@ -19,7 +19,7 @@ csharp71.cs:
1919
# 8| 0: [EQExpr] ... == ...
2020
# 8| 0: [LocalVariableAccess] access to local variable x
2121
# 8| 1: [CastExpr] (...) ...
22-
# 8| 0: [DefaultValueExpr] default
22+
# 8| 1: [DefaultValueExpr] default
2323
# 9| 1: [EmptyStmt] ;
2424
# 10| 2: [SwitchStmt] switch (...) {...}
2525
# 10| 0: [LocalVariableAccess] access to local variable x
@@ -30,25 +30,25 @@ csharp71.cs:
3030
# 14| 0: [AssignExpr] ... = ...
3131
# 14| 0: [LocalVariableAccess] access to local variable x
3232
# 14| 1: [CastExpr] (...) ...
33-
# 14| 0: [DefaultValueExpr] default
33+
# 14| 1: [DefaultValueExpr] default
3434
# 15| 4: [LocalVariableDeclStmt] ... ...;
3535
# 15| 0: [LocalVariableDeclAndInitExpr] String s = ...
3636
# 15| -1: [TypeMention] String
3737
# 15| 0: [LocalVariableAccess] access to local variable s
3838
# 15| 1: [CastExpr] (...) ...
39-
# 15| 0: [DefaultValueExpr] default
39+
# 15| 1: [DefaultValueExpr] default
4040
# 16| 5: [LocalVariableDeclStmt] ... ...;
4141
# 16| 0: [LocalVariableDeclAndInitExpr] Boolean b = ...
4242
# 16| -1: [TypeMention] Boolean
4343
# 16| 0: [LocalVariableAccess] access to local variable b
4444
# 16| 1: [CastExpr] (...) ...
45-
# 16| 0: [DefaultValueExpr] default
45+
# 16| 1: [DefaultValueExpr] default
4646
# 17| 6: [LocalVariableDeclStmt] ... ...;
4747
# 17| 0: [LocalVariableDeclAndInitExpr] Double d = ...
4848
# 17| -1: [TypeMention] Double
4949
# 17| 0: [LocalVariableAccess] access to local variable d
5050
# 17| 1: [CastExpr] (...) ...
51-
# 17| 0: [DefaultValueExpr] default
51+
# 17| 1: [DefaultValueExpr] default
5252
# 21| [Class] IsConstants
5353
# 23| 5: [Method] f
5454
# 23| -1: [TypeMention] Void

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ patterns.cs:
758758
# 7| -1: [TypeMention] Object
759759
# 7| 0: [LocalVariableAccess] access to local variable o
760760
# 7| 1: [CastExpr] (...) ...
761-
# 7| 0: [ObjectCreation] object creation of type MyStruct
761+
# 7| 1: [ObjectCreation] object creation of type MyStruct
762762
# 7| -2: [TypeMention] MyStruct
763763
# 7| -1: [ObjectInitializer] { ..., ... }
764764
# 7| 0: [MemberInitializer] ... = ...
@@ -970,7 +970,7 @@ patterns.cs:
970970
# 78| 1: [VariablePatternExpr] Single y
971971
# 78| 1: [LTExpr] ... < ...
972972
# 78| 0: [CastExpr] (...) ...
973-
# 78| 0: [LocalVariableAccess] access to local variable x
973+
# 78| 1: [LocalVariableAccess] access to local variable x
974974
# 78| 1: [LocalVariableAccess] access to local variable y
975975
# 79| 1: [BreakStmt] break;
976976
# 80| 2: [CaseStmt] case ...:

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,14 @@ definitions.cs:
8383
# 44| 0: [MethodCall] call to method ExtensionMethod
8484
# 44| -1: [ThisAccess] this access
8585
# 44| 0: [CastExpr] (...) ...
86-
# 44| 0: [IntLiteral] 1
86+
# 44| 1: [IntLiteral] 1
8787
# 45| 9: [ExprStmt] ...;
8888
# 45| 0: [MethodCall] call to method ExtensionMethod
8989
# 45| -1: [ThisAccess] this access
9090
# 45| 0: [CastExpr] (...) ...
91-
# 45| 0: [IntLiteral] 1
91+
# 45| 1: [IntLiteral] 1
9292
# 45| 1: [CastExpr] (...) ...
93-
# 45| 0: [IntLiteral] 2
93+
# 45| 1: [IntLiteral] 2
9494
# 47| 10: [ExprStmt] ...;
9595
# 47| 0: [MethodCall] call to method GenericFn
9696
# 47| 0: [IntLiteral] 1
@@ -385,9 +385,9 @@ definitions.cs:
385385
# 156| 4: [BlockStmt] {...}
386386
# 156| 0: [ReturnStmt] return ...;
387387
# 156| 0: [CastExpr] (...) ...
388-
# 156| 0: [ObjectCreation] object creation of type S1
388+
# 156| 0: [TypeAccess] access to type S1
389389
# 156| 0: [TypeMention] S1
390-
# 156| 1: [TypeAccess] access to type S1
390+
# 156| 1: [ObjectCreation] object creation of type S1
391391
# 156| 0: [TypeMention] S1
392392
# 158| 9: [Class] Nested<>
393393
#-----| 1: (Type parameters)

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ delegates.cs:
2626
# 14| 4: [BlockStmt] {...}
2727
# 14| 0: [ReturnStmt] return ...;
2828
# 14| 0: [CastExpr] (...) ...
29-
# 14| 0: [AddExpr] ... + ...
29+
# 14| 0: [TypeAccess] access to type Int32
30+
# 14| 0: [TypeMention] Int32
31+
# 14| 1: [AddExpr] ... + ...
3032
# 14| 0: [CastExpr] (...) ...
31-
# 14| 0: [ParameterAccess] access to parameter a
33+
# 14| 1: [ParameterAccess] access to parameter a
3234
# 14| 1: [ParameterAccess] access to parameter b
33-
# 14| 1: [TypeAccess] access to type Int32
34-
# 14| 0: [TypeMention] Int32
3535
# 18| 4: [Class] B
3636
# 21| 5: [DelegateType] D2
3737
#-----| 2: (Parameters)
@@ -51,9 +51,9 @@ delegates.cs:
5151
# 23| 0: [SubExpr] ... - ...
5252
# 23| 0: [ParameterAccess] access to parameter f
5353
# 23| 1: [CastExpr] (...) ...
54-
# 23| 0: [ParameterAccess] access to parameter g
55-
# 23| 1: [TypeAccess] access to type Int32
54+
# 23| 0: [TypeAccess] access to type Int32
5655
# 23| 0: [TypeMention] Int32
56+
# 23| 1: [ParameterAccess] access to parameter g
5757
# 25| 7: [Method] M2
5858
# 25| -1: [TypeMention] Void
5959
#-----| 2: (Parameters)

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ dynamic.cs:
4040
# 13| -1: [TypeMention] dynamic
4141
# 13| 0: [LocalVariableAccess] access to local variable d
4242
# 13| 1: [CastExpr] (...) ...
43-
# 13| 0: [IntLiteral] 0
43+
# 13| 1: [IntLiteral] 0
4444
# 16| 4: [ExprStmt] ...;
4545
# 16| 0: [DynamicObjectCreation] dynamic object creation of type DynamicTest
4646
# 16| -1: [TypeMention] DynamicTest
@@ -52,7 +52,7 @@ dynamic.cs:
5252
# 17| 0: [MemberInitializer] ... = ...
5353
# 17| 0: [FieldAccess] access to field Field
5454
# 17| 1: [CastExpr] (...) ...
55-
# 17| 0: [LocalVariableAccess] access to local variable d
55+
# 17| 1: [LocalVariableAccess] access to local variable d
5656
# 17| 0: [LocalVariableAccess] access to local variable d
5757
# 18| 6: [ExprStmt] ...;
5858
# 18| 0: [DynamicObjectCreation] dynamic object creation of type KeyValuePair<String,dynamic>
@@ -77,7 +77,7 @@ dynamic.cs:
7777
# 23| 0: [MemberInitializer] ... = ...
7878
# 23| 0: [FieldAccess] access to field Field
7979
# 23| 1: [CastExpr] (...) ...
80-
# 23| 0: [LocalVariableAccess] access to local variable d
80+
# 23| 1: [LocalVariableAccess] access to local variable d
8181
# 23| 0: [IntLiteral] 0
8282
# 26| 10: [ExprStmt] ...;
8383
# 26| 0: [DynamicMethodCall] dynamic call to method Bar
@@ -97,7 +97,7 @@ dynamic.cs:
9797
# 34| 0: [AssignExpr] ... = ...
9898
# 34| 0: [LocalVariableAccess] access to local variable d
9999
# 34| 1: [CastExpr] (...) ...
100-
# 34| 0: [IntLiteral] 0
100+
# 34| 1: [IntLiteral] 0
101101
# 35| 15: [ExprStmt] ...;
102102
# 35| 0: [AssignExpr] ... = ...
103103
# 35| 0: [LocalVariableAccess] access to local variable d
@@ -186,21 +186,21 @@ dynamic.cs:
186186
# 66| 0: [AssignExpr] ... = ...
187187
# 66| 0: [LocalVariableAccess] access to local variable d
188188
# 66| 1: [CastExpr] (...) ...
189-
# 66| 0: [IntLiteral] 0
189+
# 66| 1: [IntLiteral] 0
190190
# 67| 33: [ExprStmt] ...;
191191
# 67| 0: [AssignExpr] ... = ...
192192
# 67| 0: [IndexerCall] access to indexer
193193
# 67| -1: [LocalVariableAccess] access to local variable dt
194194
# 67| 0: [IntLiteral] 0
195195
# 67| 1: [CastExpr] (...) ...
196-
# 67| 0: [IndexerCall] access to indexer
196+
# 67| 1: [IndexerCall] access to indexer
197197
# 67| -1: [LocalVariableAccess] access to local variable dt
198198
# 67| 0: [LocalVariableAccess] access to local variable d
199199
# 68| 34: [ExprStmt] ...;
200200
# 68| 0: [AssignExpr] ... = ...
201201
# 68| 0: [LocalVariableAccess] access to local variable d
202202
# 68| 1: [CastExpr] (...) ...
203-
# 68| 0: [IndexerCall] access to indexer
203+
# 68| 1: [IndexerCall] access to indexer
204204
# 68| -1: [LocalVariableAccess] access to local variable dt
205205
# 68| 0: [IntLiteral] 0
206206
# 69| 35: [ExprStmt] ...;
@@ -211,12 +211,12 @@ dynamic.cs:
211211
# 69| 1: [ArrayAccess] access to array element
212212
# 69| -1: [LocalVariableAccess] access to local variable array
213213
# 69| 0: [CastExpr] (...) ...
214-
# 69| 0: [LocalVariableAccess] access to local variable d
214+
# 69| 1: [LocalVariableAccess] access to local variable d
215215
# 70| 36: [ExprStmt] ...;
216216
# 70| 0: [AssignExpr] ... = ...
217217
# 70| 0: [LocalVariableAccess] access to local variable d
218218
# 70| 1: [CastExpr] (...) ...
219-
# 70| 0: [ArrayAccess] access to array element
219+
# 70| 1: [ArrayAccess] access to array element
220220
# 70| -1: [LocalVariableAccess] access to local variable array
221221
# 70| 0: [IntLiteral] 0
222222
# 73| 37: [ExprStmt] ...;

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ enums.cs:
1414
# 28| 1: [AssignExpr] ... = ...
1515
# 28| 0: [MemberConstantAccess] access to constant OneRed
1616
# 28| 1: [CastExpr] (...) ...
17-
# 28| 0: [IntLiteral] 1
17+
# 28| 1: [IntLiteral] 1
1818
# 29| 6: [Field] TwoGreen
1919
# 29| 1: [AssignExpr] ... = ...
2020
# 29| 0: [MemberConstantAccess] access to constant TwoGreen
2121
# 29| 1: [CastExpr] (...) ...
22-
# 29| 0: [IntLiteral] 2
22+
# 29| 1: [IntLiteral] 2
2323
# 30| 7: [Field] FourBlue
2424
# 30| 1: [AssignExpr] ... = ...
2525
# 30| 0: [MemberConstantAccess] access to constant FourBlue
2626
# 30| 1: [CastExpr] (...) ...
27-
# 30| 0: [IntLiteral] 4
27+
# 30| 1: [IntLiteral] 4
2828
# 34| 5: [Enum] SparseColor
2929
# 37| 5: [Field] Red
3030
# 38| 6: [Field] Green
@@ -37,9 +37,9 @@ enums.cs:
3737
# 40| 0: [MemberConstantAccess] access to constant AnotherBlue
3838
# 40| 1: [AddExpr] ... + ...
3939
# 40| 0: [CastExpr] (...) ...
40-
# 40| 0: [MemberConstantAccess] access to constant Blue
40+
# 40| 1: [MemberConstantAccess] access to constant Blue
4141
# 40| 1: [CastExpr] (...) ...
42-
# 40| 0: [MemberConstantAccess] access to constant Red
42+
# 40| 1: [MemberConstantAccess] access to constant Red
4343
# 44| 6: [Class] Test
4444
# 47| 5: [Method] Main
4545
# 47| -1: [TypeMention] Void
@@ -86,10 +86,10 @@ enums.cs:
8686
# 58| 0: [TypeMention] String
8787
# 58| 0: [StringLiteral] "Red = {0}"
8888
# 58| 1: [CastExpr] (...) ...
89-
# 58| 0: [CastExpr] (...) ...
90-
# 58| 0: [ParameterAccess] access to parameter c
91-
# 58| 1: [TypeAccess] access to type Int32
89+
# 58| 1: [CastExpr] (...) ...
90+
# 58| 0: [TypeAccess] access to type Int32
9291
# 58| 0: [TypeMention] Int32
92+
# 58| 1: [ParameterAccess] access to parameter c
9393
# 59| 2: [ConstCase] case ...:
9494
# 59| 0: [ConstantPatternExpr,MemberConstantAccess] access to constant Green
9595
# 59| -1: [TypeAccess] access to type SparseColor
@@ -100,10 +100,10 @@ enums.cs:
100100
# 59| 0: [TypeMention] String
101101
# 59| 0: [StringLiteral] "Green = {0}"
102102
# 59| 1: [CastExpr] (...) ...
103-
# 59| 0: [CastExpr] (...) ...
104-
# 59| 0: [ParameterAccess] access to parameter c
105-
# 59| 1: [TypeAccess] access to type Int32
103+
# 59| 1: [CastExpr] (...) ...
104+
# 59| 0: [TypeAccess] access to type Int32
106105
# 59| 0: [TypeMention] Int32
106+
# 59| 1: [ParameterAccess] access to parameter c
107107
# 60| 4: [ConstCase] case ...:
108108
# 60| 0: [ConstantPatternExpr,MemberConstantAccess] access to constant Blue
109109
# 60| -1: [TypeAccess] access to type SparseColor
@@ -114,10 +114,10 @@ enums.cs:
114114
# 60| 0: [TypeMention] String
115115
# 60| 0: [StringLiteral] "Blue = {0}"
116116
# 60| 1: [CastExpr] (...) ...
117-
# 60| 0: [CastExpr] (...) ...
118-
# 60| 0: [ParameterAccess] access to parameter c
119-
# 60| 1: [TypeAccess] access to type Int32
117+
# 60| 1: [CastExpr] (...) ...
118+
# 60| 0: [TypeAccess] access to type Int32
120119
# 60| 0: [TypeMention] Int32
120+
# 60| 1: [ParameterAccess] access to parameter c
121121
# 61| 6: [DefaultCase] default:
122122
# 61| 7: [ReturnStmt] return ...;
123123
# 61| 0: [StringLiteral] "Invalid color"

0 commit comments

Comments
 (0)