Skip to content

Commit 8110039

Browse files
committed
C++: Bring back the StackVariable QL class
The new `StackVariable` class actually denotes what its name suggests.
1 parent 351cb46 commit 8110039

3 files changed

Lines changed: 24 additions & 19 deletions

File tree

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -307,21 +307,30 @@ class ParameterDeclarationEntry extends VariableDeclarationEntry {
307307
* }
308308
* ```
309309
*
310-
* Local variables can be static; use the `isStatic` member predicate to
311-
* detect those.
310+
* See also `StackVariable`, which is the class of local-scope variables
311+
* without statics and thread-locals.
312312
*/
313313
class LocalScopeVariable extends Variable, @localscopevariable {
314314
/** Gets the function to which this variable belongs. */
315315
/*abstract*/ Function getFunction() { none() }
316316
}
317317

318318
/**
319-
* DEPRECATED: use `LocalScopeVariable` instead.
319+
* A C/C++ variable with _automatic storage duration_. In other words, a
320+
* function parameter or a local variable that is not static or thread-local.
321+
* For example, the variables `a` and `b` in the following code.
322+
* ```
323+
* void myFunction(int a) {
324+
* int b;
325+
* static int c;
326+
* }
327+
* ```
320328
*/
321-
deprecated class StackVariable extends Variable {
322-
StackVariable() { this instanceof LocalScopeVariable }
323-
324-
Function getFunction() { result = this.(LocalScopeVariable).getFunction() }
329+
class StackVariable extends LocalScopeVariable {
330+
StackVariable() {
331+
not this.isStatic() and
332+
not this.isThreadLocal()
333+
}
325334
}
326335

327336
/**
@@ -496,7 +505,7 @@ class TemplateVariable extends Variable {
496505
* `myTemplateFunction<T>`:
497506
* ```
498507
* void myFunction() {
499-
* T a;
508+
* float a;
500509
* }
501510
*
502511
* template<type T>
@@ -509,9 +518,6 @@ class TemplateVariable extends Variable {
509518
* myTemplateFunction<int>();
510519
* ```
511520
*/
512-
class SemanticStackVariable extends LocalScopeVariable {
513-
SemanticStackVariable() {
514-
not this.isStatic() and
515-
not this.isFromUninstantiatedTemplate(_)
516-
}
521+
class SemanticStackVariable extends StackVariable {
522+
SemanticStackVariable() { not this.isFromUninstantiatedTemplate(_) }
517523
}

cpp/ql/test/library-tests/variables/variables/variable.expected

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@
22
| file://:0:0:0:0 | gp_offset | file://:0:0:0:0 | unsigned int | Field | | |
33
| file://:0:0:0:0 | overflow_arg_area | file://:0:0:0:0 | void * | Field | | |
44
| file://:0:0:0:0 | p#0 | file://:0:0:0:0 | __va_list_tag && | SemanticStackVariable | | |
5-
| file://:0:0:0:0 | p#0 | file://:0:0:0:0 | __va_list_tag && | StackVariable | | |
65
| file://:0:0:0:0 | p#0 | file://:0:0:0:0 | address && | SemanticStackVariable | | |
7-
| file://:0:0:0:0 | p#0 | file://:0:0:0:0 | address && | StackVariable | | |
86
| file://:0:0:0:0 | p#0 | file://:0:0:0:0 | const __va_list_tag & | SemanticStackVariable | | |
9-
| file://:0:0:0:0 | p#0 | file://:0:0:0:0 | const __va_list_tag & | StackVariable | | |
107
| file://:0:0:0:0 | p#0 | file://:0:0:0:0 | const address & | SemanticStackVariable | | |
11-
| file://:0:0:0:0 | p#0 | file://:0:0:0:0 | const address & | StackVariable | | |
128
| file://:0:0:0:0 | reg_save_area | file://:0:0:0:0 | void * | Field | | |
139
| variables.cpp:1:12:1:12 | i | file://:0:0:0:0 | int | GlobalVariable | | |
1410
| variables.cpp:2:12:2:12 | i | file://:0:0:0:0 | int | GlobalVariable | | |
@@ -38,11 +34,10 @@
3834
| variables.cpp:37:6:37:8 | ap3 | file://:0:0:0:0 | int * | GlobalVariable | | |
3935
| variables.cpp:41:7:41:11 | local | file://:0:0:0:0 | char[] | LocalVariable | | |
4036
| variables.cpp:41:7:41:11 | local | file://:0:0:0:0 | char[] | SemanticStackVariable | | |
41-
| variables.cpp:41:7:41:11 | local | file://:0:0:0:0 | char[] | StackVariable | | |
4237
| variables.cpp:43:14:43:18 | local | file://:0:0:0:0 | int | LocalVariable | | static |
43-
| variables.cpp:43:14:43:18 | local | file://:0:0:0:0 | int | StackVariable | | static |
4438
| variables.cpp:48:9:48:12 | name | file://:0:0:0:0 | char * | Field | | |
4539
| variables.cpp:49:12:49:17 | number | file://:0:0:0:0 | long | Field | | |
4640
| variables.cpp:50:9:50:14 | street | file://:0:0:0:0 | char * | Field | | |
4741
| variables.cpp:51:9:51:12 | town | file://:0:0:0:0 | char * | Field | | |
4842
| variables.cpp:52:16:52:22 | country | file://:0:0:0:0 | char * | MemberVariable | | static |
43+
| variables.cpp:56:14:56:29 | externInFunction | file://:0:0:0:0 | int | GlobalVariable | | |

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,7 @@ struct address {
5151
char* town;
5252
static char* country;
5353
};
54+
55+
void hasExtern() {
56+
extern int externInFunction;
57+
}

0 commit comments

Comments
 (0)