1
+ /**
2
+ * Provides queries to pretty-print a C++ AST as a graph.
3
+ *
4
+ * 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.
7
+ */
8
+
1
9
import cpp
2
10
private import semmle.code.cpp.Print
3
11
@@ -7,6 +15,9 @@ private newtype TPrintASTConfiguration = MkPrintASTConfiguration()
7
15
* The query can extend this class to control which functions are printed.
8
16
*/
9
17
class PrintASTConfiguration extends TPrintASTConfiguration {
18
+ /**
19
+ * Gets a textual representation of this `PrintASTConfiguration`.
20
+ */
10
21
string toString ( ) { result = "PrintASTConfiguration" }
11
22
12
23
/**
@@ -96,6 +107,9 @@ private newtype TPrintASTNode =
96
107
* A node in the output tree.
97
108
*/
98
109
class PrintASTNode extends TPrintASTNode {
110
+ /**
111
+ * Gets a textual representation of this node in the PrintAST output tree.
112
+ */
99
113
abstract string toString ( ) ;
100
114
101
115
/**
@@ -208,6 +222,9 @@ class ExprNode extends ASTNode {
208
222
result = expr .getValueCategoryString ( )
209
223
}
210
224
225
+ /**
226
+ * Gets the value of this expression, if it is a constant.
227
+ */
211
228
string getValue ( ) { result = expr .getValue ( ) }
212
229
}
213
230
@@ -373,6 +390,9 @@ class ParametersNode extends PrintASTNode, TParametersNode {
373
390
374
391
override ASTNode getChild ( int childIndex ) { result .getAST ( ) = func .getParameter ( childIndex ) }
375
392
393
+ /**
394
+ * Gets the function for which this node represents the parameters.
395
+ */
376
396
final Function getFunction ( ) { result = func }
377
397
}
378
398
@@ -392,6 +412,9 @@ class ConstructorInitializersNode extends PrintASTNode, TConstructorInitializers
392
412
result .getAST ( ) = ctor .getInitializer ( childIndex )
393
413
}
394
414
415
+ /**
416
+ * Gets the `Constructor` for which this node represents the initializer list.
417
+ */
395
418
final Constructor getConstructor ( ) { result = ctor }
396
419
}
397
420
@@ -411,6 +434,9 @@ class DestructorDestructionsNode extends PrintASTNode, TDestructorDestructionsNo
411
434
result .getAST ( ) = dtor .getDestruction ( childIndex )
412
435
}
413
436
437
+ /**
438
+ * Gets the `Destructor` for which this node represents the destruction list.
439
+ */
414
440
final Destructor getDestructor ( ) { result = dtor }
415
441
}
416
442
@@ -464,6 +490,9 @@ class FunctionNode extends ASTNode {
464
490
key = "semmle.order" and result = getOrder ( ) .toString ( )
465
491
}
466
492
493
+ /**
494
+ * Gets the `Function` this node represents.
495
+ */
467
496
final Function getFunction ( ) { result = func }
468
497
}
469
498
@@ -499,11 +528,16 @@ class ArrayAggregateLiteralNode extends ExprNode {
499
528
}
500
529
}
501
530
531
+ /**
532
+ * Holds if `node` is printed in the PrintAST output tree and has the property `key` with the
533
+ * value `value`.
534
+ */
502
535
query predicate nodes ( PrintASTNode node , string key , string value ) {
503
536
node .shouldPrint ( ) and
504
537
value = node .getProperty ( key )
505
538
}
506
539
540
+ /** Holds if `node` belongs to the output tree, and its property `key` has the given `value`. */
507
541
query predicate edges ( PrintASTNode source , PrintASTNode target , string key , string value ) {
508
542
exists ( int childIndex |
509
543
source .shouldPrint ( ) and
@@ -517,6 +551,7 @@ query predicate edges(PrintASTNode source, PrintASTNode target, string key, stri
517
551
)
518
552
}
519
553
554
+ /** Holds if property `key` of the graph has the given `value`. */
520
555
query predicate graphProperties ( string key , string value ) {
521
556
key = "semmle.graphKind" and value = "tree"
522
557
}
0 commit comments