Skip to content

Commit a50c865

Browse files
committed
Java: Add VariableWrite class.
1 parent 19d8dfa commit a50c865

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

java/ql/lib/semmle/code/java/Expr.qll

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1808,6 +1808,52 @@ class VariableAssign extends VariableUpdate {
18081808
}
18091809
}
18101810

1811+
private newtype TVariableWrite =
1812+
TParamInit(Parameter p) or
1813+
TVarWriteExpr(VariableUpdate u)
1814+
1815+
/**
1816+
* A write to a variable. This is either a local variable declaration,
1817+
* including parameter declarations, or an update to a variable.
1818+
*/
1819+
class VariableWrite extends TVariableWrite {
1820+
/** Gets the expression representing this write, if any. */
1821+
Expr asExpr() { this = TVarWriteExpr(result) }
1822+
1823+
/**
1824+
* Gets the expression with the value being written, if any.
1825+
*
1826+
* This can be the same expression as returned by `asExpr()`, which is the
1827+
* case for, for example, `++x` and `x += e`. For simple assignments like
1828+
* `x = e`, `asExpr()` gets the whole assignment expression while
1829+
* `getValue()` gets the right-hand side `e`. Post-crement operations like
1830+
* `x++` do not have an expression with the value being written.
1831+
*/
1832+
Expr getValue() {
1833+
this.asExpr().(VariableAssign).getSource() = result or
1834+
this.asExpr().(AssignOp) = result or
1835+
this.asExpr().(PreIncExpr) = result or
1836+
this.asExpr().(PreDecExpr) = result
1837+
}
1838+
1839+
/** Holds if this write is an initialization of parameter `p`. */
1840+
predicate isParameterInit(Parameter p) { this = TParamInit(p) }
1841+
1842+
/** Gets a textual representation of this write. */
1843+
string toString() {
1844+
exists(Parameter p | this = TParamInit(p) and result = p.toString())
1845+
or
1846+
result = this.asExpr().toString()
1847+
}
1848+
1849+
/** Gets the location of this write. */
1850+
Location getLocation() {
1851+
exists(Parameter p | this = TParamInit(p) and result = p.getLocation())
1852+
or
1853+
result = this.asExpr().getLocation()
1854+
}
1855+
}
1856+
18111857
/** A type literal. For example, `String.class`. */
18121858
class TypeLiteral extends Expr, @typeliteral {
18131859
/** Gets the access to the type whose class is accessed. */

0 commit comments

Comments
 (0)