Skip to content

Commit 4bd332d

Browse files
committed
Java: Add Expr.isParenthesized, adjust VarAccess.toString, and fix tests.
1 parent 597d8e7 commit 4bd332d

File tree

5 files changed

+14
-6
lines changed

5 files changed

+14
-6
lines changed

java/ql/src/Complexity/ComplexCondition.ql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import java
1313

1414
predicate nontrivialLogicalOperator(BinaryExpr e) {
1515
e instanceof LogicExpr and
16-
not e.getParent().(Expr).getKind() = e.getKind()
16+
(
17+
not e.getParent().(Expr).getKind() = e.getKind() or
18+
e.isParenthesized()
19+
)
1720
}
1821

1922
Expr getSimpleParent(Expr e) {

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ class Expr extends ExprParent, @expr {
9595
or
9696
exists(LambdaExpr lam | lam.asMethod() = getEnclosingCallable() and lam.isInStaticContext())
9797
}
98+
99+
/** Holds if this expression is parenthesized. */
100+
predicate isParenthesized() { isParenthesized(this, _) }
98101
}
99102

100103
/**
@@ -1330,7 +1333,11 @@ class VarAccess extends Expr, @varaccess {
13301333

13311334
/** Gets a printable representation of this expression. */
13321335
override string toString() {
1333-
result = this.getQualifier().toString() + "." + this.getVariable().getName()
1336+
exists(Expr q | q = this.getQualifier() |
1337+
if q.isParenthesized()
1338+
then result = "(...)." + this.getVariable().getName()
1339+
else result = q.toString() + "." + this.getVariable().getName()
1340+
)
13341341
or
13351342
not this.hasQualifier() and result = this.getVariable().getName()
13361343
}

java/ql/test/library-tests/constants/CompileTimeConstantExpr.expected

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
| constants/Constants.java:15:15:15:37 | ... + ... |
88
| constants/Constants.java:15:37:15:37 | 1 |
99
| constants/Constants.java:16:23:16:41 | Initializers.SFIELD |
10-
| constants/Constants.java:18:15:18:18 | (...) |
1110
| constants/Constants.java:18:16:18:17 | 12 |
1211
| constants/Constants.java:19:19:19:28 | "a string" |
13-
| constants/Constants.java:20:17:20:23 | (...) |
1412
| constants/Constants.java:20:17:20:31 | ...?...:... |
1513
| constants/Constants.java:20:18:20:18 | 3 |
1614
| constants/Constants.java:20:18:20:22 | ... < ... |

java/ql/test/library-tests/constants/getIntValue.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
| constants/Values.java:67:27:67:33 | ... & ... | 42 |
3232
| constants/Values.java:68:26:68:32 | ... \| ... | 42 |
3333
| constants/Values.java:69:27:69:33 | ... ^ ... | 42 |
34-
| constants/Values.java:83:19:83:22 | (...) | 42 |
34+
| constants/Values.java:83:20:83:21 | 42 | 42 |
3535
| constants/Values.java:86:25:86:35 | final_field | 42 |
3636
| constants/Values.java:87:33:87:34 | 42 | 42 |
3737
| constants/Values.java:88:25:88:35 | final_local | 42 |
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
| ExprDeref.java:7:12:7:34 | (...) | This expression is dereferenced and may be null. |
1+
| ExprDeref.java:7:13:7:33 | ...?...:... | This expression is dereferenced and may be null. |

0 commit comments

Comments
 (0)