]> granicus.if.org Git - clang/commitdiff
Add warning for functions/blocks that have attribute 'noreturn' but return a non...
authorTed Kremenek <kremenek@apple.com>
Thu, 19 Aug 2010 00:52:13 +0000 (00:52 +0000)
committerTed Kremenek <kremenek@apple.com>
Thu, 19 Aug 2010 00:52:13 +0000 (00:52 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@111492 91177308-0d34-0410-b5e6-96231b3b80d8

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

index b333a833b0030ad3196eb11c6e4c56af09f10fa2..2a262980a98adc1f410f0994c652ab83ccdcd949 100644 (file)
@@ -3135,6 +3135,9 @@ def ext_return_has_void_expr : Extension<
 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">>;
index accd7e63ed866c285fc85cea0fb14ba19268d13e..e3628c1d5f09355849f547927a978805e3822f35 100644 (file)
@@ -1833,6 +1833,14 @@ static void HandleObjCGCTypeAttribute(QualType &Type,
   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 @@ bool ProcessFnAttr(Sema &S, QualType &Type, const AttributeList &Attr) {
         && !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;
index f6a92831d7392a8dadffb99ee33f157a3767d292..b4fa77ef3e78a37432a215ec4024cbb0a9d03e33 100644 (file)
@@ -323,7 +323,7 @@ int test_invalidate_by_ref() {
 // 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);
index 5333a2c13fc28e7d13473360b0baeb483a5c5343..927df7f41224fa6450b20aa51fae93e2b3b40158 100644 (file)
@@ -9,7 +9,7 @@ static void __attribute__((noreturn)) f0(void) {
 } // 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'}}
 
index 5a4ec010d3a2113b358e1f9fc8024ac7daf35323..d40cef18d2112d8b59c5e0d022dc9ad1c42febfd 100644 (file)
@@ -100,10 +100,10 @@ int (*funcptr3[5])(long);
 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}}
 }
 
 
index ff43754a429941802ef2d20d67dd11a5cbc6e2d8..a7f4e6f21634fe201151c869ecadee87a7646c4e 100644 (file)
@@ -20,11 +20,11 @@ void test2_positive() {
 // 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}}
 
index 54c340634d39850d7907aaf346f93930f62e5666..af78b410512559ea7bef71982787bef8b346732d 100644 (file)
@@ -60,7 +60,7 @@ int test8() {
   (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());
index 10ed6961a556285c72d68fe67ad2712a9953ca35..0d2efe95d6634a7a16ae03742420d6c85c4f35ce 100644 (file)
@@ -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();
 
index b7d39992b8fb1f7ff7257cb1e067a4f4a55e973e..dbf73fbae8c10424ed7e81fe7aec5c94ee74d31e 100644 (file)
@@ -31,7 +31,7 @@ void test_f3() {
 
 
 class xpto {
-  int blah() __attribute__((noreturn));
+  int blah() __attribute__((noreturn)); // expected-warning{{functions declared 'noreturn' should have a 'void' result type}}
 };
 
 int xpto::blah() {
index f5601cd2df08156e0065482d37a89047d8fb4c13..827dc48ce7c869bd9a5c53eb2e9efe90929c5364 100644 (file)
@@ -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 @@ void test5() {
   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}}
 }