https://github.com/zeyi2 updated 
https://github.com/llvm/llvm-project/pull/171975

>From 526536114f2bfc8283658a3938a64890a97002d4 Mon Sep 17 00:00:00 2001
From: mtx <[email protected]>
Date: Thu, 11 Dec 2025 15:42:35 +0800
Subject: [PATCH 1/2] [clang-tidy] Add `IgnoreMacro` option to
 `bugprone-chained-comparison`

---
 .../clang-tidy/bugprone/ChainedComparisonCheck.cpp   | 12 ++++++++++++
 .../clang-tidy/bugprone/ChainedComparisonCheck.h     |  7 +++++--
 clang-tools-extra/docs/ReleaseNotes.rst              |  5 +++++
 .../checks/bugprone/chained-comparison.rst           |  8 ++++++++
 .../bugprone/chained-comparison-ignore-macros.cpp    | 12 ++++++++++++
 5 files changed, 42 insertions(+), 2 deletions(-)
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/bugprone/chained-comparison-ignore-macros.cpp

diff --git a/clang-tools-extra/clang-tidy/bugprone/ChainedComparisonCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/ChainedComparisonCheck.cpp
index 47acc217b2c24..8b1c8aaa48e3e 100644
--- a/clang-tools-extra/clang-tidy/bugprone/ChainedComparisonCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/ChainedComparisonCheck.cpp
@@ -112,6 +112,15 @@ void ChainedComparisonData::extract(const Expr *Op) {
   }
 }
 
+ChainedComparisonCheck::ChainedComparisonCheck(StringRef Name,
+                                               ClangTidyContext *Context)
+    : ClangTidyCheck(Name, Context),
+      IgnoreMacros(Options.get("IgnoreMacros", false)) {}
+
+void ChainedComparisonCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
+  Options.store(Opts, "IgnoreMacros", IgnoreMacros);
+}
+
 void ChainedComparisonCheck::registerMatchers(MatchFinder *Finder) {
   const auto OperatorMatcher = expr(anyOf(
       binaryOperator(isComparisonOperator(),
@@ -128,6 +137,9 @@ void ChainedComparisonCheck::registerMatchers(MatchFinder 
*Finder) {
 void ChainedComparisonCheck::check(const MatchFinder::MatchResult &Result) {
   const auto *MatchedOperator = Result.Nodes.getNodeAs<Expr>("op");
 
+  if (IgnoreMacros && MatchedOperator->getBeginLoc().isMacroID())
+    return;
+
   ChainedComparisonData Data(MatchedOperator);
   if (Data.Operands.empty())
     return;
diff --git a/clang-tools-extra/clang-tidy/bugprone/ChainedComparisonCheck.h 
b/clang-tools-extra/clang-tidy/bugprone/ChainedComparisonCheck.h
index 7c1022904a3a6..dacfc68a8a42a 100644
--- a/clang-tools-extra/clang-tidy/bugprone/ChainedComparisonCheck.h
+++ b/clang-tools-extra/clang-tidy/bugprone/ChainedComparisonCheck.h
@@ -20,13 +20,16 @@ namespace clang::tidy::bugprone {
 /// 
https://clang.llvm.org/extra/clang-tidy/checks/bugprone/chained-comparison.html
 class ChainedComparisonCheck : public ClangTidyCheck {
 public:
-  ChainedComparisonCheck(StringRef Name, ClangTidyContext *Context)
-      : ClangTidyCheck(Name, Context) {}
+  ChainedComparisonCheck(StringRef Name, ClangTidyContext *Context);
   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+  void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
   std::optional<TraversalKind> getCheckTraversalKind() const override {
     return TK_IgnoreUnlessSpelledInSource;
   }
+
+private:
+  const bool IgnoreMacros;
 };
 
 } // namespace clang::tidy::bugprone
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index d1fb1cba3e45a..17c16330b6e8b 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -331,6 +331,11 @@ New check aliases
 Changes in existing checks
 ^^^^^^^^^^^^^^^^^^^^^^^^^^
 
+- Improved :doc:`bugprone-chained-comparison
+  <clang-tidy/checks/bugprone/chained-comparison>` check by adding a
+  new option `IgnoreMacros` to suppress warnings within macro
+  expansions.
+
 - Improved :doc:`bugprone-easily-swappable-parameters
   <clang-tidy/checks/bugprone/easily-swappable-parameters>` check by
   correcting a spelling mistake on its option
diff --git 
a/clang-tools-extra/docs/clang-tidy/checks/bugprone/chained-comparison.rst 
b/clang-tools-extra/docs/clang-tidy/checks/bugprone/chained-comparison.rst
index 45b069a8e29de..c66f5cb401db3 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/chained-comparison.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/chained-comparison.rst
@@ -71,3 +71,11 @@ developer's intention more explicit and help avoid 
misunderstanding.
         // This block will be executed
     }
 
+Options
+-------
+
+.. option:: IgnoreMacros
+
+    If ``true``, the check will not warn on chained comparisons inside macros.
+    Default is ``false``.
+
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/chained-comparison-ignore-macros.cpp
 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/chained-comparison-ignore-macros.cpp
new file mode 100644
index 0000000000000..a5800be3686cc
--- /dev/null
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/chained-comparison-ignore-macros.cpp
@@ -0,0 +1,12 @@
+// RUN: %check_clang_tidy -std=c++98-or-later 
--extra-arg=-Wno-error=parentheses %s bugprone-chained-comparison %t -- 
-config="{CheckOptions: {bugprone-chained-comparison.IgnoreMacros: true}}"
+
+#define CHAINED_COMPARE(a, b, c) (a < b < c)
+
+void macro_test(int x, int y, int z) {
+    bool result = CHAINED_COMPARE(x, y, z);
+}
+
+void normal_test(int x, int y, int z) {
+    bool result = x < y < z;
+    // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: chained comparison 'v0 < v1 < 
v2' may generate unintended results
+}

>From f990441f2028b4e075b493a6d2e7415137a429af Mon Sep 17 00:00:00 2001
From: mitchell <[email protected]>
Date: Fri, 12 Dec 2025 17:59:19 +0800
Subject: [PATCH 2/2] Update
 clang-tools-extra/docs/clang-tidy/checks/bugprone/chained-comparison.rst

Co-authored-by: Baranov Victor <[email protected]>
---
 .../docs/clang-tidy/checks/bugprone/chained-comparison.rst      | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git 
a/clang-tools-extra/docs/clang-tidy/checks/bugprone/chained-comparison.rst 
b/clang-tools-extra/docs/clang-tidy/checks/bugprone/chained-comparison.rst
index c66f5cb401db3..34bb00bf2ec63 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/chained-comparison.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/chained-comparison.rst
@@ -76,6 +76,6 @@ Options
 
 .. option:: IgnoreMacros
 
-    If ``true``, the check will not warn on chained comparisons inside macros.
+    If `true`, the check will not warn on chained comparisons inside macros.
     Default is ``false``.
 

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

Reply via email to