[cfe-commits] r111492 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaType.cpp test/Analysis/misc-ps.m test/Sema/attr-noreturn.c test/Sema/block-return.c test/Sema/return-noreturn.c test/Sema/return.c test/Sema/warn-unreachable.c test/SemaCXX/attr-noreturn.cpp test/SemaCXX/warn-unreachable.cpp

Ted Kremenek kremenek at apple.com
Wed Aug 18 17:52:13 PDT 2010


Author: kremenek
Date: Wed Aug 18 19:52:13 2010
New Revision: 111492

URL: http://llvm.org/viewvc/llvm-project?rev=111492&view=rev
Log:
Add warning for functions/blocks that have attribute 'noreturn' but return a non-void result. (<rdar://problem/7562925>)

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaType.cpp
    cfe/trunk/test/Analysis/misc-ps.m
    cfe/trunk/test/Sema/attr-noreturn.c
    cfe/trunk/test/Sema/block-return.c
    cfe/trunk/test/Sema/return-noreturn.c
    cfe/trunk/test/Sema/return.c
    cfe/trunk/test/Sema/warn-unreachable.c
    cfe/trunk/test/SemaCXX/attr-noreturn.cpp
    cfe/trunk/test/SemaCXX/warn-unreachable.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=111492&r1=111491&r2=111492&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Wed Aug 18 19:52:13 2010
@@ -3135,6 +3135,9 @@
 def warn_noreturn_function_has_return_expr : Warning<
   "function %0 declared 'noreturn' should not return">,
   InGroup<DiagGroup<"invalid-noreturn">>;
+def warn_noreturn_function_has_nonvoid_result : Warning<
+  "%select{functions|blocks}0 declared 'noreturn' should have a 'void' result type">,
+  InGroup<DiagGroup<"invalid-noreturn">>;
 def warn_falloff_noreturn_function : Warning<
   "function declared 'noreturn' should not return">,
   InGroup<DiagGroup<"invalid-noreturn">>;

Modified: cfe/trunk/lib/Sema/SemaType.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaType.cpp?rev=111492&r1=111491&r2=111492&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaType.cpp (original)
+++ cfe/trunk/lib/Sema/SemaType.cpp Wed Aug 18 19:52:13 2010
@@ -1833,6 +1833,14 @@
   Type = S.Context.getObjCGCQualType(Type, GCAttr);
 }
 
+static QualType GetResultType(QualType T) {
+  if (const PointerType *PT = T->getAs<PointerType>())
+    T = PT->getPointeeType();
+  else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
+    T = BT->getPointeeType();
+  return T->getAs<FunctionType>()->getResultType();
+}
+
 /// Process an individual function attribute.  Returns true if the
 /// attribute does not make sense to apply to this type.
 bool ProcessFnAttr(Sema &S, QualType &Type, const AttributeList &Attr) {
@@ -1849,7 +1857,12 @@
         && !Type->isBlockPointerType()
         && !Type->isFunctionType())
       return true;
-
+    
+    if (!GetResultType(Type)->isVoidType()) {
+      S.Diag(Attr.getLoc(), diag::warn_noreturn_function_has_nonvoid_result)
+        << (Type->isBlockPointerType() ? /* blocks */ 1 : /* functions */ 0);
+    }
+    
     // Otherwise we can process right away.
     Type = S.Context.getNoReturnType(Type);
     return false;

Modified: cfe/trunk/test/Analysis/misc-ps.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/misc-ps.m?rev=111492&r1=111491&r2=111492&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/misc-ps.m (original)
+++ cfe/trunk/test/Analysis/misc-ps.m Wed Aug 18 19:52:13 2010
@@ -323,7 +323,7 @@
 // was the block containing the merge for '?', which would trigger an
 // assertion failure.
 int rdar_7027684_aux();
-int rdar_7027684_aux_2() __attribute__((noreturn));
+int rdar_7027684_aux_2() __attribute__((noreturn)); // expected-warning{{functions declared 'noreturn' should have a 'void' result type}}
 void rdar_7027684(int x, int y) {
   {}; // this empty compound statement is critical.
   (rdar_7027684_aux() ? rdar_7027684_aux_2() : (void) 0);

Modified: cfe/trunk/test/Sema/attr-noreturn.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/attr-noreturn.c?rev=111492&r1=111491&r2=111492&view=diff
==============================================================================
--- cfe/trunk/test/Sema/attr-noreturn.c (original)
+++ cfe/trunk/test/Sema/attr-noreturn.c Wed Aug 18 19:52:13 2010
@@ -9,7 +9,7 @@
 } // expected-warning {{function declared 'noreturn' should not return}}
 
 // On K&R
-int f1() __attribute__((noreturn));
+int f1() __attribute__((noreturn)); // expected-warning{{functions declared 'noreturn' should have a 'void' result type}}
 
 int g0 __attribute__((noreturn)); // expected-warning {{'noreturn' only applies to function types; type here is 'int'}}
 

