Skip to content

Commit 4f70af5

Browse files
Merge pull request #4261 from joefarebrother/printAST-java
Java: Add PrintAst
2 parents d828bc5 + 7e9b1a2 commit 4f70af5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+2316
-5
lines changed

java/ql/src/printAst.ql

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @name Print AST
3+
* @description Outputs a representation of a file's Abstract Syntax Tree. This
4+
* query is used by the VS Code extension.
5+
* @id java/print-ast
6+
* @kind graph
7+
* @tags ide-contextual-queries/print-ast
8+
*/
9+
10+
import java
11+
import semmle.code.java.PrintAst
12+
import definitions
13+
14+
/**
15+
* The source file to generate an AST from.
16+
*/
17+
external string selectedSourceFile();
18+
19+
class PrintAstConfigurationOverride extends PrintAstConfiguration {
20+
/**
21+
* Holds if the location matches the selected file in the VS Code extension and
22+
* the element is `fromSource`.
23+
*/
24+
override predicate shouldPrint(Element e, Location l) {
25+
super.shouldPrint(e, l) and
26+
l.getFile() = getEncodedFile(selectedSourceFile())
27+
}
28+
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@ class Top extends @top {
8686
/** Gets a textual representation of this element. */
8787
cached
8888
string toString() { hasName(this, result) }
89+
90+
/**
91+
* Gets the name of a primary CodeQL class to which this element belongs.
92+
*
93+
* For most elements, this is simply the most precise syntactic category to
94+
* which they belong; for example, `AddExpr` is a primary class, but
95+
* `BinaryExpr` is not.
96+
*
97+
* This predicate always has a result. If no primary class can be
98+
* determined, the result is `"???"`. If multiple primary classes match,
99+
* this predicate can have multiple results.
100+
*/
101+
string getAPrimaryQlClass() { result = "???" }
89102
}
90103

91104
/** A location maps language elements to positions in source files. */

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ class Annotation extends @annotation, Expr {
7272
if value instanceof ArrayInit then result = value.(ArrayInit).getAnInit() else result = value
7373
)
7474
}
75+
76+
override string getAPrimaryQlClass() { result = "Annotation" }
7577
}
7678

7779
/** An `Annotation` that applies to a declaration. */

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,6 @@ class CompilationUnit extends Element, File {
3030
* Gets the module associated with this compilation unit, if any.
3131
*/
3232
Module getModule() { cumodule(this, result) }
33+
34+
override string getAPrimaryQlClass() { result = "CompilationUnit" }
3335
}

0 commit comments

Comments
 (0)