Skip to content

Commit 2a63116

Browse files
committed
C++: Rename shouldPrintFunction to shouldPrintDeclaration
1 parent 0a0e9bb commit 2a63116

File tree

14 files changed

+96
-88
lines changed

14 files changed

+96
-88
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
category: breaking
3+
---
4+
* The `shouldPrintFunction` predicate from `PrintAstConfiguration` has been replaced by `shouldPrintDeclaration`. Users should now override `shouldPrintDeclaration` if they want to limit the declarations that should be printed.
5+
* The `shouldPrintFunction` predicate from `PrintIRConfiguration` has been replaced by `shouldPrintDeclaration`. Users should now override `shouldPrintDeclaration` if they want to limit the declarations that should be printed.

cpp/ql/lib/printAst.ql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ external string selectedSourceFile();
1818

1919
class Cfg extends PrintAstConfiguration {
2020
/**
21-
* Holds if the AST for `func` should be printed.
22-
* Print All functions from the selected file.
21+
* Holds if the AST for `decl` should be printed.
22+
* Print All declarations from the selected file.
2323
*/
24-
override predicate shouldPrintFunction(Function func) {
25-
func.getFile() = getFileBySourceArchiveName(selectedSourceFile())
24+
override predicate shouldPrintDeclaration(Declaration decl) {
25+
decl.getFile() = getFileBySourceArchiveName(selectedSourceFile())
2626
}
2727
}

cpp/ql/lib/semmle/code/cpp/Print.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ private predicate shouldPrintDeclaration(Declaration decl) {
1010
or
1111
not exists(PrintAstConfiguration c)
1212
or
13-
exists(PrintAstConfiguration config | config.shouldPrintFunction(decl))
13+
exists(PrintAstConfiguration config | config.shouldPrintDeclaration(decl))
1414
}
1515

1616
/**

cpp/ql/lib/semmle/code/cpp/PrintAST.ql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import cpp
99
import PrintAST
1010

1111
/**
12-
* Temporarily tweak this class or make a copy to control which functions are
12+
* Temporarily tweak this class or make a copy to control which declarations are
1313
* printed.
1414
*/
1515
class Cfg extends PrintAstConfiguration {
1616
/**
1717
* TWEAK THIS PREDICATE AS NEEDED.
18-
* Holds if the AST for `func` should be printed.
18+
* Holds if the AST for `decl` should be printed.
1919
*/
20-
override predicate shouldPrintFunction(Function func) { any() }
20+
override predicate shouldPrintDeclaration(Declaration decl) { any() }
2121
}

cpp/ql/lib/semmle/code/cpp/PrintAST.qll

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
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

99
import cpp
@@ -12,7 +12,7 @@ private import semmle.code.cpp.Print
1212
private 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
*/
1717
class 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

3437
bindingset[s]
@@ -86,21 +89,21 @@ private Function getEnclosingFunction(Locatable ast) {
8689
* nodes for things like parameter lists and constructor init lists.
8790
*/
8891
private 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

630633
private 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

649652
private 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

737740
private 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

cpp/ql/lib/semmle/code/cpp/ir/PrintIR.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* This file contains the actual implementation of `PrintIR.ql`. For test cases and very small
55
* databases, `PrintIR.ql` can be run directly to dump the IR for the entire database. For most
66
* uses, however, it is better to write a query that imports `PrintIR.qll`, extends
7-
* `PrintIRConfiguration`, and overrides `shouldPrintFunction()` to select a subset of functions to
8-
* dump.
7+
* `PrintIRConfiguration`, and overrides `shouldPrintDeclaration()` to select a subset of declarations
8+
* to dump.
99
*/
1010

1111
import implementation.aliased_ssa.PrintIR

cpp/ql/lib/semmle/code/cpp/ir/implementation/aliased_ssa/PrintIR.qll

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* This file contains the actual implementation of `PrintIR.ql`. For test cases and very small
55
* databases, `PrintIR.ql` can be run directly to dump the IR for the entire database. For most
66
* uses, however, it is better to write a query that imports `PrintIR.qll`, extends
7-
* `PrintIRConfiguration`, and overrides `shouldPrintFunction()` to select a subset of functions to
8-
* dump.
7+
* `PrintIRConfiguration`, and overrides `shouldPrintDeclaration()` to select a subset of declarations
8+
* to dump.
99
*/
1010

1111
private import internal.IRInternal
@@ -16,30 +16,30 @@ import Imports::IRConfiguration
1616
private newtype TPrintIRConfiguration = MkPrintIRConfiguration()
1717

1818
/**
19-
* The query can extend this class to control which functions are printed.
19+
* The query can extend this class to control which declarations are printed.
2020
*/
2121
class PrintIRConfiguration extends TPrintIRConfiguration {
2222
/** Gets a textual representation of this configuration. */
2323
string toString() { result = "PrintIRConfiguration" }
2424

2525
/**
2626
* Holds if the IR for `func` should be printed. By default, holds for all
27-
* functions.
27+
* functions, global and namespace variables, and static local variables.
2828
*/
29-
predicate shouldPrintFunction(Language::Declaration decl) { any() }
29+
predicate shouldPrintDeclaration(Language::Declaration decl) { any() }
3030
}
3131

3232
/**
3333
* Override of `IRConfiguration` to only evaluate debug strings for the functions that are to be dumped.
3434
*/
3535
private class FilteredIRConfiguration extends IRConfiguration {
3636
override predicate shouldEvaluateDebugStringsForFunction(Language::Declaration func) {
37-
shouldPrintFunction(func)
37+
shouldPrintDeclaration(func)
3838
}
3939
}
4040

41-
private predicate shouldPrintFunction(Language::Declaration decl) {
42-
exists(PrintIRConfiguration config | config.shouldPrintFunction(decl))
41+
private predicate shouldPrintDeclaration(Language::Declaration decl) {
42+
exists(PrintIRConfiguration config | config.shouldPrintDeclaration(decl))
4343
}
4444

4545
private predicate shouldPrintInstruction(Instruction i) {
@@ -90,10 +90,10 @@ private string getOperandPropertyString(Operand operand) {
9090
}
9191

9292
private newtype TPrintableIRNode =
93-
TPrintableIRFunction(IRFunction irFunc) { shouldPrintFunction(irFunc.getFunction()) } or
94-
TPrintableIRBlock(IRBlock block) { shouldPrintFunction(block.getEnclosingFunction()) } or
93+
TPrintableIRFunction(IRFunction irFunc) { shouldPrintDeclaration(irFunc.getFunction()) } or
94+
TPrintableIRBlock(IRBlock block) { shouldPrintDeclaration(block.getEnclosingFunction()) } or
9595
TPrintableInstruction(Instruction instr) {
96-
shouldPrintInstruction(instr) and shouldPrintFunction(instr.getEnclosingFunction())
96+
shouldPrintInstruction(instr) and shouldPrintDeclaration(instr.getEnclosingFunction())
9797
}
9898

9999
/**

cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/PrintIR.qll

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* This file contains the actual implementation of `PrintIR.ql`. For test cases and very small
55
* databases, `PrintIR.ql` can be run directly to dump the IR for the entire database. For most
66
* uses, however, it is better to write a query that imports `PrintIR.qll`, extends
7-
* `PrintIRConfiguration`, and overrides `shouldPrintFunction()` to select a subset of functions to
8-
* dump.
7+
* `PrintIRConfiguration`, and overrides `shouldPrintDeclaration()` to select a subset of declarations
8+
* to dump.
99
*/
1010

1111
private import internal.IRInternal
@@ -16,30 +16,30 @@ import Imports::IRConfiguration
1616
private newtype TPrintIRConfiguration = MkPrintIRConfiguration()
1717

1818
/**
19-
* The query can extend this class to control which functions are printed.
19+
* The query can extend this class to control which declarations are printed.
2020
*/
2121
class PrintIRConfiguration extends TPrintIRConfiguration {
2222
/** Gets a textual representation of this configuration. */
2323
string toString() { result = "PrintIRConfiguration" }
2424

2525
/**
2626
* Holds if the IR for `func` should be printed. By default, holds for all
27-
* functions.
27+
* functions, global and namespace variables, and static local variables.
2828
*/
29-
predicate shouldPrintFunction(Language::Declaration decl) { any() }
29+
predicate shouldPrintDeclaration(Language::Declaration decl) { any() }
3030
}
3131

3232
/**
3333
* Override of `IRConfiguration` to only evaluate debug strings for the functions that are to be dumped.
3434
*/
3535
private class FilteredIRConfiguration extends IRConfiguration {
3636
override predicate shouldEvaluateDebugStringsForFunction(Language::Declaration func) {
37-
shouldPrintFunction(func)
37+
shouldPrintDeclaration(func)
3838
}
3939
}
4040

41-
private predicate shouldPrintFunction(Language::Declaration decl) {
42-
exists(PrintIRConfiguration config | config.shouldPrintFunction(decl))
41+
private predicate shouldPrintDeclaration(Language::Declaration decl) {
42+
exists(PrintIRConfiguration config | config.shouldPrintDeclaration(decl))
4343
}
4444

4545
private predicate shouldPrintInstruction(Instruction i) {
@@ -90,10 +90,10 @@ private string getOperandPropertyString(Operand operand) {
9090
}
9191

9292
private newtype TPrintableIRNode =
93-
TPrintableIRFunction(IRFunction irFunc) { shouldPrintFunction(irFunc.getFunction()) } or
94-
TPrintableIRBlock(IRBlock block) { shouldPrintFunction(block.getEnclosingFunction()) } or
93+
TPrintableIRFunction(IRFunction irFunc) { shouldPrintDeclaration(irFunc.getFunction()) } or
94+
TPrintableIRBlock(IRBlock block) { shouldPrintDeclaration(block.getEnclosingFunction()) } or
9595
TPrintableInstruction(Instruction instr) {
96-
shouldPrintInstruction(instr) and shouldPrintFunction(instr.getEnclosingFunction())
96+
shouldPrintInstruction(instr) and shouldPrintDeclaration(instr.getEnclosingFunction())
9797
}
9898

9999
/**

cpp/ql/lib/semmle/code/cpp/ir/implementation/unaliased_ssa/PrintIR.qll

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* This file contains the actual implementation of `PrintIR.ql`. For test cases and very small
55
* databases, `PrintIR.ql` can be run directly to dump the IR for the entire database. For most
66
* uses, however, it is better to write a query that imports `PrintIR.qll`, extends
7-
* `PrintIRConfiguration`, and overrides `shouldPrintFunction()` to select a subset of functions to
8-
* dump.
7+
* `PrintIRConfiguration`, and overrides `shouldPrintDeclaration()` to select a subset of declarations
8+
* to dump.
99
*/
1010

1111
private import internal.IRInternal
@@ -16,30 +16,30 @@ import Imports::IRConfiguration
1616
private newtype TPrintIRConfiguration = MkPrintIRConfiguration()
1717

1818
/**
19-
* The query can extend this class to control which functions are printed.
19+
* The query can extend this class to control which declarations are printed.
2020
*/
2121
class PrintIRConfiguration extends TPrintIRConfiguration {
2222
/** Gets a textual representation of this configuration. */
2323
string toString() { result = "PrintIRConfiguration" }
2424

2525
/**
2626
* Holds if the IR for `func` should be printed. By default, holds for all
27-
* functions.
27+
* functions, global and namespace variables, and static local variables.
2828
*/
29-
predicate shouldPrintFunction(Language::Declaration decl) { any() }
29+
predicate shouldPrintDeclaration(Language::Declaration decl) { any() }
3030
}
3131

3232
/**
3333
* Override of `IRConfiguration` to only evaluate debug strings for the functions that are to be dumped.
3434
*/
3535
private class FilteredIRConfiguration extends IRConfiguration {
3636
override predicate shouldEvaluateDebugStringsForFunction(Language::Declaration func) {
37-
shouldPrintFunction(func)
37+
shouldPrintDeclaration(func)
3838
}
3939
}
4040

41-
private predicate shouldPrintFunction(Language::Declaration decl) {
42-
exists(PrintIRConfiguration config | config.shouldPrintFunction(decl))
41+
private predicate shouldPrintDeclaration(Language::Declaration decl) {
42+
exists(PrintIRConfiguration config | config.shouldPrintDeclaration(decl))
4343
}
4444

4545
private predicate shouldPrintInstruction(Instruction i) {
@@ -90,10 +90,10 @@ private string getOperandPropertyString(Operand operand) {
9090
}
9191

9292
private newtype TPrintableIRNode =
93-
TPrintableIRFunction(IRFunction irFunc) { shouldPrintFunction(irFunc.getFunction()) } or
94-
TPrintableIRBlock(IRBlock block) { shouldPrintFunction(block.getEnclosingFunction()) } or
93+
TPrintableIRFunction(IRFunction irFunc) { shouldPrintDeclaration(irFunc.getFunction()) } or
94+
TPrintableIRBlock(IRBlock block) { shouldPrintDeclaration(block.getEnclosingFunction()) } or
9595
TPrintableInstruction(Instruction instr) {
96-
shouldPrintInstruction(instr) and shouldPrintFunction(instr.getEnclosingFunction())
96+
shouldPrintInstruction(instr) and shouldPrintDeclaration(instr.getEnclosingFunction())
9797
}
9898

9999
/**

cpp/ql/test/library-tests/ir/ir/PrintAST.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ private import semmle.code.cpp.PrintAST
77
private import PrintConfig
88

99
private class PrintConfig extends PrintAstConfiguration {
10-
override predicate shouldPrintFunction(Function func) { shouldDumpFunction(func) }
10+
override predicate shouldPrintDeclaration(Declaration decl) { shouldDumpDeclaration(decl) }
1111
}

cpp/ql/test/library-tests/ir/ir/PrintConfig.qll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ predicate locationIsInStandardHeaders(Location loc) {
88
}
99

1010
/**
11-
* Holds if the AST or IR for the specified function should be printed in the test output.
11+
* Holds if the AST or IR for the specified declaration should be printed in the test output.
1212
*
13-
* This predicate excludes functions defined in standard headers.
13+
* This predicate excludes declarations defined in standard headers.
1414
*/
15-
predicate shouldDumpFunction(Declaration decl) {
15+
predicate shouldDumpDeclaration(Declaration decl) {
1616
not locationIsInStandardHeaders(decl.getLocation()) and
1717
(
1818
decl instanceof Function

cpp/ql/test/library-tests/ir/ir/raw_ir.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ private import semmle.code.cpp.ir.implementation.raw.PrintIR
77
private import PrintConfig
88

99
private class PrintConfig extends PrintIRConfiguration {
10-
override predicate shouldPrintFunction(Declaration decl) { shouldDumpFunction(decl) }
10+
override predicate shouldPrintDeclaration(Declaration decl) { shouldDumpDeclaration(decl) }
1111
}

0 commit comments

Comments
 (0)