[clang] [clang-tools-extra] [clang]: Propagate `*noreturn` attributes in `CFG` (PR #146355)
Balazs Benics via cfe-commits
cfe-commits at lists.llvm.org
Thu Jul 17 11:35:02 PDT 2025
================
@@ -6298,10 +6308,60 @@ static bool isImmediateSinkBlock(const CFGBlock *Blk) {
// at least for now, but once we have better support for exceptions,
// we'd need to carefully handle the case when the throw is being
// immediately caught.
- if (llvm::any_of(*Blk, [](const CFGElement &Elm) {
+ if (llvm::any_of(*Blk, [](const CFGElement &Elm) -> bool {
+ if (std::optional<CFGStmt> StmtElm = Elm.getAs<CFGStmt>())
+ return isa<CXXThrowExpr>(StmtElm->getStmt());
+ return false;
+ }))
+ return true;
+
+ auto HasNoReturnCall = [&](const CallExpr *CE) {
+ if (!CE)
+ return false;
+
+ auto *FD = CE->getDirectCallee();
+
+ if (!FD)
+ return false;
+
+ auto *CanCD = FD->getCanonicalDecl();
+ auto *DefFD = CanCD->getDefinition();
+ auto NoRetAttrOpt = CanCD->getAnalyzerNoReturn();
+ auto NoReturn = false;
+
+ if (!NoRetAttrOpt && DefFD && DefFD->getBody()) {
+ // HACK: we are gonna cache analysis result as implicit
+ // `analyzer_noreturn` attribute
+ auto *MutCD = const_cast<FunctionDecl *>(CanCD);
+
+ // Mark function as `analyzer_noreturn(false)` to:
+ // * prevent infinite recursion in noreturn analysis
+ // * indicate that we've already analyzed(-ing) this function
+ // * serve as a safe default assumption (function may return)
+ MutCD->addAttr(AnalyzerNoReturnAttr::CreateImplicit(
+ CanCD->getASTContext(), false, CanCD->getLocation()));
+
+ auto CalleeCFG =
----------------
steakhal wrote:
I've discussed this with @Xazax-hun and I agree with him that we should have a look at the performance of some warnings. I think they live under the [AnalysisBasedWarnings.cpp](https://github.com/llvm/llvm-project/blob/main/clang/lib/Sema/AnalysisBasedWarnings.cpp) file.
There could be many, but I can recall one which is the noreturn analysis, like `CheckFallThrough`.
Another problem with the use of this `analyzer_noreturn(bool)` attribute is that it will basically touch/pollute every FunctionDecl in the AST - if I understand it correctly.
https://github.com/llvm/llvm-project/pull/146355
More information about the cfe-commits
mailing list