Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
[Clang][Sema] Fix Wswitch-default bad warning in template (#76007)
Browse files Browse the repository at this point in the history
llvm/llvm-project#73077 added -Wswitch-default
diagnostic but it produced false positives in templates. This PR will
address that. llvm/llvm-project#75943
  • Loading branch information
hstk30-hw authored Dec 22, 2023
1 parent 7c3b67d commit 033ec09
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 31 deletions.
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}}
}

0 comments on commit 033ec09

Please sign in to comment.