mirror of
https://github.com/github/codeql.git
synced 2026-04-24 00:05:14 +02:00
Merge pull request #14206 from smowton/smowton/feature/add-java-miscompilation-tests
Java: add tests for programs that don't compile
This commit is contained in:
@@ -477,6 +477,8 @@ private module ControlFlowGraphImpl {
|
||||
or
|
||||
this instanceof Call // includes both expressions and statements
|
||||
or
|
||||
this instanceof ErrorExpr
|
||||
or
|
||||
this instanceof ReturnStmt
|
||||
or
|
||||
this instanceof ThrowStmt
|
||||
|
||||
@@ -276,7 +276,8 @@ class ExprParent extends @exprparent, Top { }
|
||||
* An error expression.
|
||||
*
|
||||
* These may be generated by upgrade or downgrade scripts when databases
|
||||
* cannot be fully converted.
|
||||
* cannot be fully converted, or generated by the extractor when extracting
|
||||
* source code containing errors.
|
||||
*/
|
||||
class ErrorExpr extends Expr, @errorexpr {
|
||||
override string toString() { result = "<error expr>" }
|
||||
@@ -1243,7 +1244,11 @@ class ClassInstanceExpr extends Expr, ConstructorCall, @classinstancexpr {
|
||||
override Stmt getEnclosingStmt() { result = Expr.super.getEnclosingStmt() }
|
||||
|
||||
/** Gets a printable representation of this expression. */
|
||||
override string toString() { result = "new " + this.getConstructor().getName() + "(...)" }
|
||||
override string toString() {
|
||||
result = "new " + this.getConstructor().getName() + "(...)"
|
||||
or
|
||||
not exists(this.getConstructor()) and result = "<ClassInstanceExpr that calls a missing constructor>"
|
||||
}
|
||||
|
||||
override string getAPrimaryQlClass() { result = "ClassInstanceExpr" }
|
||||
}
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
| exprs.kt:278:52:278:66 | <error expr> | ErrorExpr | unexpected dead end |
|
||||
| exprs.kt:278:52:278:66 | { ... } | BlockStmt | unexpected dead end |
|
||||
30
java/ql/test/library-tests/errorexpr/PrintAst.expected
Normal file
30
java/ql/test/library-tests/errorexpr/PrintAst.expected
Normal file
@@ -0,0 +1,30 @@
|
||||
Test.java:
|
||||
# 0| [CompilationUnit] Test
|
||||
# 1| 1: [Class] Test
|
||||
# 3| 2: [Method] yield
|
||||
# 3| 3: [TypeAccess] int
|
||||
#-----| 4: (Parameters)
|
||||
# 3| 0: [Parameter] x
|
||||
# 3| 0: [TypeAccess] int
|
||||
# 3| 5: [BlockStmt] { ... }
|
||||
# 3| 0: [ReturnStmt] return ...
|
||||
# 3| 0: [VarAccess] x
|
||||
# 5| 3: [Method] secondCall
|
||||
# 5| 3: [TypeAccess] void
|
||||
# 5| 5: [BlockStmt] { ... }
|
||||
# 7| 4: [Method] yieldWrapper
|
||||
# 7| 3: [TypeAccess] int
|
||||
#-----| 4: (Parameters)
|
||||
# 7| 0: [Parameter] x
|
||||
# 7| 0: [TypeAccess] int
|
||||
# 7| 5: [BlockStmt] { ... }
|
||||
# 8| 0: [LocalVariableDeclStmt] var ...;
|
||||
# 8| 0: [TypeAccess] int
|
||||
# 8| 1: [LocalVariableDeclExpr] ret
|
||||
# 8| 0: [ErrorExpr] <error expr>
|
||||
# 8| 0: [MethodAccess] yield(...)
|
||||
# 8| 0: [VarAccess] x
|
||||
# 9| 1: [ExprStmt] <Expr>;
|
||||
# 9| 0: [MethodAccess] secondCall(...)
|
||||
# 10| 2: [ReturnStmt] return ...
|
||||
# 10| 0: [VarAccess] ret
|
||||
1
java/ql/test/library-tests/errorexpr/PrintAst.qlref
Normal file
1
java/ql/test/library-tests/errorexpr/PrintAst.qlref
Normal file
@@ -0,0 +1 @@
|
||||
semmle/code/java/PrintAst.ql
|
||||
17
java/ql/test/library-tests/errorexpr/Test.java
Normal file
17
java/ql/test/library-tests/errorexpr/Test.java
Normal file
@@ -0,0 +1,17 @@
|
||||
public class Test {
|
||||
|
||||
public int yield(int x) { return x; }
|
||||
|
||||
public void secondCall() { }
|
||||
|
||||
public int yieldWrapper(int x) {
|
||||
int ret = yield(x);
|
||||
secondCall();
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Diagnostic Matches: Erroneous node in tree: (ERROR)
|
||||
// Diagnostic Matches: In file Test.java:8:15 no end location for JCMethodInvocation : yield(x)
|
||||
// Diagnostic Matches: 1 errors during annotation processing
|
||||
1
java/ql/test/library-tests/errorexpr/options
Normal file
1
java/ql/test/library-tests/errorexpr/options
Normal file
@@ -0,0 +1 @@
|
||||
//semmle-extractor-options: --no-strict-javac-errors --javac-args -source 17 -target 17
|
||||
@@ -0,0 +1 @@
|
||||
| file://:0:0:0:0 | 1 errors during annotation processing |
|
||||
@@ -0,0 +1,4 @@
|
||||
import java
|
||||
import semmle.code.java.Diagnostics
|
||||
|
||||
select any(Diagnostic d | not d.toString().matches("Not rewriting trap file for%"))
|
||||
@@ -0,0 +1,3 @@
|
||||
import java
|
||||
|
||||
select any(Expr e | e.getType() instanceof ErrorType)
|
||||
@@ -0,0 +1,6 @@
|
||||
Test.java:
|
||||
# 0| [CompilationUnit] Test
|
||||
# 1| 1: [Class] Test
|
||||
#-----| -1: (Base Types)
|
||||
# 1| 0: [TypeAccess] <any>
|
||||
# 1| 0: [TypeAccess] String
|
||||
@@ -0,0 +1 @@
|
||||
semmle/code/java/PrintAst.ql
|
||||
@@ -0,0 +1,3 @@
|
||||
public class Test implements Unavailable<String> { }
|
||||
|
||||
// Diagnostic Matches: 1 errors during annotation processing
|
||||
1
java/ql/test/library-tests/errortype-with-params/options
Normal file
1
java/ql/test/library-tests/errortype-with-params/options
Normal file
@@ -0,0 +1 @@
|
||||
//semmle-extractor-options: --no-strict-javac-errors --javac-args -source 17 -target 17
|
||||
3
java/ql/test/library-tests/errortype/Diags.expected
Normal file
3
java/ql/test/library-tests/errortype/Diags.expected
Normal file
@@ -0,0 +1,3 @@
|
||||
| Test.java:0:0:0:0 | 2 javac errors |
|
||||
| Test.java:6:23:6:39 | Unexpected symbol for constructor: new NoSuchClass() |
|
||||
| file://:0:0:0:0 | 2 errors during annotation processing |
|
||||
4
java/ql/test/library-tests/errortype/Diags.ql
Normal file
4
java/ql/test/library-tests/errortype/Diags.ql
Normal file
@@ -0,0 +1,4 @@
|
||||
import java
|
||||
import semmle.code.java.Diagnostics
|
||||
|
||||
select any(Diagnostic d | not d.toString().matches("Not rewriting trap file for%"))
|
||||
1
java/ql/test/library-tests/errortype/ErrorTypes.expected
Normal file
1
java/ql/test/library-tests/errortype/ErrorTypes.expected
Normal file
@@ -0,0 +1 @@
|
||||
| Test.java:7:12:7:14 | nsc |
|
||||
3
java/ql/test/library-tests/errortype/ErrorTypes.ql
Normal file
3
java/ql/test/library-tests/errortype/ErrorTypes.ql
Normal file
@@ -0,0 +1,3 @@
|
||||
import java
|
||||
|
||||
select any(Expr e | e.getType() instanceof ErrorType)
|
||||
15
java/ql/test/library-tests/errortype/PrintAst.expected
Normal file
15
java/ql/test/library-tests/errortype/PrintAst.expected
Normal file
@@ -0,0 +1,15 @@
|
||||
Test.java:
|
||||
# 0| [CompilationUnit] Test
|
||||
#-----| -1: (Imports)
|
||||
# 1| 1: [ImportType] import NoSuchClass
|
||||
# 3| 1: [Class] Test
|
||||
# 5| 2: [Method] test
|
||||
# 5| 3: [TypeAccess] NoSuchClass
|
||||
# 5| 5: [BlockStmt] { ... }
|
||||
# 6| 0: [LocalVariableDeclStmt] var ...;
|
||||
# 6| 0: [TypeAccess] NoSuchClass
|
||||
# 6| 1: [LocalVariableDeclExpr] nsc
|
||||
# 6| 0: [ClassInstanceExpr] <ClassInstanceExpr that calls a missing constructor>
|
||||
# 6| -3: [TypeAccess] NoSuchClass
|
||||
# 7| 1: [ReturnStmt] return ...
|
||||
# 7| 0: [VarAccess] nsc
|
||||
1
java/ql/test/library-tests/errortype/PrintAst.qlref
Normal file
1
java/ql/test/library-tests/errortype/PrintAst.qlref
Normal file
@@ -0,0 +1 @@
|
||||
semmle/code/java/PrintAst.ql
|
||||
14
java/ql/test/library-tests/errortype/Test.java
Normal file
14
java/ql/test/library-tests/errortype/Test.java
Normal file
@@ -0,0 +1,14 @@
|
||||
import nosuchpackage.NoSuchClass;
|
||||
|
||||
public class Test {
|
||||
|
||||
public NoSuchClass test() {
|
||||
NoSuchClass nsc = new NoSuchClass();
|
||||
return nsc;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Diagnostic Matches: Unexpected symbol for constructor: new NoSuchClass()
|
||||
// Diagnostic Matches: 2 javac errors
|
||||
// Diagnostic Matches: 2 errors during annotation processing
|
||||
1
java/ql/test/library-tests/errortype/options
Normal file
1
java/ql/test/library-tests/errortype/options
Normal file
@@ -0,0 +1 @@
|
||||
//semmle-extractor-options: --no-strict-javac-errors --javac-args -source 17 -target 17
|
||||
@@ -0,0 +1 @@
|
||||
| file://:0:0:0:0 | 2 errors during annotation processing |
|
||||
@@ -0,0 +1,4 @@
|
||||
import java
|
||||
import semmle.code.java.Diagnostics
|
||||
|
||||
select any(Diagnostic d | not d.toString().matches("Not rewriting trap file for%"))
|
||||
@@ -0,0 +1,5 @@
|
||||
module module.with.wrong.name {
|
||||
exports somepkg;
|
||||
}
|
||||
|
||||
// Diagnostic Matches: 2 errors during annotation processing
|
||||
@@ -0,0 +1 @@
|
||||
//semmle-extractor-options: --no-strict-javac-errors --javac-args -source 17 -target 17
|
||||
@@ -0,0 +1,5 @@
|
||||
package somepkg;
|
||||
|
||||
public class SomeClass {
|
||||
public static void someMethod() {}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
| Test.java:0:0:0:0 | 1 javac errors |
|
||||
| Test.java:4:13:4:30 | Unable to extract method reference Unavailable.f()::g with no owner type |
|
||||
@@ -0,0 +1,4 @@
|
||||
import java
|
||||
import semmle.code.java.Diagnostics
|
||||
|
||||
select any(Diagnostic d | not d.toString().matches("Not rewriting trap file for%"))
|
||||
@@ -0,0 +1,10 @@
|
||||
public class Test {
|
||||
|
||||
public static void test() {
|
||||
var x = Unavailable.f()::g;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Diagnostic Matches: 1 javac errors
|
||||
// Diagnostic Matches: Unable to extract method reference Unavailable.f()::g with no owner type
|
||||
@@ -0,0 +1 @@
|
||||
//semmle-extractor-options: --no-strict-javac-errors --javac-args -source 17 -target 17
|
||||
Reference in New Issue
Block a user