r202349 - [-Wunreachable-code] Don't warn about unreachable 'default:' cases.
Ted Kremenek
kremenek at apple.com
Wed Feb 26 21:42:08 PST 2014
Author: kremenek
Date: Wed Feb 26 23:42:07 2014
New Revision: 202349
URL: http://llvm.org/viewvc/llvm-project?rev=202349&view=rev
Log:
[-Wunreachable-code] Don't warn about unreachable 'default:' cases.
They are covered by -Wcovered-switch-default.
Modified:
cfe/trunk/lib/Analysis/ReachableCode.cpp
cfe/trunk/test/Sema/warn-unreachable.c
Modified: cfe/trunk/lib/Analysis/ReachableCode.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/ReachableCode.cpp?rev=202349&r1=202348&r2=202349&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/ReachableCode.cpp (original)
+++ cfe/trunk/lib/Analysis/ReachableCode.cpp Wed Feb 26 23:42:07 2014
@@ -284,6 +284,12 @@ void DeadCodeScan::reportDeadCode(const
if (isBreakPrecededByNoReturn(B, S))
return;
+ // Was this an unreachable 'default' case? Such cases are covered
+ // by -Wcovered-switch-default, if the user so desires.
+ const Stmt *Label = B->getLabel();
+ if (Label && isa<DefaultStmt>(Label))
+ return;
+
SourceRange R1, R2;
SourceLocation Loc = GetUnreachableLoc(S, R1, R2);
CB.HandleUnreachable(Loc, R1, R2);
Modified: cfe/trunk/test/Sema/warn-unreachable.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/warn-unreachable.c?rev=202349&r1=202348&r2=202349&view=diff
==============================================================================
--- cfe/trunk/test/Sema/warn-unreachable.c (original)
+++ cfe/trunk/test/Sema/warn-unreachable.c Wed Feb 26 23:42:07 2014
@@ -109,7 +109,7 @@ int test_enum_cases(enum Cases C) {
case C3:
return 1;
default: {
- int i = 0; // expected-warning{{will never be executed}}
+ int i = 0; // no-warning
++i;
return i;
}
@@ -164,3 +164,26 @@ int test_break_preceded_by_noreturn(int
}
return i;
}
+
+// Don't warn about unreachable 'default' cases, as that is covered
+// by -Wcovered-switch-default.
+typedef enum { Value1 = 1 } MyEnum;
+void unreachable_default(MyEnum e) {
+ switch (e) {
+ case Value1:
+ calledFun();
+ break;
+ default:
+ calledFun(); // no-warning
+ break;
+ }
+}
+void unreachable_in_default(MyEnum e) {
+ switch (e) {
+ default:
+ raze();
+ calledFun(); // expected-warning {{will never be executed}}
+ break;
+ }
+}
+
More information about the cfe-commits
mailing list