[clang] [analyzer] Suppress EnumCastOutOfRange diagnostics in system headers (PR #196323)

Vineet Agarwal via cfe-commits cfe-commits at lists.llvm.org
Thu May 7 07:12:18 PDT 2026


https://github.com/PlutoDog95 created https://github.com/llvm/llvm-project/pull/196323

## Summary

Suppress EnumCastOutOfRange diagnostics emitted from system headers.

The checker already receives locations correctly classified as system headers by SourceManager, but it did not suppress diagnostics for them. This caused warnings to be emitted from libraries included through -isystem, such as Boost.Regex.

Add a system-header guard in reportWarning() and add a regression test.

## Testing

```bash
./bin/llvm-lit ../clang/test/Analysis/enum-cast-out-of-range*


>From a488e1500568f16ad796e1b331c402f6d0dd7190 Mon Sep 17 00:00:00 2001
From: Vineet Agarwal <agarwal.vineet2006 at gmail.com>
Date: Thu, 7 May 2026 19:29:58 +0530
Subject: [PATCH] [analyzer] Suppress EnumCastOutOfRange diagnostics in system
 headers

Suppress EnumCastOutOfRange warnings emitted from system headers.

The checker already receives locations correctly classified as system
headers by SourceManager, but it did not suppress diagnostics for them.
This caused warnings to be emitted from libraries included through
-isystem, such as Boost.Regex.

Add a system-header guard in reportWarning() and add a regression test.

Signed-off-by: Vineet Agarwal <agarwal.vineet2006 at gmail.com>
---
 .../Checkers/EnumCastOutOfRangeChecker.cpp           | 10 +++++++---
 clang/test/Analysis/Inputs/enum-system-header.h      |  8 ++++++++
 .../enum-cast-out-of-range-system-header.cpp         | 12 ++++++++++++
 3 files changed, 27 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Analysis/Inputs/enum-system-header.h
 create mode 100644 clang/test/Analysis/enum-cast-out-of-range-system-header.cpp

diff --git a/clang/lib/StaticAnalyzer/Checkers/EnumCastOutOfRangeChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/EnumCastOutOfRangeChecker.cpp
index 76a1470aaac44..32a73a1d5c44f 100644
--- a/clang/lib/StaticAnalyzer/Checkers/EnumCastOutOfRangeChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/EnumCastOutOfRangeChecker.cpp
@@ -85,8 +85,13 @@ void EnumCastOutOfRangeChecker::reportWarning(CheckerContext &C,
                                               const CastExpr *CE,
                                               const EnumDecl *E) const {
   assert(E && "valid EnumDecl* is expected");
-  if (const ExplodedNode *N = C.generateNonFatalErrorNode()) {
-    std::string ValueStr = "", NameStr = "the enum";
+    auto &SM = C.getSourceManager();
+
+    if (SM.isInSystemHeader(CE->getExprLoc()))
+      return;
+
+    if (const ExplodedNode *N = C.generateNonFatalErrorNode()) {
+      std::string ValueStr = "", NameStr = "the enum";
 
     // Try to add details to the message:
     const auto ConcreteValue =
@@ -101,7 +106,6 @@ void EnumCastOutOfRangeChecker::reportWarning(CheckerContext &C,
     std::string Msg = formatv("The value{0} provided to the cast expression is "
                               "not in the valid range of values for {1}",
                               ValueStr, NameStr);
-
     auto BR = std::make_unique<PathSensitiveBugReport>(EnumValueCastOutOfRange,
                                                        Msg, N);
     bugreporter::trackExpressionValue(N, CE->getSubExpr(), *BR);
diff --git a/clang/test/Analysis/Inputs/enum-system-header.h b/clang/test/Analysis/Inputs/enum-system-header.h
new file mode 100644
index 0000000000000..53c6bdac24093
--- /dev/null
+++ b/clang/test/Analysis/Inputs/enum-system-header.h
@@ -0,0 +1,8 @@
+enum MyEnum {
+  A = 1,
+  B = 2
+};
+
+static inline MyEnum bad_cast(int x) {
+  return (MyEnum)x;
+}
diff --git a/clang/test/Analysis/enum-cast-out-of-range-system-header.cpp b/clang/test/Analysis/enum-cast-out-of-range-system-header.cpp
new file mode 100644
index 0000000000000..073cde677bcd1
--- /dev/null
+++ b/clang/test/Analysis/enum-cast-out-of-range-system-header.cpp
@@ -0,0 +1,12 @@
+// RUN: %clang_analyze_cc1 \
+// RUN:   -analyzer-checker=core,optin.core.EnumCastOutOfRange \
+// RUN:   -isystem %S/Inputs \
+// RUN:   -verify %s
+
+#include "enum-system-header.h"
+
+void test() {
+  bad_cast(100);
+}
+
+// expected-no-diagnostics



More information about the cfe-commits mailing list