Modified: cfe/trunk/test/Sema/block-return.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/block-return.c?rev=111492&r1=111491&r2=111492&view=diff
==============================================================================
--- cfe/trunk/test/Sema/block-return.c (original)
+++ cfe/trunk/test/Sema/block-return.c Wed Aug 18 19:52:13 2010
@@ -100,10 +100,10 @@
 int sz8 = sizeof(^int (*[5])(long) {return funcptr3;}); // expected-error {{block declared as returning an array}}
 
 void foo6() {
-  int (^b)(int) __attribute__((noreturn));
+  int (^b)(int) __attribute__((noreturn)); // expected-warning{{blocks declared 'noreturn' should have a 'void' result type}}
   b = ^ (int i) __attribute__((noreturn)) { return 1; };  // expected-error {{block declared 'noreturn' should not return}}
   b(1);
-  int (^c)(void) __attribute__((noreturn)) = ^ __attribute__((noreturn)) { return 100; }; // expected-error {{block declared 'noreturn' should not return}}
+  int (^c)(void) __attribute__((noreturn)) = ^ __attribute__((noreturn)) { return 100; }; // expected-error {{block declared 'noreturn' should not return}} expected-warning{{blocks declared 'noreturn' should have a 'void' result type}}
 }
 
 

Modified: cfe/trunk/test/Sema/return-noreturn.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/return-noreturn.c?rev=111492&r1=111491&r2=111492&view=diff
==============================================================================
--- cfe/trunk/test/Sema/return-noreturn.c (original)
+++ cfe/trunk/test/Sema/return-noreturn.c Wed Aug 18 19:52:13 2010
@@ -20,11 +20,11 @@
 // This test case illustrates that we don't warn about the missing return
 // because the function is marked noreturn and there is an infinite loop.
 extern int foo_test_3();
-__attribute__((__noreturn__)) void* test3(int arg) {
+__attribute__((__noreturn__)) void* test3(int arg) { // expected-warning{{functions declared 'noreturn' should have a 'void' result type}}
   while (1) foo_test_3();
 }
 
-__attribute__((__noreturn__)) void* test3_positive(int arg) {
+__attribute__((__noreturn__)) void* test3_positive(int arg) { // expected-warning{{functions declared 'noreturn' should have a 'void' result type}}
   while (0) foo_test_3();
 } // expected-warning{{function declared 'noreturn' should not return}}
 

Modified: cfe/trunk/test/Sema/return.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/return.c?rev=111492&r1=111491&r2=111492&view=diff
==============================================================================
--- cfe/trunk/test/Sema/return.c (original)
+++ cfe/trunk/test/Sema/return.c Wed Aug 18 19:52:13 2010
@@ -60,7 +60,7 @@
   (void)(1 + unknown());
 } // expected-warning {{control reaches end of non-void function}}
 
-int halt3() __attribute__((noreturn));
+int halt3() __attribute__((noreturn)); // expected-warning{{functions declared 'noreturn' should have a 'void' result type}}
 
 int test9() {
   (void)(halt3() + unknown());

Modified: cfe/trunk/test/Sema/warn-unreachable.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/warn-unreachable.c?rev=111492&r1=111491&r2=111492&view=diff
==============================================================================
--- cfe/trunk/test/Sema/warn-unreachable.c (original)
+++ cfe/trunk/test/Sema/warn-unreachable.c Wed Aug 18 19:52:13 2010
@@ -1,6 +1,6 @@
 // RUN: %clang %s -fsyntax-only -Xclang -verify -fblocks -Wunreachable-code -Wno-unused-value
 
-int halt() __attribute__((noreturn));
+int halt() __attribute__((noreturn)); // expected-warning{{functions declared 'noreturn' should have a 'void' result type}}
 int live();
 int dead();
 

Modified: cfe/trunk/test/SemaCXX/attr-noreturn.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/attr-noreturn.cpp?rev=111492&r1=111491&r2=111492&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/attr-noreturn.cpp (original)
+++ cfe/trunk/test/SemaCXX/attr-noreturn.cpp Wed Aug 18 19:52:13 2010
@@ -31,7 +31,7 @@
 
 
 class xpto {
-  int blah() __attribute__((noreturn));
+  int blah() __attribute__((noreturn)); // expected-warning{{functions declared 'noreturn' should have a 'void' result type}}
 };
 
 int xpto::blah() {

Modified: cfe/trunk/test/SemaCXX/warn-unreachable.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-unreachable.cpp?rev=111492&r1=111491&r2=111492&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-unreachable.cpp (original)
+++ cfe/trunk/test/SemaCXX/warn-unreachable.cpp Wed Aug 18 19:52:13 2010
@@ -1,6 +1,6 @@
 // RUN: %clang_cc1 %s -fsyntax-only -verify -fblocks -Wunreachable-code -Wno-unused-value
 
-int &halt() __attribute__((noreturn));
+int &halt() __attribute__((noreturn)); // expected-warning{{functions declared 'noreturn' should have a 'void' result type}}
 int &live();
 int dead();
 int liveti() throw(int);
@@ -60,7 +60,7 @@
   struct S {
     int mem;
   } s;
-  S &foor() __attribute__((noreturn));
+  S &foor() __attribute__((noreturn)); // expected-warning{{functions declared 'noreturn' should have a 'void' result type}}
   foor()
     .mem;       // expected-warning {{will never be executed}}
 }





More information about the cfe-commits mailing list