Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions change-notes/1.20/analysis-cpp.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@
* There is a new `Namespace.isInline()` predicate, which holds if the namespace was declared as `inline namespace`.
* 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()`.
* 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`.
* There is a new `Variable.isConstexpr()` predicate. It can be used to tell whether a variable is `constexpr`.
7 changes: 7 additions & 0 deletions cpp/ql/src/semmle/code/cpp/Variable.qll
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ class Variable extends Declaration, @variable {
result.getLValue() = this.getAnAccess()
}

/**
* Holds if this variable is `constexpr`.
*/
predicate isConstexpr() {
this.hasSpecifier("is_constexpr")
}

/**
* Holds if this variable is constructed from `v` as a result
* of template instantiation. If so, it originates either from a template
Expand Down
6 changes: 6 additions & 0 deletions cpp/ql/test/library-tests/variables/constexpr/constexpr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

constexpr int var_constexpr = 5;
int var_not_constexpr_initialised = 6;
const int var_not_constexpr_const = 7;
int var_not_constexpr;

10 changes: 10 additions & 0 deletions cpp/ql/test/library-tests/variables/constexpr/constexpr.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
| constexpr.cpp:2:15:2:27 | var_constexpr | true |
| constexpr.cpp:3:5:3:33 | var_not_constexpr_initialised | false |
| constexpr.cpp:4:11:4:33 | var_not_constexpr_const | false |
| constexpr.cpp:5:5:5:21 | var_not_constexpr | false |
| file://:0:0:0:0 | fp_offset | false |
| file://:0:0:0:0 | gp_offset | false |
| file://:0:0:0:0 | overflow_arg_area | false |
| file://:0:0:0:0 | p#0 | false |
| file://:0:0:0:0 | p#0 | false |
| file://:0:0:0:0 | reg_save_area | false |
5 changes: 5 additions & 0 deletions cpp/ql/test/library-tests/variables/constexpr/constexpr.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import cpp

from Variable v
select v,
any(boolean b | if v.isConstexpr() then b = true else b = false)