Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit b8c4004

Browse files
committed
PrintAst: support excluding comments
1 parent e0aa59c commit b8c4004

File tree

4 files changed

+639
-2
lines changed

4 files changed

+639
-2
lines changed

ql/src/semmle/go/PrintAst.ql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ class Cfg extends PrintAstConfiguration {
1515
override predicate shouldPrintFunction(FuncDef func) { any() }
1616

1717
override predicate shouldPrintFile(File file) { any() }
18+
19+
override predicate shouldPrintComments(File file) { any() }
1820
}

ql/src/semmle/go/PrintAst.qll

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import go
99
*
1010
* For an AstNode to be printed, it always requires `shouldPrintFile(f)` to hold
1111
* for its containing file `f`, and additionally requires `shouldPrintFunction(fun)`
12-
* if it is, or falls within, function `fun`.
12+
* to hold if it is, or is a child of, function `fun`.
1313
*/
1414
class PrintAstConfiguration extends string {
1515
/**
@@ -28,6 +28,12 @@ class PrintAstConfiguration extends string {
2828
* files.
2929
*/
3030
predicate shouldPrintFile(File file) { any() }
31+
32+
/**
33+
* Holds if the AST for `file` should include comments. By default, holds for all
34+
* files.
35+
*/
36+
predicate shouldPrintComments(File file) { any() }
3137
}
3238

3339
private predicate shouldPrintFunction(FuncDef func) {
@@ -38,6 +44,10 @@ private predicate shouldPrintFile(File file) {
3844
exists(PrintAstConfiguration config | config.shouldPrintFile(file))
3945
}
4046

47+
private predicate shouldPrintComments(File file) {
48+
exists(PrintAstConfiguration config | config.shouldPrintComments(file))
49+
}
50+
4151
private FuncDef getEnclosingFunction(AstNode n) {
4252
result = n or
4353
result = n.getEnclosingFunction()
@@ -49,7 +59,13 @@ private FuncDef getEnclosingFunction(AstNode n) {
4959
private newtype TPrintAstNode =
5060
TAstNode(AstNode ast) {
5161
shouldPrintFile(ast.getFile()) and
52-
forall(FuncDef f | f = getEnclosingFunction(ast) | shouldPrintFunction(f))
62+
// Do print ast nodes without an enclosing function, e.g. file headers, that are not otherwise excluded
63+
forall(FuncDef f | f = getEnclosingFunction(ast) | shouldPrintFunction(f)) and
64+
(
65+
shouldPrintComments(ast.getFile())
66+
or
67+
not ast instanceof Comment and not ast instanceof CommentGroup
68+
)
5369
}
5470

5571
/**

0 commit comments

Comments
 (0)