[clang] [Clang][Sema] Fix Wswitch-default bad warning in template (PR #76007)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Dec 19 20:33:49 PST 2023
https://github.com/hstk30-hw created https://github.com/llvm/llvm-project/pull/76007
Fix https://github.com/llvm/llvm-project/issues/75943
>From c3d5ac42726c49fd7036972042eb70d3e5dc01a6 Mon Sep 17 00:00:00 2001
From: hstk-hw <hanwei62 at huawei.com>
Date: Wed, 20 Dec 2023 12:26:15 +0800
Subject: [PATCH] fix: fix Wswitch-default bad warning in template
---
clang/lib/Sema/SemaStmt.cpp | 4 +--
clang/test/Sema/switch-default-template.cpp | 27 +++++++++++++++++++++
2 files changed, 28 insertions(+), 3 deletions(-)
create mode 100644 clang/test/Sema/switch-default-template.cpp
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index 63348d27a8c94a..adc2055ec4e659 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -1327,9 +1327,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 +1341,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-template.cpp b/clang/test/Sema/switch-default-template.cpp
new file mode 100644
index 00000000000000..c671164bd785b0
--- /dev/null
+++ b/clang/test/Sema/switch-default-template.cpp
@@ -0,0 +1,27 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wswitch-default %s
+
+template<typename Index>
+int f1(Index i)
+{
+ switch (i) { // expected-warning {{'switch' missing 'default' label}}
+ case 0: return 0;
+ case 1: return 1;
+ }
+ return 0;
+}
+
+template<typename Index>
+int f2(Index i)
+{
+ switch (i) { // no-warning
+ case 0: return 0;
+ case 1: return 1;
+ default: return 2;
+ }
+ return 0;
+}
+
+int main() {
+ return f1(1); // expected-note {{in instantiation of function template specialization 'f1<int>' requested here}}
+}
+
More information about the cfe-commits
mailing list