Skip to content

Commit 0aad24e

Browse files
committed
Java: Extend library support for switch expressions.
1 parent 8ce9c9d commit 0aad24e

File tree

15 files changed

+77
-31
lines changed

15 files changed

+77
-31
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,8 @@ private module ControlFlowGraphImpl {
579579
n instanceof Stmt and
580580
not n instanceof PostOrderNode and
581581
not n instanceof SynchronizedStmt
582+
or
583+
result = n and n instanceof SwitchExpr
582584
}
583585

584586
/**

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,18 @@ class MemberRefExpr extends FunctionalExpr, @memberref {
10511051
override string toString() { result = "...::..." }
10521052
}
10531053

1054+
/** A conditional expression or a `switch` expression. */
1055+
class ChooseExpr extends Expr {
1056+
ChooseExpr() { this instanceof ConditionalExpr or this instanceof SwitchExpr }
1057+
1058+
/** Gets a result expression of this `switch` or conditional expression. */
1059+
Expr getAResultExpr() {
1060+
result = this.(ConditionalExpr).getTrueExpr() or
1061+
result = this.(ConditionalExpr).getFalseExpr() or
1062+
result = this.(SwitchExpr).getAResult()
1063+
}
1064+
}
1065+
10541066
/**
10551067
* A conditional expression of the form `a ? b : c`, where `a` is the condition,
10561068
* `b` is the expression that is evaluated if the condition evaluates to `true`,

java/ql/src/semmle/code/java/StringFormat.qll

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,7 @@ private predicate formatStringFragment(Expr fmt) {
248248
e.(VarAccess).getVariable().getAnAssignedValue() = fmt or
249249
e.(AddExpr).getLeftOperand() = fmt or
250250
e.(AddExpr).getRightOperand() = fmt or
251-
e.(ConditionalExpr).getTrueExpr() = fmt or
252-
e.(ConditionalExpr).getFalseExpr() = fmt
251+
e.(ChooseExpr).getAResultExpr() = fmt
253252
)
254253
}
255254

@@ -293,9 +292,7 @@ private predicate formatStringValue(Expr e, string fmtvalue) {
293292
fmtvalue = left + right
294293
)
295294
or
296-
formatStringValue(e.(ConditionalExpr).getTrueExpr(), fmtvalue)
297-
or
298-
formatStringValue(e.(ConditionalExpr).getFalseExpr(), fmtvalue)
295+
formatStringValue(e.(ChooseExpr).getAResultExpr(), fmtvalue)
299296
or
300297
exists(Method getprop, MethodAccess ma, string prop |
301298
e = ma and

java/ql/src/semmle/code/java/controlflow/internal/GuardsLogic.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ private predicate hasPossibleUnknownValue(SsaVariable v) {
213213

214214
/**
215215
* Gets a sub-expression of `e` whose value can flow to `e` through
216-
* `ConditionalExpr`s. Parentheses are also removed.
216+
* `ConditionalExpr`s.
217217
*/
218218
private Expr possibleValue(Expr e) {
219219
result = possibleValue(e.(ConditionalExpr).getTrueExpr())

java/ql/src/semmle/code/java/dataflow/IntegerGuards.qll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ private import RangeAnalysis
1010
/** Gets an expression that might have the value `i`. */
1111
private Expr exprWithIntValue(int i) {
1212
result.(ConstantIntegerExpr).getIntValue() = i or
13-
result.(ConditionalExpr).getTrueExpr() = exprWithIntValue(i) or
14-
result.(ConditionalExpr).getFalseExpr() = exprWithIntValue(i)
13+
result.(ChooseExpr).getAResultExpr() = exprWithIntValue(i)
1514
}
1615

1716
/**

java/ql/src/semmle/code/java/dataflow/Nullness.qll

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ private import semmle.code.java.frameworks.Assertions
4545
/** Gets an expression that may be `null`. */
4646
Expr nullExpr() {
4747
result instanceof NullLiteral or
48-
result.(ConditionalExpr).getTrueExpr() = nullExpr() or
49-
result.(ConditionalExpr).getFalseExpr() = nullExpr() or
48+
result.(ChooseExpr).getAResultExpr() = nullExpr() or
5049
result.(AssignExpr).getSource() = nullExpr() or
5150
result.(CastExpr).getExpr() = nullExpr()
5251
}
@@ -81,9 +80,7 @@ private predicate unboxed(Expr e) {
8180
or
8281
exists(UnaryExpr un | un.getExpr() = e)
8382
or
84-
exists(ConditionalExpr cond | cond.getType() instanceof PrimitiveType |
85-
cond.getTrueExpr() = e or cond.getFalseExpr() = e
86-
)
83+
exists(ChooseExpr cond | cond.getType() instanceof PrimitiveType | cond.getAResultExpr() = e)
8784
or
8885
exists(ConditionNode cond | cond.getCondition() = e)
8986
or

java/ql/src/semmle/code/java/dataflow/SignAnalysis.qll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -552,9 +552,7 @@ private Sign exprSign(Expr e) {
552552
result = s1.urshift(s2)
553553
)
554554
or
555-
result = exprSign(e.(ConditionalExpr).getTrueExpr())
556-
or
557-
result = exprSign(e.(ConditionalExpr).getFalseExpr())
555+
result = exprSign(e.(ChooseExpr).getAResultExpr())
558556
or
559557
result = exprSign(e.(CastExpr).getExpr())
560558
)

java/ql/src/semmle/code/java/dataflow/TypeFlow.qll

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ private predicate privateParamArg(Parameter p, Argument arg) {
7272
* necessarily functionally determined by `n2`.
7373
*/
7474
private predicate joinStep0(TypeFlowNode n1, TypeFlowNode n2) {
75-
n2.asExpr().(ConditionalExpr).getTrueExpr() = n1.asExpr()
76-
or
77-
n2.asExpr().(ConditionalExpr).getFalseExpr() = n1.asExpr()
75+
n2.asExpr().(ChooseExpr).getAResultExpr() = n1.asExpr()
7876
or
7977
exists(Field f, Expr e |
8078
f = n2.asField() and
@@ -226,9 +224,8 @@ private predicate upcastCand(TypeFlowNode n, RefType t, RefType t1, RefType t2)
226224
or
227225
exists(Parameter p | privateParamArg(p, n.asExpr()) and t2 = p.getType().getErasure())
228226
or
229-
exists(ConditionalExpr cond |
230-
cond.getTrueExpr() = n.asExpr() or cond.getFalseExpr() = n.asExpr()
231-
|
227+
exists(ChooseExpr cond |
228+
cond.getAResultExpr() = n.asExpr() and
232229
t2 = cond.getType().getErasure()
233230
)
234231
)

java/ql/src/semmle/code/java/dataflow/internal/DataFlowUtil.qll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,7 @@ predicate simpleLocalFlowStep(Node node1, Node node2) {
397397
or
398398
node2.asExpr().(CastExpr).getExpr() = node1.asExpr()
399399
or
400-
node2.asExpr().(ConditionalExpr).getTrueExpr() = node1.asExpr()
401-
or
402-
node2.asExpr().(ConditionalExpr).getFalseExpr() = node1.asExpr()
400+
node2.asExpr().(ChooseExpr).getAResultExpr() = node1.asExpr()
403401
or
404402
node2.asExpr().(AssignExpr).getSource() = node1.asExpr()
405403
}

java/ql/src/semmle/code/java/dispatch/DispatchFlow.qll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,7 @@ private predicate flowStep(RelevantNode n1, RelevantNode n2) {
202202
or
203203
n2.asExpr().(CastExpr).getExpr() = n1.asExpr()
204204
or
205-
n2.asExpr().(ConditionalExpr).getTrueExpr() = n1.asExpr()
206-
or
207-
n2.asExpr().(ConditionalExpr).getFalseExpr() = n1.asExpr()
205+
n2.asExpr().(ChooseExpr).getAResultExpr() = n1.asExpr()
208206
or
209207
n2.asExpr().(AssignExpr).getSource() = n1.asExpr()
210208
or

0 commit comments

Comments
 (0)