-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang] Sequence C++20 Parenthesized List Init #83476
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write If you have received no comments on your PR for a week, you can request a review If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-clang Author: Douglas Deslauriers (vapdrs) ChangesParenthesized list intializers are sequenced operations, see C++20 [decl.init]p17.5 and [decl.init]p17.6.2.2 for more details. Fixes #83474 Full diff: https://github.com/llvm/llvm-project/pull/83476.diff 2 Files Affected:
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 016e9830662042..eaa45378f8eb49 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -17626,6 +17626,25 @@ class SequenceChecker : public ConstEvaluatedExprVisitor<SequenceChecker> {
for (unsigned I = 0; I < Elts.size(); ++I)
Tree.merge(Elts[I]);
}
+
+ void VisitCXXParenListInitExpr(const CXXParenListInitExpr *PLIE) {
+ // C++20 parenthesized list initializations are sequenced. See C++20
+ // [decl.init]p17.5 and [decl.init]p17.6.2.2
+ SmallVector<SequenceTree::Seq, 32> Elts;
+ SequenceTree::Seq Parent = Region;
+ for (const Expr *E : PLIE->getInitExprs()) {
+ if (!E)
+ continue;
+ Region = Tree.allocate(Parent);
+ Elts.push_back(Region);
+ Visit(E);
+ }
+
+ // Forget that the initializers are sequenced.
+ Region = Parent;
+ for (unsigned I = 0; I < Elts.size(); ++I)
+ Tree.merge(Elts[I]);
+ }
};
SequenceChecker::UsageInfo::UsageInfo() = default;
diff --git a/clang/test/SemaCXX/warn-unsequenced-paren-list-init.cpp b/clang/test/SemaCXX/warn-unsequenced-paren-list-init.cpp
new file mode 100644
index 00000000000000..5aeeb45f81e226
--- /dev/null
+++ b/clang/test/SemaCXX/warn-unsequenced-paren-list-init.cpp
@@ -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'}}
+}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
[I'm not sure when I'll have time to circle back to this, so I'd be happy for someone else to finish the review and approve.]
clang/lib/Sema/SemaChecking.cpp
Outdated
Region = Parent; | ||
for (unsigned I = 0; I < Elts.size(); ++I) | ||
Tree.merge(Elts[I]); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it feasible to factor out the common code between this and VisitInitListExpr
? This looks nearly identical.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can do you one better and also factor out code in VisitCXXConstructExpr
too.
82e371b
to
85958bd
Compare
85958bd
to
cc14e7a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM after addressing comment.
clang/lib/Sema/SemaChecking.cpp
Outdated
Region = Parent; | ||
for (unsigned I = 0; I < Elts.size(); ++I) | ||
Tree.merge(Elts[I]); | ||
SequenceExpressionsInOrder({CCE->getArgs(), CCE->getNumArgs()}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took me a bit of checking to convince myself this was doing the right thing. It might be nice to refactor CXXConstructExpr
to have an a member that does the same as ILE->inits()
and returns an ArrayRef
. It looks like do similar things to create an ArrayRef
in other places as well but probably should be a second PR.
Maybe change this to llvm::ArrayRef(CCE->getArgs(), CCE->getNumArgs())
to make it more explicit for now.
Parenthesized list intializers are sequenced operations, see C++20 [decl.init]p16.5 and [decl.init]p16.6.2.2 for more details. Fixes llvm#83474
cc14e7a
to
f66254c
Compare
@vapdrs Do you need us to merge that for you? Thanks |
Yes, I do not have write access to the repository. Thank you! |
@vapdrs Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested Please check whether problems have been caused by your change specifically, as How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
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