[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:34:18 PST 2023
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: None (hstk30-hw)
<details>
<summary>Changes</summary>
Fix https://github.com/llvm/llvm-project/issues/75943
---
Full diff: https://github.com/llvm/llvm-project/pull/76007.diff
2 Files Affected:
- (modified) clang/lib/Sema/SemaStmt.cpp (+1-3)
- (added) clang/test/Sema/switch-default-template.cpp (+27)
``````````diff
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}}
+}
+
``````````
</details>
https://github.com/llvm/llvm-project/pull/76007
More information about the cfe-commits
mailing list