<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On 6 October 2016 at 15:50, David Blaikie <span dir="ltr"><<a href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><br><br><div class="gmail_quote"><div><div class="gmail-h5"><div dir="ltr">On Thu, Oct 6, 2016 at 6:16 AM Alex Lorenz <<a href="mailto:arphaman@gmail.com" target="_blank">arphaman@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">arphaman created this revision.<br class="gmail-m_499611871092886359gmail_msg">
arphaman added reviewers: dblaikie, krememek.<br class="gmail-m_499611871092886359gmail_msg">
arphaman added a subscriber: cfe-commits.<br class="gmail-m_499611871092886359gmail_msg">
arphaman set the repository for this revision to rL LLVM.<br class="gmail-m_499611871092886359gmail_msg">
<br class="gmail-m_499611871092886359gmail_msg">
This patch fixes the issue of clang emitting an unreachable warning when it encounters unreachable `__builtin_unreachable()` statements. It detects references to `__builtin_unreachable` and doesn't emit an unreachable warning for them.<br class="gmail-m_499611871092886359gmail_msg">
<br class="gmail-m_499611871092886359gmail_msg">
<br class="gmail-m_499611871092886359gmail_msg">
Repository:<br class="gmail-m_499611871092886359gmail_msg">
  rL LLVM<br class="gmail-m_499611871092886359gmail_msg">
<br class="gmail-m_499611871092886359gmail_msg">
<a href="https://reviews.llvm.org/D25321" rel="noreferrer" class="gmail-m_499611871092886359gmail_msg" target="_blank">https://reviews.llvm.org/<wbr>D25321</a><br class="gmail-m_499611871092886359gmail_msg">
<br class="gmail-m_499611871092886359gmail_msg">
Files:<br class="gmail-m_499611871092886359gmail_msg">
  lib/Analysis/ReachableCode.cpp<br class="gmail-m_499611871092886359gmail_msg">
  test/Sema/warn-unreachable.c<br class="gmail-m_499611871092886359gmail_msg">
<br class="gmail-m_499611871092886359gmail_msg">
<br class="gmail-m_499611871092886359gmail_msg">
Index: test/Sema/warn-unreachable.c<br class="gmail-m_499611871092886359gmail_msg">
==============================<wbr>==============================<wbr>=======<br class="gmail-m_499611871092886359gmail_msg">
--- test/Sema/warn-unreachable.c<br class="gmail-m_499611871092886359gmail_msg">
+++ test/Sema/warn-unreachable.c<br class="gmail-m_499611871092886359gmail_msg">
@@ -396,3 +396,40 @@<br class="gmail-m_499611871092886359gmail_msg">
   else<br class="gmail-m_499611871092886359gmail_msg">
     calledFun();<br class="gmail-m_499611871092886359gmail_msg">
 }<br class="gmail-m_499611871092886359gmail_msg">
