[clang] 8322593 - PR51158: Don't emit -Wswitch or -Wcovered-switch-default for empty enums

David Blaikie via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 22 14:52:16 PDT 2021


Author: David Blaikie
Date: 2021-07-22T14:51:56-07:00
New Revision: 83225936af317e6bdd7103a8a039c51a29ce9f57

URL: https://github.com/llvm/llvm-project/commit/83225936af317e6bdd7103a8a039c51a29ce9f57
DIFF: https://github.com/llvm/llvm-project/commit/83225936af317e6bdd7103a8a039c51a29ce9f57.diff

LOG: PR51158: Don't emit -Wswitch or -Wcovered-switch-default for empty enums

An empty enum is used to implement C++'s new-ish "byte" type (to make
sure it's a separate type for overloading, etc - compared to a typedef)
- without any enumerators. Some clang warnings don't make sense in this
sort of situation, so let's skip them for empty enums.

It's arguable that possibly some situations of enumerations without
enumerators might want the previous-to-this-patch behavior (if the enum
is autogenerated and in some cases comes up empty, then maybe a default
in an empty switch would still be considered problematic - so that when
you add the first enumeration you do get a -Wswitch warning). But I
think that's niche enough & this std::byte case is mainstream enough
that we should prioritize the latter over the former.

If someone's got a middle ground proposal to account for both of those
situations, I'm open to patches/suggestions/etc.

Added: 
    

Modified: 
    clang/lib/Sema/SemaStmt.cpp
    clang/test/SemaCXX/switch.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index 2ae27de9b207..64c53d6666f0 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -1461,7 +1461,8 @@ Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, Stmt *Switch,
 
     // If switch has default case, then ignore it.
     if (!CaseListIsErroneous && !CaseListIsIncomplete && !HasConstantCond &&
-        ET && ET->getDecl()->isCompleteDefinition()) {
+        ET && ET->getDecl()->isCompleteDefinition() &&
+        !empty(ET->getDecl()->enumerators())) {
       const EnumDecl *ED = ET->getDecl();
       EnumValsTy EnumVals;
 

diff  --git a/clang/test/SemaCXX/switch.cpp b/clang/test/SemaCXX/switch.cpp
index 0c60ce02097b..380f4d559ef8 100644
--- a/clang/test/SemaCXX/switch.cpp
+++ b/clang/test/SemaCXX/switch.cpp
@@ -130,3 +130,19 @@ void test(Opaque o, OpaqueClass oc, Defined d) {
 }
 
 }
+
+namespace EmptyEnum {
+
+enum Empty : int {};
+void test(Empty e) {
+  switch (e) {
+  case (Empty)0:
+    break;
+  }
+  switch (e) {
+  default:
+    break;
+  }
+}
+
+} // namespace EmptyEnum


        


More information about the cfe-commits mailing list