Skip to content

Commit e6ddf7f

Browse files
authored
Merge pull request #1012 from ian-semmle/constexpr
C++: Add Variable.isConstexpr()
2 parents a30b456 + af397d3 commit e6ddf7f

File tree

5 files changed

+29
-0
lines changed

5 files changed

+29
-0
lines changed

change-notes/1.20/analysis-cpp.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@
3939
* There is a new `Namespace.isInline()` predicate, which holds if the namespace was declared as `inline namespace`.
4040
* The `Expr.isConstant()` predicate now also holds for _address constant expressions_, which are addresses that will be constant after the program has been linked. These address constants do not have a result for `Expr.getValue()`.
4141
* There are new `Function.isDeclaredConstexpr()` and `Function.isConstexpr()` predicates. They can be used to tell whether a function was declared as `constexpr`, and whether it actually is `constexpr`.
42+
* There is a new `Variable.isConstexpr()` predicate. It can be used to tell whether a variable is `constexpr`.

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ class Variable extends Declaration, @variable {
121121
result.getLValue() = this.getAnAccess()
122122
}
123123

124+
/**
125+
* Holds if this variable is `constexpr`.
126+
*/
127+
predicate isConstexpr() {
128+
this.hasSpecifier("is_constexpr")
129+
}
130+
124131
/**
125132
* Holds if this variable is constructed from `v` as a result
126133
* of template instantiation. If so, it originates either from a template
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
constexpr int var_constexpr = 5;
3+
int var_not_constexpr_initialised = 6;
4+
const int var_not_constexpr_const = 7;
5+
int var_not_constexpr;
6+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
| constexpr.cpp:2:15:2:27 | var_constexpr | true |
2+
| constexpr.cpp:3:5:3:33 | var_not_constexpr_initialised | false |
3+
| constexpr.cpp:4:11:4:33 | var_not_constexpr_const | false |
4+
| constexpr.cpp:5:5:5:21 | var_not_constexpr | false |
5+
| file://:0:0:0:0 | fp_offset | false |
6+
| file://:0:0:0:0 | gp_offset | false |
7+
| file://:0:0:0:0 | overflow_arg_area | false |
8+
| file://:0:0:0:0 | p#0 | false |
9+
| file://:0:0:0:0 | p#0 | false |
10+
| file://:0:0:0:0 | reg_save_area | false |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import cpp
2+
3+
from Variable v
4+
select v,
5+
any(boolean b | if v.isConstexpr() then b = true else b = false)

0 commit comments

Comments
 (0)