[clang] 033ec09 - [Clang][Sema] Fix Wswitch-default bad warning in template (#76007)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Dec 21 17:01:18 PST 2023
Author: hstk30-hw
Date: 2023-12-22T09:00:41+08:00
New Revision: 033ec098be730bff04bfb929d254ce57e5ec8534
URL: https://github.com/llvm/llvm-project/commit/033ec098be730bff04bfb929d254ce57e5ec8534
DIFF: https://github.com/llvm/llvm-project/commit/033ec098be730bff04bfb929d254ce57e5ec8534.diff
LOG: [Clang][Sema] Fix Wswitch-default bad warning in template (#76007)
https://github.com/llvm/llvm-project/pull/73077 added -Wswitch-default
diagnostic but it produced false positives in templates. This PR will
address that. https://github.com/llvm/llvm-project/issues/75943
Added:
clang/test/Sema/switch-default.cpp
Modified:
clang/lib/Sema/SemaStmt.cpp
Removed:
clang/test/Sema/switch-default.c
################################################################################
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