Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Clang][Sema] Fix Wswitch-default bad warning in template #76007

Merged
merged 1 commit into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions clang/lib/Sema/SemaStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {

Expand Down Expand Up @@ -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.
Expand All @@ -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;

Expand Down
28 changes: 0 additions & 28 deletions clang/test/Sema/switch-default.c

This file was deleted.

53 changes: 53 additions & 0 deletions clang/test/Sema/switch-default.cpp
Original file line number Diff line number Diff line change
@@ -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}}
}