[clang] [clang][analyzer] Ignore try-statements in dead code checker (PR #91675)

Andrew Sukach via cfe-commits cfe-commits at lists.llvm.org
Mon May 13 08:53:14 PDT 2024


https://github.com/soukatch updated https://github.com/llvm/llvm-project/pull/91675

>From 4ed555c81ae5b773361da22c0736f82fbf14c90d Mon Sep 17 00:00:00 2001
From: Andrew Sukach <andrewsukach at gmail.com>
Date: Thu, 9 May 2024 18:49:41 -0400
Subject: [PATCH] [clang][static analyzer] ignore try statements in dead code
 checker

---
 .../Checkers/UnreachableCodeChecker.cpp         |  4 +++-
 .../Analysis/unreachable-code-exceptions.cpp    | 17 +++++++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/Analysis/unreachable-code-exceptions.cpp

diff --git a/clang/lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp
index d24a124f5ffee..7ce9a5b5bb6dc 100644
--- a/clang/lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/UnreachableCodeChecker.cpp
@@ -159,6 +159,8 @@ void UnreachableCodeChecker::checkEndAnalysis(ExplodedGraph &G,
       SL = DL.asLocation();
       if (SR.isInvalid() || !SL.isValid())
         continue;
+      if (isa<CXXTryStmt>(S))
+        continue;
     }
     else
       continue;
@@ -254,4 +256,4 @@ void ento::registerUnreachableCodeChecker(CheckerManager &mgr) {
 
 bool ento::shouldRegisterUnreachableCodeChecker(const CheckerManager &mgr) {
   return true;
-}
+}
\ No newline at end of file
diff --git a/clang/test/Analysis/unreachable-code-exceptions.cpp b/clang/test/Analysis/unreachable-code-exceptions.cpp
new file mode 100644
index 0000000000000..db4c87bc3bad6
--- /dev/null
+++ b/clang/test/Analysis/unreachable-code-exceptions.cpp
@@ -0,0 +1,17 @@
+// RUN: %clang_analyze_cc1 -verify %s -fcxx-exceptions -fexceptions -analyzer-checker=core -analyzer-checker=alpha.deadcode.UnreachableCode
+
+// expected-no-diagnostics
+
+class BaseException {};
+
+class DerivedException : public BaseException {};
+
+void foo();
+
+void f4() {
+  try {
+    foo();
+  } catch (BaseException &b) {
+  } catch (DerivedException &d) {
+  }
+}
\ No newline at end of file



More information about the cfe-commits mailing list