Skip to content

Commit 3342fac

Browse files
authored
Merge pull request github#4688 from criemen/printast-performance
C++: Speed up PrintAST.
2 parents 056b0c2 + 1afd32c commit 3342fac

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

cpp/ql/src/semmle/code/cpp/Element.qll

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,10 @@ class ElementBase extends @element {
6565
* which they belong; for example, `AddExpr` is a primary class, but
6666
* `BinaryOperation` is not.
6767
*
68-
* This predicate always has a result. If no primary class can be
69-
* determined, the result is `"???"`. If multiple primary classes match,
70-
* this predicate can have multiple results.
68+
* This predicate can have multiple results if multiple primary classes match.
69+
* For some elements, this predicate may not have a result.
7170
*/
72-
string getAPrimaryQlClass() { result = "???" }
71+
string getAPrimaryQlClass() { none() }
7372
}
7473

7574
/**

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ private newtype TPrintASTNode =
9191
TDeclarationEntryNode(DeclStmt stmt, DeclarationEntry entry) {
9292
// We create a unique node for each pair of (stmt, entry), to avoid having one node with
9393
// multiple parents due to extractor bug CPP-413.
94-
stmt.getADeclarationEntry() = entry
94+
stmt.getADeclarationEntry() = entry and
95+
shouldPrintFunction(stmt.getEnclosingFunction())
9596
} or
9697
TParametersNode(Function func) { shouldPrintFunction(func) } or
9798
TConstructorInitializersNode(Constructor ctor) {
@@ -234,11 +235,27 @@ class PrintASTNode extends TPrintASTNode {
234235
private Function getEnclosingFunction() { result = getParent*().(FunctionNode).getFunction() }
235236
}
236237

238+
/**
239+
* Class that restricts the elements that we compute `qlClass` for.
240+
*/
241+
private class PrintableElement extends Element {
242+
PrintableElement() {
243+
exists(TASTNode(this))
244+
or
245+
exists(TDeclarationEntryNode(_, this))
246+
or
247+
this instanceof Type
248+
}
249+
250+
pragma[noinline]
251+
string getAPrimaryQlClass0() { result = getAPrimaryQlClass() }
252+
}
253+
237254
/**
238255
* Retrieves the canonical QL class(es) for entity `el`
239256
*/
240-
private string qlClass(ElementBase el) {
241-
result = "[" + concat(el.getAPrimaryQlClass(), ",") + "] "
257+
private string qlClass(PrintableElement el) {
258+
result = "[" + concat(el.getAPrimaryQlClass0(), ",") + "] "
242259
// Alternative implementation -- do not delete. It is useful for QL class discovery.
243260
//result = "["+ concat(el.getAQlClass(), ",") + "] "
244261
}

0 commit comments

Comments
 (0)