[PATCH] NoReturn Warning: Non-Void Return Type

Michael Bao mike.h.bao at gmail.com
Sun Jan 5 14:17:31 PST 2014


  Rebased off Revision 198572.

  - Reword diagnostic to avoid double negative
  - Pass in -std=c11 to the C test.
  - Put [[noreturn]] attribute in front of function decl in C++ tests.

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

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D2507?vs=6345&id=6353#toc

Files:
  include/clang/Basic/DiagnosticSemaKinds.td
  lib/Sema/SemaDecl.cpp
  test/Sema/warn-noreturn-return-type.c
  test/SemaCXX/warn-noreturn-return-type.cpp

Index: include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- include/clang/Basic/DiagnosticSemaKinds.td
+++ include/clang/Basic/DiagnosticSemaKinds.td
@@ -6459,6 +6459,9 @@
 def warn_falloff_noreturn_function : Warning<
   "function declared 'noreturn' should not return">,
   InGroup<InvalidNoreturn>;
+def warn_noreturn_function_has_nonvoid_type : Warning<
+  "function %0 declared 'noreturn' should have a 'void' return type">,
+  InGroup<InvalidNoreturn>;
 def err_noreturn_block_has_return_expr : Error<
   "block declared 'noreturn' should not return">;
 def err_noreturn_missing_on_first_decl : Error<
Index: lib/Sema/SemaDecl.cpp
===================================================================
--- lib/Sema/SemaDecl.cpp
+++ lib/Sema/SemaDecl.cpp
@@ -7391,6 +7391,9 @@
     AddToScope = false;
   }
 
+  if (NewFD->isNoReturn() && !NewFD->getResultType()->isVoidType())
+    Diag(NewFD->getLocation(), diag::warn_noreturn_function_has_nonvoid_type)
+      << NewFD;
   return NewFD;
 }
 
Index: test/Sema/warn-noreturn-return-type.c
===================================================================
--- test/Sema/warn-noreturn-return-type.c
+++ test/Sema/warn-noreturn-return-type.c
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 %s -fsyntax-only -verify -Winvalid-noreturn -std=c11
+__attribute__((noreturn)) void test1();
+
+__attribute__((noreturn)) int test2(); // expected-warning{{function 'test2' declared 'noreturn' should have a 'void' return type}}
+
+__attribute__((noreturn)) float test3(); // expected-warning{{function 'test3' declared 'noreturn' should have a 'void' return type}}
+
+_Noreturn void test4();
+
+_Noreturn int test5(); // expected-warning{{function 'test5' declared 'noreturn' should have a 'void' return type}}
+
+_Noreturn float test6(); // expected-warning{{function 'test6' declared 'noreturn' should have a 'void' return type}}
Index: test/SemaCXX/warn-noreturn-return-type.cpp
===================================================================
--- test/SemaCXX/warn-noreturn-return-type.cpp
+++ test/SemaCXX/warn-noreturn-return-type.cpp
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 %s -fsyntax-only -verify -Winvalid-noreturn -std=c++11
+[[noreturn]] void test1();
+
+[[noreturn]] int test2(); // expected-warning{{function 'test2' declared 'noreturn' should have a 'void' return type}}
+
+[[noreturn]] float test3(); // expected-warning{{function 'test3' declared 'noreturn' should have a 'void' return type}}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2507.3.patch
Type: text/x-patch
Size: 2504 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140105/b810ab45/attachment.bin>


More information about the cfe-commits mailing list