From f7c41dab1a8de29b0991e853b8822bb0d1ddc01c Mon Sep 17 00:00:00 2001 From: Mike Stump Date: Wed, 29 Apr 2009 00:43:21 +0000 Subject: [PATCH] Implement sema checking for noreturn. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@70353 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticSemaKinds.td | 2 ++ lib/Sema/SemaStmt.cpp | 9 +++++++-- test/Sema/attr-noreturn.c | 7 +++++++ 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()) { + 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}} +} + -- 2.40.0