Skip to content

Commit

Permalink
[clang][Sema] Add -Wswitch-default warning option (llvm#73077)
Browse files Browse the repository at this point in the history
Adds a warning, issued by the clang semantic analysis. The patch warns
on switch which don't have the default branch.

This is a counterpart of gcc's Wswitch-default.
  • Loading branch information
dongjianqiang2 authored Dec 7, 2023
1 parent e1a4b00 commit c281782
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 1 deletion.
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,8 @@ Improvements to Clang's diagnostics
of a base class is not called in the constructor of its derived class.
- Clang no longer emits ``-Wmissing-variable-declarations`` for variables declared
with the ``register`` storage class.
- Clang's ``-Wswitch-default`` flag now diagnoses whenever a ``switch`` statement
does not have a ``default`` label.
- Clang's ``-Wtautological-negation-compare`` flag now diagnoses logical
tautologies like ``x && !x`` and ``!x || x`` in expressions. This also
makes ``-Winfinite-recursion`` diagnose more cases.
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/Basic/DiagnosticGroups.td
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ def ShadowAll : DiagGroup<"shadow-all", [Shadow, ShadowFieldInConstructor,
def Shorten64To32 : DiagGroup<"shorten-64-to-32">;
def : DiagGroup<"sign-promo">;
def SignCompare : DiagGroup<"sign-compare">;
def : DiagGroup<"switch-default">;
def SwitchDefault : DiagGroup<"switch-default">;
def : DiagGroup<"synth">;
def SizeofArrayArgument : DiagGroup<"sizeof-array-argument">;
def SizeofArrayDecay : DiagGroup<"sizeof-array-decay">;
Expand Down
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -10064,6 +10064,8 @@ def warn_missing_case : Warning<"%plural{"
"3:enumeration values %1, %2, and %3 not handled in switch|"
":%0 enumeration values not handled in switch: %1, %2, %3...}0">,
InGroup<Switch>;
def warn_switch_default : Warning<"'switch' missing 'default' label">,
InGroup<SwitchDefault>, DefaultIgnore;

def warn_unannotated_fallthrough : Warning<
"unannotated fall-through between switch labels">,
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Sema/SemaStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,9 @@ 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 Down
17 changes: 17 additions & 0 deletions clang/test/Sema/switch-default.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RUN: %clang_cc1 -fsyntax-only -verify -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;
}

0 comments on commit c281782

Please sign in to comment.