Skip to content

Commit 49ebb31

Browse files
authored
Merge pull request github#3472 from geoffw0/paramstring
C++: Improve getParameterString().
2 parents 23532ae + 6583012 commit 49ebb31

File tree

4 files changed

+53
-74
lines changed

4 files changed

+53
-74
lines changed

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

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -184,17 +184,8 @@ class Function extends Declaration, ControlFlowNode, AccessHolder, @function {
184184
* For example: for a function `int Foo(int p1, int p2)` this would
185185
* return `int p1, int p2`.
186186
*/
187-
string getParameterString() { result = getParameterStringFrom(0) }
188-
189-
private string getParameterStringFrom(int index) {
190-
index = getNumberOfParameters() and
191-
result = ""
192-
or
193-
index = getNumberOfParameters() - 1 and
194-
result = getParameter(index).getTypedName()
195-
or
196-
index < getNumberOfParameters() - 1 and
197-
result = getParameter(index).getTypedName() + ", " + getParameterStringFrom(index + 1)
187+
string getParameterString() {
188+
result = concat(int i | | min(getParameter(i).getTypedName()), ", " order by i)
198189
}
199190

200191
/** Gets a call to this function. */
@@ -616,18 +607,8 @@ class FunctionDeclarationEntry extends DeclarationEntry, @fun_decl {
616607
* For example: for a function 'int Foo(int p1, int p2)' this would
617608
* return 'int p1, int p2'.
618609
*/
619-
string getParameterString() { result = getParameterStringFrom(0) }
620-
621-
private string getParameterStringFrom(int index) {
622-
index = getNumberOfParameters() and
623-
result = ""
624-
or
625-
index = getNumberOfParameters() - 1 and
626-
result = getParameterDeclarationEntry(index).getTypedName()
627-
or
628-
index < getNumberOfParameters() - 1 and
629-
result =
630-
getParameterDeclarationEntry(index).getTypedName() + ", " + getParameterStringFrom(index + 1)
610+
string getParameterString() {
611+
result = concat(int i | | min(getParameterDeclarationEntry(i).getTypedName()), ", " order by i)
631612
}
632613

633614
/**
Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,31 @@
1-
| ODASA-5186.cpp:4:8:4:8 | operator= | operator= | | | ODASA-5186.cpp:4:8:4:8 | declaration |
2-
| ODASA-5186.cpp:4:8:4:8 | operator= | operator= | | | ODASA-5186.cpp:4:8:4:8 | declaration |
3-
| ODASA-5186.cpp:5:8:5:8 | operator== | operator== | | | ODASA-5186.cpp:5:8:5:8 | declaration |
4-
| ODASA-5186.cpp:5:8:5:8 | operator== | operator== | | | ODASA-5186.cpp:5:8:5:8 | definition |
5-
| ODASA-5186.cpp:5:8:5:17 | operator== | operator== | | | ODASA-5186.cpp:5:8:5:17 | declaration |
6-
| ODASA-5186.cpp:5:8:5:17 | operator== | operator== | | | ODASA-5186.cpp:5:8:5:17 | definition |
7-
| ODASA-5186.cpp:8:6:8:9 | test | test | isTopLevel | TopLevelFunction | ODASA-5186.cpp:8:6:8:9 | declaration |
8-
| ODASA-5186.cpp:8:6:8:9 | test | test | isTopLevel | TopLevelFunction | ODASA-5186.cpp:8:6:8:9 | definition |
9-
| ODASA-5186.hpp:2:8:2:8 | operator= | operator= | | | ODASA-5186.hpp:2:8:2:8 | declaration |
10-
| ODASA-5186.hpp:2:8:2:8 | operator= | operator= | | | ODASA-5186.hpp:2:8:2:8 | declaration |
11-
| ODASA-5186.hpp:4:18:4:27 | operator!= | operator!= | isTopLevel | TopLevelFunction | ODASA-5186.hpp:4:18:4:27 | declaration |
12-
| ODASA-5186.hpp:4:18:4:27 | operator!= | operator!= | isTopLevel | TopLevelFunction | ODASA-5186.hpp:4:18:4:27 | declaration |
13-
| ODASA-5186.hpp:4:18:4:27 | operator!= | operator!= | isTopLevel | TopLevelFunction | ODASA-5186.hpp:4:18:4:27 | definition |
14-
| ODASA-5186.hpp:4:18:4:27 | operator!= | operator!= | isTopLevel | TopLevelFunction | ODASA-5186.hpp:4:18:4:27 | definition |
15-
| functions.cpp:1:6:1:6 | f | f | isTopLevel | TopLevelFunction | functions.cpp:1:6:1:6 | declaration |
16-
| functions.cpp:1:6:1:6 | f | f | isTopLevel | TopLevelFunction | functions.cpp:1:6:1:6 | definition |
17-
| functions.cpp:7:8:7:8 | operator= | operator= | | | functions.cpp:7:8:7:8 | declaration |
18-
| functions.cpp:7:8:7:8 | operator= | operator= | | | functions.cpp:7:8:7:8 | declaration |
19-
| functions.cpp:8:7:8:8 | af | af | | | functions.cpp:8:7:8:8 | declaration |
20-
| functions.cpp:8:7:8:8 | af | af | | | functions.cpp:8:7:8:8 | definition |
21-
| functions.cpp:11:7:11:8 | ag | ag | | | functions.cpp:11:7:11:8 | declaration |
22-
| functions.cpp:14:6:14:6 | g | g | isTopLevel | TopLevelFunction | functions.cpp:5:6:5:6 | declaration |
23-
| functions.cpp:14:6:14:6 | g | g | isTopLevel | TopLevelFunction | functions.cpp:14:6:14:6 | declaration |
24-
| functions.cpp:14:6:14:6 | g | g | isTopLevel | TopLevelFunction | functions.cpp:14:6:14:6 | definition |
25-
| functions.cpp:19:7:19:7 | operator= | operator= | | | functions.cpp:19:7:19:7 | declaration |
26-
| functions.cpp:19:7:19:7 | operator= | operator= | | | functions.cpp:19:7:19:7 | declaration |
27-
| functions.cpp:23:7:23:7 | Table | Table | | | functions.cpp:23:7:23:7 | declaration |
28-
| functions.cpp:23:7:23:7 | operator= | operator= | | | functions.cpp:23:7:23:7 | declaration |
29-
| functions.cpp:27:3:27:7 | Table | Table | | | functions.cpp:27:3:27:7 | declaration |
30-
| functions.cpp:27:3:27:7 | Table | Table | | | functions.cpp:27:3:27:7 | definition |
31-
| functions.cpp:28:3:28:8 | ~Table | ~Table | | | functions.cpp:28:3:28:8 | declaration |
32-
| functions.cpp:28:3:28:8 | ~Table | ~Table | | | functions.cpp:28:3:28:8 | definition |
33-
| functions.cpp:29:9:29:14 | lookup | lookup | | | functions.cpp:29:9:29:14 | declaration |
34-
| functions.cpp:30:8:30:13 | insert | insert | | | functions.cpp:30:8:30:13 | declaration |
35-
| functions.cpp:33:7:33:7 | operator= | operator= | | | functions.cpp:33:7:33:7 | declaration |
36-
| functions.cpp:33:7:33:7 | operator= | operator= | | | functions.cpp:33:7:33:7 | definition |
37-
| functions.cpp:36:2:36:8 | MyClass | MyClass | | | functions.cpp:36:2:36:8 | declaration |
38-
| functions.cpp:37:2:37:8 | MyClass | MyClass | | | functions.cpp:37:2:37:8 | declaration |
39-
| functions.cpp:38:2:38:8 | MyClass | MyClass | | | functions.cpp:38:2:38:8 | declaration |
40-
| functions.cpp:39:2:39:8 | MyClass | MyClass | | | functions.cpp:39:2:39:8 | declaration |
41-
| functions.cpp:40:2:40:13 | operator int | operator int | | | functions.cpp:40:2:40:13 | declaration |
1+
| ODASA-5186.cpp:4:8:4:8 | operator= | operator= | MyClass<int> && p#0 | declaration:ODASA-5186.cpp:4:8:4:8 |
2+
| ODASA-5186.cpp:4:8:4:8 | operator= | operator= | const MyClass<int> & p#0 | declaration:ODASA-5186.cpp:4:8:4:8 |
3+
| ODASA-5186.cpp:5:8:5:8 | operator== | operator== | const MyClass<int> & other | declaration:ODASA-5186.cpp:5:8:5:8, definition:ODASA-5186.cpp:5:8:5:8 |
4+
| ODASA-5186.cpp:5:8:5:17 | operator== | operator== | const MyClass<T> & other | declaration:ODASA-5186.cpp:5:8:5:17, definition:ODASA-5186.cpp:5:8:5:17 |
5+
| ODASA-5186.cpp:8:6:8:9 | test | test | | TopLevelFunction, declaration:ODASA-5186.cpp:8:6:8:9, definition:ODASA-5186.cpp:8:6:8:9, isTopLevel |
6+
| ODASA-5186.hpp:2:8:2:8 | operator= | operator= | NEQ_helper<MyClass<int>> && p#0 | declaration:ODASA-5186.hpp:2:8:2:8 |
7+
| ODASA-5186.hpp:2:8:2:8 | operator= | operator= | const NEQ_helper<MyClass<int>> & p#0 | declaration:ODASA-5186.hpp:2:8:2:8 |
8+
| ODASA-5186.hpp:4:18:4:27 | operator!= | operator!= | const MyClass<int> & x, const MyClass<int> & y | TopLevelFunction, declaration:ODASA-5186.hpp:4:18:4:27, definition:ODASA-5186.hpp:4:18:4:27, isTopLevel |
9+
| ODASA-5186.hpp:4:18:4:27 | operator!= | operator!= | const T & x, const T & y | TopLevelFunction, declaration:ODASA-5186.hpp:4:18:4:27, definition:ODASA-5186.hpp:4:18:4:27, isTopLevel |
10+
| functions.cpp:1:6:1:6 | f | f | int a, int b | TopLevelFunction, declaration:functions.cpp:1:6:1:6, definition:functions.cpp:1:6:1:6, isTopLevel |
11+
| functions.cpp:7:8:7:8 | operator= | operator= | A && p#0 | declaration:functions.cpp:7:8:7:8 |
12+
| functions.cpp:7:8:7:8 | operator= | operator= | const A & p#0 | declaration:functions.cpp:7:8:7:8 |
13+
| functions.cpp:8:7:8:8 | af | af | | declaration:functions.cpp:8:7:8:8, definition:functions.cpp:8:7:8:8 |
14+
| functions.cpp:11:7:11:8 | ag | ag | | declaration:functions.cpp:11:7:11:8 |
15+
| functions.cpp:14:6:14:6 | g | g | | TopLevelFunction, declaration:functions.cpp:14:6:14:6, declaration:functions.cpp:5:6:5:6, definition:functions.cpp:14:6:14:6, isTopLevel |
16+
| functions.cpp:19:7:19:7 | operator= | operator= | Name && p#0 | declaration:functions.cpp:19:7:19:7 |
17+
| functions.cpp:19:7:19:7 | operator= | operator= | const Name & p#0 | declaration:functions.cpp:19:7:19:7 |
18+
| functions.cpp:23:7:23:7 | Table | Table | const Table & p#0 | declaration:functions.cpp:23:7:23:7 |
19+
| functions.cpp:23:7:23:7 | operator= | operator= | const Table & p#0 | declaration:functions.cpp:23:7:23:7 |
20+
| functions.cpp:27:3:27:7 | Table | Table | int s | declaration:functions.cpp:27:3:27:7, definition:functions.cpp:27:3:27:7 |
21+
| functions.cpp:28:3:28:8 | ~Table | ~Table | | declaration:functions.cpp:28:3:28:8, definition:functions.cpp:28:3:28:8 |
22+
| functions.cpp:29:9:29:14 | lookup | lookup | const char * p#0 | declaration:functions.cpp:29:9:29:14 |
23+
| functions.cpp:30:8:30:13 | insert | insert | Name * p#0 | declaration:functions.cpp:30:8:30:13 |
24+
| functions.cpp:33:7:33:7 | operator= | operator= | const MyClass & p#0 | declaration:functions.cpp:33:7:33:7, definition:functions.cpp:33:7:33:7 |
25+
| functions.cpp:36:2:36:8 | MyClass | MyClass | | declaration:functions.cpp:36:2:36:8 |
26+
| functions.cpp:37:2:37:8 | MyClass | MyClass | int from | declaration:functions.cpp:37:2:37:8 |
27+
| functions.cpp:38:2:38:8 | MyClass | MyClass | const MyClass & from | declaration:functions.cpp:38:2:38:8 |
28+
| functions.cpp:39:2:39:8 | MyClass | MyClass | MyClass && from | declaration:functions.cpp:39:2:39:8 |
29+
| functions.cpp:40:2:40:13 | operator int | operator int | | declaration:functions.cpp:40:2:40:13 |
30+
| functions.cpp:43:6:43:6 | h | h | int x | TopLevelFunction, declaration:functions.cpp:43:6:43:6, declaration:functions.cpp:44:6:44:6, isTopLevel |
31+
| functions.cpp:44:6:44:6 | h | h | int x | TopLevelFunction, declaration:functions.cpp:43:6:43:6, declaration:functions.cpp:44:6:44:6, isTopLevel |

cpp/ql/test/library-tests/functions/functions/Functions1.ql

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44

55
import cpp
66

7-
from Function f, string top1, string top2, Location loc, string loctype
8-
where
9-
(if f.isTopLevel() then top1 = "isTopLevel" else top1 = "") and
10-
(if f instanceof TopLevelFunction then top2 = "TopLevelFunction" else top2 = "") and
11-
(
12-
loc = f.getADeclarationLocation() and loctype = "declaration"
13-
or
14-
loc = f.getDefinitionLocation() and loctype = "definition"
15-
)
16-
select f, f.getName(), top1, top2, loc.toString(), loctype
7+
string describe(Function f) {
8+
f.isTopLevel() and
9+
result = "isTopLevel"
10+
or
11+
f instanceof TopLevelFunction and
12+
result = "TopLevelFunction"
13+
or
14+
result = "declaration:" + f.getADeclarationLocation()
15+
or
16+
result = "definition:" + f.getDefinitionLocation()
17+
}
18+
19+
from Function f
20+
where exists(f.getLocation())
21+
select f, f.getName(), f.getParameterString(), concat(describe(f), ", ")

cpp/ql/test/library-tests/functions/functions/functions.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,6 @@ class MyClass
3939
MyClass(MyClass &&from);
4040
operator int();
4141
};
42+
43+
void h(int x);
44+
void h(int y);

0 commit comments

Comments
 (0)