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 templates #18362

Draft
wants to merge 4 commits into
base: jketema/template-parameters-3
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,377 changes: 2,377 additions & 0 deletions cpp/downgrades/859cb83a827782f1d83b47320a61fb39b52f81be/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,5 @@
description: Support concept templates
compatibility: full
concept_templates.rel: delete
concept_template_argument.rel: delete
concept_template_argument_value.rel: delete
5 changes: 5 additions & 0 deletions cpp/ql/lib/change-notes/2024-12-23-concept-template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
category: feature
---
* A new class `Concept` was introduced, which represents C++20 concepts.
* The `getTemplateArgumentType` and `getTemplateArgumentValue` predicates of the `Declaration` class now also yield template arguments of concepts.
29 changes: 29 additions & 0 deletions cpp/ql/lib/semmle/code/cpp/Concept.qll
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,32 @@ class ConceptIdExpr extends RequirementExpr, @concept_id {

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

/**
* A C++ concept.
*
* For example:
* ```cpp
* template<class T>
* concept C = true;
* ```
*/
class Concept extends Declaration, @concept_template {
override string getAPrimaryQlClass() { result = "Concept" }

override Location getLocation() { concept_templates(underlyingElement(this), _, result) }

override string getName() { concept_templates(underlyingElement(this), result, _) }

/**
* Get the constraint expression of the concept.
*
* For example, in
* ```cpp
* template<class T>
* concept C = true;
* ```
* the constraint expression is `true`.
*/
Expr getExpr() { result.getParent() = this }
}
4 changes: 4 additions & 0 deletions cpp/ql/lib/semmle/code/cpp/Declaration.qll
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ class Declaration extends Locatable, @declaration {
variable_template_argument(underlyingElement(this), index, unresolveElement(result))
or
template_template_argument(underlyingElement(this), index, unresolveElement(result))
or
concept_template_argument(underlyingElement(this), index, unresolveElement(result))
}

private Expr getTemplateArgumentValue(int index) {
Expand All @@ -289,6 +291,8 @@ class Declaration extends Locatable, @declaration {
variable_template_argument_value(underlyingElement(this), index, unresolveElement(result))
or
template_template_argument_value(underlyingElement(this), index, unresolveElement(result))
or
concept_template_argument_value(underlyingElement(this), index, unresolveElement(result))
}
}

Expand Down
21 changes: 20 additions & 1 deletion cpp/ql/lib/semmlecode.cpp.dbscheme
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,24 @@ template_template_argument_value(
int arg_value: @expr ref
);

@concept = @concept_template | @concept_id;

concept_templates(
unique int concept_id: @concept_template,
string name: string ref,
int location: @location_default ref
);
concept_template_argument(
int concept_id: @concept ref,
int index: int ref,
int arg_type: @type ref
);
concept_template_argument_value(
int type_id: @concept ref,
int index: int ref,
int arg_value: @expr ref
);

routinetypes(
unique int id: @routinetype,
int return_type: @type ref
Expand Down Expand Up @@ -1106,7 +1124,8 @@ frienddecls(
| @declaredtype
| @variable
| @enumconstant
| @frienddecl;
| @frienddecl
| @concept_template;

@member = @membervariable
| @function
Expand Down
Loading
Loading