Skip to content

Commit ecf3c53

Browse files
committed
C++: Introduce SizeofPackOperator subclasses for expressions and types
Note that template template parameters are considered types in this context.
1 parent 51f625b commit ecf3c53

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: feature
3+
---
4+
* A new classes `SizeofPackExprOperator` and `SizeofPackTypeOperator` were introduced, which represent the C++ `sizeof...` operator taking an expression and a type argument, respectively.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,8 @@ private predicate namedExprChildPredicates(Expr expr, Element ele, string pred)
10561056
or
10571057
expr.(SizeofExprOperator).getExprOperand() = ele and pred = "getExprOperand()"
10581058
or
1059+
expr.(SizeofPackExprOperator).getExprOperand() = ele and pred = "getExprOperand()"
1060+
or
10591061
expr.(StmtExpr).getStmt() = ele and pred = "getStmt()"
10601062
or
10611063
expr.(ThrowExpr).getExpr() = ele and pred = "getExpr()"

cpp/ql/lib/semmle/code/cpp/exprs/Cast.qll

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,8 @@ class TypeidOperator extends Expr, @type_id {
684684
}
685685

686686
/**
687-
* A C++11 `sizeof...` expression which determines the size of a template parameter pack.
687+
* A C++11 `sizeof...` expression which determines the size of a template
688+
* parameter pack.
688689
*
689690
* This expression only appears in templates themselves - in any actual
690691
* instantiations, "sizeof...(x)" will be replaced by its integer value.
@@ -694,15 +695,56 @@ class TypeidOperator extends Expr, @type_id {
694695
* ```
695696
*/
696697
class SizeofPackOperator extends Expr, @sizeof_pack {
697-
override string toString() { result = "sizeof...(...)" }
698-
699-
override string getAPrimaryQlClass() { result = "SizeofPackOperator" }
700-
701698
override predicate mayBeImpure() { none() }
702699

703700
override predicate mayBeGloballyImpure() { none() }
704701
}
705702

703+
/**
704+
* A C++11 `sizeof...` expression which determines the size of a template
705+
* parameter pack and whose operand is an expression.
706+
*
707+
* This expression only appears in templates themselves - in any actual
708+
* instantiations, "sizeof...(x)" will be replaced by its integer value.
709+
* ```
710+
* template < typename... T >
711+
* int count ( T &&... t ) { return sizeof... ( t ); }
712+
* ```
713+
*/
714+
class SizeofPackExprOperator extends SizeofPackOperator {
715+
SizeofPackExprOperator() { exists(this.getChild(0)) }
716+
717+
override string getAPrimaryQlClass() { result = "SizeofPackExprOperator" }
718+
719+
/** Gets the contained expression. */
720+
Expr getExprOperand() { result = this.getChild(0) }
721+
722+
override string toString() { result = "sizeof...(<expr>)" }
723+
}
724+
725+
/**
726+
* A C++11 `sizeof...` expression which determines the size of a template
727+
* parameter pack and whose operand is an type name or a template template
728+
* parameter.
729+
*
730+
* This expression only appears in templates themselves - in any actual
731+
* instantiations, "sizeof...(x)" will be replaced by its integer value.
732+
* ```
733+
* template < typename... T >
734+
* int count ( T &&... t ) { return sizeof... ( T ); }
735+
* ```
736+
*/
737+
class SizeofPackTypeOperator extends SizeofPackOperator {
738+
SizeofPackTypeOperator() { sizeof_bind(underlyingElement(this), _) }
739+
740+
override string getAPrimaryQlClass() { result = "SizeofPackTypeOperator" }
741+
742+
/** Gets the contained type. */
743+
Type getTypeOperand() { sizeof_bind(underlyingElement(this), unresolveElement(result)) }
744+
745+
override string toString() { result = "sizeof...(" + this.getTypeOperand().getName() + ")" }
746+
}
747+
706748
/**
707749
* A C/C++ sizeof expression.
708750
*/

0 commit comments

Comments
 (0)