mirror of
https://github.com/github/codeql.git
synced 2025-12-18 18:10:39 +01:00
This PR separates the core cpp packs into `codeql/cpp-queries` and `codeql/cpp-all`. There are very few lines of code changed. Almost all changes are moving files around.
61 lines
1.6 KiB
Plaintext
61 lines
1.6 KiB
Plaintext
/**
|
|
* Provides classes for loop iteration variables.
|
|
*/
|
|
|
|
import semmle.code.cpp.Variable
|
|
|
|
/**
|
|
* A C/C++ variable which is used within the condition of a 'for' loop, and
|
|
* mutated within the update expression of the same 'for' loop.
|
|
*/
|
|
class LoopCounter extends Variable {
|
|
LoopCounter() { exists(ForStmt f | f.getAnIterationVariable() = this) }
|
|
|
|
/**
|
|
* Gets an access of this variable within loop `f`.
|
|
*/
|
|
VariableAccess getVariableAccessInLoop(ForStmt f) {
|
|
this.getALoop() = f and
|
|
result.getEnclosingStmt().getParent*() = f and
|
|
this = result.getTarget()
|
|
}
|
|
|
|
/**
|
|
* Gets a loop which uses this variable as its counter.
|
|
*/
|
|
ForStmt getALoop() { result.getAnIterationVariable() = this }
|
|
}
|
|
|
|
/**
|
|
* A C/C++ variable which is used within the initialization, condition, or
|
|
* update expression of a 'for' loop.
|
|
*/
|
|
class LoopControlVariable extends Variable {
|
|
LoopControlVariable() { this = loopControlVariable(_) }
|
|
|
|
/**
|
|
* Gets an access of this variable within loop `f`.
|
|
*/
|
|
VariableAccess getVariableAccessInLoop(ForStmt f) {
|
|
this.getALoop() = f and
|
|
result.getEnclosingStmt().getParent*() = f and
|
|
this = result.getTarget()
|
|
}
|
|
|
|
/**
|
|
* Gets a loop which uses this variable as its control variable.
|
|
*/
|
|
ForStmt getALoop() { this = loopControlVariable(result) }
|
|
}
|
|
|
|
/**
|
|
* Gets a control variable of loop `f`.
|
|
*/
|
|
private Variable loopControlVariable(ForStmt f) {
|
|
exists(Expr e | result.getAnAccess().getParent*() = e |
|
|
e = f.getControllingExpr() or
|
|
e = f.getInitialization().(ExprStmt).getExpr() or
|
|
e = f.getUpdate()
|
|
)
|
|
}
|