r204430 - [-Wunreachable-code] add a specialized diagnostic for unreachable increment expressions of loops.

Ted Kremenek kremenek at apple.com
Thu Mar 20 23:02:36 PDT 2014


Author: kremenek
Date: Fri Mar 21 01:02:36 2014
New Revision: 204430

URL: http://llvm.org/viewvc/llvm-project?rev=204430&view=rev
Log:
[-Wunreachable-code] add a specialized diagnostic for unreachable increment expressions of loops.

Modified:
    cfe/trunk/include/clang/Analysis/Analyses/ReachableCode.h
    cfe/trunk/include/clang/Basic/DiagnosticGroups.td
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Analysis/ReachableCode.cpp
    cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp
    cfe/trunk/test/SemaCXX/unreachable-code.cpp
    cfe/trunk/test/SemaCXX/warn-unreachable.cpp
    cfe/trunk/test/SemaObjC/warn-unreachable.m

Modified: cfe/trunk/include/clang/Analysis/Analyses/ReachableCode.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Analysis/Analyses/ReachableCode.h?rev=204430&r1=204429&r2=204430&view=diff
==============================================================================
--- cfe/trunk/include/clang/Analysis/Analyses/ReachableCode.h (original)
+++ cfe/trunk/include/clang/Analysis/Analyses/ReachableCode.h Fri Mar 21 01:02:36 2014
@@ -41,6 +41,7 @@ namespace reachable_code {
 enum UnreachableKind {
   UK_Return,
   UK_Break,
+  UK_Loop_Increment,
   UK_Other
 };
 

Modified: cfe/trunk/include/clang/Basic/DiagnosticGroups.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticGroups.td?rev=204430&r1=204429&r2=204430&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticGroups.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticGroups.td Fri Mar 21 01:02:36 2014
@@ -430,7 +430,9 @@ def DuplicateArgDecl : DiagGroup<"duplic
 //  least actively used, with more noisy versions of the warning covered
 //  under separate flags.
 //
-def UnreachableCode : DiagGroup<"unreachable-code">;
+def UnreachableCodeLoopIncrement : DiagGroup<"unreachable-code-loop-increment">;
+def UnreachableCode : DiagGroup<"unreachable-code",
+                                [UnreachableCodeLoopIncrement]>;
 def UnreachableCodeBreak : DiagGroup<"unreachable-code-break">;
 def UnreachableCodeReturn : DiagGroup<"unreachable-code-return">;
 def UnreachableCodeAggressive : DiagGroup<"unreachable-code-aggressive",

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=204430&r1=204429&r2=204430&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Fri Mar 21 01:02:36 2014
@@ -370,6 +370,9 @@ def warn_unreachable_break : Warning<
 def warn_unreachable_return : Warning<
   "'return' will never be executed">,
   InGroup<UnreachableCodeReturn>, DefaultIgnore;
+def warn_unreachable_loop_increment : Warning<
+  "loop will run at most once (loop increment never executed)">,
+  InGroup<UnreachableCodeLoopIncrement>, DefaultIgnore;
 
 /// Built-in functions.
 def ext_implicit_lib_function_decl : ExtWarn<

Modified: cfe/trunk/lib/Analysis/ReachableCode.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/ReachableCode.cpp?rev=204430&r1=204429&r2=204430&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/ReachableCode.cpp (original)
+++ cfe/trunk/lib/Analysis/ReachableCode.cpp Fri Mar 21 01:02:36 2014
@@ -529,6 +529,26 @@ void DeadCodeScan::reportDeadCode(const
     UK = reachable_code::UK_Return;
   }
 
+  if (UK == reachable_code::UK_Other) {
+    // Check if the dead code is part of the "loop target" of
+    // a for/for-range loop.  This is the block that contains
+    // the increment code.
+    if (const Stmt *LoopTarget = B->getLoopTarget()) {
+      SourceLocation Loc = LoopTarget->getLocStart();
+      SourceRange R1(Loc, Loc), R2;
+
+      if (const ForStmt *FS = dyn_cast<ForStmt>(LoopTarget)) {
+        const Expr *Inc = FS->getInc();
+        Loc = Inc->getLocStart();
+        R2 = Inc->getSourceRange();
+      }
+
+      CB.HandleUnreachable(reachable_code::UK_Loop_Increment,
+                           Loc, SourceRange(Loc, Loc), R2);
+      return;
+    }
+  }
+
   SourceRange R1, R2;
   SourceLocation Loc = GetUnreachableLoc(S, R1, R2);
   CB.HandleUnreachable(UK, Loc, R1, R2);

Modified: cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp?rev=204430&r1=204429&r2=204430&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp (original)
+++ cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp Fri Mar 21 01:02:36 2014
@@ -76,6 +76,9 @@ namespace {
         case reachable_code::UK_Return:
           diag = diag::warn_unreachable_return;
           break;
+        case reachable_code::UK_Loop_Increment:
+          diag = diag::warn_unreachable_loop_increment;
+          break;
         case reachable_code::UK_Other:
           break;
       }
@@ -1688,7 +1691,8 @@ clang::sema::AnalysisBasedWarnings::Anal
   DefaultPolicy.enableCheckUnreachable =
     isEnabled(D, warn_unreachable) ||
     isEnabled(D, warn_unreachable_break) ||
-    isEnabled(D, warn_unreachable_return);
+    isEnabled(D, warn_unreachable_return) ||
+    isEnabled(D, warn_unreachable_loop_increment);
 
   DefaultPolicy.enableThreadSafetyAnalysis =
     isEnabled(D, warn_double_lock);

Modified: cfe/trunk/test/SemaCXX/unreachable-code.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/unreachable-code.cpp?rev=204430&r1=204429&r2=204430&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/unreachable-code.cpp (original)
+++ cfe/trunk/test/SemaCXX/unreachable-code.cpp Fri Mar 21 01:02:36 2014
@@ -5,7 +5,7 @@ int bar();
 int test1() {
   for (int i = 0;
        i != 10;
-       ++i) {  // expected-warning {{will never be executed}}
+       ++i) {  // expected-warning {{loop will run at most once (loop increment never executed)}}
     if (j == 23) // missing {}'s
       bar();
       return 1;
@@ -17,7 +17,7 @@ int test1() {
 int test1_B() {
   for (int i = 0;
        i != 10;
-       ++i) {  // expected-warning {{will never be executed}}
+       ++i) {  // expected-warning {{loop will run at most once (loop increment never executed)}}
     if (j == 23) // missing {}'s
       bar();
       return 1;

Modified: cfe/trunk/test/SemaCXX/warn-unreachable.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-unreachable.cpp?rev=204430&r1=204429&r2=204430&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-unreachable.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-unreachable.cpp Fri Mar 21 01:02:36 2014
@@ -282,3 +282,15 @@ void test_static_class_var(Frodo &F) {
     somethingToCall(); // no-warning
 }
 
+void test_unreachable_for_null_increment() {
+  for (unsigned i = 0; i < 10 ; ) // no-warning
+    break;
+}
+
+void test_unreachable_forrange_increment() {
+  int x[10] = { 0 };
+  for (auto i : x) { // expected-warning {{loop will run at most once (loop increment never executed)}}
+    break;
+  }
+}
+

Modified: cfe/trunk/test/SemaObjC/warn-unreachable.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/warn-unreachable.m?rev=204430&r1=204429&r2=204430&view=diff
==============================================================================
--- cfe/trunk/test/SemaObjC/warn-unreachable.m (original)
+++ cfe/trunk/test/SemaObjC/warn-unreachable.m Fri Mar 21 01:02:36 2014
@@ -40,3 +40,12 @@ int test_CONFIG() {
   else
     return 0;
 }
+
+// FIXME: This should at some point report a warning
+// that the loop increment is unreachable.
+void test_loop_increment(id container) {
+  for (id x in container) { // no-warning
+    break;
+  }
+}
+





More information about the cfe-commits mailing list