diff options
author | Samarth Narang <70980689+snarang181@users.noreply.github.com> | 2025-06-27 10:52:22 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-06-27 10:52:22 -0400 |
commit | 794edd187cee49e3f12cc2ce587d60fc85e08dd6 (patch) | |
tree | 857613bc850f25fe23a2a40be486f85a5fbc9f21 /clang/lib/Sema/AnalysisBasedWarnings.cpp | |
parent | 07f1502b867e85b0b67a54664e57b71dcb37f1cb (diff) | |
download | llvm-794edd187cee49e3f12cc2ce587d60fc85e08dd6.zip llvm-794edd187cee49e3f12cc2ce587d60fc85e08dd6.tar.gz llvm-794edd187cee49e3f12cc2ce587d60fc85e08dd6.tar.bz2 |
[clang] Suppress noreturn warning if last statement in a function is a throw (#145166)
Fixes https://github.com/llvm/llvm-project/issues/144952
Diffstat (limited to 'clang/lib/Sema/AnalysisBasedWarnings.cpp')
-rw-r--r-- | clang/lib/Sema/AnalysisBasedWarnings.cpp | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/clang/lib/Sema/AnalysisBasedWarnings.cpp b/clang/lib/Sema/AnalysisBasedWarnings.cpp index 2a107a36..f3798bb 100644 --- a/clang/lib/Sema/AnalysisBasedWarnings.cpp +++ b/clang/lib/Sema/AnalysisBasedWarnings.cpp @@ -643,7 +643,7 @@ static void CheckFallThroughForBody(Sema &S, const Decl *D, const Stmt *Body, ReturnsVoid = CBody->getFallthroughHandler() != nullptr; else ReturnsVoid = FD->getReturnType()->isVoidType(); - HasNoReturn = FD->isNoReturn(); + HasNoReturn = FD->isNoReturn() || FD->hasAttr<InferredNoReturnAttr>(); } else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) { ReturnsVoid = MD->getReturnType()->isVoidType(); @@ -681,6 +681,28 @@ static void CheckFallThroughForBody(Sema &S, const Decl *D, const Stmt *Body, if (CD.diag_FallThrough_HasNoReturn) S.Diag(RBrace, CD.diag_FallThrough_HasNoReturn) << CD.FunKind; } else if (!ReturnsVoid && CD.diag_FallThrough_ReturnsNonVoid) { + // If the final statement is a call to an always-throwing function, + // don't warn about the fall-through. + if (const auto *FD = D->getAsFunction()) { + if (const auto *CS = dyn_cast<CompoundStmt>(Body); + CS && !CS->body_empty()) { + const Stmt *LastStmt = CS->body_back(); + // Unwrap ExprWithCleanups if necessary. + if (const auto *EWC = dyn_cast<ExprWithCleanups>(LastStmt)) { + LastStmt = EWC->getSubExpr(); + } + if (const auto *CE = dyn_cast<CallExpr>(LastStmt)) { + if (const FunctionDecl *Callee = CE->getDirectCallee(); + Callee && Callee->hasAttr<InferredNoReturnAttr>()) { + return; // Don't warn about fall-through. + } + } + // Direct throw. + if (isa<CXXThrowExpr>(LastStmt)) { + return; // Don't warn about fall-through. + } + } + } bool NotInAllControlPaths = FallThroughType == MaybeFallThrough; S.Diag(RBrace, CD.diag_FallThrough_ReturnsNonVoid) << CD.FunKind << NotInAllControlPaths; |