Files
codeql/cpp/ql/src/Likely Bugs/NestedLoopSameVar.qll
Jonas Jensen 4ef5c9af62 C++: Autoformat everything
Some files that will change in #1736 have been spared.

    ./build -j4 target/jars/qlformat
    find ql/cpp/ql -name "*.ql"  -print0 | xargs -0 target/jars/qlformat --input
    find ql/cpp/ql -name "*.qll" -print0 | xargs -0 target/jars/qlformat --input
    (cd ql && git checkout 'cpp/ql/src/semmle/code/cpp/ir/implementation/**/*SSA*.qll')
    buildutils-internal/scripts/pr-checks/sync-identical-files.py --latest
2019-09-09 11:25:53 +02:00

37 lines
1.1 KiB
Plaintext

/**
* Provides the implementation of the query 'Nested loops with same variable'.
*/
import cpp
/**
* An access to a field of the form `object.field`.
*/
predicate simpleFieldAccess(Variable object, Variable field, VariableAccess access) {
access.getTarget() = field and
access.getQualifier().(VariableAccess).getTarget() = object
}
/**
* Holds if `inner` and `outer` are nested for statements that
* use the same loop variable `iteration`.
*/
predicate nestedForViolation(ForStmt inner, Variable iteration, ForStmt outer) {
// same variable
iteration = inner.getAnIterationVariable() and
iteration = outer.getAnIterationVariable() and
// field accesses must have the same object
(
iteration instanceof Field
implies
exists(Variable obj |
simpleFieldAccess(obj, iteration, inner.getCondition().getAChild*()) and
simpleFieldAccess(obj, iteration, outer.getCondition().getAChild*())
)
) and
// ordinary nested loops
exists(inner.getInitialization()) and
inner.getParent+() = outer and
inner.getASuccessor+() = outer.getCondition()
}