aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/clang/Basic/DiagnosticSemaKinds.td2
-rw-r--r--lib/Sema/SemaStmt.cpp9
-rw-r--r--test/Sema/attr-noreturn.c7
3 files changed, 16 insertions, 2 deletions
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td
index fb1820d8ec..306fcaccfa 100644
--- a/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/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">;
diff --git a/lib/Sema/SemaStmt.cpp b/lib/Sema/SemaStmt.cpp
index cf240291c7..311496c720 100644
--- a/lib/Sema/SemaStmt.cpp
+++ b/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();
diff --git a/test/Sema/attr-noreturn.c b/test/Sema/attr-noreturn.c
index 35c2bb5fdb..1335c765b3 100644
--- a/test/Sema/attr-noreturn.c
+++ b/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}}
+}
+