[clang-tools-extra] [clang-tidy] Add IgnoreMacros option to modernize-pass-by-value (PR #181465)

via cfe-commits cfe-commits at lists.llvm.org
Sat Feb 14 00:15:38 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-tools-extra

Author: mitchell (zeyi2)

<details>
<summary>Changes</summary>

We need this option to address #<!-- -->156153

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


5 Files Affected:

- (modified) clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp (+6-1) 
- (modified) clang-tools-extra/clang-tidy/modernize/PassByValueCheck.h (+1) 
- (modified) clang-tools-extra/docs/ReleaseNotes.rst (+4) 
- (modified) clang-tools-extra/docs/clang-tidy/checks/modernize/pass-by-value.rst (+5) 
- (added) clang-tools-extra/test/clang-tidy/checkers/modernize/pass-by-value-ignore-macros.cpp (+24) 


``````````diff
diff --git a/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp b/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp
index 9f2c3eb65a3ca..2619331cba0b0 100644
--- a/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.cpp
@@ -216,11 +216,13 @@ PassByValueCheck::PassByValueCheck(StringRef Name, ClangTidyContext *Context)
       Inserter(Options.getLocalOrGlobal("IncludeStyle",
                                         utils::IncludeSorter::IS_LLVM),
                areDiagsSelfContained()),
-      ValuesOnly(Options.get("ValuesOnly", false)) {}
+      ValuesOnly(Options.get("ValuesOnly", false)),
+      IgnoreMacros(Options.get("IgnoreMacros", false)) {}
 
 void PassByValueCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
   Options.store(Opts, "IncludeStyle", Inserter.getStyle());
   Options.store(Opts, "ValuesOnly", ValuesOnly);
+  Options.store(Opts, "IgnoreMacros", IgnoreMacros);
 }
 
 void PassByValueCheck::registerMatchers(MatchFinder *Finder) {
@@ -273,6 +275,9 @@ void PassByValueCheck::check(const MatchFinder::MatchResult &Result) {
       Result.Nodes.getNodeAs<CXXCtorInitializer>("Initializer");
   const SourceManager &SM = *Result.SourceManager;
 
+  if (IgnoreMacros && ParamDecl->getBeginLoc().isMacroID())
+    return;
+
   // If the parameter is used or anything other than the copy, do not apply
   // the changes.
   if (!paramReferredExactlyOnce(Ctor, ParamDecl))
diff --git a/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.h b/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.h
index eb51f4a4c46ac..aefc81842e633 100644
--- a/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.h
+++ b/clang-tools-extra/clang-tidy/modernize/PassByValueCheck.h
@@ -29,6 +29,7 @@ class PassByValueCheck : public ClangTidyCheck {
 private:
   utils::IncludeInserter Inserter;
   const bool ValuesOnly;
+  const bool IgnoreMacros;
 };
 
 } // namespace clang::tidy::modernize
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 5c0060877a67f..6799b2c136f38 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -199,6 +199,10 @@ Changes in existing checks
   - Added support for analyzing function parameters with the `AnalyzeParameters`
     option.
 
+- Improved :doc:`modernize-pass-by-value
+  <clang-tidy/checks/modernize/pass-by-value>` check by adding `IgnoreMacros`
+  option to suppress warnings in macros.
+
 - Improved :doc:`modernize-use-std-format
   <clang-tidy/checks/modernize/use-std-format>` check by fixing a crash
   when an argument is part of a macro expansion.
diff --git a/clang-tools-extra/docs/clang-tidy/checks/modernize/pass-by-value.rst b/clang-tools-extra/docs/clang-tidy/checks/modernize/pass-by-value.rst
index b8d933aab702c..0f5758dc097e6 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/modernize/pass-by-value.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/modernize/pass-by-value.rst
@@ -164,3 +164,8 @@ Options
 
    When `true`, the check only warns about copied parameters that are already
    passed by value. Default is `false`.
+
+.. option:: IgnoreMacros
+
+   When `true`, the check will not give warnings inside macros. Default is
+   `false`.
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/pass-by-value-ignore-macros.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/pass-by-value-ignore-macros.cpp
new file mode 100644
index 0000000000000..8b21e97b0fd1f
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/pass-by-value-ignore-macros.cpp
@@ -0,0 +1,24 @@
+// RUN: %check_clang_tidy -check-suffixes=DEFAULT %s modernize-pass-by-value %t -- -config="{CheckOptions: {modernize-pass-by-value.IgnoreMacros: false}}"
+// RUN: %check_clang_tidy -check-suffixes=IGNORE %s modernize-pass-by-value %t -- -config="{CheckOptions: {modernize-pass-by-value.IgnoreMacros: true}}"
+
+struct A {
+  A(const A &);
+  A(A &&);
+};
+
+#define MACRO_CTOR(TYPE) \
+struct TYPE { \
+  TYPE(const A &a) : a(a) {} \
+  A a; \
+};
+// CHECK-MESSAGES-DEFAULT: :[[@LINE+3]]:1: warning: pass by value and use std::move [modernize-pass-by-value]
+// CHECK-MESSAGES-IGNORE-NOT: warning: pass by value and use std::move
+
+MACRO_CTOR(B)
+
+struct C {
+  C(const A &a) : a(a) {}
+  A a;
+};
+// CHECK-MESSAGES-DEFAULT: :[[@LINE-3]]:5: warning: pass by value and use std::move [modernize-pass-by-value]
+// CHECK-MESSAGES-IGNORE: :[[@LINE-4]]:5: warning: pass by value and use std::move [modernize-pass-by-value]

``````````

</details>


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


More information about the cfe-commits mailing list