Skip to content

Commit 29e0174

Browse files
authored
Merge pull request #21267 from aschackmull/java/rename-misc
Java: Rename several AST predicates.
2 parents e4daeec + 11003e6 commit 29e0174

File tree

79 files changed

+273
-229
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+273
-229
lines changed

java/ql/consistency-queries/UnaryExpr.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import java
22

33
from UnaryExpr ue
44
where
5-
not exists(ue.getExpr())
5+
not exists(ue.getOperand())
66
or
77
exists(Expr e, int i | e.isNthChildOf(ue, i) and i != 0)
88
select ue

java/ql/examples/snippets/returnstatement.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
import java
1111

1212
from ReturnStmt r
13-
where r.getResult() instanceof NullLiteral
13+
where r.getExpr() instanceof NullLiteral
1414
select r

java/ql/examples/snippets/ternaryconditional.ql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import java
1111

1212
from ConditionalExpr e
1313
where
14-
e.getTrueExpr().getType() != e.getFalseExpr().getType() and
15-
not e.getTrueExpr().getType() instanceof NullType and
16-
not e.getFalseExpr().getType() instanceof NullType
14+
e.getThen().getType() != e.getElse().getType() and
15+
not e.getThen().getType() instanceof NullType and
16+
not e.getElse().getType() instanceof NullType
1717
select e
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
category: deprecated
3+
---
4+
* Renamed the following predicates to increase uniformity across languages. The `getBody` predicate already existed on `LoopStmt`, but is now properly inherited.
5+
- `UnaryExpr.getExpr` to `getOperand`.
6+
- `ConditionalExpr.getTrueExpr` to `getThen`.
7+
- `ConditionalExpr.getFalseExpr` to `getElse`.
8+
- `ReturnStmt.getResult` to `getExpr`.
9+
- `WhileStmt.getStmt` to `getBody`.
10+
- `DoStmt.getStmt` to `getBody`.
11+
- `ForStmt.getStmt` to `getBody`.
12+
- `EnhancedForStmt.getStmt` to `getBody`.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module CalculateConstants<getBoolValSig/1 getBoolVal, getIntValSig/1 getIntVal>
2222
boolean calculateBooleanValue(Expr e) {
2323
// No casts relevant to booleans.
2424
// `!` is the only unary operator that evaluates to a boolean.
25-
result = getBoolVal(e.(LogNotExpr).getExpr()).booleanNot()
25+
result = getBoolVal(e.(LogNotExpr).getOperand()).booleanNot()
2626
or
2727
// Handle binary expressions that have integer operands and a boolean result.
2828
exists(BinaryExpr b, int left, int right |
@@ -115,11 +115,11 @@ module CalculateConstants<getBoolValSig/1 getBoolVal, getIntValSig/1 getIntVal>
115115
else result = val
116116
)
117117
or
118-
result = getIntVal(e.(PlusExpr).getExpr())
118+
result = getIntVal(e.(PlusExpr).getOperand())
119119
or
120-
result = -getIntVal(e.(MinusExpr).getExpr())
120+
result = -getIntVal(e.(MinusExpr).getOperand())
121121
or
122-
result = getIntVal(e.(BitNotExpr).getExpr()).bitNot()
122+
result = getIntVal(e.(BitNotExpr).getOperand()).bitNot()
123123
or
124124
// No `int` value for `LogNotExpr`.
125125
exists(BinaryExpr b, int v1, int v2 |

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@ private module ControlFlowGraphImpl {
827827
index = 1 and result = e.getRightOperand()
828828
)
829829
or
830-
index = 0 and result = this.(UnaryExpr).getExpr()
830+
index = 0 and result = this.(UnaryExpr).getOperand()
831831
or
832832
index = 0 and result = this.(CastingExpr).getExpr()
833833
or
@@ -849,7 +849,7 @@ private module ControlFlowGraphImpl {
849849
or
850850
index = 0 and result = this.(ClassExpr).getExpr()
851851
or
852-
index = 0 and result = this.(ReturnStmt).getResult()
852+
index = 0 and result = this.(ReturnStmt).getExpr()
853853
or
854854
index = 0 and result = this.(ThrowStmt).getExpr()
855855
or
@@ -1044,7 +1044,7 @@ private module ControlFlowGraphImpl {
10441044
or
10451045
// The last node of a `LogNotExpr` is in its sub-expression with an inverted boolean completion
10461046
// (or a `normalCompletion`).
1047-
exists(Completion subcompletion | last(n.(LogNotExpr).getExpr(), last, subcompletion) |
1047+
exists(Completion subcompletion | last(n.(LogNotExpr).getOperand(), last, subcompletion) |
10481048
subcompletion = NormalCompletion() and
10491049
completion = NormalCompletion() and
10501050
not inBooleanContext(n)
@@ -1356,7 +1356,7 @@ private module ControlFlowGraphImpl {
13561356
(
13571357
result = first(n.asExpr().(AndLogicalExpr).getLeftOperand()) or
13581358
result = first(n.asExpr().(OrLogicalExpr).getLeftOperand()) or
1359-
result = first(n.asExpr().(LogNotExpr).getExpr()) or
1359+
result = first(n.asExpr().(LogNotExpr).getOperand()) or
13601360
result = first(n.asExpr().(ConditionalExpr).getCondition())
13611361
)
13621362
or
@@ -1427,7 +1427,7 @@ private module ControlFlowGraphImpl {
14271427
condentry = first(for.getCondition())
14281428
or
14291429
// ...or the body if the for doesn't include a condition.
1430-
not exists(for.getCondition()) and condentry = first(for.getStmt())
1430+
not exists(for.getCondition()) and condentry = first(for.getBody())
14311431
|
14321432
// From the entry point, which is the for statement itself, control goes to either the first init expression...
14331433
n.asStmt() = for and result = first(for.getInit(0)) and completion = NormalCompletion()
@@ -1448,7 +1448,7 @@ private module ControlFlowGraphImpl {
14481448
// The true-successor of the condition is the body of the for loop.
14491449
last(for.getCondition(), n, completion) and
14501450
completion = BooleanCompletion(true, _) and
1451-
result = first(for.getStmt())
1451+
result = first(for.getBody())
14521452
or
14531453
// The updates execute sequentially, after which control is transferred to the condition.
14541454
exists(int i | last(for.getUpdate(i), n, completion) and completion = NormalCompletion() |
@@ -1458,7 +1458,7 @@ private module ControlFlowGraphImpl {
14581458
)
14591459
or
14601460
// The back edge of the loop: control goes to either the first update or the condition if no updates exist.
1461-
last(for.getStmt(), n, completion) and
1461+
last(for.getBody(), n, completion) and
14621462
continues(completion, for) and
14631463
(
14641464
result = first(for.getUpdate(0))
@@ -1479,11 +1479,11 @@ private module ControlFlowGraphImpl {
14791479
or
14801480
// ...and then control goes to the body of the loop.
14811481
n.asExpr() = for.getVariable() and
1482-
result = first(for.getStmt()) and
1482+
result = first(for.getBody()) and
14831483
completion = NormalCompletion()
14841484
or
14851485
// Finally, the back edge of the loop goes to reassign the variable.
1486-
last(for.getStmt(), n, completion) and
1486+
last(for.getBody(), n, completion) and
14871487
continues(completion, for) and
14881488
result.asExpr() = for.getVariable()
14891489
)
@@ -1492,7 +1492,7 @@ private module ControlFlowGraphImpl {
14921492
result = first(n.asStmt().(WhileStmt).getCondition()) and completion = NormalCompletion()
14931493
or
14941494
// ...and do-while loops start at the body.
1495-
result = first(n.asStmt().(DoStmt).getStmt()) and completion = NormalCompletion()
1495+
result = first(n.asStmt().(DoStmt).getBody()) and completion = NormalCompletion()
14961496
or
14971497
exists(LoopStmt loop | loop instanceof WhileStmt or loop instanceof DoStmt |
14981498
// Control goes from the condition via a true-completion to the body...

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class AssignmentConversionContext extends ConversionSite {
6969
class ReturnConversionSite extends ConversionSite {
7070
ReturnStmt r;
7171

72-
ReturnConversionSite() { this = r.getResult() }
72+
ReturnConversionSite() { this = r.getExpr() }
7373

7474
override Type getConversionTarget() { result = r.getEnclosingCallable().getReturnType() }
7575

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

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class Expr extends ExprParent, @expr {
113113
if this instanceof CastingExpr or this instanceof NotNullExpr
114114
then
115115
result = this.(CastingExpr).getExpr().getUnderlyingExpr() or
116-
result = this.(NotNullExpr).getExpr().getUnderlyingExpr()
116+
result = this.(NotNullExpr).getOperand().getUnderlyingExpr()
117117
else result = this
118118
}
119119
}
@@ -144,13 +144,13 @@ class CompileTimeConstantExpr extends Expr {
144144
this.(CastingExpr).getExpr().isCompileTimeConstant()
145145
or
146146
// The unary operators `+`, `-`, `~`, and `!` (but not `++` or `--`).
147-
this.(PlusExpr).getExpr().isCompileTimeConstant()
147+
this.(PlusExpr).getOperand().isCompileTimeConstant()
148148
or
149-
this.(MinusExpr).getExpr().isCompileTimeConstant()
149+
this.(MinusExpr).getOperand().isCompileTimeConstant()
150150
or
151-
this.(BitNotExpr).getExpr().isCompileTimeConstant()
151+
this.(BitNotExpr).getOperand().isCompileTimeConstant()
152152
or
153-
this.(LogNotExpr).getExpr().isCompileTimeConstant()
153+
this.(LogNotExpr).getOperand().isCompileTimeConstant()
154154
or
155155
// The multiplicative operators `*`, `/`, and `%`,
156156
// the additive operators `+` and `-`,
@@ -166,8 +166,8 @@ class CompileTimeConstantExpr extends Expr {
166166
// The ternary conditional operator ` ? : `.
167167
exists(ConditionalExpr e | this = e |
168168
e.getCondition().isCompileTimeConstant() and
169-
e.getTrueExpr().isCompileTimeConstant() and
170-
e.getFalseExpr().isCompileTimeConstant()
169+
e.getThen().isCompileTimeConstant() and
170+
e.getElse().isCompileTimeConstant()
171171
)
172172
or
173173
// Access to a final variable initialized by a compile-time constant.
@@ -943,7 +943,7 @@ class LogicExpr extends Expr {
943943
/** Gets an operand of this logical expression. */
944944
Expr getAnOperand() {
945945
this.(BinaryExpr).getAnOperand() = result or
946-
this.(UnaryExpr).getExpr() = result
946+
this.(UnaryExpr).getOperand() = result
947947
}
948948
}
949949

@@ -1039,8 +1039,15 @@ class ReferenceEqualityTest extends EqualityTest {
10391039

10401040
/** A common super-class that represents unary operator expressions. */
10411041
class UnaryExpr extends Expr, @unaryexpr {
1042+
/**
1043+
* DEPRECATED: Use `getOperand()` instead.
1044+
*
1045+
* Gets the operand expression.
1046+
*/
1047+
deprecated Expr getExpr() { result.getParent() = this }
1048+
10421049
/** Gets the operand expression. */
1043-
Expr getExpr() { result.getParent() = this }
1050+
Expr getOperand() { result.getParent() = this }
10441051
}
10451052

10461053
/**
@@ -1305,7 +1312,7 @@ class LambdaExpr extends FunctionalExpr, @lambdaexpr {
13051312

13061313
/** Gets the body of this lambda expression, if it is an expression. */
13071314
Expr getExprBody() {
1308-
this.hasExprBody() and result = this.asMethod().getBody().getAChild().(ReturnStmt).getResult()
1315+
this.hasExprBody() and result = this.asMethod().getBody().getAChild().(ReturnStmt).getExpr()
13091316
}
13101317

13111318
/** Gets the body of this lambda expression, if it is a statement. */
@@ -1340,7 +1347,7 @@ class MemberRefExpr extends FunctionalExpr, @memberref {
13401347
exists(Stmt stmt |
13411348
stmt = this.asMethod().getBody().(SingletonBlock).getStmt() and
13421349
(
1343-
result = stmt.(ReturnStmt).getResult()
1350+
result = stmt.(ReturnStmt).getExpr()
13441351
or
13451352
// Note: Currently never an ExprStmt, but might change once https://github.com/github/codeql/issues/3605 is fixed
13461353
result = stmt.(ExprStmt).getExpr()
@@ -1456,27 +1463,43 @@ class ConditionalExpr extends Expr, @conditionalexpr {
14561463
/** Gets the condition of this conditional expression. */
14571464
Expr getCondition() { result.isNthChildOf(this, 0) }
14581465

1466+
/**
1467+
* DEPRECATED: Use `getThen()` instead.
1468+
*
1469+
* Gets the expression that is evaluated if the condition of this
1470+
* conditional expression evaluates to `true`.
1471+
*/
1472+
deprecated Expr getTrueExpr() { result.isNthChildOf(this, 1) }
1473+
1474+
/**
1475+
* DEPRECATED: Use `getElse()` instead.
1476+
*
1477+
* Gets the expression that is evaluated if the condition of this
1478+
* conditional expression evaluates to `false`.
1479+
*/
1480+
deprecated Expr getFalseExpr() { result.isNthChildOf(this, 2) }
1481+
14591482
/**
14601483
* Gets the expression that is evaluated if the condition of this
14611484
* conditional expression evaluates to `true`.
14621485
*/
1463-
Expr getTrueExpr() { result.isNthChildOf(this, 1) }
1486+
Expr getThen() { result.isNthChildOf(this, 1) }
14641487

14651488
/**
14661489
* Gets the expression that is evaluated if the condition of this
14671490
* conditional expression evaluates to `false`.
14681491
*/
1469-
Expr getFalseExpr() { result.isNthChildOf(this, 2) }
1492+
Expr getElse() { result.isNthChildOf(this, 2) }
14701493

14711494
/**
14721495
* Gets the expression that is evaluated by the specific branch of this
1473-
* conditional expression. If `true` that is `getTrueExpr()`, if `false`
1474-
* it is `getFalseExpr()`.
1496+
* conditional expression. If `true` that is `getThen()`, if `false`
1497+
* it is `getElse()`.
14751498
*/
14761499
Expr getBranchExpr(boolean branch) {
1477-
branch = true and result = this.getTrueExpr()
1500+
branch = true and result = this.getThen()
14781501
or
1479-
branch = false and result = this.getFalseExpr()
1502+
branch = false and result = this.getElse()
14801503
}
14811504

14821505
/**
@@ -1773,14 +1796,14 @@ class VariableUpdate extends Expr {
17731796
VariableUpdate() {
17741797
this.(Assignment).getDest() instanceof VarAccess or
17751798
this instanceof LocalVariableDeclExpr or
1776-
this.(UnaryAssignExpr).getExpr() instanceof VarAccess
1799+
this.(UnaryAssignExpr).getOperand() instanceof VarAccess
17771800
}
17781801

17791802
/** Gets the destination of this variable update. */
17801803
Variable getDestVar() {
17811804
result.getAnAccess() = this.(Assignment).getDest() or
17821805
result = this.(LocalVariableDeclExpr).getVariable() or
1783-
result.getAnAccess() = this.(UnaryAssignExpr).getExpr()
1806+
result.getAnAccess() = this.(UnaryAssignExpr).getOperand()
17841807
}
17851808
}
17861809

@@ -1970,7 +1993,7 @@ class VarAccess extends Expr, @varaccess {
19701993
*/
19711994
predicate isVarWrite() {
19721995
exists(Assignment a | a.getDest() = this) or
1973-
exists(UnaryAssignExpr e | e.getExpr() = this)
1996+
exists(UnaryAssignExpr e | e.getOperand() = this)
19741997
}
19751998

19761999
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -680,13 +680,13 @@ class GetterMethod extends Method {
680680
GetterMethod() {
681681
this.hasNoParameters() and
682682
exists(ReturnStmt s, Field f | s = this.getBody().(SingletonBlock).getStmt() |
683-
s.getResult() = f.getAnAccess()
683+
s.getExpr() = f.getAnAccess()
684684
)
685685
}
686686

687687
/** Gets the field whose value is returned by this getter method. */
688688
Field getField() {
689-
exists(ReturnStmt r | r.getEnclosingCallable() = this | r.getResult() = result.getAnAccess())
689+
exists(ReturnStmt r | r.getEnclosingCallable() = this | r.getExpr() = result.getAnAccess())
690690
}
691691
}
692692

0 commit comments

Comments
 (0)