+<br class="gmail-m_499611871092886359gmail_msg">
+int pr13910_foo(int x) {<br class="gmail-m_499611871092886359gmail_msg">
+  if (x == 1)<br class="gmail-m_499611871092886359gmail_msg">
+    return 0;<br class="gmail-m_499611871092886359gmail_msg">
+  else<br class="gmail-m_499611871092886359gmail_msg">
+    return x;<br class="gmail-m_499611871092886359gmail_msg">
+  __builtin_unreachable(); // expected no warning<br class="gmail-m_499611871092886359gmail_msg">
+}<br class="gmail-m_499611871092886359gmail_msg">
+<br class="gmail-m_499611871092886359gmail_msg">
+int pr13910_bar(int x) {<br class="gmail-m_499611871092886359gmail_msg">
+  switch (x) {<br class="gmail-m_499611871092886359gmail_msg">
+  default:<br class="gmail-m_499611871092886359gmail_msg">
+    return x + 1;<br class="gmail-m_499611871092886359gmail_msg">
+  }<br class="gmail-m_499611871092886359gmail_msg">
+  pr13910_foo(x); // expected-warning {{code will never be executed}}<br class="gmail-m_499611871092886359gmail_msg">
+}<br class="gmail-m_499611871092886359gmail_msg">
+<br class="gmail-m_499611871092886359gmail_msg">
+int pr13910_bar2(int x) {<br class="gmail-m_499611871092886359gmail_msg">
+  if (x == 1)<br class="gmail-m_499611871092886359gmail_msg">
+    return 0;<br class="gmail-m_499611871092886359gmail_msg">
+  else<br class="gmail-m_499611871092886359gmail_msg">
+    return x;<br class="gmail-m_499611871092886359gmail_msg">
+  pr13910_foo(x);          // expected-warning {{code will never be executed}}<br class="gmail-m_499611871092886359gmail_msg">
+  __builtin_unreachable(); // expected no warning<br class="gmail-m_499611871092886359gmail_msg">
+  pr13910_foo(x);          // expected-warning {{code will never be executed}}<br class="gmail-m_499611871092886359gmail_msg"></blockquote><div><br></div></div></div><div>Seems strange to have two warnings here ^ is that a separate bug?</div></div></div></blockquote><div><br></div><div>It seems that it might be, yeah. For example, clang emits two unreachable warnings for the two `foo` calls in this code:</div><div><br></div><div><div>int foo() {</div><div>  return 0; </div><div>}</div><div>int bar(int x) {</div><div>  if (x == 0)</div><div>    return x + 1;</div><div>  else</div><div>    return x;</div><div>  foo(); </div><div>  return 0;</div><div>  foo();</div><div>}</div></div><div><br></div><div>But just one when the `return 0` that's between the two `foo` calls is removed. It doesn't seem like there's a PR for this issue, I'll file one later on today.</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><div><div class="gmail-h5"><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
+}<br class="gmail-m_499611871092886359gmail_msg">
+<br class="gmail-m_499611871092886359gmail_msg">
+void pr13910_noreturn() {<br class="gmail-m_499611871092886359gmail_msg">
+  raze();<br class="gmail-m_499611871092886359gmail_msg">
+  __builtin_unreachable(); // expected no warning<br class="gmail-m_499611871092886359gmail_msg">
+}<br class="gmail-m_499611871092886359gmail_msg">
+<br class="gmail-m_499611871092886359gmail_msg">
+void pr13910_assert() {<br class="gmail-m_499611871092886359gmail_msg">
+  myassert(0 && "unreachable");<br class="gmail-m_499611871092886359gmail_msg">
+  return;<br class="gmail-m_499611871092886359gmail_msg">
+  __builtin_unreachable(); // expected no warning<br class="gmail-m_499611871092886359gmail_msg">
+}<br class="gmail-m_499611871092886359gmail_msg">
Index: lib/Analysis/ReachableCode.cpp<br class="gmail-m_499611871092886359gmail_msg">
==============================<wbr>==============================<wbr>=======<br class="gmail-m_499611871092886359gmail_msg">
--- lib/Analysis/ReachableCode.cpp<br class="gmail-m_499611871092886359gmail_msg">
+++ lib/Analysis/ReachableCode.cpp<br class="gmail-m_499611871092886359gmail_msg">
@@ -58,6 +58,15 @@<br class="gmail-m_499611871092886359gmail_msg">
   return false;<br class="gmail-m_499611871092886359gmail_msg">
 }<br class="gmail-m_499611871092886359gmail_msg">
<br class="gmail-m_499611871092886359gmail_msg">
+static bool isBuiltinUnreachable(const CFGBlock *B, const Stmt *S) {<br class="gmail-m_499611871092886359gmail_msg">
+  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) {<br class="gmail-m_499611871092886359gmail_msg">
+    const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE-><wbr>getDecl());<br class="gmail-m_499611871092886359gmail_msg">
+    return FDecl && FDecl->getIdentifier() &&<br class="gmail-m_499611871092886359gmail_msg">
+           FDecl->getBuiltinID() == Builtin::BI__builtin_<wbr>unreachable;<br class="gmail-m_499611871092886359gmail_msg">
+  }<br class="gmail-m_499611871092886359gmail_msg">
+  return false;<br class="gmail-m_499611871092886359gmail_msg">
+}<br class="gmail-m_499611871092886359gmail_msg">
+<br class="gmail-m_499611871092886359gmail_msg">
 static bool isDeadReturn(const CFGBlock *B, const Stmt *S) {<br class="gmail-m_499611871092886359gmail_msg">
   // Look to see if the current control flow ends with a 'return', and see if<br class="gmail-m_499611871092886359gmail_msg">
   // 'S' is a substatement. The 'return' may not be the last element in the<br class="gmail-m_499611871092886359gmail_msg">
@@ -574,8 +583,7 @@<br class="gmail-m_499611871092886359gmail_msg">
<br class="gmail-m_499611871092886359gmail_msg">
   if (isa<BreakStmt>(S)) {<br class="gmail-m_499611871092886359gmail_msg">
     UK = reachable_code::UK_Break;<br class="gmail-m_499611871092886359gmail_msg">
-  }<br class="gmail-m_499611871092886359gmail_msg">
-  else if (isTrivialDoWhile(B, S)) {<br class="gmail-m_499611871092886359gmail_msg">
+  } else if (isTrivialDoWhile(B, S) || isBuiltinUnreachable(B, S)) {<br class="gmail-m_499611871092886359gmail_msg">
     return;<br class="gmail-m_499611871092886359gmail_msg">
   }<br class="gmail-m_499611871092886359gmail_msg">
   else if (isDeadReturn(B, S)) {<br class="gmail-m_499611871092886359gmail_msg">
<br class="gmail-m_499611871092886359gmail_msg">
<br class="gmail-m_499611871092886359gmail_msg">
</blockquote></div></div></div></div>
</blockquote></div><br></div></div>