From: Alex Lorenz Date: Wed, 5 Apr 2017 14:07:21 +0000 (+0000) Subject: -Wunreachable-code: 'true' and 'false' should not be treated as configuration X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2c1528e18a33f31e3b639fa92adcb63117222b08;p=clang -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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@299541 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Analysis/ReachableCode.cpp b/lib/Analysis/ReachableCode.cpp index a2f3203762..8a9674484a 100644 --- a/lib/Analysis/ReachableCode.cpp +++ b/lib/Analysis/ReachableCode.cpp @@ -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; } diff --git a/test/Sema/warn-unreachable.c b/test/Sema/warn-unreachable.c index 34e0296a20..1f7921610b 100644 --- a/test/Sema/warn-unreachable.c +++ b/test/Sema/warn-unreachable.c @@ -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}} +}