Skip to content

Commit 4564039

Browse files
Merge pull request #1803 from geoffw0/qldoceg9
CPP: Add syntax examples to QLDoc in Variable.qll
2 parents 7f61082 + d1cc28e commit 4564039

File tree

3 files changed

+175
-61
lines changed

3 files changed

+175
-61
lines changed

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

Lines changed: 123 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,21 @@ import semmle.code.cpp.Initializer
44
private import semmle.code.cpp.internal.ResolveClass
55

66
/**
7-
* A C/C++ variable.
7+
* A C/C++ variable. For example, in the following code there are four
8+
* variables, `a`, `b`, `c` and `d`:
9+
* ```
10+
* extern int a;
11+
* int a;
12+
*
13+
* void myFunction(int b) {
14+
* int c;
15+
* }
16+
*
17+
* namespace N {
18+
* extern int d;
19+
* int d = 1;
20+
* }
21+
* ```
822
*
923
* For local variables, there is a one-to-one correspondence between
1024
* `Variable` and `VariableDeclarationEntry`.
@@ -162,7 +176,22 @@ class Variable extends Declaration, @variable {
162176
}
163177

164178
/**
165-
* A particular declaration or definition of a C/C++ variable.
179+
* A particular declaration or definition of a C/C++ variable. For example, in
180+
* the following code there are six variable declaration entries - two each for
181+
* `a` and `d`, and one each for `b` and `c`:
182+
* ```
183+
* extern int a;
184+
* int a;
185+
*
186+
* void myFunction(int b) {
187+
* int c;
188+
* }
189+
*
190+
* namespace N {
191+
* extern int d;
192+
* int d = 1;
193+
* }
194+
* ```
166195
*/
167196
class VariableDeclarationEntry extends DeclarationEntry, @var_decl {
168197
override Variable getDeclaration() { result = getVariable() }
@@ -183,13 +212,13 @@ class VariableDeclarationEntry extends DeclarationEntry, @var_decl {
183212
* because the parameter may have a different name in the declaration
184213
* than in the definition. For example:
185214
*
186-
* ```
187-
* // Declaration. Parameter is named "x".
188-
* int f(int x);
215+
* ```
216+
* // Declaration. Parameter is named "x".
217+
* int f(int x);
189218
*
190-
* // Definition. Parameter is named "y".
191-
* int f(int y) { return y; }
192-
* ```
219+
* // Definition. Parameter is named "y".
220+
* int f(int y) { return y; }
221+
* ```
193222
*/
194223
override string getName() { var_decls(underlyingElement(this), _, _, result, _) and result != "" }
195224

@@ -215,7 +244,13 @@ class VariableDeclarationEntry extends DeclarationEntry, @var_decl {
215244

216245
/**
217246
* A parameter as described within a particular declaration or definition
218-
* of a C/C++ function.
247+
* of a C/C++ function. For example the declaration of `a` in the following
248+
* code:
249+
* ```
250+
* void myFunction(int a) {
251+
* int b;
252+
* }
253+
* ```
219254
*/
220255
class ParameterDeclarationEntry extends VariableDeclarationEntry {
221256
ParameterDeclarationEntry() { param_decl_bind(underlyingElement(this), _, _) }
@@ -272,8 +307,17 @@ class ParameterDeclarationEntry extends VariableDeclarationEntry {
272307

273308
/**
274309
* A C/C++ variable with block scope [N4140 3.3.3]. In other words, a local
275-
* variable or a function parameter. Local variables can be static; use the
276-
* `isStatic` member predicate to detect those.
310+
* variable or a function parameter. For example, the variables `a`, `b` and
311+
* `c` in the following code:
312+
* ```
313+
* void myFunction(int a) {
314+
* int b;
315+
* static int c;
316+
* }
317+
* ```
318+
*
319+
* Local variables can be static; use the `isStatic` member predicate to
320+
* detect those.
277321
*/
278322
class LocalScopeVariable extends Variable, @localscopevariable {
279323
/** Gets the function to which this variable belongs. */
@@ -292,6 +336,14 @@ deprecated class StackVariable extends Variable {
292336
/**
293337
* A C/C++ local variable. In other words, any variable that has block
294338
* scope [N4140 3.3.3], but is not a parameter of a `Function` or `CatchBlock`.
339+
* For example the variables `b` and `c` in the following code:
340+
* ```
341+
* void myFunction(int a) {
342+
* int b;
343+
* static int c;
344+
* }
345+
* ```
346+
*
295347
* Local variables can be static; use the `isStatic` member predicate to detect
296348
* those.
297349
*
@@ -310,7 +362,15 @@ class LocalVariable extends LocalScopeVariable, @localvariable {
310362
}
311363

312364
/**
313-
* A C/C++ variable which has global scope or namespace scope.
365+
* A C/C++ variable which has global scope or namespace scope. For example the
366+
* variables `a` and `b` in the following code:
367+
* ```
368+
* int a;
369+
*
370+
* namespace N {
371+
* int b;
372+
* }
373+
* ```
314374
*/
315375
class GlobalOrNamespaceVariable extends Variable, @globalvariable {
316376
override string getName() { globalvariables(underlyingElement(this), _, result) }
@@ -321,7 +381,15 @@ class GlobalOrNamespaceVariable extends Variable, @globalvariable {
321381
}
322382

323383
/**
324-
* A C/C++ variable which has namespace scope.
384+
* A C/C++ variable which has namespace scope. For example the variable `b`
385+
* in the following code:
386+
* ```
387+
* int a;
388+
*
389+
* namespace N {
390+
* int b;
391+
* }
392+
* ```
325393
*/
326394
class NamespaceVariable extends GlobalOrNamespaceVariable {
327395
NamespaceVariable() {
@@ -330,7 +398,15 @@ class NamespaceVariable extends GlobalOrNamespaceVariable {
330398
}
331399

332400
/**
333-
* A C/C++ variable which has global scope.
401+
* A C/C++ variable which has global scope. For example the variable `a`
402+
* in the following code:
403+
* ```
404+
* int a;
405+
*
406+
* namespace N {
407+
* int b;
408+
* }
409+
* ```
334410
*
335411
* Note that variables declared in anonymous namespaces have namespace scope,
336412
* even though they are accessed in the same manner as variables declared in
@@ -341,7 +417,15 @@ class GlobalVariable extends GlobalOrNamespaceVariable {
341417
}
342418

343419
/**
344-
* A C structure member or C++ member variable.
420+
* A C structure member or C++ member variable. For example the member
421+
* variables `m` and `s` in the following code:
422+
* ```
423+
* class MyClass {
424+
* public:
425+
* int m;
426+
* static int s;
427+
* };
428+
* ```
345429
*
346430
* This includes static member variables in C++. To exclude static member
347431
* variables, use `Field` instead of `MemberVariable`.
@@ -395,7 +479,12 @@ deprecated class FunctionPointerMemberVariable extends MemberVariable {
395479
}
396480

397481
/**
398-
* A C++14 variable template.
482+
* A C++14 variable template. For example, in the following code the variable
483+
* template `v` defines a family of variables:
484+
* ```
485+
* template<class T>
486+
* T v;
487+
* ```
399488
*/
400489
class TemplateVariable extends Variable {
401490
TemplateVariable() { is_variable_template(underlyingElement(this)) }
@@ -410,7 +499,24 @@ class TemplateVariable extends Variable {
410499
* A non-static local variable or parameter that is not part of an
411500
* uninstantiated template. Uninstantiated templates are purely syntax, and
412501
* only on instantiation will they be complete with information about types,
413-
* conversions, call targets, etc.
502+
* conversions, call targets, etc. For example in the following code, the
503+
* variables `a` in `myFunction` and `b` in the instantiation
504+
* `myTemplateFunction<int>`, but not `b` in the template
505+
* `myTemplateFunction<T>`:
506+
* ```
507+
* void myFunction() {
508+
* T a;
509+
* }
510+
*
511+
* template<type T>
512+
* void myTemplateFunction() {
513+
* T b;
514+
* }
515+
*
516+
* ...
517+
*
518+
* myTemplateFunction<int>();
519+
* ```
414520
*/
415521
class SemanticStackVariable extends LocalScopeVariable {
416522
SemanticStackVariable() {
Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,44 @@
1-
| variables.cpp:2:13:2:13 | pi | variables.cpp:25:12:25:16 | pi | 0 | T |
2-
| variables.cpp:2:13:2:13 | pi | variables.cpp:25:12:25:16 | pi | 0 | float |
3-
| variables.cpp:2:13:2:13 | pi | variables.cpp:25:12:25:16 | pi | 0 | int |
4-
| variables.cpp:2:13:2:13 | pi | variables.cpp:37:16:37:24 | pi | 0 | float |
5-
| variables.cpp:2:13:2:13 | pi | variables.cpp:38:16:38:22 | pi | 0 | int |
6-
| variables.cpp:8:13:8:13 | multi_arg | variables.cpp:33:19:33:33 | multi_arg | 0 | S |
7-
| variables.cpp:8:13:8:13 | multi_arg | variables.cpp:33:19:33:33 | multi_arg | 0 | float |
8-
| variables.cpp:8:13:8:13 | multi_arg | variables.cpp:33:19:33:33 | multi_arg | 0 | short |
9-
| variables.cpp:8:13:8:13 | multi_arg | variables.cpp:33:19:33:33 | multi_arg | 1 | T |
10-
| variables.cpp:8:13:8:13 | multi_arg | variables.cpp:33:19:33:33 | multi_arg | 1 | char |
11-
| variables.cpp:8:13:8:13 | multi_arg | variables.cpp:33:19:33:33 | multi_arg | 1 | long |
12-
| variables.cpp:8:13:8:13 | multi_arg | variables.cpp:40:23:40:60 | multi_arg | 0 | unsigned int |
13-
| variables.cpp:8:13:8:13 | multi_arg | variables.cpp:40:23:40:60 | multi_arg | 1 | unsigned char |
14-
| variables.cpp:8:13:8:13 | multi_arg | variables.cpp:41:23:41:42 | multi_arg | 0 | int |
15-
| variables.cpp:8:13:8:13 | multi_arg | variables.cpp:41:23:41:42 | multi_arg | 1 | char |
16-
| variables.cpp:11:3:11:3 | mutable_val | variables.cpp:26:3:26:16 | mutable_val | 0 | T |
17-
| variables.cpp:11:3:11:3 | mutable_val | variables.cpp:26:3:26:16 | mutable_val | 0 | float |
18-
| variables.cpp:11:3:11:3 | mutable_val | variables.cpp:26:3:26:16 | mutable_val | 0 | int |
19-
| variables.cpp:11:3:11:3 | mutable_val | variables.cpp:43:3:43:18 | mutable_val | 0 | int |
20-
| variables.cpp:11:3:11:3 | mutable_val | variables.cpp:44:3:44:19 | mutable_val | 0 | long |
21-
| variables.cpp:19:8:19:8 | bar | variables.cpp:27:3:27:13 | bar | 0 | T |
22-
| variables.cpp:19:8:19:8 | bar | variables.cpp:27:3:27:13 | bar | 0 | float |
23-
| variables.cpp:19:8:19:8 | bar | variables.cpp:27:3:27:13 | bar | 0 | int |
24-
| variables.cpp:19:8:19:8 | bar | variables.cpp:46:3:46:17 | bar | 0 | short |
25-
| variables.cpp:19:8:19:8 | bar | variables.cpp:47:3:47:18 | bar | 0 | double |
26-
| variables.cpp:21:5:21:15 | no_template | variables.cpp:28:3:28:13 | no_template | -1 | <none> |
27-
| variables.cpp:21:5:21:15 | no_template | variables.cpp:28:3:28:13 | no_template | -1 | <none> |
28-
| variables.cpp:21:5:21:15 | no_template | variables.cpp:28:3:28:13 | no_template | -1 | <none> |
29-
| variables.cpp:21:5:21:15 | no_template | variables.cpp:49:3:49:13 | no_template | -1 | <none> |
30-
| variables.cpp:24:27:24:29 | val | variables.cpp:26:20:26:22 | val | -1 | <none> |
31-
| variables.cpp:24:27:24:29 | val | variables.cpp:26:20:26:22 | val | -1 | <none> |
32-
| variables.cpp:24:27:24:29 | val | variables.cpp:26:20:26:22 | val | -1 | <none> |
33-
| variables.cpp:24:27:24:29 | val | variables.cpp:27:17:27:19 | val | -1 | <none> |
34-
| variables.cpp:24:27:24:29 | val | variables.cpp:27:17:27:19 | val | -1 | <none> |
35-
| variables.cpp:24:27:24:29 | val | variables.cpp:27:17:27:19 | val | -1 | <none> |
1+
| file://:0:0:0:0 | fp_offset | | | |
2+
| file://:0:0:0:0 | gp_offset | | | |
3+
| file://:0:0:0:0 | overflow_arg_area | | | |
4+
| file://:0:0:0:0 | p#0 | | | |
5+
| file://:0:0:0:0 | p#0 | | | |
6+
| file://:0:0:0:0 | p#0 | | | |
7+
| file://:0:0:0:0 | p#0 | | | |
8+
| file://:0:0:0:0 | reg_save_area | | | |
9+
| variables.cpp:2:13:2:13 | pi | variables.cpp:25:12:25:16 | T | |
10+
| variables.cpp:2:13:2:13 | pi | variables.cpp:25:12:25:16, variables.cpp:37:16:37:24 | float | |
11+
| variables.cpp:2:13:2:13 | pi | variables.cpp:25:12:25:16, variables.cpp:38:16:38:22 | int | |
12+
| variables.cpp:2:16:2:16 | pi | | T | TemplateVariable |
13+
| variables.cpp:5:23:5:37 | pi | | const char * | |
14+
| variables.cpp:8:13:8:13 | multi_arg | variables.cpp:33:19:33:33 | S, T | |
15+
| variables.cpp:8:13:8:13 | multi_arg | variables.cpp:33:19:33:33 | float, char | |
16+
| variables.cpp:8:13:8:13 | multi_arg | variables.cpp:33:19:33:33 | short, long | |
17+
| variables.cpp:8:13:8:13 | multi_arg | variables.cpp:40:23:40:60 | unsigned int, unsigned char | |
18+
| variables.cpp:8:13:8:13 | multi_arg | variables.cpp:41:23:41:42 | int, char | |
19+
| variables.cpp:8:23:8:23 | multi_arg | | S, T | TemplateVariable |
20+
| variables.cpp:11:3:11:3 | mutable_val | variables.cpp:26:3:26:16 | T | |
21+
| variables.cpp:11:3:11:3 | mutable_val | variables.cpp:26:3:26:16 | float | |
22+
| variables.cpp:11:3:11:3 | mutable_val | variables.cpp:26:3:26:16, variables.cpp:43:3:43:18 | int | |
23+
| variables.cpp:11:3:11:3 | mutable_val | variables.cpp:44:3:44:19 | long | |
24+
| variables.cpp:11:15:11:15 | mutable_val | | T | TemplateVariable |
25+
| variables.cpp:19:3:19:10 | bar | | T | TemplateVariable |
26+
| variables.cpp:19:8:19:8 | bar | variables.cpp:27:3:27:13 | T | |
27+
| variables.cpp:19:8:19:8 | bar | variables.cpp:27:3:27:13 | float | |
28+
| variables.cpp:19:8:19:8 | bar | variables.cpp:27:3:27:13 | int | |
29+
| variables.cpp:19:8:19:8 | bar | variables.cpp:46:3:46:17 | short | |
30+
| variables.cpp:19:8:19:8 | bar | variables.cpp:47:3:47:18 | double | |
31+
| variables.cpp:21:5:21:15 | no_template | variables.cpp:28:3:28:13, variables.cpp:28:3:28:13, variables.cpp:28:3:28:13, variables.cpp:49:3:49:13 | | |
32+
| variables.cpp:24:27:24:29 | val | variables.cpp:26:20:26:22, variables.cpp:27:17:27:19 | | |
33+
| variables.cpp:24:27:24:29 | val | variables.cpp:26:20:26:22, variables.cpp:27:17:27:19 | | |
34+
| variables.cpp:24:27:24:29 | val | variables.cpp:26:20:26:22, variables.cpp:27:17:27:19 | | |
35+
| variables.cpp:25:5:25:8 | pi_t | | | |
36+
| variables.cpp:25:5:25:8 | pi_t | | | |
37+
| variables.cpp:25:5:25:8 | pi_t | | | |
38+
| variables.cpp:33:5:33:15 | multi_arg_s | | | |
39+
| variables.cpp:33:5:33:15 | multi_arg_s | | | |
40+
| variables.cpp:33:5:33:15 | multi_arg_s | | | |
41+
| variables.cpp:37:9:37:12 | pi_f | | | |
42+
| variables.cpp:38:9:38:12 | pi_i | | | |
43+
| variables.cpp:40:9:40:19 | multi_arg_a | | | |
44+
| variables.cpp:41:9:41:19 | multi_arg_b | | | |
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import cpp
22

3-
from Variable v, VariableAccess a, int i, string s
4-
where
5-
v = a.getTarget() and
6-
if exists(v.getATemplateArgument())
7-
then s = v.getTemplateArgument(i).toString()
8-
else (
9-
s = "<none>" and i = -1
10-
)
11-
select v, a, i, s
3+
string describe(Variable v) {
4+
v instanceof TemplateVariable and
5+
result = "TemplateVariable"
6+
}
7+
8+
from Variable v
9+
select v, concat(VariableAccess a | a.getTarget() = v | a.getLocation().toString(), ", "),
10+
concat(int i | | v.getTemplateArgument(i).toString(), ", " order by i), concat(describe(v), ", ")

0 commit comments

Comments
 (0)