Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++: Support concept id expressions #18366

Draft
wants to merge 5 commits into
base: jketema/template-parameters-4
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,382 changes: 2,382 additions & 0 deletions cpp/downgrades/1a64d0cb5b948b23b6ec32a521de628ca814c335/old.dbscheme

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
description: Support concept id expressions
compatibility: full
concept_instantiation.rel: delete
is_type_constraint.rel: delete
5 changes: 5 additions & 0 deletions cpp/ql/lib/change-notes/2024-12-24-concept-id.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
category: feature
---
* A new class `ConceptIdExpr` was introduced, which represents C++20 concept id expressions.

105 changes: 103 additions & 2 deletions cpp/ql/lib/semmle/code/cpp/Concept.qll
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,104 @@ class NestedRequirementExpr extends Expr, @nested_requirement {

/**
* A C++ concept id expression.
*
* For example, if:
* ```cpp
* template<typename T, T X> concept C = ...;
* ...
* requires { C<int, 1>; };
* ```
* then `C<int, 1>` is a concept id expression that refers to
* the concept `C`.
*/
class ConceptIdExpr extends RequirementExpr, @concept_id {
override string toString() { result = "concept<...>" }
override string toString() {
exists(string name |
concept_templates(this.getConcept(), name, _) and
result = name + "<...>"
)
}

override string getAPrimaryQlClass() { result = "ConceptIdExpr" }

/**
* Holds if the concept id is used as a type constraint.
*
* In this case, the first template argument is implicit.
*/
predicate isTypeConstraint() { is_type_constraint(underlyingElement(this)) }

/**
* Gets the concept this concept id refers to.
*/
Concept getConcept() { concept_instantiation(underlyingElement(this), unresolveElement(result)) }

/**
* Gets a template argument passed to the concept.
*/
final Locatable getATemplateArgument() { result = this.getTemplateArgument(_) }

/**
* Gets the kind of a non-type template argument passed to the concept.
*/
final Locatable getATemplateArgumentKind() { result = this.getTemplateArgumentKind(_) }

/**
* Gets the `i`th template argument passed to the concept.
*
* For example, if:
* ```cpp
* template<typename T, T X> concept C = ...;
* ...
* requires { C<int, 1>; };
* ```
* then `getTemplateArgument(0)` yields `int`, and `getTemplateArgument(1)`
* yields `1`.
*
* If the concept id is a type constraint, then `getTemplateArgument(0)`
* will not yield a result.
*/
final Locatable getTemplateArgument(int index) {

Check warning

Code scanning / CodeQL

Missing QLDoc for parameter Warning

The QLDoc has no documentation for index, but the QLDoc mentions int
if exists(this.getTemplateArgumentValue(index))
then result = this.getTemplateArgumentValue(index)
else result = this.getTemplateArgumentType(index)
}

/**
* Gets the kind of the `i`th template argument value passed to the concept.
*
* For example, if:
* ```cpp
* template<typename T, T X> concept C = ...;
* ...
* requires { C<int, 1>; };
* ```
* then `getTemplateArgumentKind(1)` yields `int`, and there is no result for
* `getTemplateArgumentKind(0)`.
*/
final Locatable getTemplateArgumentKind(int index) {

Check warning

Code scanning / CodeQL

Missing QLDoc for parameter Warning

The QLDoc has no documentation for index, but the QLDoc mentions int
exists(this.getTemplateArgumentValue(index)) and
result = this.getTemplateArgumentType(index)
}

/**
* Gets the number of template arguments passed to the concept.
*/
final int getNumberOfTemplateArguments() {
result = count(int i | exists(this.getTemplateArgument(i)))
}

private Type getTemplateArgumentType(int index) {
exists(int i | if this.isTypeConstraint() then i = index - 1 else i = index |
concept_template_argument(underlyingElement(this), i, unresolveElement(result))
)
}

private Expr getTemplateArgumentValue(int index) {
exists(int i | if this.isTypeConstraint() then i = index - 1 else i = index |
concept_template_argument_value(underlyingElement(this), i, unresolveElement(result))
)
}
}

/**
Expand All @@ -177,7 +270,7 @@ class Concept extends Declaration, @concept_template {
override string getName() { concept_templates(underlyingElement(this), result, _) }

/**
* Get the constraint expression of the concept.
* Gets the constraint expression of the concept.
*
* For example, in
* ```cpp
Expand All @@ -187,4 +280,12 @@ class Concept extends Declaration, @concept_template {
* the constraint expression is `true`.
*/
Expr getExpr() { result.getParent() = this }

/**
* Gets a concept id expression that refers to this concept
*/
ConceptIdExpr getAReferringConceptIdExpr() {
this = result.getConcept() and
exists(result.getATemplateArgument())
}
}
2 changes: 1 addition & 1 deletion cpp/ql/lib/semmle/code/cpp/Declaration.qll
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ class Declaration extends Locatable, @declaration {
*
* `Foo<int, 1> bar;`
*
* Will have `getTemplateArgument())` return `int`, and
* Will have `getTemplateArgument(0)` return `int`, and
* `getTemplateArgument(1)` return `1`.
*/
final Locatable getTemplateArgument(int index) {
Expand Down
67 changes: 67 additions & 0 deletions cpp/ql/lib/semmle/code/cpp/PrintAST.qll
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ private Declaration getAnEnclosingDeclaration(Locatable ast) {
or
result = ast.(Initializer).getDeclaration()
or
exists(ConceptIdExpr concept | ast = concept.getATemplateArgument() |
result = concept.getEnclosingFunction()
)
or
result = ast
}

Expand All @@ -107,6 +111,9 @@ private newtype TPrintAstNode =
TRequiresExprParametersNode(RequiresExpr req) {
shouldPrintDeclaration(getAnEnclosingDeclaration(req))
} or
TConceptIdExprArgumentsNode(ConceptIdExpr concept) {
shouldPrintDeclaration(getAnEnclosingDeclaration(concept))
} or
TConstructorInitializersNode(Constructor ctor) {
ctor.hasEntryPoint() and
shouldPrintDeclaration(ctor)
Expand Down Expand Up @@ -357,6 +364,26 @@ class StringLiteralNode extends ExprNode {
override string getValue() { result = "\"" + escapeString(expr.getValue()) + "\"" }
}

/**
* A node representing a `ConceptIdExpr`.
*/
class ConceptIdExprNode extends ExprNode {
override ConceptIdExpr expr;

override PrintAstNode getChildInternal(int childIndex) {
result = super.getChildInternal(childIndex)
or
childIndex = -1 and
result.(ConceptIdExprArgumentsNode).getConceptIdExpr() = expr
}

override string getChildAccessorPredicateInternal(int childIndex) {
result = super.getChildAccessorPredicateInternal(childIndex)
or
childIndex = -1 and result = "<args>"
}
}

/**
* A node representing a `Conversion`.
*/
Expand Down Expand Up @@ -574,6 +601,19 @@ 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 All @@ -593,6 +633,33 @@ class InitializerNode extends AstNode {
}
}

/**
* A node representing the arguments of a `ConceptIdExpr`.
*/
class ConceptIdExprArgumentsNode extends PrintAstNode, TConceptIdExprArgumentsNode {
ConceptIdExpr concept;

ConceptIdExprArgumentsNode() { this = TConceptIdExprArgumentsNode(concept) }

final override string toString() { result = "" }

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

override AstNode getChildInternal(int childIndex) {
result.getAst() = concept.getTemplateArgument(childIndex)
}

override string getChildAccessorPredicateInternal(int childIndex) {
exists(this.getChildInternal(childIndex)) and
result = "getTemplateArgument(" + childIndex.toString() + ")"
}

/**
* Gets the `ConceptIdExpr` for which this node represents the parameters.
*/
final ConceptIdExpr getConceptIdExpr() { result = concept }
}

/**
* A node representing the parameters of a `Function`.
*/
Expand Down
5 changes: 5 additions & 0 deletions cpp/ql/lib/semmlecode.cpp.dbscheme
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,11 @@ concept_templates(
string name: string ref,
int location: @location_default ref
);
concept_instantiation(
unique int to: @concept_id ref,
int from: @concept_template ref
);
is_type_constraint(int concept_id: @concept_id ref);
concept_template_argument(
int concept_id: @concept ref,
int index: int ref,
Expand Down
Loading
Loading