Skip to content

Commit 20c323a

Browse files
[clang-tidy] Do not lint on attribute macros (#164806)
`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))) ``` Signed-off-by: Xiangfei Ding <[email protected]>
1 parent d1c086e commit 20c323a

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

clang-tools-extra/clang-tidy/cppcoreguidelines/MacroUsageCheck.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,26 @@ void MacroUsageCheck::registerPPCallbacks(const SourceManager &SM,
8282
void MacroUsageCheck::warnMacro(const MacroDirective *MD, StringRef MacroName) {
8383
const MacroInfo *Info = MD->getMacroInfo();
8484
StringRef Message;
85+
bool MacroBodyExpressionLike;
86+
if (Info->getNumTokens() > 0) {
87+
const Token &Tok = Info->getReplacementToken(0);
88+
// Now notice that keywords like `__attribute` cannot be a leading
89+
// token in an expression.
90+
MacroBodyExpressionLike = !Tok.is(tok::kw___attribute);
91+
} else {
92+
MacroBodyExpressionLike = true;
93+
}
8594

8695
if (llvm::all_of(Info->tokens(), std::mem_fn(&Token::isLiteral)))
8796
Message = "macro '%0' used to declare a constant; consider using a "
8897
"'constexpr' constant";
8998
// A variadic macro is function-like at the same time. Therefore variadic
9099
// macros are checked first and will be excluded for the function-like
91100
// diagnostic.
92-
else if (Info->isVariadic())
101+
else if (Info->isVariadic() && MacroBodyExpressionLike)
93102
Message = "variadic macro '%0' used; consider using a 'constexpr' "
94103
"variadic template function";
95-
else if (Info->isFunctionLike())
104+
else if (Info->isFunctionLike() && MacroBodyExpressionLike)
96105
Message = "function-like macro '%0' used; consider a 'constexpr' template "
97106
"function";
98107

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,12 @@ Changes in existing checks
325325
<clang-tidy/checks/cppcoreguidelines/init-variables>` check by fixing the
326326
insertion location for function pointers with multiple parameters.
327327

328+
- Improved :doc:`cppcoreguidelines-macro-usage
329+
<clang-tidy/checks/cppcoreguidelines/macro-usage>` check by excluding macro
330+
bodies that starts with ``__attribute__((..))`` keyword.
331+
Such a macro body is unlikely a proper expression and so suggesting users
332+
an impossible rewrite into a template function should be avoided.
333+
328334
- Improved :doc:`cppcoreguidelines-prefer-member-initializer
329335
<clang-tidy/checks/cppcoreguidelines/prefer-member-initializer>` check to
330336
avoid false positives on inherited members in class templates.

clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/macro-usage.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,6 @@
4747
#define DLLEXPORTS __declspec(dllimport)
4848
#endif
4949

50+
#define ATTRIBUTE_MACRO(...) __attribute__(__VA_ARGS__)
51+
5052
#endif

0 commit comments

Comments
 (0)