[clang-tools-extra] [clang-tidy] Do not lint on attribute macros (PR #164806)

via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 23 04:43:43 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tidy

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

Author: wieDasDing (dingxiangfei2009)

<details>
<summary>Changes</summary>

`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.

In short, we should not suggest code writers to rewrite this macro with `constexpr` functions.

```c
#define FORMAT_STR(format_msg, first_idx) __attribute__((format(printf, format_msg, first_idx)))
```

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


2 Files Affected:

- (modified) clang-tools-extra/clang-tidy/cppcoreguidelines/MacroUsageCheck.cpp (+9-2) 
- (modified) clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/macro-usage.cpp (+2) 


``````````diff
diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/MacroUsageCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/MacroUsageCheck.cpp
index 766cae45f15b5..8370e51284867 100644
--- a/clang-tools-extra/clang-tidy/cppcoreguidelines/MacroUsageCheck.cpp
+++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/MacroUsageCheck.cpp
@@ -82,6 +82,13 @@ void MacroUsageCheck::registerPPCallbacks(const SourceManager &SM,
 void MacroUsageCheck::warnMacro(const MacroDirective *MD, StringRef MacroName) {
   const MacroInfo *Info = MD->getMacroInfo();
   StringRef Message;
+  bool PossiblyNotFunctionLike;
+  if (Info->getNumTokens() > 0) {
+    const Token &Tok = Info->getReplacementToken(0);
+    PossiblyNotFunctionLike = Tok.is(tok::kw___attribute);
+  } else {
+    PossiblyNotFunctionLike = false;
+  }
 
   if (llvm::all_of(Info->tokens(), std::mem_fn(&Token::isLiteral)))
     Message = "macro '%0' used to declare a constant; consider using a "
@@ -89,10 +96,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() && !PossiblyNotFunctionLike)
     Message = "variadic macro '%0' used; consider using a 'constexpr' "
               "variadic template function";
-  else if (Info->isFunctionLike())
+  else if (Info->isFunctionLike() && !PossiblyNotFunctionLike)
     Message = "function-like macro '%0' used; consider a 'constexpr' template "
               "function";
 
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

``````````

</details>


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


More information about the cfe-commits mailing list