[PATCH] D125882: Correct the diagnostic behavior for unreachable _Generic associations in C++

Aaron Ballman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed May 18 09:46:03 PDT 2022


This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG47b8424a533d: Correct the diagnostic behavior for unreachable _Generic associations in C++ (authored by aaron.ballman).

Changed prior to commit:
  https://reviews.llvm.org/D125882?vs=430354&id=430417#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D125882/new/

https://reviews.llvm.org/D125882

Files:
  clang/lib/Sema/SemaExpr.cpp
  clang/test/Sema/generic-selection.c
  clang/test/SemaCXX/generic-selection.cpp


Index: clang/test/SemaCXX/generic-selection.cpp
===================================================================
--- clang/test/SemaCXX/generic-selection.cpp
+++ clang/test/SemaCXX/generic-selection.cpp
@@ -44,3 +44,28 @@
 static_assert(TypeMask<int, long, short>::result == 7, "fail");
 static_assert(TypeMask<float, short>::result == 12, "fail");
 static_assert(TypeMask<int, float, float>::result == 9, "fail");
+
+
+struct Test {
+  int i;
+};
+
+void unreachable_associations(const int i, const Test t) {
+  // FIXME: it's not clear to me whether we intended to deviate from the C
+  // semantics in terms of how qualifiers are handled, so this documents the
+  // existing behavior but perhaps not the desired behavior.
+  static_assert(
+    _Generic(i,
+      const int : 1,    // expected-warning {{due to lvalue conversion of the controlling expression, association of type 'const int' will never be selected because it is qualified}}
+      volatile int : 2, // expected-warning {{due to lvalue conversion of the controlling expression, association of type 'volatile int' will never be selected because it is qualified}}
+      int[12] : 3,      // expected-warning {{due to lvalue conversion of the controlling expression, association of type 'int[12]' will never be selected because it is of array type}}
+      int : 4,
+      default : 5
+    ) == 4, "we had better pick int, not const int!");
+  static_assert(
+    _Generic(t,
+      Test : 1,
+      const Test : 2,  // Ok in C++, warned in C
+      default : 3
+    ) == 2, "we had better pick const Test, not Test!"); // C++-specific result
+}
Index: clang/test/Sema/generic-selection.c
===================================================================
--- clang/test/Sema/generic-selection.c
+++ clang/test/Sema/generic-selection.c
@@ -58,7 +58,11 @@
     ), int : 0);
 }
 
-void unreachable_associations(const int i) {
+struct Test {
+  int i;
+};
+
+void unreachable_associations(const int i, const struct Test t) {
   _Static_assert( // ext-warning {{'_Static_assert' is a C11 extension}}
     _Generic(i, // ext-warning {{'_Generic' is a C11 extension}}
       const int : 1,    // expected-warning {{due to lvalue conversion of the controlling expression, association of type 'const int' will never be selected because it is qualified}}
@@ -67,4 +71,10 @@
       int : 4,
       default : 5
     ) == 4, "we had better pick int!");
+  _Static_assert( // ext-warning {{'_Static_assert' is a C11 extension}}
+    _Generic(t, // ext-warning {{'_Generic' is a C11 extension}}
+      struct Test : 1,
+      const struct Test : 2, // expected-warning {{due to lvalue conversion of the controlling expression, association of type 'const struct Test' will never be selected because it is qualified}}
+      default : 3
+    ) == 1, "we had better pick struct Test, not const struct Test!"); // C-specific result
 }
Index: clang/lib/Sema/SemaExpr.cpp
===================================================================
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -1692,11 +1692,20 @@
           // reached. We will warn about this so users are less surprised by
           // the unreachable association. However, we don't have to handle
           // function types; that's not an object type, so it's handled above.
+          //
+          // The logic is somewhat different for C++ because C++ has different
+          // lvalue to rvalue conversion rules than C. [conv.lvalue]p1 says,
+          // If T is a non-class type, the type of the prvalue is the cv-
+          // unqualified version of T. Otherwise, the type of the prvalue is T.
+          // The result of these rules is that all qualified types in an
+          // association in C are unreachable, and in C++, only qualified non-
+          // class types are unreachable.
           unsigned Reason = 0;
           QualType QT = Types[i]->getType();
           if (QT->isArrayType())
             Reason = 1;
-          else if (QT.hasQualifiers())
+          else if (QT.hasQualifiers() &&
+                   (!LangOpts.CPlusPlus || !QT->isRecordType()))
             Reason = 2;
 
           if (Reason)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D125882.430417.patch
Type: text/x-patch
Size: 4174 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220518/c2b7306b/attachment.bin>


More information about the cfe-commits mailing list