MatzeB created this revision. MatzeB added reviewers: GorNishanov, EricWF, ChuanqiXu, bruno. Herald added subscribers: modimo, wenlei, martong, mcrosier. Herald added a reviewer: shafik. Herald added a project: All. MatzeB requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
This fixes an assertion error when writing a coroutine with a function-try-block. In this case the function body is not a `CompoundStmt` so the code constructing an artificial CXXTryStmt must also construct a `CompoundStmt` for it. While on it adjust the `CXXStmt::Create` function to only accept `CompoundStmt*`. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D146758 Files: clang/include/clang/AST/StmtCXX.h clang/lib/AST/ASTImporter.cpp clang/lib/AST/StmtCXX.cpp clang/lib/CodeGen/CGCoroutine.cpp clang/lib/Sema/SemaStmt.cpp clang/test/CodeGenCoroutines/coro-function-try-block.cpp
Index: clang/test/CodeGenCoroutines/coro-function-try-block.cpp =================================================================== --- /dev/null +++ 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( Index: clang/lib/Sema/SemaStmt.cpp =================================================================== --- clang/lib/Sema/SemaStmt.cpp +++ 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<CompoundStmt>(TryBlock), + Handlers); } StmtResult Sema::ActOnSEHTryBlock(bool IsCXXTry, SourceLocation TryLoc, Index: clang/lib/CodeGen/CGCoroutine.cpp =================================================================== --- clang/lib/CodeGen/CGCoroutine.cpp +++ clang/lib/CodeGen/CGCoroutine.cpp @@ -721,8 +721,14 @@ auto Loc = S.getBeginLoc(); CXXCatchStmt Catch(Loc, /*exDecl=*/nullptr, CurCoro.Data->ExceptionHandler); - auto *TryStmt = - CXXTryStmt::Create(getContext(), Loc, S.getBody(), &Catch); + Stmt *BodyStmt = S.getBody(); + CompoundStmt *Body = dyn_cast<CompoundStmt>(BodyStmt); + if (Body == nullptr) { + Body = + CompoundStmt::Create(getContext(), {BodyStmt}, FPOptionsOverride(), + SourceLocation(), SourceLocation()); + } + auto *TryStmt = CXXTryStmt::Create(getContext(), Loc, Body, &Catch); EnterCXXTryStmt(*TryStmt); emitBodyAndFallthrough(*this, S, TryStmt->getTryBlock()); Index: clang/lib/AST/StmtCXX.cpp =================================================================== --- clang/lib/AST/StmtCXX.cpp +++ clang/lib/AST/StmtCXX.cpp @@ -23,7 +23,8 @@ } CXXTryStmt *CXXTryStmt::Create(const ASTContext &C, SourceLocation tryLoc, - Stmt *tryBlock, ArrayRef<Stmt *> handlers) { + CompoundStmt *tryBlock, + ArrayRef<Stmt *> handlers) { const size_t Size = totalSizeToAlloc<Stmt *>(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<Stmt *> handlers) : Stmt(CXXTryStmtClass), TryLoc(tryLoc), NumHandlers(handlers.size()) { Stmt **Stmts = getStmts(); Index: clang/lib/AST/ASTImporter.cpp =================================================================== --- clang/lib/AST/ASTImporter.cpp +++ clang/lib/AST/ASTImporter.cpp @@ -6777,8 +6777,8 @@ return ToHandlerOrErr.takeError(); } - return CXXTryStmt::Create( - Importer.getToContext(), *ToTryLocOrErr,*ToTryBlockOrErr, ToHandlers); + return CXXTryStmt::Create(Importer.getToContext(), *ToTryLocOrErr, + cast<CompoundStmt>(*ToTryBlockOrErr), ToHandlers); } ExpectedStmt ASTNodeImporter::VisitCXXForRangeStmt(CXXForRangeStmt *S) { Index: clang/include/clang/AST/StmtCXX.h =================================================================== --- clang/include/clang/AST/StmtCXX.h +++ clang/include/clang/AST/StmtCXX.h @@ -75,7 +75,8 @@ unsigned NumHandlers; size_t numTrailingObjects(OverloadToken<Stmt *>) const { return NumHandlers; } - CXXTryStmt(SourceLocation tryLoc, Stmt *tryBlock, ArrayRef<Stmt*> handlers); + CXXTryStmt(SourceLocation tryLoc, CompoundStmt *tryBlock, + ArrayRef<Stmt *> 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<Stmt*> handlers); + CompoundStmt *tryBlock, ArrayRef<Stmt *> handlers); static CXXTryStmt *Create(const ASTContext &C, EmptyShell Empty, unsigned numHandlers);
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits