Author: arphaman
Date: Tue Apr 11 10:36:06 2017
New Revision: 299951

URL: http://llvm.org/viewvc/llvm-project?rev=299951&view=rev
Log:
Fix PR13910: Don't warn that __builtin_unreachable() is unreachable

Differential Revision: https://reviews.llvm.org/D25321

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=299951&r1=299950&r2=299951&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/ReachableCode.cpp (original)
+++ cfe/trunk/lib/Analysis/ReachableCode.cpp Tue Apr 11 10:36:06 2017
@@ -58,6 +58,14 @@ static bool isTrivialDoWhile(const CFGBl
   return false;
 }
 
+static bool isBuiltinUnreachable(const Stmt *S) {
+  if (const auto *DRE = dyn_cast<DeclRefExpr>(S))
+    if (const auto *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl()))
+      return FDecl->getIdentifier() &&
+             FDecl->getBuiltinID() == Builtin::BI__builtin_unreachable;
+  return false;
+}
+
 static bool isDeadReturn(const CFGBlock *B, const Stmt *S) {
   // Look to see if the current control flow ends with a 'return', and see if
   // 'S' is a substatement. The 'return' may not be the last element in the
@@ -592,8 +600,7 @@ void DeadCodeScan::reportDeadCode(const
 
   if (isa<BreakStmt>(S)) {
     UK = reachable_code::UK_Break;
-  }
-  else if (isTrivialDoWhile(B, S)) {
+  } else if (isTrivialDoWhile(B, S) || isBuiltinUnreachable(S)) {
     return;
   }
   else if (isDeadReturn(B, S)) {

Modified: cfe/trunk/test/Sema/warn-unreachable.c
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/warn-unreachable.c?rev=299951&r1=299950&r2=299951&view=diff
==============================================================================
--- cfe/trunk/test/Sema/warn-unreachable.c (original)
+++ cfe/trunk/test/Sema/warn-unreachable.c Tue Apr 11 10:36:06 2017
@@ -461,3 +461,40 @@ void testTrueFalseMacros() {
   if (!true) // expected-note {{silence by adding parentheses to mark code as 
explicitly dead}}
     testTrueFalseMacros(); // expected-warning {{code will never be executed}}
 }
+
+int pr13910_foo(int x) {
+  if (x == 1)
+    return 0;
+  else
+    return x;
+  __builtin_unreachable(); // expected no warning
+}
+
+int pr13910_bar(int x) {
+  switch (x) {
+  default:
+    return x + 1;
+  }
+  pr13910_foo(x); // expected-warning {{code will never be executed}}
+}
+
+int pr13910_bar2(int x) {
+  if (x == 1)
+    return 0;
+  else
+    return x;
+  pr13910_foo(x);          // expected-warning {{code will never be executed}}
+  __builtin_unreachable(); // expected no warning
+  pr13910_foo(x);          // expected-warning {{code will never be executed}}
+}
+
+void pr13910_noreturn() {
+  raze();
+  __builtin_unreachable(); // expected no warning
+}
+
+void pr13910_assert() {
+  myassert(0 && "unreachable");
+  return;
+  __builtin_unreachable(); // expected no warning
+}


_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to