Skip to content

Commit 8e00f93

Browse files
committed
[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.
1 parent 92f1771 commit 8e00f93

File tree

3 files changed

+57
-31
lines changed

3 files changed

+57
-31
lines changed

clang/lib/Sema/SemaStmt.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,6 +1271,9 @@ Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, Stmt *Switch,
12711271

12721272
bool CaseListIsErroneous = false;
12731273

1274+
// FIXME: We'd better diagnose missing or duplicate default labels even
1275+
// in the dependent case. Because default labels themselves are never
1276+
// dependent.
12741277
for (SwitchCase *SC = SS->getSwitchCaseList(); SC && !HasDependentValue;
12751278
SC = SC->getNextSwitchCase()) {
12761279

@@ -1327,9 +1330,6 @@ Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, Stmt *Switch,
13271330
}
13281331
}
13291332

1330-
if (!TheDefaultStmt)
1331-
Diag(SwitchLoc, diag::warn_switch_default);
1332-
13331333
if (!HasDependentValue) {
13341334
// If we don't have a default statement, check whether the
13351335
// condition is constant.
@@ -1344,6 +1344,7 @@ Sema::ActOnFinishSwitchStmt(SourceLocation SwitchLoc, Stmt *Switch,
13441344
assert(!HasConstantCond ||
13451345
(ConstantCondValue.getBitWidth() == CondWidth &&
13461346
ConstantCondValue.isSigned() == CondIsSigned));
1347+
Diag(SwitchLoc, diag::warn_switch_default);
13471348
}
13481349
bool ShouldCheckConstantCond = HasConstantCond;
13491350

clang/test/Sema/switch-default.c

Lines changed: 0 additions & 28 deletions
This file was deleted.

clang/test/Sema/switch-default.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 -Wswitch-default %s
2+
3+
int f1(int a) {
4+
switch (a) { // expected-warning {{'switch' missing 'default' label}}
5+
case 1: a++; break;
6+
case 2: a += 2; break;
7+
}
8+
return a;
9+
}
10+
11+
int f2(int a) {
12+
switch (a) { // no-warning
13+
default:
14+
;
15+
}
16+
return a;
17+
}
18+
19+
// Warn even completely covered Enum cases(GCC compatibility).
20+
enum E { A, B };
21+
enum E check_enum(enum E e) {
22+
switch (e) { // expected-warning {{'switch' missing 'default' label}}
23+
case A: break;
24+
case B: break;
25+
}
26+
return e;
27+
}
28+
29+
template<typename Index>
30+
int t1(Index i)
31+
{
32+
switch (i) { // expected-warning {{'switch' missing 'default' label}}
33+
case 0: return 0;
34+
case 1: return 1;
35+
}
36+
return 0;
37+
}
38+
39+
template<typename Index>
40+
int t2(Index i)
41+
{
42+
switch (i) { // no-warning
43+
case 0: return 0;
44+
case 1: return 1;
45+
default: return 2;
46+
}
47+
return 0;
48+
}
49+
50+
int main() {
51+
return t1(1); // expected-note {{in instantiation of function template specialization 't1<int>' requested here}}
52+
}
53+

0 commit comments

Comments
 (0)