]> granicus.if.org Git - clang/commitdiff
-Wunreachable-code: 'true' and 'false' should not be treated as configuration
authorAlex Lorenz <arphaman@gmail.com>
Wed, 5 Apr 2017 14:07:21 +0000 (14:07 +0000)
committerAlex Lorenz <arphaman@gmail.com>
Wed, 5 Apr 2017 14:07:21 +0000 (14:07 +0000)
macros

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@299541 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/ReachableCode.cpp
test/Sema/warn-unreachable.c

index a2f3203762f7662d57bb02530acf3fc575efed98..8a9674484a5f7a9733d8df275097e3a0df0a56d6 100644 (file)
@@ -132,15 +132,21 @@ static bool isExpandedFromConfigurationMacro(const Stmt *S,
   // 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;
   }
index 34e0296a20490405310c8761bbd236df91850c45..1f7921610b9b1def95cd6b731d926a0d71369f76 100644 (file)
@@ -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}}
+}