Skip to content

Commit

Permalink
C++: Fix printing of concept id type arguments in PrintAST
Browse files Browse the repository at this point in the history
If a type would be used in multiple places in the AST, rendering of the
AST would be broken.
  • Loading branch information
jketema committed Dec 26, 2024
1 parent bf19755 commit 1ed4f74
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions cpp/ql/lib/semmle/code/cpp/PrintAST.qll
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ private newtype TPrintAstNode =
TConceptIdExprArgumentsNode(ConceptIdExpr concept) {
shouldPrintDeclaration(getAnEnclosingDeclaration(concept))
} or
TConceptIdExprTypeArgumentNode(Type type, ConceptIdExpr concept, int childIndex) {
type = concept.getTemplateArgument(childIndex)
} or
TConstructorInitializersNode(Constructor ctor) {
ctor.hasEntryPoint() and
shouldPrintDeclaration(ctor)
Expand Down Expand Up @@ -601,19 +604,6 @@ class ParameterNode extends AstNode {
}
}

/**
* A node representing a `Type`.
*/
class TypeNode extends AstNode {
Type t;

TypeNode() { t = ast }

final override PrintAstNode getChildInternal(int childIndex) { none() }

final override string getChildAccessorPredicateInternal(int childIndex) { none() }
}

/**
* A node representing an `Initializer`.
*/
Expand Down Expand Up @@ -645,8 +635,12 @@ class ConceptIdExprArgumentsNode extends PrintAstNode, TConceptIdExprArgumentsNo

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

override AstNode getChildInternal(int childIndex) {
result.getAst() = concept.getTemplateArgument(childIndex)
override PrintAstNode getChildInternal(int childIndex) {
exists(Locatable arg | arg = concept.getTemplateArgument(childIndex) |
result.(ConceptIdExprTypeArgumentNode).isArgumentNode(arg, concept, childIndex)
or
result.(ExprNode).getAst() = arg
)
}

override string getChildAccessorPredicateInternal(int childIndex) {
Expand All @@ -660,6 +654,29 @@ class ConceptIdExprArgumentsNode extends PrintAstNode, TConceptIdExprArgumentsNo
final ConceptIdExpr getConceptIdExpr() { result = concept }
}

class ConceptIdExprTypeArgumentNode extends PrintAstNode, TConceptIdExprTypeArgumentNode {
Type type;
ConceptIdExpr concept;
int index;

ConceptIdExprTypeArgumentNode() { this = TConceptIdExprTypeArgumentNode(type, concept, index) }

final override string toString() { result = qlClass(type) + type.toString() }

final override Location getLocation() { result = getRepresentativeLocation(type) }

override AstNode getChildInternal(int childIndex) { none() }

override string getChildAccessorPredicateInternal(int childIndex) { none() }

/**
* Holds if `t` is the `i`th template argument of `c`.
*/
predicate isArgumentNode(Type t, ConceptIdExpr c, int i) {
type = t and concept = c and index = i
}
}

/**
* A node representing the parameters of a `Function`.
*/
Expand Down

0 comments on commit 1ed4f74

Please sign in to comment.