[clang] [clang][NFC] Add `FunctionDecl::isNothrow()` (PR #246812)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 2 00:24:27 PST 2026


https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/246812

>From ab49b35faaf349ec51b5deb89d7d7861076918fe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Sat, 28 Nov 2026 08:48:37 +0100
Subject: [PATCH] [clang] Add `FunctionDecl::isNothrow()`

To check whether a function is declared with `noexcept` or the `nothrow`
attribute.
---
 clang/include/clang/AST/Decl.h           |  5 +++++
 clang/lib/AST/Decl.cpp                   | 10 ++++++++++
 clang/lib/Sema/AnalysisBasedWarnings.cpp | 10 ++--------
 clang/lib/Sema/SemaFunctionEffects.cpp   |  9 ++-------
 4 files changed, 19 insertions(+), 15 deletions(-)

diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h
index 4104ddc89c50f9..6d31e6b2117ec0 100644
--- a/clang/include/clang/AST/Decl.h
+++ b/clang/include/clang/AST/Decl.h
@@ -2848,6 +2848,11 @@ 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.
+  /// Returns \c false for unresolved exception specifications.
+  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 a50c4a6375253a..88d1eb8ce32ec8 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -3739,6 +3739,16 @@ bool FunctionDecl::isAnalyzerNoReturn() const {
   return hasAttr<AnalyzerNoReturnAttr>();
 }
 
+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 && !isUnresolvedExceptionSpec(FPT->getExceptionSpecType()) &&
+         FPT->isNothrow();
+}
+
 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 cdf69b2bf691cf..e345b0fe145329 100644
--- a/clang/lib/Sema/AnalysisBasedWarnings.cpp
+++ b/clang/lib/Sema/AnalysisBasedWarnings.cpp
@@ -397,13 +397,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) {
@@ -3391,7 +3384,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 bb446179e0b9a4..6102e3391d96b9 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;
         }
       }



More information about the cfe-commits mailing list