Skip to content

Commit ea77828

Browse files
authored
Merge pull request #4116 from hvitved/csharp/print-ast-order-top-level
C#: Order top-level elements by location in `PrintAst.qll`
2 parents 29183fa + b8cde18 commit ea77828

File tree

5 files changed

+112
-88
lines changed

5 files changed

+112
-88
lines changed

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,13 @@ class PrintAstNode extends TPrintAstNode {
151151
/**
152152
* Gets a textual representation of this node in the PrintAst output tree.
153153
*/
154-
abstract string toString();
154+
string toString() { none() }
155155

156156
/**
157157
* Gets the child node at index `childIndex`. Child indices must be unique,
158158
* but need not be contiguous.
159159
*/
160-
abstract PrintAstNode getChild(int childIndex);
160+
PrintAstNode getChild(int childIndex) { none() }
161161

162162
/**
163163
* Gets a child of this node.
@@ -172,7 +172,7 @@ class PrintAstNode extends TPrintAstNode {
172172
/**
173173
* Gets the location of this node in the source code.
174174
*/
175-
abstract Location getLocation();
175+
Location getLocation() { none() }
176176

177177
/**
178178
* Gets the value of the property of this node, where the name of the property
@@ -194,6 +194,30 @@ class PrintAstNode extends TPrintAstNode {
194194
}
195195
}
196196

197+
/** A top-level AST node. */
198+
class TopLevelPrintAstNode extends PrintAstNode {
199+
TopLevelPrintAstNode() { not exists(this.getParent()) }
200+
201+
private int getOrder() {
202+
this =
203+
rank[result](TopLevelPrintAstNode n, Location l |
204+
l = n.getLocation()
205+
|
206+
n
207+
order by
208+
l.getFile().getRelativePath(), l.getStartLine(), l.getStartColumn(), l.getEndLine(),
209+
l.getEndColumn()
210+
)
211+
}
212+
213+
override string getProperty(string key) {
214+
result = super.getProperty(key)
215+
or
216+
key = "semmle.order" and
217+
result = this.getOrder().toString()
218+
}
219+
}
220+
197221
/**
198222
* A node representing an AST node with an underlying `Element`.
199223
*/

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ assignop.cs:
148148
# 17| 0: [IntLiteral] 2
149149
# 17| 1: [LocalVariableAccess] access to local variable c
150150
casts.cs:
151+
# 1| [Class] Casts_A
152+
# 5| [Class] Casts_B
153+
#-----| 3: (Base types)
154+
# 5| 0: [Class] Casts_A
151155
# 9| [Class] Casts
152156
# 11| 5: [Method] Main
153157
# 12| 4: [BlockStmt] {...}
@@ -167,10 +171,6 @@ casts.cs:
167171
# 15| 0: [LocalVariableAccess] access to local variable Aobj
168172
# 15| 1: [TypeAccess] access to type Casts_B
169173
# 15| 1: [LocalVariableAccess] access to local variable bobjAS
170-
# 1| [Class] Casts_A
171-
# 5| [Class] Casts_B
172-
#-----| 3: (Base types)
173-
# 5| 0: [Class] Casts_A
174174
collections.cs:
175175
# 3| [Class] Collections
176176
# 5| 5: [Class] MyClass
@@ -469,6 +469,9 @@ inheritance_polymorphism.cs:
469469
# 4| 4: [BlockStmt] {...}
470470
# 5| 0: [ReturnStmt] return ...;
471471
# 5| 0: [IntLiteral] 0
472+
# 9| [Class] B
473+
#-----| 3: (Base types)
474+
# 9| 0: [Class] A
472475
# 13| [Class] C
473476
#-----| 3: (Base types)
474477
# 13| 0: [Class] B
@@ -502,9 +505,6 @@ inheritance_polymorphism.cs:
502505
# 34| 6: [ExprStmt] ...;
503506
# 34| 0: [MethodCall] call to method function
504507
# 34| -1: [LocalVariableAccess] access to local variable objC
505-
# 9| [Class] B
506-
#-----| 3: (Base types)
507-
# 9| 0: [Class] A
508508
inoutref.cs:
509509
# 1| [Class] MyClass
510510
# 2| 5: [Field] fld

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ csharp72.cs:
2222
# 28| 0: [RefExpr] ref ...
2323
# 28| 0: [FieldAccess] access to field s
2424
# 31| 7: [DelegateType] Del
25+
# 34| [Struct] ReadonlyStruct
26+
# 38| [Struct] RefStruct
27+
# 42| [Struct] ReadonlyRefStruct
2528
# 46| [Class] NumericLiterals
2629
# 48| 5: [Field] binaryValue
2730
# 48| 1: [AssignExpr] ... = ...
@@ -34,6 +37,3 @@ csharp72.cs:
3437
# 53| 1: [FieldAccess] access to field X
3538
# 55| 6: [Method] F
3639
# 55| 4: [BlockStmt] {...}
37-
# 34| [Struct] ReadonlyStruct
38-
# 38| [Struct] RefStruct
39-
# 42| [Struct] ReadonlyRefStruct

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ csharp73.cs:
5050
# 22| 0: [IntLiteral] 10
5151
# 22| 1: [LocalVariableAccess] access to local variable t
5252
# 25| 1: [BlockStmt] {...}
53+
# 30| [Class] UnmanagedConstraint<>
54+
#-----| 1: (Type parameters)
55+
# 30| 0: [TypeParameter] T
56+
# 34| [Class] EnumConstraint<>
57+
#-----| 1: (Type parameters)
58+
# 34| 0: [TypeParameter] T
59+
# 38| [Class] DelegateConstraint<>
60+
#-----| 1: (Type parameters)
61+
# 38| 0: [TypeParameter] T
5362
# 42| [Class] ExpressionVariables
5463
# 44| 4: [InstanceConstructor] ExpressionVariables
5564
#-----| 2: (Parameters)
@@ -69,12 +78,3 @@ csharp73.cs:
6978
# 51| 0: [InterpolatedStringExpr] $"..."
7079
# 51| 0: [StringLiteral] "x is "
7180
# 51| 1: [LocalVariableAccess] access to local variable x
72-
# 30| [Class] UnmanagedConstraint<>
73-
#-----| 1: (Type parameters)
74-
# 30| 0: [TypeParameter] T
75-
# 34| [Class] EnumConstraint<>
76-
#-----| 1: (Type parameters)
77-
# 34| 0: [TypeParameter] T
78-
# 38| [Class] DelegateConstraint<>
79-
#-----| 1: (Type parameters)
80-
# 38| 0: [TypeParameter] T

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

Lines changed: 66 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,6 @@ AlternateInterpolatedStrings.cs:
1313
# 6| 1: [IntLiteral] 12
1414
# 6| 1: [FieldAccess] access to field s2
1515
AsyncStreams.cs:
16-
# 24| [NamespaceDeclaration] namespace ... { ... }
17-
# 26| 1: [Interface] IAsyncDisposable
18-
# 28| 4: [Method] DisposeAsync
19-
# 32| [NamespaceDeclaration] namespace ... { ... }
20-
# 34| 1: [Interface] IAsyncEnumerable<>
21-
#-----| 1: (Type parameters)
22-
# 34| 0: [TypeParameter] T
23-
# 36| 4: [Method] GetAsyncEnumerator
24-
#-----| 2: (Parameters)
25-
# 36| 0: [Parameter] cancellationToken
26-
# 36| 1: [DefaultValueExpr] default(...)
27-
# 36| 0: [TypeAccess] access to type CancellationToken
28-
# 39| 2: [Interface] IAsyncEnumerator<>
29-
#-----| 1: (Type parameters)
30-
# 39| 0: [TypeParameter] T
31-
#-----| 3: (Base types)
32-
# 39| 1: [Interface] IAsyncDisposable
33-
# 41| 4: [Property] Current
34-
# 41| 3: [Getter] get_Current
35-
# 42| 5: [Method] MoveNextAsync
3616
# 8| [Class] AsyncStreams
3717
# 10| 5: [Method] Items
3818
# 10| 4: [BlockStmt] {...}
@@ -56,6 +36,26 @@ AsyncStreams.cs:
5636
# 20| 0: [MethodCall] call to method WriteLine
5737
# 20| -1: [TypeAccess] access to type Console
5838
# 20| 0: [LocalVariableAccess] access to local variable item
39+
# 24| [NamespaceDeclaration] namespace ... { ... }
40+
# 26| 1: [Interface] IAsyncDisposable
41+
# 28| 4: [Method] DisposeAsync
42+
# 32| [NamespaceDeclaration] namespace ... { ... }
43+
# 34| 1: [Interface] IAsyncEnumerable<>
44+
#-----| 1: (Type parameters)
45+
# 34| 0: [TypeParameter] T
46+
# 36| 4: [Method] GetAsyncEnumerator
47+
#-----| 2: (Parameters)
48+
# 36| 0: [Parameter] cancellationToken
49+
# 36| 1: [DefaultValueExpr] default(...)
50+
# 36| 0: [TypeAccess] access to type CancellationToken
51+
# 39| 2: [Interface] IAsyncEnumerator<>
52+
#-----| 1: (Type parameters)
53+
# 39| 0: [TypeParameter] T
54+
#-----| 3: (Base types)
55+
# 39| 1: [Interface] IAsyncDisposable
56+
# 41| 4: [Property] Current
57+
# 41| 3: [Getter] get_Current
58+
# 42| 5: [Method] MoveNextAsync
5959
DefaultInterfaceMethods.cs:
6060
# 3| [Interface] IPerson
6161
# 5| 4: [IndexerProperty] Name
@@ -332,7 +332,6 @@ NullableRefTypes.cs:
332332
# 129| 14: [Field] j
333333
# 130| 15: [Field] k
334334
# 131| 16: [Field] l
335-
# 165| [Struct] MyStruct
336335
# 136| [Class] ToStringWithTypes2
337336
# 138| 5: [Field] a
338337
# 139| 6: [Field] b
@@ -361,6 +360,7 @@ NullableRefTypes.cs:
361360
# 160| 1: [LocalVariableAccess] access to local variable a
362361
# 161| 1: [ReturnStmt] return ...;
363362
# 161| 0: [LocalVariableAccess] access to local variable a
363+
# 165| [Struct] MyStruct
364364
# 171| [Class] TestNullableFlowStates
365365
# 173| 5: [Method] MaybeNull
366366
# 175| 6: [Method] Check
@@ -938,51 +938,6 @@ patterns.cs:
938938
# 164| 9: [Method] Deconstruct
939939
# 165| 4: [BlockStmt] {...}
940940
ranges.cs:
941-
# 25| [NamespaceDeclaration] namespace ... { ... }
942-
# 27| 1: [Struct] Index
943-
# 29| 5: [InstanceConstructor] Index
944-
#-----| 2: (Parameters)
945-
# 29| 0: [Parameter] value
946-
# 29| 1: [Parameter] fromEnd
947-
# 29| 1: [BoolLiteral] false
948-
# 29| 4: [BlockStmt] {...}
949-
# 30| 6: [ImplicitConversionOperator] implicit conversion
950-
#-----| 2: (Parameters)
951-
# 30| 0: [Parameter] value
952-
# 30| 4: [DefaultValueExpr] default(...)
953-
# 30| 0: [TypeAccess] access to type Index
954-
# 33| 2: [Struct] Range
955-
# 35| 5: [InstanceConstructor] Range
956-
#-----| 2: (Parameters)
957-
# 35| 0: [Parameter] start
958-
# 35| 1: [Parameter] end
959-
# 35| 4: [ThrowExpr] throw ...
960-
# 35| 0: [NullLiteral] null
961-
# 36| 6: [Method] StartAt
962-
#-----| 2: (Parameters)
963-
# 36| 0: [Parameter] start
964-
# 36| 4: [ThrowExpr] throw ...
965-
# 36| 0: [NullLiteral] null
966-
# 37| 7: [Method] EndAt
967-
#-----| 2: (Parameters)
968-
# 37| 0: [Parameter] end
969-
# 37| 4: [ThrowExpr] throw ...
970-
# 37| 0: [NullLiteral] null
971-
# 38| 8: [Property] All
972-
# 38| 3: [Getter] get_All
973-
# 38| 4: [ThrowExpr] throw ...
974-
# 38| 0: [NullLiteral] null
975-
# 39| 9: [Method] Create
976-
#-----| 2: (Parameters)
977-
# 39| 0: [Parameter] start
978-
# 39| 1: [Parameter] end
979-
# 39| 4: [ThrowExpr] throw ...
980-
# 39| 0: [NullLiteral] null
981-
# 40| 10: [ImplicitConversionOperator] implicit conversion
982-
#-----| 2: (Parameters)
983-
# 40| 0: [Parameter] r
984-
# 40| 4: [ThrowExpr] throw ...
985-
# 40| 0: [NullLiteral] null
986941
# 5| [Class] Ranges
987942
# 7| 5: [Method] F
988943
# 8| 4: [BlockStmt] {...}
@@ -1090,3 +1045,48 @@ ranges.cs:
10901045
# 20| 1: [OperatorCall] call to operator implicit conversion
10911046
# 20| 0: [RangeExpr] ... .. ...
10921047
# 20| 1: [LocalVariableAccess] access to local variable slice8
1048+
# 25| [NamespaceDeclaration] namespace ... { ... }
1049+
# 27| 1: [Struct] Index
1050+
# 29| 5: [InstanceConstructor] Index
1051+
#-----| 2: (Parameters)
1052+
# 29| 0: [Parameter] value
1053+
# 29| 1: [Parameter] fromEnd
1054+
# 29| 1: [BoolLiteral] false
1055+
# 29| 4: [BlockStmt] {...}
1056+
# 30| 6: [ImplicitConversionOperator] implicit conversion
1057+
#-----| 2: (Parameters)
1058+
# 30| 0: [Parameter] value
1059+
# 30| 4: [DefaultValueExpr] default(...)
1060+
# 30| 0: [TypeAccess] access to type Index
1061+
# 33| 2: [Struct] Range
1062+
# 35| 5: [InstanceConstructor] Range
1063+
#-----| 2: (Parameters)
1064+
# 35| 0: [Parameter] start
1065+
# 35| 1: [Parameter] end
1066+
# 35| 4: [ThrowExpr] throw ...
1067+
# 35| 0: [NullLiteral] null
1068+
# 36| 6: [Method] StartAt
1069+
#-----| 2: (Parameters)
1070+
# 36| 0: [Parameter] start
1071+
# 36| 4: [ThrowExpr] throw ...
1072+
# 36| 0: [NullLiteral] null
1073+
# 37| 7: [Method] EndAt
1074+
#-----| 2: (Parameters)
1075+
# 37| 0: [Parameter] end
1076+
# 37| 4: [ThrowExpr] throw ...
1077+
# 37| 0: [NullLiteral] null
1078+
# 38| 8: [Property] All
1079+
# 38| 3: [Getter] get_All
1080+
# 38| 4: [ThrowExpr] throw ...
1081+
# 38| 0: [NullLiteral] null
1082+
# 39| 9: [Method] Create
1083+
#-----| 2: (Parameters)
1084+
# 39| 0: [Parameter] start
1085+
# 39| 1: [Parameter] end
1086+
# 39| 4: [ThrowExpr] throw ...
1087+
# 39| 0: [NullLiteral] null
1088+
# 40| 10: [ImplicitConversionOperator] implicit conversion
1089+
#-----| 2: (Parameters)
1090+
# 40| 0: [Parameter] r
1091+
# 40| 4: [ThrowExpr] throw ...
1092+
# 40| 0: [NullLiteral] null

0 commit comments

Comments
 (0)