C++: Add ConditionDeclExpr convenience predicates

Also expand the QLDoc.
This commit is contained in:
Jonas Jensen
2018-12-21 11:57:19 +01:00
parent ca0517b3d6
commit a47faa2272
3 changed files with 22 additions and 5 deletions

View File

@@ -60,7 +60,7 @@ predicate nullCheckInCondition(Expr e, Variable v, Declaration qualifier) {
or exists(NotExpr exp | exp = e and nullCheckInCondition(exp.getAnOperand(), v, qualifier))
or exists(FunctionCall c | c = e and nullCheckInCondition(c.getAnArgument(), v, qualifier) and
c.getTarget().getName() = "__builtin_expect")
or exists(ConditionDeclExpr d | d = e and nullCheckInCondition(d.getExpr(), v, qualifier))
or exists(ConditionDeclExpr d | d = e and nullCheckInCondition(d.getVariableAccess(), v, qualifier))
}
predicate hasNullCheck(Function enclosing, Variable v, Declaration qualifier) {

View File

@@ -144,13 +144,30 @@ class AssignPointerSubExpr extends AssignOperation, @assignpsubexpr {
/**
* A C++ variable declaration in an expression where a condition is expected.
* For example, on the `ConditionDeclExpr` in `if (bool c = x < y)`,
* `getExpr()` is an access to `c` (with possible casts), and `getVariable` is
* the variable `c`, which has an initializer `x < y`.
* `getVariableAccess()` is an access to `c` (with possible casts),
* `getVariable` is the variable `c`, which has an initializer `x < y`, and
* `getInitializingExpr` is `x < y`.
*/
class ConditionDeclExpr extends Expr, @condition_decl {
/** Gets the access using the condition for this declaration. */
/**
* DEPRECATED: Use `getVariableAccess` or `getInitializingExpr` instead.
* Gets the access using the condition for this declaration.
*/
deprecated
Expr getExpr() { result = this.getChild(0) }
/**
* Gets the compiler-generated variable access that conceptually occurs after
* the initialization of the declared variable.
*/
VariableAccess getVariableAccess() { result = this.getChild(0) }
/**
* Gets the expression that initializes the declared variable. This predicate
* always has a result.
*/
Expr getInitializingExpr() { result = this.getVariable().getInitializer().getExpr() }
/** Gets the variable that is declared. */
Variable getVariable() { condition_decl_bind(underlyingElement(this),unresolveElement(result)) }

View File

@@ -2691,6 +2691,6 @@ class TranslatedConditionDeclExpr extends TranslatedNonConstantExpr {
}
private TranslatedExpr getConditionExpr() {
result = getTranslatedExpr(condDeclExpr.getExpr().getFullyConverted())
result = getTranslatedExpr(condDeclExpr.getVariableAccess().getFullyConverted())
}
}