Skip to content

Commit b67d4be

Browse files
committed
Compute size of initialized unbounded arrays
Signed-off-by: Roberto Raggi <[email protected]>
1 parent d6b32de commit b67d4be

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

src/parser/cxx/parser.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5368,6 +5368,28 @@ auto Parser::parse_init_declarator(InitDeclaratorAST*& yyast,
53685368

53695369
if (auto var = symbol_cast<VariableSymbol>(ast->symbol)) {
53705370
var->setInitializer(initializer);
5371+
5372+
if (auto ty = type_cast<UnboundedArrayType>(ast->symbol->type())) {
5373+
BracedInitListAST* bracedInitList = nullptr;
5374+
5375+
if (auto init = ast_cast<BracedInitListAST>(ast->initializer)) {
5376+
bracedInitList = init;
5377+
} else if (auto init = ast_cast<EqualInitializerAST>(ast->initializer)) {
5378+
bracedInitList = ast_cast<BracedInitListAST>(init->expression);
5379+
}
5380+
5381+
if (bracedInitList) {
5382+
const auto count =
5383+
std::ranges::distance(ListView{bracedInitList->expressionList});
5384+
5385+
if (count > 0) {
5386+
const auto arrayType =
5387+
control()->getBoundedArrayType(ty->elementType(), count);
5388+
5389+
symbol->setType(arrayType);
5390+
}
5391+
}
5392+
}
53715393
}
53725394

53735395
return true;

tests/unit_tests/ast/for_range_statement_02.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ int main() {
5757
// CHECK-NEXT: identifier: key
5858
// CHECK-NEXT: name-id
5959
// CHECK-NEXT: identifier: value
60-
// CHECK-NEXT: range-initializer: id-expression [lvalue int [][2]]
60+
// CHECK-NEXT: range-initializer: id-expression [lvalue int [1][2]]
6161
// CHECK-NEXT: unqualified-id: name-id
6262
// CHECK-NEXT: identifier: map
6363
// CHECK-NEXT: statement: compound-statement

tests/unit_tests/sema/sizeof_01.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ static_assert(sizeof(long) == 4);
2222
static_assert(sizeof(unsigned long) == 4);
2323
static_assert(sizeof(long double) == 16);
2424
#endif
25+
26+
int elements[] = {1, 2, 3, 4, 5};
27+
static_assert(sizeof(elements) == 5 * sizeof(int));
28+
29+
double d[]{1.0, 2.0, 3.0};
30+
static_assert(sizeof(d) == 3 * sizeof(double));

0 commit comments

Comments
 (0)