Browse Source

Implement sema checking for noreturn.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@70353 91177308-0d34-0410-b5e6-96231b3b80d8
Mike Stump 16 years ago
parent
commit
f7c41dab1a
3 changed files with 16 additions and 2 deletions
  1. 2 0
      include/clang/Basic/DiagnosticSemaKinds.td
  2. 7 2
      lib/Sema/SemaStmt.cpp
  3. 7 0
      test/Sema/attr-noreturn.c

+ 2 - 0
include/clang/Basic/DiagnosticSemaKinds.td

@@ -1598,6 +1598,8 @@ def ext_return_has_expr : ExtWarn<
   "void %select{function|method}1 %0 should not return a value">;
 def ext_return_has_void_expr : Extension<
   "void %select{function|method}1 %0 should not return void expression">;
+def err_noreturn_function_has_return_expr : Error<
+  "function %0 declared 'noreturn' should not return">;
 
 def err_shufflevector_non_vector : Error<
   "first two arguments to __builtin_shufflevector must be vectors">;

+ 7 - 2
lib/Sema/SemaStmt.cpp

@@ -788,9 +788,14 @@ Sema::ActOnReturnStmt(SourceLocation ReturnLoc, ExprArg rex) {
     return ActOnBlockReturnStmt(ReturnLoc, RetValExp);
 
   QualType FnRetType;
-  if (FunctionDecl *FD = getCurFunctionDecl())
+  if (const FunctionDecl *FD = getCurFunctionDecl()) {
     FnRetType = FD->getResultType();
-  else if (ObjCMethodDecl *MD = getCurMethodDecl())
+    if (FD->hasAttr<NoReturnAttr>()) {
+      Diag(ReturnLoc, diag::err_noreturn_function_has_return_expr)
+        << getCurFunctionOrMethodDecl()->getDeclName();
+      return StmtError();
+    }
+  } else if (ObjCMethodDecl *MD = getCurMethodDecl())
     FnRetType = MD->getResultType();
   else // If we don't have a function/method context, bail.
     return StmtError();

+ 7 - 0
test/Sema/attr-noreturn.c

@@ -12,3 +12,10 @@ int f1() __attribute__((noreturn));
 int g0 __attribute__((noreturn)); // expected-warning {{'noreturn' attribute only applies to function types}}
 
 int f2() __attribute__((noreturn(1, 2))); // expected-error {{attribute requires 0 argument(s)}}
+
+void f3() __attribute__((noreturn));
+
+void f3() {
+  return;  // expected-error {{function 'f3' declared 'noreturn' should not return}}
+}
+