[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 06:30:23 PDT 2022
aaron.ballman created this revision.
aaron.ballman added reviewers: erichkeane, tahonermann, aeubanks, clang-language-wg.
Herald added a project: All.
aaron.ballman requested review of this revision.
Herald added a project: clang.
New diagnostics were added for unreachable generic selection expression associations in ca75ac5f04f269def97e6844c2f5c9596b29c84c <https://reviews.llvm.org/rGca75ac5f04f269def97e6844c2f5c9596b29c84c>, 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.
Repository:
rG LLVM Github Monorepo
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,17 @@
// 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.
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.430354.patch
Type: text/x-patch
Size: 3977 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220518/0e1864fb/attachment-0001.bin>
More information about the cfe-commits
mailing list