diff --git a/clang/include/clang/AST/StmtCXX.h b/clang/include/clang/AST/StmtCXX.h --- a/clang/include/clang/AST/StmtCXX.h +++ b/clang/include/clang/AST/StmtCXX.h @@ -75,7 +75,8 @@ unsigned NumHandlers; size_t numTrailingObjects(OverloadToken) const { return NumHandlers; } - CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock, ArrayRef handlers); + CXXTryStmt(SourceLocation tryLoc, CompoundStmt *tryBlock, + ArrayRef handlers); CXXTryStmt(EmptyShell Empty, unsigned numHandlers) : Stmt(CXXTryStmtClass), NumHandlers(numHandlers) { } @@ -84,7 +85,7 @@ public: static CXXTryStmt *Create(const ASTContext &C, SourceLocation tryLoc, - Stmt *tryBlock, ArrayRef handlers); + CompoundStmt *tryBlock, ArrayRef handlers); static CXXTryStmt *Create(const ASTContext &C, EmptyShell Empty, unsigned numHandlers); diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp --- a/clang/lib/AST/ASTImporter.cpp +++ b/clang/lib/AST/ASTImporter.cpp @@ -6793,8 +6793,8 @@ return ToHandlerOrErr.takeError(); } - return CXXTryStmt::Create( - Importer.getToContext(), *ToTryLocOrErr,*ToTryBlockOrErr, ToHandlers); + return CXXTryStmt::Create(Importer.getToContext(), *ToTryLocOrErr, + cast(*ToTryBlockOrErr), ToHandlers); } ExpectedStmt ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) { diff --git a/clang/lib/AST/StmtCXX.cpp b/clang/lib/AST/StmtCXX.cpp --- a/clang/lib/AST/StmtCXX.cpp +++ b/clang/lib/AST/StmtCXX.cpp @@ -23,7 +23,8 @@ } CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, SourceLocation tryLoc, - Stmt *tryBlock, ArrayRef handlers) { + CompoundStmt *tryBlock, + ArrayRef handlers) { const size_t Size = totalSizeToAlloc(handlers.size() + 1); void *Mem = C.Allocate(Size, alignof(CXXTryStmt)); return new (Mem) CXXTryStmt(tryLoc, tryBlock, handlers); @@ -36,7 +37,7 @@ return new (Mem) CXXTryStmt(Empty, numHandlers); } -CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock, +CXXTryStmt::CXXTryStmt(SourceLocation tryLoc, CompoundStmt *tryBlock, ArrayRef handlers) : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(handlers.size()) { Stmt **Stmts = getStmts(); diff --git a/clang/lib/CodeGen/CGCoroutine.cpp b/clang/lib/CodeGen/CGCoroutine.cpp --- a/clang/lib/CodeGen/CGCoroutine.cpp +++ b/clang/lib/CodeGen/CGCoroutine.cpp @@ -593,6 +593,18 @@ CGF.EmitStmt(OnFallthrough); } +static CompoundStmt *CoroutineStmtBuilder(ASTContext &Context, + const CoroutineBodyStmt &S) { + Stmt *Stmt = S.getBody(); + if (CompoundStmt *Body = dyn_cast(Stmt)) + return Body; + // We are about to create a `CXXTryStmt` which requires a `CompoundStmt`. + // If the function body is not a `CompoundStmt` yet then we have to create + // a new one. This happens for cases like the "function-try-block" syntax. + return CompoundStmt::Create(Context, {Stmt}, FPOptionsOverride(), + SourceLocation(), SourceLocation()); +} + void CodeGenFunction::EmitCoroutineBody(const CoroutineBodyStmt &S) { auto *NullPtr = llvm::ConstantPointerNull::get(Builder.getInt8PtrTy()); auto &TI = CGM.getContext().getTargetInfo(); @@ -721,8 +733,8 @@ auto Loc = S.getBeginLoc(); CXXCatchStmt Catch(Loc, /*exDecl=*/nullptr, CurCoro.Data->ExceptionHandler); - auto *TryStmt = - CXXTryStmt::Create(getContext(), Loc, S.getBody(), &Catch); + CompoundStmt *Body = CoroutineStmtBuilder(getContext(), S); + auto *TryStmt = CXXTryStmt::Create(getContext(), Loc, Body, &Catch); EnterCXXTryStmt(*TryStmt); emitBodyAndFallthrough(*this, S, TryStmt->getTryBlock()); diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp --- a/clang/lib/Sema/SemaStmt.cpp +++ b/clang/lib/Sema/SemaStmt.cpp @@ -4546,7 +4546,8 @@ FSI->setHasCXXTry(TryLoc); - return CXXTryStmt::Create(Context, TryLoc, TryBlock, Handlers); + return CXXTryStmt::Create(Context, TryLoc, cast(TryBlock), + Handlers); } StmtResult Sema::ActOnSEHTryBlock(bool IsCXXTry, SourceLocation TryLoc, diff --git a/clang/test/CodeGenCoroutines/coro-function-try-block.cpp b/clang/test/CodeGenCoroutines/coro-function-try-block.cpp new file mode 100644 --- /dev/null +++ b/clang/test/CodeGenCoroutines/coro-function-try-block.cpp @@ -0,0 +1,23 @@ +// RUN: %clang_cc1 -std=c++20 -triple=x86_64-- -emit-llvm -fcxx-exceptions \ +// RUN: -disable-llvm-passes %s -o - | FileCheck %s + +#include "Inputs/coroutine.h" + +struct task { + struct promise_type { + task get_return_object(); + std::suspend_never initial_suspend(); + std::suspend_never final_suspend() noexcept; + void return_void(); + void unhandled_exception() noexcept; + }; +}; + +task f() try { + co_return; +} catch(...) { +} + +// CHECK-LABEL: define{{.*}} void @_Z1fv( +// CHECK: call void @_ZNSt13suspend_never13await_suspendESt16coroutine_handleIvE( +// CHECK: call void @_ZN4task12promise_type11return_voidEv(