22 * Provides queries to pretty-print a C++ AST as a graph.
33 *
44 * By default, this will print the AST for all functions in the database. To change this behavior,
5- * extend `PrintASTConfiguration` and override `shouldPrintFunction ` to hold for only the functions
6- * you wish to view the AST for.
5+ * extend `PrintASTConfiguration` and override `shouldPrintDeclaration ` to hold for only the
6+ * declarations you wish to view the AST for.
77 */
88
99import cpp
@@ -12,7 +12,7 @@ private import semmle.code.cpp.Print
1212private newtype TPrintAstConfiguration = MkPrintAstConfiguration ( )
1313
1414/**
15- * The query can extend this class to control which functions are printed.
15+ * The query can extend this class to control which declarations are printed.
1616 */
1717class PrintAstConfiguration extends TPrintAstConfiguration {
1818 /**
@@ -21,14 +21,17 @@ class PrintAstConfiguration extends TPrintAstConfiguration {
2121 string toString ( ) { result = "PrintASTConfiguration" }
2222
2323 /**
24- * Holds if the AST for `func ` should be printed. By default, holds for all
25- * functions.
24+ * Holds if the AST for `decl ` should be printed. By default, holds for all
25+ * functions. Currently, does not support any other declaration types.
2626 */
27- predicate shouldPrintFunction ( Function func ) { any ( ) }
27+ predicate shouldPrintDeclaration ( Declaration decl ) { any ( ) }
2828}
2929
30- private predicate shouldPrintFunction ( Function func ) {
31- exists ( PrintAstConfiguration config | config .shouldPrintFunction ( func ) )
30+ private predicate shouldPrintDeclaration ( Declaration decl ) {
31+ exists ( PrintAstConfiguration config |
32+ config .shouldPrintDeclaration ( decl ) and
33+ decl instanceof Function
34+ )
3235}
3336
3437bindingset [ s]
@@ -86,21 +89,21 @@ private Function getEnclosingFunction(Locatable ast) {
8689 * nodes for things like parameter lists and constructor init lists.
8790 */
8891private newtype TPrintAstNode =
89- TAstNode ( Locatable ast ) { shouldPrintFunction ( getEnclosingFunction ( ast ) ) } or
92+ TAstNode ( Locatable ast ) { shouldPrintDeclaration ( getEnclosingFunction ( ast ) ) } or
9093 TDeclarationEntryNode ( DeclStmt stmt , DeclarationEntry entry ) {
9194 // We create a unique node for each pair of (stmt, entry), to avoid having one node with
9295 // multiple parents due to extractor bug CPP-413.
9396 stmt .getADeclarationEntry ( ) = entry and
94- shouldPrintFunction ( stmt .getEnclosingFunction ( ) )
97+ shouldPrintDeclaration ( stmt .getEnclosingFunction ( ) )
9598 } or
96- TParametersNode ( Function func ) { shouldPrintFunction ( func ) } or
99+ TParametersNode ( Function func ) { shouldPrintDeclaration ( func ) } or
97100 TConstructorInitializersNode ( Constructor ctor ) {
98101 ctor .hasEntryPoint ( ) and
99- shouldPrintFunction ( ctor )
102+ shouldPrintDeclaration ( ctor )
100103 } or
101104 TDestructorDestructionsNode ( Destructor dtor ) {
102105 dtor .hasEntryPoint ( ) and
103- shouldPrintFunction ( dtor )
106+ shouldPrintDeclaration ( dtor )
104107 }
105108
106109/**
@@ -159,9 +162,9 @@ class PrintAstNode extends TPrintAstNode {
159162 /**
160163 * Holds if this node should be printed in the output. By default, all nodes
161164 * within a function are printed, but the query can override
162- * `PrintASTConfiguration.shouldPrintFunction ` to filter the output.
165+ * `PrintASTConfiguration.shouldPrintDeclaration ` to filter the output.
163166 */
164- final predicate shouldPrint ( ) { shouldPrintFunction ( this .getEnclosingFunction ( ) ) }
167+ final predicate shouldPrint ( ) { shouldPrintDeclaration ( this .getEnclosingFunction ( ) ) }
165168
166169 /**
167170 * Gets the children of this node.
@@ -628,7 +631,7 @@ class FunctionNode extends AstNode {
628631}
629632
630633private string getChildAccessorWithoutConversions ( Locatable parent , Element child ) {
631- shouldPrintFunction ( getEnclosingFunction ( parent ) ) and
634+ shouldPrintDeclaration ( getEnclosingFunction ( parent ) ) and
632635 (
633636 exists ( Stmt s | s = parent |
634637 namedStmtChildPredicates ( s , child , result )
@@ -647,7 +650,7 @@ private string getChildAccessorWithoutConversions(Locatable parent, Element chil
647650}
648651
649652private predicate namedStmtChildPredicates ( Locatable s , Element e , string pred ) {
650- shouldPrintFunction ( getEnclosingFunction ( s ) ) and
653+ shouldPrintDeclaration ( getEnclosingFunction ( s ) ) and
651654 (
652655 exists ( int n | s .( BlockStmt ) .getStmt ( n ) = e and pred = "getStmt(" + n + ")" )
653656 or
@@ -735,7 +738,7 @@ private predicate namedStmtChildPredicates(Locatable s, Element e, string pred)
735738}
736739
737740private predicate namedExprChildPredicates ( Expr expr , Element ele , string pred ) {
738- shouldPrintFunction ( expr .getEnclosingFunction ( ) ) and
741+ shouldPrintDeclaration ( expr .getEnclosingFunction ( ) ) and
739742 (
740743 expr .( Access ) .getTarget ( ) = ele and pred = "getTarget()"
741744 or
0 commit comments