<div dir="ltr"><br><br><div class="gmail_quote"><div dir="ltr">On Thu, Oct 6, 2016 at 6:16 AM Alex Lorenz <<a href="mailto:arphaman@gmail.com">arphaman@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">arphaman created this revision.<br class="gmail_msg">
arphaman added reviewers: dblaikie, krememek.<br class="gmail_msg">
arphaman added a subscriber: cfe-commits.<br class="gmail_msg">
arphaman set the repository for this revision to rL LLVM.<br class="gmail_msg">
<br class="gmail_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_msg">
<br class="gmail_msg">
<br class="gmail_msg">
Repository:<br class="gmail_msg">
rL LLVM<br class="gmail_msg">
<br class="gmail_msg">
<a href="https://reviews.llvm.org/D25321" rel="noreferrer" class="gmail_msg" target="_blank">https://reviews.llvm.org/D25321</a><br class="gmail_msg">
<br class="gmail_msg">
Files:<br class="gmail_msg">
lib/Analysis/ReachableCode.cpp<br class="gmail_msg">
test/Sema/warn-unreachable.c<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
Index: test/Sema/warn-unreachable.c<br class="gmail_msg">
===================================================================<br class="gmail_msg">
--- test/Sema/warn-unreachable.c<br class="gmail_msg">
+++ test/Sema/warn-unreachable.c<br class="gmail_msg">
@@ -396,3 +396,40 @@<br class="gmail_msg">
else<br class="gmail_msg">
calledFun();<br class="gmail_msg">
}<br class="gmail_msg">
+<br class="gmail_msg">
+int pr13910_foo(int x) {<br class="gmail_msg">
+ if (x == 1)<br class="gmail_msg">
+ return 0;<br class="gmail_msg">
+ else<br class="gmail_msg">
+ return x;<br class="gmail_msg">
+ __builtin_unreachable(); // expected no warning<br class="gmail_msg">
+}<br class="gmail_msg">
+<br class="gmail_msg">
+int pr13910_bar(int x) {<br class="gmail_msg">
+ switch (x) {<br class="gmail_msg">
+ default:<br class="gmail_msg">
+ return x + 1;<br class="gmail_msg">
+ }<br class="gmail_msg">
+ pr13910_foo(x); // expected-warning {{code will never be executed}}<br class="gmail_msg">
+}<br class="gmail_msg">
+<br class="gmail_msg">
+int pr13910_bar2(int x) {<br class="gmail_msg">
+ if (x == 1)<br class="gmail_msg">
+ return 0;<br class="gmail_msg">
+ else<br class="gmail_msg">
+ return x;<br class="gmail_msg">
+ pr13910_foo(x); // expected-warning {{code will never be executed}}<br class="gmail_msg">
+ __builtin_unreachable(); // expected no warning<br class="gmail_msg">
+ pr13910_foo(x); // expected-warning {{code will never be executed}}<br class="gmail_msg"></blockquote><div><br></div><div>Seems strange to have two warnings here ^ is that a separate bug?</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+}<br class="gmail_msg">
+<br class="gmail_msg">
+void pr13910_noreturn() {<br class="gmail_msg">
+ raze();<br class="gmail_msg">
+ __builtin_unreachable(); // expected no warning<br class="gmail_msg">
+}<br class="gmail_msg">
+<br class="gmail_msg">
+void pr13910_assert() {<br class="gmail_msg">
+ myassert(0 && "unreachable");<br class="gmail_msg">
+ return;<br class="gmail_msg">
+ __builtin_unreachable(); // expected no warning<br class="gmail_msg">
+}<br class="gmail_msg">
Index: lib/Analysis/ReachableCode.cpp<br class="gmail_msg">
===================================================================<br class="gmail_msg">
--- lib/Analysis/ReachableCode.cpp<br class="gmail_msg">
+++ lib/Analysis/ReachableCode.cpp<br class="gmail_msg">
@@ -58,6 +58,15 @@<br class="gmail_msg">
return false;<br class="gmail_msg">
}<br class="gmail_msg">
<br class="gmail_msg">
+static bool isBuiltinUnreachable(const CFGBlock *B, const Stmt *S) {<br class="gmail_msg">
+ if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) {<br class="gmail_msg">
+ const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());<br class="gmail_msg">
+ return FDecl && FDecl->getIdentifier() &&<br class="gmail_msg">
+ FDecl->getBuiltinID() == Builtin::BI__builtin_unreachable;<br class="gmail_msg">
+ }<br class="gmail_msg">
+ return false;<br class="gmail_msg">
+}<br class="gmail_msg">
+<br class="gmail_msg">
static bool isDeadReturn(const CFGBlock *B, const Stmt *S) {<br class="gmail_msg">
// Look to see if the current control flow ends with a 'return', and see if<br class="gmail_msg">
// 'S' is a substatement. The 'return' may not be the last element in the<br class="gmail_msg">
@@ -574,8 +583,7 @@<br class="gmail_msg">
<br class="gmail_msg">
if (isa<BreakStmt>(S)) {<br class="gmail_msg">
UK = reachable_code::UK_Break;<br class="gmail_msg">
- }<br class="gmail_msg">
- else if (isTrivialDoWhile(B, S)) {<br class="gmail_msg">
+ } else if (isTrivialDoWhile(B, S) || isBuiltinUnreachable(B, S)) {<br class="gmail_msg">
return;<br class="gmail_msg">
}<br class="gmail_msg">
else if (isDeadReturn(B, S)) {<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
</blockquote></div></div>