Skip to content

Commit

Permalink
[clang] Sequence C++20 Parenthesized List Init (#83476)
Browse files Browse the repository at this point in the history
Parenthesized list intializers are sequenced operations, see C++20
[decl.init]p16.5 and [decl.init]p16.6.2.2 for more details.

Fixes #83474
  • Loading branch information
vapdrs committed Mar 5, 2024
1 parent 0d8e16a commit 4ce737b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
30 changes: 14 additions & 16 deletions clang/lib/Sema/SemaChecking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17624,31 +17624,29 @@ class SequenceChecker : public ConstEvaluatedExprVisitor<SequenceChecker> {
return VisitExpr(CCE);

// In C++11, list initializations are sequenced.
SmallVector<SequenceTree::Seq, 32> Elts;
SequenceTree::Seq Parent = Region;
for (CXXConstructExpr::const_arg_iterator I = CCE->arg_begin(),
E = CCE->arg_end();
I != E; ++I) {
Region = Tree.allocate(Parent);
Elts.push_back(Region);
Visit(*I);
}

// Forget that the initializers are sequenced.
Region = Parent;
for (unsigned I = 0; I < Elts.size(); ++I)
Tree.merge(Elts[I]);
SequenceExpressionsInOrder(
llvm::ArrayRef(CCE->getArgs(), CCE->getNumArgs()));
}

void VisitInitListExpr(const InitListExpr *ILE) {
if (!SemaRef.getLangOpts().CPlusPlus11)
return VisitExpr(ILE);

// In C++11, list initializations are sequenced.
SequenceExpressionsInOrder(ILE->inits());
}

void VisitCXXParenListInitExpr(const CXXParenListInitExpr *PLIE) {
// C++20 parenthesized list initializations are sequenced. See C++20
// [decl.init.general]p16.5 and [decl.init.general]p16.6.2.2.
SequenceExpressionsInOrder(PLIE->getInitExprs());
}

private:
void SequenceExpressionsInOrder(ArrayRef<const Expr *> ExpressionList) {
SmallVector<SequenceTree::Seq, 32> Elts;
SequenceTree::Seq Parent = Region;
for (unsigned I = 0; I < ILE->getNumInits(); ++I) {
const Expr *E = ILE->getInit(I);
for (const Expr *E : ExpressionList) {
if (!E)
continue;
Region = Tree.allocate(Parent);
Expand Down
15 changes: 15 additions & 0 deletions clang/test/SemaCXX/warn-unsequenced-paren-list-init.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// RUN: %clang_cc1 -fsyntax-only -std=c++20 -Wno-unused -Wunsequenced -verify %s

struct A {
int x, y;
};

void test() {
int a = 0;

A agg1( a++, a++ ); // no warning
A agg2( a++ + a, a++ ); // expected-warning {{unsequenced modification and access to 'a'}}

int arr1[]( a++, a++ ); // no warning
int arr2[]( a++ + a, a++ ); // expected-warning {{unsequenced modification and access to 'a'}}
}

0 comments on commit 4ce737b

Please sign in to comment.