Skip to content

Commit d81e6e9

Browse files
committed
C++: Add TranslatedElement::isIRConstant
Now that there exist constants with no QL-representable value, we need to make sure they're not treated as constants in the IR.
1 parent 3edadc3 commit d81e6e9

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

cpp/ql/src/semmle/code/cpp/ir/implementation/raw/internal/TranslatedElement.qll

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ private Element getRealParent(Expr expr) {
4949
)
5050
}
5151

52+
/**
53+
* Holds if `expr` is a constant of a type that can be replaced directly with
54+
* its value in the IR. This does not include address constants as we have no
55+
* means to express those as QL values.
56+
*/
57+
predicate isIRConstant(Expr expr) { exists(expr.getValue()) }
58+
5259
/**
5360
* Holds if `expr` and all of its descendants should be ignored for the purposes
5461
* of IR generation due to some property of `expr` itself. Unlike
@@ -63,7 +70,7 @@ private predicate ignoreExprAndDescendants(Expr expr) {
6370
getRealParent(expr) instanceof SwitchCase or
6471
// Ignore descendants of constant expressions, since we'll just substitute the
6572
// constant value.
66-
getRealParent(expr).(Expr).isConstant() or
73+
isIRConstant(getRealParent(expr)) or
6774
// The `DestructorCall` node for a `DestructorFieldDestruction` has a `FieldAccess`
6875
// node as its qualifier, but that `FieldAccess` does not have a child of its own.
6976
// We'll ignore that `FieldAccess`, and supply the receiver as part of the calling
@@ -146,7 +153,7 @@ private predicate translateStmt(Stmt stmt) {
146153
*/
147154
private predicate isNativeCondition(Expr expr) {
148155
expr instanceof BinaryLogicalOperation and
149-
not expr.isConstant()
156+
not isIRConstant(expr)
150157
}
151158

152159
/**
@@ -159,7 +166,7 @@ private predicate isFlexibleCondition(Expr expr) {
159166
expr instanceof NotExpr
160167
) and
161168
usedAsCondition(expr) and
162-
not expr.isConstant()
169+
not isIRConstant(expr)
163170
}
164171

165172
/**

cpp/ql/src/semmle/code/cpp/ir/implementation/raw/internal/TranslatedExpr.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,7 @@ class TranslatedFunctionAccess extends TranslatedNonConstantExpr {
962962
abstract class TranslatedNonConstantExpr extends TranslatedCoreExpr {
963963
TranslatedNonConstantExpr() {
964964
this = TTranslatedValueExpr(expr) and
965-
not expr.isConstant()
965+
not isIRConstant(expr)
966966
}
967967
}
968968

@@ -974,7 +974,7 @@ abstract class TranslatedNonConstantExpr extends TranslatedCoreExpr {
974974
abstract class TranslatedConstantExpr extends TranslatedCoreExpr {
975975
TranslatedConstantExpr() {
976976
this = TTranslatedValueExpr(expr) and
977-
expr.isConstant()
977+
isIRConstant(expr)
978978
}
979979

980980
override final Instruction getFirstInstruction() {

0 commit comments

Comments
 (0)