@@ -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 */
1414class 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
3339private 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+
4151private FuncDef getEnclosingFunction ( AstNode n ) {
4252 result = n or
4353 result = n .getEnclosingFunction ( )
@@ -49,7 +59,13 @@ private FuncDef getEnclosingFunction(AstNode n) {
4959private 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