[PATCH] NoReturn Warning: Removed Warning for Unreachable Return

Michael Bao mike.h.bao at gmail.com
Fri Jan 3 11:18:18 PST 2014


>From Bug 10642 (http://llvm.org/bugs/show_bug.cgi?id=10642). 

If a return statement is unreachable, do not emit the warning stating that the function that is declared 'noreturn' should not return. 

http://llvm-reviews.chandlerc.com/D2508

Files:
  lib/Sema/SemaStmt.cpp
  test/Sema/return-noreturn.c

Index: lib/Sema/SemaStmt.cpp
===================================================================
--- lib/Sema/SemaStmt.cpp
+++ lib/Sema/SemaStmt.cpp
@@ -2776,9 +2776,6 @@
   QualType RelatedRetType;
   if (const FunctionDecl *FD = getCurFunctionDecl()) {
     FnRetType = FD->getResultType();
-    if (FD->isNoReturn())
-      Diag(ReturnLoc, diag::warn_noreturn_function_has_return_expr)
-        << FD->getDeclName();
   } else if (ObjCMethodDecl *MD = getCurMethodDecl()) {
     FnRetType = MD->getResultType();
     if (MD->hasRelatedResultType() && MD->getClassInterface()) {
@@ -2953,6 +2950,12 @@
       !CurContext->isDependentContext())
     FunctionScopes.back()->Returns.push_back(Result);
 
+  if (const FunctionDecl *FD = getCurFunctionDecl()) {
+    if (FD->isNoReturn()) 
+      DiagRuntimeBehavior(ReturnLoc, Result,
+        PDiag(diag::warn_noreturn_function_has_return_expr) << FD->getDeclName());
+  }
+
   return Owned(Result);
 }
 
Index: test/Sema/return-noreturn.c
===================================================================
--- test/Sema/return-noreturn.c
+++ test/Sema/return-noreturn.c
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 %s -fsyntax-only -verify -fblocks -Wmissing-noreturn -Wno-unreachable-code
+// RUN: %clang_cc1 %s -fsyntax-only -verify -fblocks -Wmissing-noreturn -Wno-unreachable-code -Winvalid-noreturn
 
 int j;
 void test1() { // expected-warning {{function 'test1' could be declared with attribute 'noreturn'}}
@@ -40,3 +40,11 @@
 _Noreturn void test5() {
   test2_positive();
 }
+
+// Should not warn about an invalid return even if the function is 
+// marked 'noreturn' and a return statement is present 
+// in the case that the return statement is unreachable.
+__attribute__((noreturn)) void test6() {
+  while (1) {}  
+  return;
+}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2508.1.patch
Type: text/x-patch
Size: 1786 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140103/f1e6f788/attachment.bin>


More information about the cfe-commits mailing list