r299541 - -Wunreachable-code: 'true' and 'false' should not be treated as configuration

Alex Lorenz via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 5 07:07:21 PDT 2017


Author: arphaman
Date: Wed Apr  5 09:07:21 2017
New Revision: 299541

URL: http://llvm.org/viewvc/llvm-project?rev=299541&view=rev
Log:
-Wunreachable-code: 'true' and 'false' should not be treated as configuration
macros

Clang should emit -Wunreachable-code warnings in C mode for code that's
unreachable because of a 'false' or '!true' condition.

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=299541&r1=299540&r2=299541&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/ReachableCode.cpp (original)
+++ cfe/trunk/lib/Analysis/ReachableCode.cpp Wed Apr  5 09:07:21 2017
@@ -132,15 +132,21 @@ static bool isExpandedFromConfigurationM
   // so that we can refine it later.
   SourceLocation L = S->getLocStart();
   if (L.isMacroID()) {
+    SourceManager &SM = PP.getSourceManager();
     if (IgnoreYES_NO) {
       // The Objective-C constant 'YES' and 'NO'
       // are defined as macros.  Do not treat them
       // as configuration values.
-      SourceManager &SM = PP.getSourceManager();
       SourceLocation TopL = getTopMostMacro(L, SM);
       StringRef MacroName = PP.getImmediateMacroName(TopL);
       if (MacroName == "YES" || MacroName == "NO")
         return false;
+    } else if (!PP.getLangOpts().CPlusPlus) {
+      // Do not treat C 'false' and 'true' macros as configuration values.
+      SourceLocation TopL = getTopMostMacro(L, SM);
+      StringRef MacroName = PP.getImmediateMacroName(TopL);
+      if (MacroName == "false" || MacroName == "true")
+        return false;
     }
     return true;
   }

Modified: cfe/trunk/test/Sema/warn-unreachable.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/warn-unreachable.c?rev=299541&r1=299540&r2=299541&view=diff
==============================================================================
--- cfe/trunk/test/Sema/warn-unreachable.c (original)
+++ cfe/trunk/test/Sema/warn-unreachable.c Wed Apr  5 09:07:21 2017
@@ -451,3 +451,13 @@ void unaryOpFixitCastSubExpr(int x) {
             // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:15-[[@LINE-2]]:15}:")"
   unaryOpFixitCastSubExpr(x); // expected-warning {{code will never be executed}}
 }
+
+#define false 0
+#define true 1
+
+void testTrueFalseMacros() {
+  if (false) // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
+    testTrueFalseMacros(); // expected-warning {{code will never be executed}}
+  if (!true) // expected-note {{silence by adding parentheses to mark code as explicitly dead}}
+    testTrueFalseMacros(); // expected-warning {{code will never be executed}}
+}




More information about the cfe-commits mailing list