Skip to content

Commit 307b92f

Browse files
committed
C++: Unknown template literals are constant
1 parent e0d1da3 commit 307b92f

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

cpp/ql/src/semmle/code/cpp/exprs/Expr.qll

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ class Expr extends StmtParent, @expr {
131131
valuebind(_, underlyingElement(this))
132132
or
133133
addressConstantExpression(this)
134+
or
135+
constantTemplateLiteral(this)
134136
}
135137

136138
/**
@@ -1119,3 +1121,17 @@ private predicate isStandardPlacementNewAllocator(Function operatorNew) {
11191121

11201122
// Pulled out for performance. See QL-796.
11211123
private predicate hasNoConversions(Expr e) { not e.hasConversion() }
1124+
1125+
/**
1126+
* Holds if `e` is a literal of unknown value in a template, or a cast thereof.
1127+
* We assume that such literals are constant.
1128+
*/
1129+
private predicate constantTemplateLiteral(Expr e) {
1130+
// Unknown literals in uninstantiated templates could be enum constant
1131+
// accesses or pointer-to-member literals.
1132+
e instanceof Literal and
1133+
e.isFromUninstantiatedTemplate(_) and
1134+
not exists(e.getValue())
1135+
or
1136+
constantTemplateLiteral(e.(Cast).getExpr())
1137+
}
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
| staticlocals__staticlocals_f2 | file://:0:0:0:0 | call to C | staticlocals.cpp:30:1:30:1 | return ... | Standard edge, only from QL | |
22
| staticlocals__staticlocals_f2 | file://:0:0:0:0 | initializer for c | file://:0:0:0:0 | call to C | Standard edge, only from QL | |
33
| staticlocals__staticlocals_f2 | staticlocals.cpp:29:5:29:17 | declaration | file://:0:0:0:0 | initializer for c | Standard edge, only from QL | |
4-
| staticlocals__staticlocals_f3 | staticlocals.cpp:39:3:39:34 | declaration | staticlocals.cpp:39:18:39:33 | initializer for i | Standard edge, only from QL | uninstantiated |
5-
| staticlocals__staticlocals_f3 | staticlocals.cpp:39:18:39:33 | Unknown literal | staticlocals.cpp:40:1:40:1 | return ... | Standard edge, only from QL | uninstantiated |
6-
| staticlocals__staticlocals_f3 | staticlocals.cpp:39:18:39:33 | initializer for i | staticlocals.cpp:39:18:39:33 | Unknown literal | Standard edge, only from QL | uninstantiated |

0 commit comments

Comments
 (0)