Skip to content

Commit a47faa2

Browse files
committed
C++: Add ConditionDeclExpr convenience predicates
Also expand the QLDoc.
1 parent ca0517b commit a47faa2

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

cpp/ql/src/Likely Bugs/InconsistentCheckReturnNull.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ predicate nullCheckInCondition(Expr e, Variable v, Declaration qualifier) {
6060
or exists(NotExpr exp | exp = e and nullCheckInCondition(exp.getAnOperand(), v, qualifier))
6161
or exists(FunctionCall c | c = e and nullCheckInCondition(c.getAnArgument(), v, qualifier) and
6262
c.getTarget().getName() = "__builtin_expect")
63-
or exists(ConditionDeclExpr d | d = e and nullCheckInCondition(d.getExpr(), v, qualifier))
63+
or exists(ConditionDeclExpr d | d = e and nullCheckInCondition(d.getVariableAccess(), v, qualifier))
6464
}
6565

6666
predicate hasNullCheck(Function enclosing, Variable v, Declaration qualifier) {

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,30 @@ class AssignPointerSubExpr extends AssignOperation, @assignpsubexpr {
144144
/**
145145
* A C++ variable declaration in an expression where a condition is expected.
146146
* For example, on the `ConditionDeclExpr` in `if (bool c = x < y)`,
147-
* `getExpr()` is an access to `c` (with possible casts), and `getVariable` is
148-
* the variable `c`, which has an initializer `x < y`.
147+
* `getVariableAccess()` is an access to `c` (with possible casts),
148+
* `getVariable` is the variable `c`, which has an initializer `x < y`, and
149+
* `getInitializingExpr` is `x < y`.
149150
*/
150151
class ConditionDeclExpr extends Expr, @condition_decl {
151-
/** Gets the access using the condition for this declaration. */
152+
/**
153+
* DEPRECATED: Use `getVariableAccess` or `getInitializingExpr` instead.
154+
* Gets the access using the condition for this declaration.
155+
*/
156+
deprecated
152157
Expr getExpr() { result = this.getChild(0) }
153158

159+
/**
160+
* Gets the compiler-generated variable access that conceptually occurs after
161+
* the initialization of the declared variable.
162+
*/
163+
VariableAccess getVariableAccess() { result = this.getChild(0) }
164+
165+
/**
166+
* Gets the expression that initializes the declared variable. This predicate
167+
* always has a result.
168+
*/
169+
Expr getInitializingExpr() { result = this.getVariable().getInitializer().getExpr() }
170+
154171
/** Gets the variable that is declared. */
155172
Variable getVariable() { condition_decl_bind(underlyingElement(this),unresolveElement(result)) }
156173

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2691,6 +2691,6 @@ class TranslatedConditionDeclExpr extends TranslatedNonConstantExpr {
26912691
}
26922692

26932693
private TranslatedExpr getConditionExpr() {
2694-
result = getTranslatedExpr(condDeclExpr.getExpr().getFullyConverted())
2694+
result = getTranslatedExpr(condDeclExpr.getVariableAccess().getFullyConverted())
26952695
}
26962696
}

0 commit comments

Comments
 (0)