55import go
66
77/**
8- * Hook to customize the functions printed by this module.
8+ * Hook to customize the files and functions printed by this module.
9+ *
10+ * For an AstNode to be printed, it always requires `shouldPrintFile(f)` to hold
11+ * for its containing file `f`, and additionally requires `shouldPrintFunction(fun)`
12+ * to hold if it is, or is a child of, function `fun`.
913 */
1014class PrintAstConfiguration extends string {
1115 /**
@@ -17,20 +21,48 @@ class PrintAstConfiguration extends string {
1721 * Holds if the AST for `func` should be printed. By default, holds for all
1822 * functions.
1923 */
20- predicate shouldPrintFunction ( FuncDef func ) { any ( ) }
24+ predicate shouldPrintFunction ( FuncDecl func ) { any ( ) }
25+
26+ /**
27+ * Holds if the AST for `file` should be printed. By default, holds for all
28+ * files.
29+ */
30+ 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 ( ) }
2137}
2238
2339private predicate shouldPrintFunction ( FuncDef func ) {
2440 exists ( PrintAstConfiguration config | config .shouldPrintFunction ( func ) )
2541}
2642
43+ private predicate shouldPrintFile ( File file ) {
44+ exists ( PrintAstConfiguration config | config .shouldPrintFile ( file ) )
45+ }
46+
47+ private predicate shouldPrintComments ( File file ) {
48+ exists ( PrintAstConfiguration config | config .shouldPrintComments ( file ) )
49+ }
50+
51+ private FuncDecl getEnclosingFunctionDecl ( AstNode n ) { result = n .getParent * ( ) }
52+
2753/**
2854 * An AST node that should be printed.
2955 */
3056private newtype TPrintAstNode =
3157 TAstNode ( AstNode ast ) {
32- // Do print ast nodes without an enclosing function, e.g. file headers
33- forall ( FuncDef f | f = ast .getEnclosingFunction ( ) | shouldPrintFunction ( f ) )
58+ shouldPrintFile ( ast .getFile ( ) ) and
59+ // Do print ast nodes without an enclosing function, e.g. file headers, that are not otherwise excluded
60+ forall ( FuncDecl f | f = getEnclosingFunctionDecl ( ast ) | shouldPrintFunction ( f ) ) and
61+ (
62+ shouldPrintComments ( ast .getFile ( ) )
63+ or
64+ not ast instanceof Comment and not ast instanceof CommentGroup
65+ )
3466 }
3567
3668/**
@@ -149,6 +181,19 @@ class ExprNode extends BaseAstNode {
149181class FileNode extends BaseAstNode {
150182 override File ast ;
151183
184+ private string getRelativePath ( ) { result = ast .getRelativePath ( ) }
185+
186+ private int getSortOrder ( ) {
187+ rank [ result ] ( FileNode fn | any ( ) | fn order by fn .getRelativePath ( ) ) = this
188+ }
189+
190+ override string getProperty ( string key ) {
191+ result = super .getProperty ( key )
192+ or
193+ key = "semmle.order" and
194+ result = getSortOrder ( ) .toString ( )
195+ }
196+
152197 /**
153198 * Gets the string representation of this File. Note explicitly using a relative path
154199 * like this rather than absolute as per default for the File class is a workaround for
0 commit comments