Timm =?utf-8?q?Bäder?= <[email protected]> Message-ID: In-Reply-To: <llvm.org/llvm/llvm-project/pull/[email protected]>
https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/246812 >From 05d40a578843997eefa5d7921e4f600a342fbd9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timm=20B=C3=A4der?= <[email protected]> Date: Sat, 28 Nov 2026 08:48:37 +0100 Subject: [PATCH 1/2] [clang] Add `FunctionDecl::isNothrow()` To check whether a function is declared with `noexcept` or the `nothrow` attribute. --- clang/include/clang/AST/Decl.h | 4 ++++ clang/lib/AST/Decl.cpp | 5 +++++ clang/lib/Sema/AnalysisBasedWarnings.cpp | 10 ++-------- clang/lib/Sema/SemaFunctionEffects.cpp | 9 ++------- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h index 4104ddc89c50f..a17af8119df4b 100644 --- a/clang/include/clang/AST/Decl.h +++ b/clang/include/clang/AST/Decl.h @@ -2848,6 +2848,10 @@ class FunctionDecl : public DeclaratorDecl, /// through an `analyzer_noreturn` attribute on its declaration. bool isAnalyzerNoReturn() const; + /// Determines whether this function is known not to throw, through + /// a non-throwing exception specification or a 'nothrow' attribute. + bool isNothrow() const; + /// True if the function was a definition but its body was skipped. bool hasSkippedBody() const { return FunctionDeclBits.HasSkippedBody; } void setHasSkippedBody(bool Skipped = true) { diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp index a50c4a6375253..de1cc514a1d97 100644 --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -3739,6 +3739,11 @@ bool FunctionDecl::isAnalyzerNoReturn() const { return hasAttr<AnalyzerNoReturnAttr>(); } +bool FunctionDecl::isNothrow() const { + const auto *FPT = getType()->getAs<FunctionProtoType>(); + return FPT && (FPT->isNothrow() || hasAttr<NoThrowAttr>()); +} + bool FunctionDecl::isMemberLikeConstrainedFriend() const { // C++20 [temp.friend]p9: // A non-template friend declaration with a requires-clause [or] diff --git a/clang/lib/Sema/AnalysisBasedWarnings.cpp b/clang/lib/Sema/AnalysisBasedWarnings.cpp index 151167aa461d5..21c4dce1b1c01 100644 --- a/clang/lib/Sema/AnalysisBasedWarnings.cpp +++ b/clang/lib/Sema/AnalysisBasedWarnings.cpp @@ -399,13 +399,6 @@ static void checkThrowInNonThrowingFunc(Sema &S, const FunctionDecl *FD, }); } -static bool isNoexcept(const FunctionDecl *FD) { - const auto *FPT = FD->getType()->castAs<FunctionProtoType>(); - if (FPT->isNothrow() || FD->hasAttr<NoThrowAttr>()) - return true; - return false; -} - /// Checks if the given expression is a reference to a function with /// 'noreturn' attribute. static bool isReferenceToNoReturn(const Expr *E) { @@ -3393,7 +3386,8 @@ void clang::sema::AnalysisBasedWarnings::IssueWarnings( // Check for throw out of non-throwing function. if (!Diags.isIgnored(diag::warn_throw_in_noexcept_func, D->getBeginLoc())) if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) - if (S.getLangOpts().CPlusPlus && !fscope->isCoroutine() && isNoexcept(FD)) + if (S.getLangOpts().CPlusPlus && !fscope->isCoroutine() && + FD->isNothrow()) checkThrowInNonThrowingFunc(S, FD, AC); // If none of the previous checks caused a CFG build, trigger one here diff --git a/clang/lib/Sema/SemaFunctionEffects.cpp b/clang/lib/Sema/SemaFunctionEffects.cpp index bb446179e0b9a..6102e3391d96b 100644 --- a/clang/lib/Sema/SemaFunctionEffects.cpp +++ b/clang/lib/Sema/SemaFunctionEffects.cpp @@ -120,11 +120,6 @@ static bool functionIsVerifiable(const FunctionDecl *FD) { return FD->hasBody(); } -static bool isNoexcept(const FunctionDecl *FD) { - const auto *FPT = FD->getType()->getAs<FunctionProtoType>(); - return FPT && (FPT->isNothrow() || FD->hasAttr<NoThrowAttr>()); -} - // This list is probably incomplete. // FIXME: Investigate: // __builtin_eh_return? @@ -632,7 +627,7 @@ class Analyzer { bool IsNoexcept = false; if (auto *FD = D->getAsFunction()) { - IsNoexcept = isNoexcept(FD); + IsNoexcept = FD->isNothrow(); } else if (auto *BD = dyn_cast<BlockDecl>(D)) { if (auto *TSI = BD->getSignatureAsWritten()) { auto *FPT = TSI->getType()->castAs<FunctionProtoType>(); @@ -1068,7 +1063,7 @@ class Analyzer { // terminates. Ignore it for the purposes of effect analysis. // If not C++, `noreturn` alone is sufficient. if (FD->isNoReturn() && - (!Outer.S.getLangOpts().CPlusPlus || isNoexcept(FD))) + (!Outer.S.getLangOpts().CPlusPlus || FD->isNothrow())) return; } } >From 5cc07669bb8d19796ebced84764c871d075f4526 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timm=20B=C3=A4der?= <[email protected]> Date: Sun, 29 Nov 2026 10:02:56 +0100 Subject: [PATCH 2/2] isUnresolvedExceptionSpec --- clang/include/clang/AST/Decl.h | 1 + clang/lib/AST/Decl.cpp | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h index a17af8119df4b..6d31e6b2117ec 100644 --- a/clang/include/clang/AST/Decl.h +++ b/clang/include/clang/AST/Decl.h @@ -2850,6 +2850,7 @@ class FunctionDecl : public DeclaratorDecl, /// Determines whether this function is known not to throw, through /// a non-throwing exception specification or a 'nothrow' attribute. + /// Returns \c false for unresolved exception specifications. bool isNothrow() const; /// True if the function was a definition but its body was skipped. diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp index de1cc514a1d97..88d1eb8ce32ec 100644 --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -3740,8 +3740,13 @@ bool FunctionDecl::isAnalyzerNoReturn() const { } bool FunctionDecl::isNothrow() const { + if (hasAttr<NoThrowAttr>()) + return true; + + // Unresolved specs can't be queried; assume the function may throw. const auto *FPT = getType()->getAs<FunctionProtoType>(); - return FPT && (FPT->isNothrow() || hasAttr<NoThrowAttr>()); + return FPT && !isUnresolvedExceptionSpec(FPT->getExceptionSpecType()) && + FPT->isNothrow(); } bool FunctionDecl::isMemberLikeConstrainedFriend() const { _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
