Skip to content

Commit 95d138b

Browse files
authored
Merge pull request #2659 from aschackmull/java/remove-parexpr
Java: Update dbscheme for ParExpr removal.
2 parents 24a50fc + 0b3c90b commit 95d138b

File tree

19 files changed

+2154
-12
lines changed

19 files changed

+2154
-12
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/Likely Bugs/Arithmetic/MultiplyRemainder.ql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212

1313
import java
1414

15-
from MulExpr e
15+
from MulExpr e, RemExpr lhs
1616
where
17-
e.getLeftOperand() instanceof RemExpr and
17+
e.getLeftOperand() = lhs and
18+
not lhs.isParenthesized() and
1819
e.getRightOperand().getType().hasName("int")
1920
select e, "Result of a remainder operation multiplied by an integer."

java/ql/src/Likely Bugs/Arithmetic/WhitespaceContradictsPrecedence.ql

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,20 +120,33 @@ predicate endOfBinaryLhs(BinaryExpr expr, int line, int col) {
120120
)
121121
}
122122

123+
/** Compute the number of parenthesis characters next to the operator. */
124+
int getParensNextToOp(BinaryExpr expr) {
125+
exists(Expr left, Expr right, int pleft, int pright |
126+
left = expr.getLeftOperand() and
127+
right = expr.getRightOperand() and
128+
(if left.isParenthesized() then isParenthesized(left, pleft) else pleft = 0) and
129+
(if right.isParenthesized() then isParenthesized(right, pright) else pright = 0) and
130+
result = pleft + pright
131+
)
132+
}
133+
123134
/** Compute whitespace around the operator. */
124135
int operatorWS(BinaryExpr expr) {
125-
exists(int line, int lcol, int rcol |
136+
exists(int line, int lcol, int rcol, int parens |
126137
endOfBinaryLhs(expr, line, lcol) and
127138
startOfBinaryRhs(expr, line, rcol) and
128-
result = rcol - lcol + 1 - expr.getOp().length()
139+
parens = getParensNextToOp(expr) and
140+
result = rcol - lcol + 1 - expr.getOp().length() - parens
129141
)
130142
}
131143

132144
/** Find nested binary expressions where the programmer may have made a precedence mistake. */
133145
predicate interestingNesting(BinaryExpr inner, BinaryExpr outer) {
134146
inner = outer.getAChildExpr() and
135147
not inner instanceof AssocNestedExpr and
136-
not inner instanceof HarmlessNestedExpr
148+
not inner instanceof HarmlessNestedExpr and
149+
not inner.isParenthesized()
137150
}
138151

139152
from BinaryExpr inner, BinaryExpr outer, int wsouter, int wsinner

java/ql/src/config/semmlecode.dbscheme

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,11 @@ statementEnclosingExpr(
516516
int statement_id: @stmt ref
517517
);
518518

519+
isParenthesized(
520+
unique int id: @expr ref,
521+
int parentheses: int ref
522+
);
523+
519524
case @expr.kind of
520525
1 = @arrayaccess
521526
| 2 = @arraycreationexpr
@@ -570,7 +575,7 @@ case @expr.kind of
570575
| 51 = @castexpr
571576
| 52 = @newexpr
572577
| 53 = @conditionalexpr
573-
| 54 = @parexpr
578+
| 54 = @parexpr // deprecated
574579
| 55 = @instanceofexpr
575580
| 56 = @localvariabledeclexpr
576581
| 57 = @typeliteral

java/ql/src/config/semmlecode.dbscheme.stats

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18307,6 +18307,59 @@
1830718307
</dependencies>
1830818308
</relation>
1830918309
<relation>
18310+
<name>isParenthesized</name>
18311+
<cardinality>76010</cardinality>
18312+
<columnsizes>
18313+
<e>
18314+
<k>id</k>
18315+
<v>76010</v>
18316+
</e>
18317+
<e>
18318+
<k>parentheses</k>
18319+
<v>2</v>
18320+
</e>
18321+
</columnsizes>
18322+
<dependencies>
18323+
<dep>
18324+
<src>id</src>
18325+
<trg>parentheses</trg>
18326+
<val>
18327+
<hist>
18328+
<budget>12</budget>
18329+
<bs>
18330+
<b>
18331+
<a>1</a>
18332+
<b>2</b>
18333+
<v>76010</v>
18334+
</b>
18335+
</bs>
18336+
</hist>
18337+
</val>
18338+
</dep>
18339+
<dep>
18340+
<src>parentheses</src>
18341+
<trg>id</trg>
18342+
<val>
18343+
<hist>
18344+
<budget>12</budget>
18345+
<bs>
18346+
<b>
18347+
<a>30</a>
18348+
<b>31</b>
18349+
<v>1</v>
18350+
</b>
18351+
<b>
18352+
<a>75980</a>
18353+
<b>75981</b>
18354+
<v>1</v>
18355+
</b>
18356+
</bs>
18357+
</hist>
18358+
</val>
18359+
</dep>
18360+
</dependencies>
18361+
</relation>
18362+
<relation>
1831018363
<name>callableBinding</name>
1831118364
<cardinality>1437798</cardinality>
1831218365
<columnsizes>

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. |
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Annotation extends @annotation {
2+
string toString() { result = "annotation" }
3+
}
4+
5+
class Method extends @method {
6+
string toString() { result = "method" }
7+
}
8+
9+
class Expr extends @expr {
10+
string toString() { result = "expr" }
11+
}
12+
13+
class ParExpr extends Expr, @parexpr {
14+
override string toString() { result = "(...)" }
15+
}
16+
17+
predicate parExprGetExpr(ParExpr pe, Expr e) { exprs(e, _, _, pe, _) }
18+
19+
from Annotation parentid, Method id2, Expr oldvalue, Expr value
20+
where
21+
annotValue(parentid, id2, oldvalue) and
22+
if oldvalue instanceof ParExpr
23+
then
24+
parExprGetExpr+(oldvalue, value) and
25+
not value instanceof ParExpr
26+
else value = oldvalue
27+
select parentid, id2, value

0 commit comments

Comments
 (0)