Skip to content

Commit 24dc09e

Browse files
authored
Merge pull request #14206 from smowton/smowton/feature/add-java-miscompilation-tests
Java: add tests for programs that don't compile
2 parents 6c7833f + c5001a8 commit 24dc09e

File tree

32 files changed

+152
-4
lines changed

32 files changed

+152
-4
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,8 @@ private module ControlFlowGraphImpl {
477477
or
478478
this instanceof Call // includes both expressions and statements
479479
or
480+
this instanceof ErrorExpr
481+
or
480482
this instanceof ReturnStmt
481483
or
482484
this instanceof ThrowStmt

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,8 @@ class ExprParent extends @exprparent, Top { }
276276
* An error expression.
277277
*
278278
* These may be generated by upgrade or downgrade scripts when databases
279-
* cannot be fully converted.
279+
* cannot be fully converted, or generated by the extractor when extracting
280+
* source code containing errors.
280281
*/
281282
class ErrorExpr extends Expr, @errorexpr {
282283
override string toString() { result = "<error expr>" }
@@ -1243,7 +1244,11 @@ class ClassInstanceExpr extends Expr, ConstructorCall, @classinstancexpr {
12431244
override Stmt getEnclosingStmt() { result = Expr.super.getEnclosingStmt() }
12441245

12451246
/** Gets a printable representation of this expression. */
1246-
override string toString() { result = "new " + this.getConstructor().getName() + "(...)" }
1247+
override string toString() {
1248+
result = "new " + this.getConstructor().getName() + "(...)"
1249+
or
1250+
not exists(this.getConstructor()) and result = "<ClassInstanceExpr that calls a missing constructor>"
1251+
}
12471252

12481253
override string getAPrimaryQlClass() { result = "ClassInstanceExpr" }
12491254
}

java/ql/test/kotlin/library-tests/exprs/CONSISTENCY/cfgDeadEnds.expected

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Test.java:
2+
# 0| [CompilationUnit] Test
3+
# 1| 1: [Class] Test
4+
# 3| 2: [Method] yield
5+
# 3| 3: [TypeAccess] int
6+
#-----| 4: (Parameters)
7+
# 3| 0: [Parameter] x
8+
# 3| 0: [TypeAccess] int
9+
# 3| 5: [BlockStmt] { ... }
10+
# 3| 0: [ReturnStmt] return ...
11+
# 3| 0: [VarAccess] x
12+
# 5| 3: [Method] secondCall
13+
# 5| 3: [TypeAccess] void
14+
# 5| 5: [BlockStmt] { ... }
15+
# 7| 4: [Method] yieldWrapper
16+
# 7| 3: [TypeAccess] int
17+
#-----| 4: (Parameters)
18+
# 7| 0: [Parameter] x
19+
# 7| 0: [TypeAccess] int
20+
# 7| 5: [BlockStmt] { ... }
21+
# 8| 0: [LocalVariableDeclStmt] var ...;
22+
# 8| 0: [TypeAccess] int
23+
# 8| 1: [LocalVariableDeclExpr] ret
24+
# 8| 0: [ErrorExpr] <error expr>
25+
# 8| 0: [MethodAccess] yield(...)
26+
# 8| 0: [VarAccess] x
27+
# 9| 1: [ExprStmt] <Expr>;
28+
# 9| 0: [MethodAccess] secondCall(...)
29+
# 10| 2: [ReturnStmt] return ...
30+
# 10| 0: [VarAccess] ret
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
semmle/code/java/PrintAst.ql
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class Test {
2+
3+
public int yield(int x) { return x; }
4+
5+
public void secondCall() { }
6+
7+
public int yieldWrapper(int x) {
8+
int ret = yield(x);
9+
secondCall();
10+
return ret;
11+
}
12+
13+
}
14+
15+
// Diagnostic Matches: Erroneous node in tree: (ERROR)
16+
// Diagnostic Matches: In file Test.java:8:15 no end location for JCMethodInvocation : yield(x)
17+
// Diagnostic Matches: 1 errors during annotation processing
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//semmle-extractor-options: --no-strict-javac-errors --javac-args -source 17 -target 17
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| file://:0:0:0:0 | 1 errors during annotation processing |
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import java
2+
import semmle.code.java.Diagnostics
3+
4+
select any(Diagnostic d | not d.toString().matches("Not rewriting trap file for%"))

java/ql/test/library-tests/errortype-with-params/ErrorTypes.expected

Whitespace-only changes.

0 commit comments

Comments
 (0)