[clang-tools-extra] [clang-tidy] Do not lint on attribute macros (PR #164806)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Oct 27 02:29:59 PDT 2025
https://github.com/dingxiangfei2009 updated https://github.com/llvm/llvm-project/pull/164806
>From 9dd12ccadd901046018e31ca34439e523dc7984d Mon Sep 17 00:00:00 2001
From: Xiangfei Ding <dingxiangfei2009 at protonmail.ch>
Date: Thu, 23 Oct 2025 11:39:21 +0000
Subject: [PATCH] [clang-tidy] Do not lint on attribute macros
cppcoreguidelines-macro-usage lint incorrectly identifies these macros
as candidates for rewrite into template arguments.
There are no, variadic or not, equivalent to these macros using
templated functions.
Signed-off-by: Xiangfei Ding <dingxiangfei2009 at protonmail.ch>
---
.../cppcoreguidelines/MacroUsageCheck.cpp | 13 +++++++++++--
clang-tools-extra/docs/ReleaseNotes.rst | 5 +++++
.../checkers/cppcoreguidelines/macro-usage.cpp | 2 ++
3 files changed, 18 insertions(+), 2 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/MacroUsageCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/MacroUsageCheck.cpp
index 766cae45f15b5..0836a5c386dd8 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/MacroUsageCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/MacroUsageCheck.cpp
@@ -82,6 +82,15 @@ void MacroUsageCheck::registerPPCallbacks(const SourceManager &SM,
void MacroUsageCheck::warnMacro(const MacroDirective *MD, StringRef MacroName) {
const MacroInfo *Info = MD->getMacroInfo();
StringRef Message;
+ bool MacroBodyExpressionLike;
+ if (Info->getNumTokens() > 0) {
+ const Token &Tok = Info->getReplacementToken(0);
+ // Now notice that keywords like `__attribute` cannot be a leading
+ // token in an expression.
+ MacroBodyExpressionLike = !Tok.is(tok::kw___attribute);
+ } else {
+ MacroBodyExpressionLike = true;
+ }
if (llvm::all_of(Info->tokens(), std::mem_fn(&Token::isLiteral)))
Message = "macro '%0' used to declare a constant; consider using a "
@@ -89,10 +98,10 @@ void MacroUsageCheck::warnMacro(const MacroDirective *MD, StringRef MacroName) {
// A variadic macro is function-like at the same time. Therefore variadic
// macros are checked first and will be excluded for the function-like
// diagnostic.
- else if (Info->isVariadic())
+ else if (Info->isVariadic() && MacroBodyExpressionLike)
Message = "variadic macro '%0' used; consider using a 'constexpr' "
"variadic template function";
- else if (Info->isFunctionLike())
+ else if (Info->isFunctionLike() && MacroBodyExpressionLike)
Message = "function-like macro '%0' used; consider a 'constexpr' template "
"function";
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index 061fb114276dc..634f59f1b6de6 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -171,6 +171,11 @@ Improvements to clang-tidy
moved to the ``fuchsia`` module instead. The ``zircon`` module will be removed
in the 24th release.
+- Improved :program:`clang-tidy`'s `cppcoreguidelines-macro-usage` check.
+ The lint will not warn on macros that starts with ``__attribute__((..))`` keyword,
+ meaning that the macro body is unlikely a proper expression, so that it stops
+ suggesting users an impossible rewrite into a template function.
+
New checks
^^^^^^^^^^
diff --git a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/macro-usage.cpp b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/macro-usage.cpp
index 865ef9df1182e..f3eb8185878f2 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/macro-usage.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/macro-usage.cpp
@@ -47,4 +47,6 @@
#define DLLEXPORTS __declspec(dllimport)
#endif
+#define ATTRIBUTE_MACRO(...) __attribute__(__VA_ARGS__)
+
#endif
More information about the cfe-commits
mailing list