Skip to content

[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

Merged
merged 1 commit into from
Mar 5, 2024

Conversation

vapdrs
Copy link
Contributor

@vapdrs vapdrs commented Feb 29, 2024

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

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

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
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

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.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Feb 29, 2024
@llvmbot
Copy link
Member

llvmbot commented Feb 29, 2024

@llvm/pr-subscribers-clang

Author: Douglas Deslauriers (vapdrs)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/83476.diff

2 Files Affected:

  • (modified) clang/lib/Sema/SemaChecking.cpp (+19)
  • (added) clang/test/SemaCXX/warn-unsequenced-paren-list-init.cpp (+15)
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'}}
+}

Copy link
Collaborator

@zygoloid zygoloid left a 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.]

Region = Parent;
for (unsigned I = 0; I < Elts.size(); ++I)
Tree.merge(Elts[I]);
}
Copy link
Collaborator

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.

Copy link
Contributor Author

@vapdrs vapdrs Feb 29, 2024

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.

@vapdrs vapdrs force-pushed the feature/sequence-paren-list-init branch from 82e371b to 85958bd Compare February 29, 2024 21:08
@vapdrs vapdrs requested a review from zygoloid February 29, 2024 21:35
@vapdrs vapdrs force-pushed the feature/sequence-paren-list-init branch from 85958bd to cc14e7a Compare February 29, 2024 21:47
Copy link
Collaborator

@shafik shafik left a 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.

Region = Parent;
for (unsigned I = 0; I < Elts.size(); ++I)
Tree.merge(Elts[I]);
SequenceExpressionsInOrder({CCE->getArgs(), CCE->getNumArgs()});
Copy link
Collaborator

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
@vapdrs vapdrs force-pushed the feature/sequence-paren-list-init branch from cc14e7a to f66254c Compare March 1, 2024 13:14
@vapdrs vapdrs requested a review from shafik March 1, 2024 14:47
@cor3ntin
Copy link
Contributor

cor3ntin commented Mar 5, 2024

@vapdrs Do you need us to merge that for you? Thanks

@vapdrs
Copy link
Contributor Author

vapdrs commented Mar 5, 2024

@vapdrs Do you need us to merge that for you? Thanks

Yes, I do not have write access to the repository. Thank you!

@cor3ntin cor3ntin merged commit 4ce737b into llvm:main Mar 5, 2024
Copy link

github-actions bot commented Mar 5, 2024

@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
by our build bots. If there is a problem with a build, you may recieve a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as
the builds can include changes from many authors. It is not uncommon for your
change to be included in a build that fails due to someone else's changes, or
infrastructure issues.

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.
This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang] False positive "-Wunsequenced" for C++20 parenthesized initializer lists
5 participants