[clang] 64cfcde - [Clang] Fix the location of default init expressions

Corentin Jabot via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 18 23:54:07 PDT 2023


Author: Corentin Jabot
Date: 2023-07-19T08:54:01+02:00
New Revision: 64cfcde31a48962c3bbc703753a4ea41200da7a8

URL: https://github.com/llvm/llvm-project/commit/64cfcde31a48962c3bbc703753a4ea41200da7a8
DIFF: https://github.com/llvm/llvm-project/commit/64cfcde31a48962c3bbc703753a4ea41200da7a8.diff

LOG: [Clang] Fix the location of default init expressions

Default member initializations constructed from
a parenthesized aggregate initialization should be constructed
at the location of the left paren, to be consistent with
brace initialization.
Otherwise we get diagmostics and source_location in the wrong places.

Fixes #63903

Reviewed By: aaron.ballman, #clang-language-wg

Differential Revision: https://reviews.llvm.org/D155573

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/SemaInit.cpp
    clang/test/SemaCXX/paren-list-agg-init.cpp
    clang/test/SemaCXX/source_location.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index eb888282bf6676..805b0f5697f759 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -766,6 +766,9 @@ Bug Fixes to C++ Support
 - Merge lambdas in require expressions in standard C++ modules.
   (`#63544 <https://github.com/llvm/llvm-project/issues/63544>`_)
 
+- Fix location of default member initialization in parenthesized aggregate
+  initialization.
+  (`#63903 <https://github.com/llvm/llvm-project/issues/63903>`_)
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^

diff  --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index 8e2177bce4fd79..32c9215184eba5 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -5593,7 +5593,8 @@ static void TryOrBuildParenListInitialization(
             // C++ [dcl.init]p16.6.2.2
             //   The remaining elements are initialized with their default
             //   member initializers, if any
-            ExprResult DIE = S.BuildCXXDefaultInitExpr(FD->getLocation(), FD);
+            ExprResult DIE = S.BuildCXXDefaultInitExpr(
+                Kind.getParenOrBraceRange().getEnd(), FD);
             if (DIE.isInvalid())
               return;
             S.checkInitializerLifetime(SubEntity, DIE.get());

diff  --git a/clang/test/SemaCXX/paren-list-agg-init.cpp b/clang/test/SemaCXX/paren-list-agg-init.cpp
index e2eec1779699ac..944ea76b81d24d 100644
--- a/clang/test/SemaCXX/paren-list-agg-init.cpp
+++ b/clang/test/SemaCXX/paren-list-agg-init.cpp
@@ -299,3 +299,14 @@ namespace gh63758 {
   struct S {} s;
   auto words = (char[])s; // expected-error {{C-style cast from 'struct S' to 'char[]' is not allowed}}
 };
+
+namespace GH63903 {
+  constexpr int f(); // expected-note {{declared here}}
+  struct S {
+    int a = 0, b = f(); // expected-note {{undefined function 'f' cannot be used in a constant expression}}
+  };
+
+  // Test that errors produced by default members are produced at the location of the initialization
+  constexpr S s(0); // beforecxx20-warning {{aggregate initialization of type 'const S' from a parenthesized list of values is a C++20 extension}} \
+                    // expected-error {{constexpr variable 's' must be initialized by a constant expression}}
+}

diff  --git a/clang/test/SemaCXX/source_location.cpp b/clang/test/SemaCXX/source_location.cpp
index 6ff58d4ee8ca11..2732700bf4bee3 100644
--- a/clang/test/SemaCXX/source_location.cpp
+++ b/clang/test/SemaCXX/source_location.cpp
@@ -1,5 +1,6 @@
 // RUN: %clang_cc1 -std=c++1z -fcxx-exceptions -fexceptions -verify %s
 // RUN: %clang_cc1 -std=c++2a -fcxx-exceptions -DUSE_CONSTEVAL -fexceptions -verify %s
+// RUN: %clang_cc1 -std=c++2b -fcxx-exceptions -DUSE_CONSTEVAL -DPAREN_INIT -fexceptions -verify %s
 // RUN: %clang_cc1 -std=c++1z -fcxx-exceptions -fms-extensions -DMS -fexceptions -verify %s
 // RUN: %clang_cc1 -std=c++2a -fcxx-exceptions -fms-extensions -DMS -DUSE_CONSTEVAL -fexceptions -verify %s
 // expected-no-diagnostics
@@ -781,3 +782,20 @@ constexpr int f(int i = G<T>{}.line) {
 static_assert(f<int>() != // intentional new line
               f<int>());
 }
+
+#ifdef PAREN_INIT
+namespace GH63903 {
+struct S {
+    int _;
+    int i = SL::current().line();
+    int j = __builtin_LINE();
+};
+// Ensure parent aggregate initialization is consistent with brace
+// aggregate initialization.
+// Note: consteval functions are evaluated where they are used.
+static_assert(S(0).i == __builtin_LINE());
+static_assert(S(0).i == S{0}.i);
+static_assert(S(0).j == S{0}.j);
+static_assert(S(0).j == S{0}.i);
+}
+#endif


        


More information about the cfe-commits mailing list