Author: dong jianqiang Date: 2023-12-07T09:03:15+08:00 New Revision: c28178298513f99dc869daa301fc25257df81688
URL: https://github.com/llvm/llvm-project/commit/c28178298513f99dc869daa301fc25257df81688 DIFF: https://github.com/llvm/llvm-project/commit/c28178298513f99dc869daa301fc25257df81688.diff LOG: [clang][Sema] Add -Wswitch-default warning option (#73077) 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. Added: clang/test/Sema/switch-default.c Modified: clang/docs/ReleaseNotes.rst clang/include/clang/Basic/DiagnosticGroups.td clang/include/clang/Basic/DiagnosticSemaKinds.td clang/lib/Sema/SemaStmt.cpp Removed: ################################################################################ diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 89ea2f0930ceca..43a322030baae8 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -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. diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td index ff028bbbf74261..12b11527b30571 100644 --- a/clang/include/clang/Basic/DiagnosticGroups.td +++ b/clang/include/clang/Basic/DiagnosticGroups.td @@ -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">; diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index e0ac3af7b98e36..ea08fa84d022cd 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -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">, diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp index 2b45aa5dff7be7..63348d27a8c94a 100644 --- a/clang/lib/Sema/SemaStmt.cpp +++ b/clang/lib/Sema/SemaStmt.cpp @@ -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. diff --git a/clang/test/Sema/switch-default.c b/clang/test/Sema/switch-default.c new file mode 100644 index 00000000000000..854b561b37c48e --- /dev/null +++ b/clang/test/Sema/switch-default.c @@ -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; +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits