[llvm-branch-commits] [clang-tools-extra] release/23.x: [clang-tidy] Fix crashes when analyzing unknown exceptions in bugprone-exception-escape (#218067) (PR #218128)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sat Aug 22 06:19:18 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-tidy

Author: llvmbot

<details>
<summary>Changes</summary>

Backport c0feba8bedbfda4f1a2f3727f2ef270fcc6ca138

Requested by: @<!-- -->zeyi2

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


3 Files Affected:

- (modified) clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp (+4) 
- (added) clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape-coro-unknown.cpp (+42) 
- (modified) clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape-treat-functions-without-specification-as-throwing.cpp (+17-1) 


``````````diff
diff --git a/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp b/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
index af18fda7a3a54..3224aa0a857be 100644
--- a/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
+++ b/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
@@ -365,6 +365,8 @@ ExceptionAnalyzer::ExceptionInfo::filterByCatch(const Type *HandlerTy,
   SmallVector<const Type *, 8> TypesToDelete;
   for (const auto &ThrownException : ThrownExceptions) {
     const Type *ExceptionTy = ThrownException.getFirst();
+    if (!ExceptionTy)
+      continue;
     const CanQualType ExceptionCanTy =
         ExceptionTy->getCanonicalTypeUnqualified();
     const CanQualType HandlerCanTy = HandlerTy->getCanonicalTypeUnqualified();
@@ -612,6 +614,8 @@ ExceptionAnalyzer::throwsException(const Stmt *St,
                                   Excs.getExceptions(), CallStack));
     for (const auto &Exception : Excs.getExceptions()) {
       const Type *ExcType = Exception.getFirst();
+      if (!ExcType)
+        continue;
       if (const CXXRecordDecl *ThrowableRec = ExcType->getAsCXXRecordDecl()) {
         const ExceptionInfo DestructorExcs = throwsException(
             ThrowableRec->getDestructor(), Caught, CallStack, SourceLocation{});
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape-coro-unknown.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape-coro-unknown.cpp
new file mode 100644
index 0000000000000..a2325c401c404
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape-coro-unknown.cpp
@@ -0,0 +1,42 @@
+// RUN: %check_clang_tidy -std=c++20-or-later %s bugprone-exception-escape %t -- \
+// RUN:     -config='{"CheckOptions": { \
+// RUN:       "bugprone-exception-escape.TreatFunctionsWithoutSpecificationAsThrowing": "OnlyUndefined" \
+// RUN:     }}' -- -fexceptions
+
+namespace std {
+
+template <class Ret, typename... T> struct coroutine_traits {
+  using promise_type = typename Ret::promise_type;
+};
+
+template <class Promise = void> struct coroutine_handle {
+  template <class OtherPromise>
+  coroutine_handle(coroutine_handle<OtherPromise>) noexcept;
+  static coroutine_handle from_address(void *) noexcept;
+};
+
+struct suspend_never {
+  bool await_ready() noexcept { return true; }
+  void await_suspend(coroutine_handle<>) noexcept {}
+  void await_resume() noexcept {}
+};
+
+} // namespace std
+
+struct Task {
+  struct promise_type {
+    Task get_return_object() noexcept { return {}; }
+    std::suspend_never initial_suspend() noexcept { return {}; }
+    std::suspend_never final_suspend() noexcept { return {}; }
+    void return_void() noexcept {}
+    void unhandled_exception() noexcept {}
+  };
+};
+
+void undefined();
+
+Task calls_undefined() noexcept {
+  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: an exception may be thrown in function 'calls_undefined' which should not throw exceptions
+  undefined();
+  co_return;
+}
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape-treat-functions-without-specification-as-throwing.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape-treat-functions-without-specification-as-throwing.cpp
index ee9f19b5a2896..14c280a5901d6 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape-treat-functions-without-specification-as-throwing.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape-treat-functions-without-specification-as-throwing.cpp
@@ -37,13 +37,29 @@ void calls_unknown() noexcept {
   extern_declared();
 }
 
+struct Error {};
+
 void calls_unknown_caught() noexcept {
   // CHECK-MESSAGES-ALL-NOT: warning:
   // CHECK-MESSAGES-UNDEFINED-NOT: warning:
   // CHECK-MESSAGES-NONE-NOT: warning:
   try {
     extern_declared();
-  } catch(...) {
+  } catch (const Error &) {
+  } catch (...) {
+  }
+}
+
+void calls_unknown_typed_catch() noexcept {
+  // CHECK-MESSAGES-ALL: :[[@LINE-1]]:6: warning: an exception may be thrown in function 'calls_unknown_typed_catch' which should not throw exceptions
+  // CHECK-MESSAGES-ALL: :[[@LINE-28]]:6: note: frame #0: an exception of unknown type may be thrown in function 'extern_declared' here
+  // CHECK-MESSAGES-ALL: :[[@LINE+5]]:5: note: frame #1: function 'calls_unknown_typed_catch' calls function 'extern_declared' here
+  // CHECK-MESSAGES-UNDEFINED: :[[@LINE-4]]:6: warning: an exception may be thrown in function 'calls_unknown_typed_catch' which should not throw exceptions
+  // CHECK-MESSAGES-UNDEFINED: :[[@LINE-31]]:6: note: frame #0: an exception of unknown type may be thrown in function 'extern_declared' here
+  // CHECK-MESSAGES-UNDEFINED: :[[@LINE+2]]:5: note: frame #1: function 'calls_unknown_typed_catch' calls function 'extern_declared' here
+  try {
+    extern_declared();
+  } catch (const Error &) {
   }
 }
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/218128


More information about the llvm-branch-commits mailing list