Skip to content

Commit 24914ef

Browse files
authored
Merge pull request #16956 from github/calumgrant/cpp20-array-sizes
C++: Test C++20 implicit array sizes.
2 parents 9794269 + 13c25a4 commit 24914ef

File tree

6 files changed

+40
-0
lines changed

6 files changed

+40
-0
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+
* The class `NewArrayExpr` adds a predicate `getArraySize()` to allow a more convenient way to access the static size of the array when the extent is missing.

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,16 @@ class NewArrayExpr extends NewOrNewArrayExpr, @new_array_expr {
949949
* gives nothing, as the 10 is considered part of the type.
950950
*/
951951
Expr getExtent() { result = this.getChild(2) }
952+
953+
/**
954+
* Gets the number of elements in the array, if available.
955+
*
956+
* For example, `new int[]{1,2,3}` has an array size of 3.
957+
*/
958+
int getArraySize() {
959+
result = this.getAllocatedType().(ArrayType).getArraySize() or
960+
result = this.getInitializer().(ArrayAggregateLiteral).getArraySize()
961+
}
952962
}
953963

954964
private class TDeleteOrDeleteArrayExpr = @delete_expr or @delete_array_expr;

cpp/ql/test/library-tests/array_sizes/arr2.expected

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
| file://:0:0:0:0 | char[6] | 6 |
22
| file://:0:0:0:0 | char[26] | 26 |
3+
| file://:0:0:0:0 | char[] | |
4+
| file://:0:0:0:0 | const char[6] | 6 |
5+
| file://:0:0:0:0 | double[0] | 0 |
6+
| file://:0:0:0:0 | double[3] | 3 |
7+
| file://:0:0:0:0 | double[4] | 4 |
8+
| file://:0:0:0:0 | double[] | |
39
| file://:0:0:0:0 | int[1] | 1 |
410
| file://:0:0:0:0 | int[11] | 11 |
511
| file://:0:0:0:0 | long[] | |
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// semmle-extractor-options: -std=c++20
2+
double a1[]{1,2,3};
3+
double* p1 = new double[]{1,2,3};
4+
double* p2 = new double[0]{};
5+
double* p3 = new double[]{};
6+
char c[]{"Hello"};
7+
char* d = new char[]{"Hello"};
8+
double a2[](1,2,3);
9+
double* p4 = new double[](1,2,3);
10+
double* p5 = new double[4]{1,2}; // Size mismatch
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
| implicit_sizes.cpp:3:14:3:32 | new[] | 3 |
2+
| implicit_sizes.cpp:4:14:4:28 | new[] | 0 |
3+
| implicit_sizes.cpp:5:14:5:27 | new[] | 0 |
4+
| implicit_sizes.cpp:7:11:7:29 | new[] | 6 |
5+
| implicit_sizes.cpp:9:14:9:32 | new[] | 3 |
6+
| implicit_sizes.cpp:10:14:10:31 | new[] | 4 |
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import cpp
2+
3+
from NewArrayExpr nae
4+
select nae, nae.getArraySize()

0 commit comments

Comments
 (0)