From c0f897374949bfec08c63c59155e0612eb6b8246 Mon Sep 17 00:00:00 2001 From: Chris Smowton Date: Wed, 16 Aug 2023 11:14:04 +0100 Subject: [PATCH] Add test for extracting a Java AST with an error expression Also note that ErrorExpr can occur outside upgrade/downgrade scripts --- java/ql/lib/semmle/code/java/Expr.qll | 3 +- .../library-tests/errorexpr/PrintAst.expected | 30 +++++++++++++++++++ .../library-tests/errorexpr/PrintAst.qlref | 1 + .../ql/test/library-tests/errorexpr/Test.java | 13 ++++++++ java/ql/test/library-tests/errorexpr/options | 1 + 5 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 java/ql/test/library-tests/errorexpr/PrintAst.expected create mode 100644 java/ql/test/library-tests/errorexpr/PrintAst.qlref create mode 100644 java/ql/test/library-tests/errorexpr/Test.java create mode 100644 java/ql/test/library-tests/errorexpr/options diff --git a/java/ql/lib/semmle/code/java/Expr.qll b/java/ql/lib/semmle/code/java/Expr.qll index 312d7767ac2..0ac6d83e7fe 100644 --- a/java/ql/lib/semmle/code/java/Expr.qll +++ b/java/ql/lib/semmle/code/java/Expr.qll @@ -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 = "" } diff --git a/java/ql/test/library-tests/errorexpr/PrintAst.expected b/java/ql/test/library-tests/errorexpr/PrintAst.expected new file mode 100644 index 00000000000..175835c7d8a --- /dev/null +++ b/java/ql/test/library-tests/errorexpr/PrintAst.expected @@ -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] +# 8| 0: [MethodAccess] yield(...) +# 8| 0: [VarAccess] x +# 9| 1: [ExprStmt] ; +# 9| 0: [MethodAccess] secondCall(...) +# 10| 2: [ReturnStmt] return ... +# 10| 0: [VarAccess] ret diff --git a/java/ql/test/library-tests/errorexpr/PrintAst.qlref b/java/ql/test/library-tests/errorexpr/PrintAst.qlref new file mode 100644 index 00000000000..c7fd5faf239 --- /dev/null +++ b/java/ql/test/library-tests/errorexpr/PrintAst.qlref @@ -0,0 +1 @@ +semmle/code/java/PrintAst.ql \ No newline at end of file diff --git a/java/ql/test/library-tests/errorexpr/Test.java b/java/ql/test/library-tests/errorexpr/Test.java new file mode 100644 index 00000000000..e4f4b7626f8 --- /dev/null +++ b/java/ql/test/library-tests/errorexpr/Test.java @@ -0,0 +1,13 @@ +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; + } + +} diff --git a/java/ql/test/library-tests/errorexpr/options b/java/ql/test/library-tests/errorexpr/options new file mode 100644 index 00000000000..ea577f8c43e --- /dev/null +++ b/java/ql/test/library-tests/errorexpr/options @@ -0,0 +1 @@ +//semmle-extractor-options: --no-strict-javac-errors --javac-args -source 17 -target 17