Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9a367d9

Browse files
aschackmullyh-semmle
authored andcommitted
Java: JumpStmt.getTarget, Stmt.getEnclosingStmt, SwitchExpr.getAResult.
1 parent 61324f0 commit 9a367d9

2 files changed

Lines changed: 46 additions & 20 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,15 @@ deprecated class SwitchExpr extends Expr, @switchexpr {
11221122

11231123
/** Gets the expression of this `switch` expression. */
11241124
Expr getExpr() { result.getParent() = this }
1125+
1126+
/** Gets a result expression of this `switch` expression. */
1127+
deprecated Expr getAResult() {
1128+
result = getACase().getRuleExpression()
1129+
or
1130+
exists(BreakStmt break |
1131+
break.(JumpStmt).getTarget() = this and result = break.getValue()
1132+
)
1133+
}
11251134
}
11261135

11271136
/** A parenthesised expression. */

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

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ class Stmt extends StmtParent, ExprParent, @stmt {
2424
/** Gets the parent of this statement. */
2525
StmtParent getParent() { stmts(this, _, result, _, _) }
2626

27+
/**
28+
* DEPRECATED: Preview feature in Java 12. Subject to removal in a future release.
29+
*
30+
* Gets the statement containing this statement, if any.
31+
*/
32+
deprecated Stmt getEnclosingStmt() {
33+
result = this.getParent() or
34+
result = this.getParent().(SwitchExpr).getEnclosingStmt()
35+
}
36+
2737
/** Holds if this statement is the child of the specified parent at the specified (zero-based) position. */
2838
predicate isNthChildOf(StmtParent parent, int index) {
2939
this.getParent() = parent and this.getIndex() = index
@@ -425,35 +435,35 @@ class SwitchCase extends Stmt, @case {
425435
or
426436
exists(Stmt s | s.getParent() = this | s.getIndex() = -1)
427437
}
428-
}
429-
430-
/** A constant `case` of a switch statement. */
431-
class ConstCase extends SwitchCase {
432-
ConstCase() { exists(Expr e | e.getParent() = this | e.getIndex() >= 0) }
433-
434-
/** Gets the `case` constant at index 0. */
435-
Expr getValue() { result.getParent() = this and result.getIndex() = 0 }
436438

437439
/**
438440
* DEPRECATED: Preview feature in Java 12. Subject to removal in a future release.
439441
*
440-
* Gets the `case` constant at the specified index.
442+
* Gets the expression on the right-hand side of the arrow, if any.
441443
*/
442-
deprecated Expr getValue(int i) { result.getParent() = this and result.getIndex() = i and i >= 0 }
444+
deprecated Expr getRuleExpression() { result.getParent() = this and result.getIndex() = -1 }
443445

444446
/**
445447
* DEPRECATED: Preview feature in Java 12. Subject to removal in a future release.
446448
*
447-
* Gets the expression on the right-hand side of the arrow, if any.
449+
* Gets the statement on the right-hand side of the arrow, if any.
448450
*/
449-
deprecated Expr getRuleExpression() { result.getParent() = this and result.getIndex() = -1 }
451+
deprecated Stmt getRuleStatement() { result.getParent() = this and result.getIndex() = -1 }
452+
}
453+
454+
/** A constant `case` of a switch statement. */
455+
class ConstCase extends SwitchCase {
456+
ConstCase() { exists(Expr e | e.getParent() = this | e.getIndex() >= 0) }
457+
458+
/** Gets the `case` constant at index 0. */
459+
Expr getValue() { result.getParent() = this and result.getIndex() = 0 }
450460

451461
/**
452462
* DEPRECATED: Preview feature in Java 12. Subject to removal in a future release.
453463
*
454-
* Gets the statement on the right-hand side of the arrow, if any.
464+
* Gets the `case` constant at the specified index.
455465
*/
456-
deprecated Stmt getRuleStatement() { result.getParent() = this and result.getIndex() = -1 }
466+
deprecated Expr getValue(int i) { result.getParent() = this and result.getIndex() = i and i >= 0 }
457467

458468
/** Gets a printable representation of this statement. May include more detail than `toString()`. */
459469
override string pp() { result = "case ..." }
@@ -554,30 +564,37 @@ class JumpStmt extends Stmt {
554564
* `continue` statement refers to, if any.
555565
*/
556566
LabeledStmt getTargetLabel() {
557-
this.getParent+() = result and
567+
this.getEnclosingStmt+() = result and
558568
namestrings(result.getLabel(), _, this)
559569
}
560570

561571
private Stmt getLabelTarget() { result = getTargetLabel().getStmt() }
562572

563573
private Stmt getAPotentialTarget() {
564-
this.getParent+() = result and
574+
this.getEnclosingStmt+() = result and
565575
(
566576
result instanceof LoopStmt
567577
or
568578
this instanceof BreakStmt and result instanceof SwitchStmt
569579
)
570580
}
571581

572-
private Stmt getEnclosingTarget() {
582+
private SwitchExpr getSwitchExprTarget() {
583+
this.(BreakStmt).hasValue() and result = this.getParent+()
584+
}
585+
586+
private StmtParent getEnclosingTarget() {
587+
result = getSwitchExprTarget()
588+
or
589+
not exists(getSwitchExprTarget()) and
573590
result = getAPotentialTarget() and
574591
not exists(Stmt other | other = getAPotentialTarget() | other.getParent+() = result)
575592
}
576593

577594
/**
578-
* Gets the statement that this `break` or `continue` jumps to.
595+
* Gets the statement or `switch` expression that this `break` or `continue` jumps to.
579596
*/
580-
Stmt getTarget() {
597+
StmtParent getTarget() {
581598
result = getLabelTarget()
582599
or
583600
not exists(getLabelTarget()) and result = getEnclosingTarget()
@@ -610,7 +627,7 @@ class BreakStmt extends Stmt, @breakstmt {
610627
override string pp() {
611628
if this.hasLabel()
612629
then result = "break " + this.getLabel()
613-
else (if this.hasValue() then result = "break ..." else result = "break")
630+
else if this.hasValue() then result = "break ..." else result = "break"
614631
}
615632

616633
/** This statement's Halstead ID (used to compute Halstead metrics). */

0 commit comments

Comments
 (0)