Skip to content

Commit c8f2c7c

Browse files
committed
C++: Fix printing of concept id type arguments in PrintAST
If a type would be used in multiple places in the AST, rendering of the AST would be broken.
1 parent bf19755 commit c8f2c7c

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

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

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ private newtype TPrintAstNode =
114114
TConceptIdExprArgumentsNode(ConceptIdExpr concept) {
115115
shouldPrintDeclaration(getAnEnclosingDeclaration(concept))
116116
} or
117+
TConceptIdExprTypeArgumentNode(Type type, ConceptIdExpr concept, int childIndex) {
118+
type = concept.getTemplateArgument(childIndex)
119+
} or
117120
TConstructorInitializersNode(Constructor ctor) {
118121
ctor.hasEntryPoint() and
119122
shouldPrintDeclaration(ctor)
@@ -601,19 +604,6 @@ class ParameterNode extends AstNode {
601604
}
602605
}
603606

604-
/**
605-
* A node representing a `Type`.
606-
*/
607-
class TypeNode extends AstNode {
608-
Type t;
609-
610-
TypeNode() { t = ast }
611-
612-
final override PrintAstNode getChildInternal(int childIndex) { none() }
613-
614-
final override string getChildAccessorPredicateInternal(int childIndex) { none() }
615-
}
616-
617607
/**
618608
* A node representing an `Initializer`.
619609
*/
@@ -645,8 +635,12 @@ class ConceptIdExprArgumentsNode extends PrintAstNode, TConceptIdExprArgumentsNo
645635

646636
final override Location getLocation() { result = getRepresentativeLocation(concept) }
647637

648-
override AstNode getChildInternal(int childIndex) {
649-
result.getAst() = concept.getTemplateArgument(childIndex)
638+
override PrintAstNode getChildInternal(int childIndex) {
639+
exists(Locatable arg | arg = concept.getTemplateArgument(childIndex) |
640+
result.(ConceptIdExprTypeArgumentNode).isArgumentNode(arg, concept, childIndex)
641+
or
642+
result.(ExprNode).getAst() = arg
643+
)
650644
}
651645

652646
override string getChildAccessorPredicateInternal(int childIndex) {
@@ -660,6 +654,32 @@ class ConceptIdExprArgumentsNode extends PrintAstNode, TConceptIdExprArgumentsNo
660654
final ConceptIdExpr getConceptIdExpr() { result = concept }
661655
}
662656

657+
/**
658+
* A node representing a type argument of a `ConceptIdExpr`.
659+
*/
660+
class ConceptIdExprTypeArgumentNode extends PrintAstNode, TConceptIdExprTypeArgumentNode {
661+
Type type;
662+
ConceptIdExpr concept;
663+
int index;
664+
665+
ConceptIdExprTypeArgumentNode() { this = TConceptIdExprTypeArgumentNode(type, concept, index) }
666+
667+
final override string toString() { result = qlClass(type) + type.toString() }
668+
669+
final override Location getLocation() { result = getRepresentativeLocation(type) }
670+
671+
override AstNode getChildInternal(int childIndex) { none() }
672+
673+
override string getChildAccessorPredicateInternal(int childIndex) { none() }
674+
675+
/**
676+
* Holds if `t` is the `i`th template argument of `c`.
677+
*/
678+
predicate isArgumentNode(Type t, ConceptIdExpr c, int i) {
679+
type = t and concept = c and index = i
680+
}
681+
}
682+
663683
/**
664684
* A node representing the parameters of a `Function`.
665685
*/

0 commit comments

Comments
 (0)