llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

<details>
<summary>Changes</summary>

To check whether a function is declared with `noexcept` or the `nothrow` 
attribute.

---
Full diff: https://github.com/llvm/llvm-project/pull/246812.diff


4 Files Affected:

- (modified) clang/include/clang/AST/Decl.h (+4) 
- (modified) clang/lib/AST/Decl.cpp (+5) 
- (modified) clang/lib/Sema/AnalysisBasedWarnings.cpp (+2-8) 
- (modified) clang/lib/Sema/SemaFunctionEffects.cpp (+2-7) 


``````````diff
diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h
index 4104ddc89c50f9..a17af8119df4b8 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 a50c4a6375253a..de1cc514a1d973 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 151167aa461d5e..21c4dce1b1c018 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 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;
         }
       }

``````````

</details>


https://github.com/llvm/llvm-project/pull/246812
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to