Skip to content

Commit 59ddda8

Browse files
authored
Merge pull request #104 from dave-bartolomeo/dave/ASTWobble
C++: Fix wobble in PrintAST test
2 parents 7cae9be + 65ed9af commit 59ddda8

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed

cpp/ql/src/semmle/code/cpp/PrintAST.qll

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,26 @@ private Location getRepresentativeLocation(Locatable ast) {
3939
result = rank[1](Location loc | loc = ast.getLocation() | loc order by loc.toString())
4040
}
4141

42+
/**
43+
* Computes the sort keys to sort the given AST node by location. An AST without
44+
* a location gets an empty file name and a zero line and column number.
45+
*/
46+
private predicate locationSortKeys(Locatable ast, string file, int line,
47+
int column) {
48+
if exists(getRepresentativeLocation(ast)) then (
49+
exists(Location loc |
50+
loc = getRepresentativeLocation(ast) and
51+
file = loc.getFile().toString() and
52+
line = loc.getStartLine() and
53+
column = loc.getStartColumn()
54+
)
55+
) else (
56+
file = "" and
57+
line = 0 and
58+
column = 0
59+
)
60+
}
61+
4262
/**
4363
* Most nodes are just a wrapper around `Locatable`, but we do synthesize new
4464
* nodes for things like parameter lists and constructor init lists.
@@ -482,12 +502,14 @@ class FunctionNode extends ASTNode {
482502
}
483503

484504
private int getOrder() {
485-
this = rank[result](FunctionNode node, Function function, Location loc |
486-
node.getAST() = function and loc = getRepresentativeLocation(function) |
505+
this = rank[result](FunctionNode node, Function function, string file,
506+
int line, int column |
507+
node.getAST() = function and
508+
locationSortKeys(function, file, line, column) |
487509
node order by
488-
loc.getFile().toString(),
489-
loc.getStartLine(),
490-
loc.getStartColumn(),
510+
file,
511+
line,
512+
column,
491513
function.getFullSignature()
492514
)
493515
}

cpp/ql/test/examples/expressions/PrintAST.expected

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
#-----| params:
33
#-----| __va_list_tag::operator=() -> __va_list_tag &
44
#-----| params:
5-
#-----| operator new(unsigned long) -> void *
6-
#-----| params:
7-
#-----| 0: p#0
8-
#-----| Type = unsigned long
95
#-----| operator delete(void *) -> void
106
#-----| params:
117
#-----| 0: p#0
128
#-----| Type = void *
9+
#-----| operator new(unsigned long) -> void *
10+
#-----| params:
11+
#-----| 0: p#0
12+
#-----| Type = unsigned long
1313
AddressOf.c:
1414
# 1| AddressOf(int) -> void
1515
# 1| params:

cpp/ql/test/library-tests/ir/ir/PrintAST.expected

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@
22
#-----| params:
33
#-----| __va_list_tag::operator=() -> __va_list_tag &
44
#-----| params:
5-
#-----| operator new(unsigned long) -> void *
5+
#-----| operator delete(void *, unsigned long) -> void
66
#-----| params:
77
#-----| 0: p#0
8+
#-----| Type = void *
9+
#-----| 1: p#1
810
#-----| Type = unsigned long
9-
#-----| operator delete(void *, unsigned long) -> void
11+
#-----| operator delete[](void *, unsigned long) -> void
1012
#-----| params:
1113
#-----| 0: p#0
1214
#-----| Type = void *
1315
#-----| 1: p#1
1416
#-----| Type = unsigned long
17+
#-----| operator new(unsigned long) -> void *
18+
#-----| params:
19+
#-----| 0: p#0
20+
#-----| Type = unsigned long
1521
#-----| operator new(unsigned long, align_val_t) -> void *
1622
#-----| params:
1723
#-----| 0: p#0
@@ -22,12 +28,6 @@
2228
#-----| params:
2329
#-----| 0: p#0
2430
#-----| Type = unsigned long
25-
#-----| operator delete[](void *, unsigned long) -> void
26-
#-----| params:
27-
#-----| 0: p#0
28-
#-----| Type = void *
29-
#-----| 1: p#1
30-
#-----| Type = unsigned long
3131
#-----| operator new[](unsigned long, align_val_t) -> void *
3232
#-----| params:
3333
#-----| 0: p#0

0 commit comments

Comments
 (0)