C++: Extend tests to also test the new predicates.

This commit is contained in:
Mathias Vorreiter Pedersen
2024-03-18 16:27:10 +00:00
parent dbd47b387a
commit 032678a367
2 changed files with 87 additions and 30 deletions

View File

@@ -4,22 +4,32 @@ import semmle.code.cpp.controlflow.IRGuards
query predicate astGuards(GuardCondition guard) { any() }
query predicate astGuardsCompare(int startLine, string msg) {
exists(GuardCondition guard, Expr left, Expr right, int k, string which, string op |
exists(GuardCondition guard, Expr left, int k, string which, string op |
exists(boolean sense |
sense = true and which = "true"
or
sense = false and which = "false"
|
guard.comparesLt(left, right, k, true, sense) and op = " < "
exists(Expr right |
guard.comparesLt(left, right, k, true, sense) and op = " < "
or
guard.comparesLt(left, right, k, false, sense) and op = " >= "
or
guard.comparesEq(left, right, k, true, sense) and op = " == "
or
guard.comparesEq(left, right, k, false, sense) and op = " != "
|
msg = left + op + right + "+" + k + " when " + guard + " is " + which
)
or
guard.comparesLt(left, right, k, false, sense) and op = " >= "
or
guard.comparesEq(left, right, k, true, sense) and op = " == "
or
guard.comparesEq(left, right, k, false, sense) and op = " != "
(
guard.comparesEq(left, k, true, sense) and op = " == "
or
guard.comparesEq(left, k, false, sense) and op = " != "
) and
msg = left + op + k + " when " + guard + " is " + which
) and
startLine = guard.getLocation().getStartLine() and
msg = left + op + right + "+" + k + " when " + guard + " is " + which
startLine = guard.getLocation().getStartLine()
)
}
@@ -46,28 +56,52 @@ query predicate astGuardsEnsure(
)
}
query predicate astGuardsEnsure_const(
GuardCondition guard, Expr left, string op, int k, int start, int end
) {
exists(BasicBlock block |
guard.ensuresEq(left, k, block, true) and op = "=="
or
guard.ensuresEq(left, k, block, false) and op = "!="
|
block.hasLocationInfo(_, start, _, end, _)
)
}
query predicate irGuards(IRGuardCondition guard) { any() }
query predicate irGuardsCompare(int startLine, string msg) {
exists(IRGuardCondition guard, Operand left, Operand right, int k, string which, string op |
exists(IRGuardCondition guard, Operand left, int k, string which, string op |
exists(boolean sense |
sense = true and which = "true"
or
sense = false and which = "false"
|
guard.comparesLt(left, right, k, true, sense) and op = " < "
exists(Operand right |
guard.comparesLt(left, right, k, true, sense) and op = " < "
or
guard.comparesLt(left, right, k, false, sense) and op = " >= "
or
guard.comparesEq(left, right, k, true, sense) and op = " == "
or
guard.comparesEq(left, right, k, false, sense) and op = " != "
|
msg =
left.getAnyDef().getUnconvertedResultExpression() + op +
right.getAnyDef().getUnconvertedResultExpression() + "+" + k + " when " + guard + " is "
+ which
)
or
guard.comparesLt(left, right, k, false, sense) and op = " >= "
or
guard.comparesEq(left, right, k, true, sense) and op = " == "
or
guard.comparesEq(left, right, k, false, sense) and op = " != "
(
guard.comparesEq(left, k, true, sense) and op = " == "
or
guard.comparesEq(left, k, false, sense) and op = " != "
) and
msg =
left.getAnyDef().getUnconvertedResultExpression() + op + k + " when " + guard + " is " +
which
) and
startLine = guard.getLocation().getStartLine() and
msg =
left.getAnyDef().getUnconvertedResultExpression() + op +
right.getAnyDef().getUnconvertedResultExpression() + "+" + k + " when " + guard + " is " +
which
startLine = guard.getLocation().getStartLine()
)
}
@@ -95,3 +129,16 @@ query predicate irGuardsEnsure(
block.getLocation().hasLocationInfo(_, start, _, end, _)
)
}
query predicate irGuardsEnsure_const(
IRGuardCondition guard, Instruction left, string op, int k, int start, int end
) {
exists(IRBlock block, Operand leftOp |
guard.ensuresEq(leftOp, k, block, true) and op = "=="
or
guard.ensuresEq(leftOp, k, block, false) and op = "!="
|
leftOp = left.getAUse() and
block.getLocation().hasLocationInfo(_, start, _, end, _)
)
}

View File

@@ -7,20 +7,30 @@
import cpp
import semmle.code.cpp.controlflow.Guards
from GuardCondition guard, Expr left, Expr right, int k, string which, string op, string msg
from GuardCondition guard, Expr left, int k, string which, string op, string msg
where
exists(boolean sense |
sense = true and which = "true"
or
sense = false and which = "false"
|
guard.comparesLt(left, right, k, true, sense) and op = " < "
exists(Expr right |
guard.comparesLt(left, right, k, true, sense) and op = " < "
or
guard.comparesLt(left, right, k, false, sense) and op = " >= "
or
guard.comparesEq(left, right, k, true, sense) and op = " == "
or
guard.comparesEq(left, right, k, false, sense) and op = " != "
|
msg = left + op + right + "+" + k + " when " + guard + " is " + which
)
or
guard.comparesLt(left, right, k, false, sense) and op = " >= "
or
guard.comparesEq(left, right, k, true, sense) and op = " == "
or
guard.comparesEq(left, right, k, false, sense) and op = " != "
) and
msg = left + op + right + "+" + k + " when " + guard + " is " + which
(
guard.comparesEq(left, k, true, sense) and op = " == "
or
guard.comparesEq(left, k, false, sense) and op = " != "
) and
msg = left + op + k + " when " + guard + " is " + which
)
select guard.getLocation().getStartLine(), msg