[clang] [Clang][Sema] Fix Wswitch-default bad warning in template (PR #76007)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Dec 21 05:41:12 PST 2023
https://github.com/hstk30-hw updated https://github.com/llvm/llvm-project/pull/76007
>From 8e00f9313edb18afd087433e69d9469bec4c7b19 Mon Sep 17 00:00:00 2001
From: hstk30-hw <hanwei62 at huawei.com>
Date: Wed, 20 Dec 2023 12:26:15 +0800
Subject: [PATCH] [Clang][Sema] Fix Wswitch-default bad warning in template
[#73077] added Wswitch-default diagnostic but it produced false
positives in templates. This PR address it.
---
clang/lib/Sema/SemaStmt.cpp | 7 ++--
clang/test/Sema/switch-default.c | 28 ----------------
clang/test/Sema/switch-default.cpp | 53 ++++++++++++++++++++++++++++++
3 files changed, 57 insertions(+), 31 deletions(-)
delete mode 100644 clang/test/Sema/switch-default.c
create mode 100644 clang/test/Sema/switch-default.cpp
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index 63348d27a8c94a..f0b03db690843a 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -1271,6 +1271,9 @@ Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, Stmt *Switch,
bool CaseListIsErroneous = false;
+ // FIXME: We'd better diagnose missing or duplicate default labels even
+ // in the dependent case. Because default labels themselves are never
+ // dependent.
for (SwitchCase *SC = SS->getSwitchCaseList(); SC && !HasDependentValue;
SC = SC->getNextSwitchCase()) {
@@ -1327,9 +1330,6 @@ Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, Stmt *Switch,
}
}
- if (!TheDefaultStmt)
- Diag(SwitchLoc, diag::warn_switch_default);
-
if (!HasDependentValue) {
// If we don't have a default statement, check whether the
// condition is constant.
@@ -1344,6 +1344,7 @@ Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, Stmt *Switch,
assert(!HasConstantCond ||
(ConstantCondValue.getBitWidth() == CondWidth &&
ConstantCondValue.isSigned() == CondIsSigned));
+ Diag(SwitchLoc, diag::warn_switch_default);
}
bool ShouldCheckConstantCond = HasConstantCond;
diff --git a/clang/test/Sema/switch-default.c b/clang/test/Sema/switch-default.c
deleted file mode 100644
index 342a97ee68b1e2..00000000000000
--- a/clang/test/Sema/switch-default.c
+++ /dev/null
@@ -1,28 +0,0 @@
-// RUN: %clang_cc1 -fsyntax-only -verify -Wswitch-default %s
-
-int f1(int a) {
- switch (a) { // expected-warning {{'switch' missing 'default' label}}
- case 1: a++; break;
- case 2: a += 2; break;
- }
- return a;
-}
-
-int f2(int a) {
- switch (a) { // no-warning
- default:
- ;
- }
- return a;
-}
-
-// Warn even completely covered Enum cases(GCC compatibility).
-enum E { A, B };
-enum E check_enum(enum E e) {
- switch (e) { // expected-warning {{'switch' missing 'default' label}}
- case A: break;
- case B: break;
- }
- return e;
-}
-
diff --git a/clang/test/Sema/switch-default.cpp b/clang/test/Sema/switch-default.cpp
new file mode 100644
index 00000000000000..32d03dae882733
--- /dev/null
+++ b/clang/test/Sema/switch-default.cpp
@@ -0,0 +1,53 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wswitch-default %s
+
+int f1(int a) {
+ switch (a) { // expected-warning {{'switch' missing 'default' label}}
+ case 1: a++; break;
+ case 2: a += 2; break;
+ }
+ return a;
+}
+
+int f2(int a) {
+ switch (a) { // no-warning
+ default:
+ ;
+ }
+ return a;
+}
+
+// Warn even completely covered Enum cases(GCC compatibility).
+enum E { A, B };
+enum E check_enum(enum E e) {
+ switch (e) { // expected-warning {{'switch' missing 'default' label}}
+ case A: break;
+ case B: break;
+ }
+ return e;
+}
+
+template<typename Index>
+int t1(Index i)
+{
+ switch (i) { // expected-warning {{'switch' missing 'default' label}}
+ case 0: return 0;
+ case 1: return 1;
+ }
+ return 0;
+}
+
+template<typename Index>
+int t2(Index i)
+{
+ switch (i) { // no-warning
+ case 0: return 0;
+ case 1: return 1;
+ default: return 2;
+ }
+ return 0;
+}
+
+int main() {
+ return t1(1); // expected-note {{in instantiation of function template specialization 't1<int>' requested here}}
+}
+
More information about the cfe-commits
mailing list