Skip to content

Commit 447ba20

Browse files
committed
C++: Move Conversions in PrintAST to the side.
1 parent 0e5c44e commit 447ba20

File tree

3 files changed

+2245
-2206
lines changed

3 files changed

+2245
-2206
lines changed

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

Lines changed: 64 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,29 @@ class PrintASTNode extends TPrintASTNode {
114114

115115
/**
116116
* Gets the child node at index `childIndex`. Child indices must be unique,
117-
* but need not be contiguous (but see `getChildByRank`).
117+
* but need not be contiguous.
118118
*/
119-
abstract PrintASTNode getChild(int childIndex);
119+
abstract PrintASTNode getChildInternal(int childIndex);
120+
121+
/**
122+
* Gets the child node at index `childIndex`.
123+
* Adds edges to fully converted expressions, that are not part of the
124+
* regular parent/child relation traversal.
125+
*/
126+
PrintASTNode getChild(int childIndex) {
127+
result = getChildInternal(childIndex)
128+
or
129+
exists(int nonConvertedIndex, int nextIdx, Expr expr |
130+
nextIdx = max(int idx | exists(this.getChildInternal(idx))) + 1 and
131+
exists(getChild(nonConvertedIndex)) and
132+
childIndex - nextIdx = nonConvertedIndex and
133+
expr = getChild(nonConvertedIndex).(ASTNode).getAST()
134+
|
135+
expr.getFullyConverted() instanceof Conversion and
136+
result.(ASTNode).getAST() = expr.getFullyConverted() and
137+
not expr instanceof Conversion
138+
)
139+
}
120140

121141
/**
122142
* Holds if this node should be printed in the output. By default, all nodes
@@ -154,11 +174,29 @@ class PrintASTNode extends TPrintASTNode {
154174
* default, this is just the index of the child, but subclasses can override
155175
* this.
156176
*/
157-
string getChildEdgeLabel(int childIndex) {
177+
string getChildEdgeLabelInternal(int childIndex) {
158178
exists(getChild(childIndex)) and
159179
result = childIndex.toString()
160180
}
161181

182+
/**
183+
* Gets the label for the edge from this node to the specified child,
184+
* including labels for edges to nodes that represent conversions.
185+
*/
186+
string getChildEdgeLabel(int childIndex) {
187+
exists(getChildInternal(childIndex)) and
188+
result = getChildEdgeLabelInternal(childIndex)
189+
or
190+
not exists(getChildInternal(childIndex)) and
191+
exists(getChild(childIndex)) and
192+
exists(int nonConvertedIndex, int nextIdx |
193+
nextIdx = max(int idx | exists(this.getChildInternal(idx))) + 1 and
194+
childIndex - nextIdx = nonConvertedIndex
195+
|
196+
result = getChildEdgeLabelInternal(nonConvertedIndex) + " converted"
197+
)
198+
}
199+
162200
/**
163201
* Gets the `Function` that contains this node.
164202
*/
@@ -205,9 +243,7 @@ class ExprNode extends ASTNode {
205243

206244
ExprNode() { expr = ast }
207245

208-
override ASTNode getChild(int childIndex) {
209-
result.getAST() = expr.getChild(childIndex).getFullyConverted()
210-
}
246+
override ASTNode getChildInternal(int childIndex) { result.getAST() = expr.getChild(childIndex) }
211247

212248
override string getProperty(string key) {
213249
result = super.getProperty(key)
@@ -249,10 +285,11 @@ class ConversionNode extends ExprNode {
249285

250286
override ASTNode getChild(int childIndex) {
251287
childIndex = 0 and
252-
result.getAST() = conv.getExpr()
288+
result.getAST() = conv.getExpr() and
289+
conv.getExpr() instanceof Conversion
253290
}
254291

255-
override string getChildEdgeLabel(int childIndex) { childIndex = 0 and result = "expr" }
292+
override string getChildEdgeLabelInternal(int childIndex) { childIndex = 0 and result = "expr" }
256293
}
257294

258295
/**
@@ -280,7 +317,7 @@ class DeclarationEntryNode extends BaseASTNode, TDeclarationEntryNode {
280317

281318
DeclarationEntryNode() { this = TDeclarationEntryNode(declStmt, ast) }
282319

283-
override PrintASTNode getChild(int childIndex) { none() }
320+
override PrintASTNode getChildInternal(int childIndex) { none() }
284321

285322
override string getProperty(string key) {
286323
result = BaseASTNode.super.getProperty(key)
@@ -296,12 +333,12 @@ class DeclarationEntryNode extends BaseASTNode, TDeclarationEntryNode {
296333
class VariableDeclarationEntryNode extends DeclarationEntryNode {
297334
override VariableDeclarationEntry ast;
298335

299-
override ASTNode getChild(int childIndex) {
336+
override ASTNode getChildInternal(int childIndex) {
300337
childIndex = 0 and
301338
result.getAST() = ast.getVariable().getInitializer()
302339
}
303340

304-
override string getChildEdgeLabel(int childIndex) { childIndex = 0 and result = "init" }
341+
override string getChildEdgeLabelInternal(int childIndex) { childIndex = 0 and result = "init" }
305342
}
306343

307344
/**
@@ -312,11 +349,11 @@ class StmtNode extends ASTNode {
312349

313350
StmtNode() { stmt = ast }
314351

315-
override BaseASTNode getChild(int childIndex) {
352+
override BaseASTNode getChildInternal(int childIndex) {
316353
exists(Locatable child |
317354
child = stmt.getChild(childIndex) and
318355
(
319-
result.getAST() = child.(Expr).getFullyConverted() or
356+
result.getAST() = child.(Expr) or
320357
result.getAST() = child.(Stmt)
321358
)
322359
)
@@ -331,7 +368,7 @@ class DeclStmtNode extends StmtNode {
331368

332369
DeclStmtNode() { declStmt = stmt }
333370

334-
override DeclarationEntryNode getChild(int childIndex) {
371+
override DeclarationEntryNode getChildInternal(int childIndex) {
335372
exists(DeclarationEntry entry |
336373
declStmt.getDeclarationEntry(childIndex) = entry and
337374
result = TDeclarationEntryNode(declStmt, entry)
@@ -347,7 +384,7 @@ class ParameterNode extends ASTNode {
347384

348385
ParameterNode() { param = ast }
349386

350-
final override PrintASTNode getChild(int childIndex) { none() }
387+
final override PrintASTNode getChildInternal(int childIndex) { none() }
351388

352389
final override string getProperty(string key) {
353390
result = super.getProperty(key)
@@ -365,12 +402,12 @@ class InitializerNode extends ASTNode {
365402

366403
InitializerNode() { init = ast }
367404

368-
override ASTNode getChild(int childIndex) {
405+
override ASTNode getChildInternal(int childIndex) {
369406
childIndex = 0 and
370-
result.getAST() = init.getExpr().getFullyConverted()
407+
result.getAST() = init.getExpr()
371408
}
372409

373-
override string getChildEdgeLabel(int childIndex) {
410+
override string getChildEdgeLabelInternal(int childIndex) {
374411
childIndex = 0 and
375412
result = "expr"
376413
}
@@ -388,7 +425,9 @@ class ParametersNode extends PrintASTNode, TParametersNode {
388425

389426
final override Location getLocation() { result = getRepresentativeLocation(func) }
390427

391-
override ASTNode getChild(int childIndex) { result.getAST() = func.getParameter(childIndex) }
428+
override ASTNode getChildInternal(int childIndex) {
429+
result.getAST() = func.getParameter(childIndex)
430+
}
392431

393432
/**
394433
* Gets the `Function` for which this node represents the parameters.
@@ -408,7 +447,7 @@ class ConstructorInitializersNode extends PrintASTNode, TConstructorInitializers
408447

409448
final override Location getLocation() { result = getRepresentativeLocation(ctor) }
410449

411-
final override ASTNode getChild(int childIndex) {
450+
final override ASTNode getChildInternal(int childIndex) {
412451
result.getAST() = ctor.getInitializer(childIndex)
413452
}
414453

@@ -430,7 +469,7 @@ class DestructorDestructionsNode extends PrintASTNode, TDestructorDestructionsNo
430469

431470
final override Location getLocation() { result = getRepresentativeLocation(dtor) }
432471

433-
final override ASTNode getChild(int childIndex) {
472+
final override ASTNode getChildInternal(int childIndex) {
434473
result.getAST() = dtor.getDestruction(childIndex)
435474
}
436475

@@ -450,7 +489,7 @@ class FunctionNode extends ASTNode {
450489

451490
override string toString() { result = qlClass(func) + getIdentityString(func) }
452491

453-
override PrintASTNode getChild(int childIndex) {
492+
override PrintASTNode getChildInternal(int childIndex) {
454493
childIndex = 0 and
455494
result.(ParametersNode).getFunction() = func
456495
or
@@ -464,7 +503,7 @@ class FunctionNode extends ASTNode {
464503
result.(DestructorDestructionsNode).getDestructor() = func
465504
}
466505

467-
override string getChildEdgeLabel(int childIndex) {
506+
override string getChildEdgeLabelInternal(int childIndex) {
468507
childIndex = 0 and result = "params"
469508
or
470509
childIndex = 1 and result = "initializations"
@@ -504,7 +543,7 @@ class ClassAggregateLiteralNode extends ExprNode {
504543

505544
ClassAggregateLiteralNode() { list = ast }
506545

507-
override string getChildEdgeLabel(int childIndex) {
546+
override string getChildEdgeLabelInternal(int childIndex) {
508547
exists(Field field |
509548
list.getFieldExpr(field) = list.getChild(childIndex) and
510549
result = "." + field.getName()
@@ -520,7 +559,7 @@ class ArrayAggregateLiteralNode extends ExprNode {
520559

521560
ArrayAggregateLiteralNode() { list = ast }
522561

523-
override string getChildEdgeLabel(int childIndex) {
562+
override string getChildEdgeLabelInternal(int childIndex) {
524563
exists(int elementIndex |
525564
list.getElementExpr(elementIndex) = list.getChild(childIndex) and
526565
result = "[" + elementIndex.toString() + "]"

0 commit comments

Comments
 (0)