[clang] 47b8424 - Correct the diagnostic behavior for unreachable _Generic associations in C++

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Wed May 18 09:45:50 PDT 2022


Author: Aaron Ballman
Date: 2022-05-18T12:45:38-04:00
New Revision: 47b8424a533d5c02fd8b3517047cf93e533f00d0

URL: https://github.com/llvm/llvm-project/commit/47b8424a533d5c02fd8b3517047cf93e533f00d0
DIFF: https://github.com/llvm/llvm-project/commit/47b8424a533d5c02fd8b3517047cf93e533f00d0.diff

LOG: Correct the diagnostic behavior for unreachable _Generic associations in C++

New diagnostics were added for unreachable generic selection expression
associations in ca75ac5f04f269def97e6844c2f5c9596b29c84c, but it did
not account for a difference in behavior between C and C++ regarding
lvalue to rvalue conversions. So we would issue diagnostics about a
selection being unreachable and then reach it. This corrects the
diagnostic behavior in that case.

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 84ffa98c2cd61..9d9cb99085f33 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -1692,11 +1692,20 @@ Sema::CreateGenericSelectionExpr(SourceLocation KeyLoc,
           // 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 
diff erent for C++ because C++ has 
diff erent
+          // 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)

diff  --git a/clang/test/Sema/generic-selection.c b/clang/test/Sema/generic-selection.c
index f6299aae25046..0bd537c79d316 100644
--- a/clang/test/Sema/generic-selection.c
+++ b/clang/test/Sema/generic-selection.c
@@ -58,7 +58,11 @@ void GH50227(void) {
     ), 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 @@ void unreachable_associations(const int i) {
       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
 }

diff  --git a/clang/test/SemaCXX/generic-selection.cpp b/clang/test/SemaCXX/generic-selection.cpp
index 8bf4a784f9bff..79e0776cfa395 100644
--- a/clang/test/SemaCXX/generic-selection.cpp
+++ b/clang/test/SemaCXX/generic-selection.cpp
@@ -44,3 +44,28 @@ template <class... Args> struct TypeMask {
 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
+}


        


More information about the cfe-commits mailing list