Skip to content

Commit 82e371b

Browse files
committed
[clang] Sequence C++20 Parenthesized List Init
Parenthesized list intializers are sequenced operations, see C++20 [decl.init]p17.5 and [decl.init]p17.6.2.2 for more details. Fixes #83474
1 parent 856ce49 commit 82e371b

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

clang/lib/Sema/SemaChecking.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17626,6 +17626,25 @@ class SequenceChecker : public ConstEvaluatedExprVisitor<SequenceChecker> {
1762617626
for (unsigned I = 0; I < Elts.size(); ++I)
1762717627
Tree.merge(Elts[I]);
1762817628
}
17629+
17630+
void VisitCXXParenListInitExpr(const CXXParenListInitExpr *PLIE) {
17631+
// C++20 parenthesized list initializations are sequenced. See C++20
17632+
// [decl.init]p17.5 and [decl.init]p17.6.2.2
17633+
SmallVector<SequenceTree::Seq, 32> Elts;
17634+
SequenceTree::Seq Parent = Region;
17635+
for (const Expr *E : PLIE->getInitExprs()) {
17636+
if (!E)
17637+
continue;
17638+
Region = Tree.allocate(Parent);
17639+
Elts.push_back(Region);
17640+
Visit(E);
17641+
}
17642+
17643+
// Forget that the initializers are sequenced.
17644+
Region = Parent;
17645+
for (unsigned I = 0; I < Elts.size(); ++I)
17646+
Tree.merge(Elts[I]);
17647+
}
1762917648
};
1763017649

1763117650
SequenceChecker::UsageInfo::UsageInfo() = default;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %clang_cc1 -fsyntax-only -std=c++20 -Wno-unused -Wunsequenced -verify %s
2+
3+
struct A {
4+
int x, y;
5+
};
6+
7+
void test() {
8+
int a = 0;
9+
10+
A agg1( a++, a++ ); // no warning
11+
A agg2( a++ + a, a++ ); // expected-warning {{unsequenced modification and access to 'a'}}
12+
13+
int arr1[]( a++, a++ ); // no warning
14+
int arr2[]( a++ + a, a++ ); // expected-warning {{unsequenced modification and access to 'a'}}
15+
}

0 commit comments

Comments
 (0)