Skip to content

Commit 9dd12cc

Browse files
[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 <[email protected]>
1 parent cf6db63 commit 9dd12cc

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ Improvements to clang-tidy
171171
moved to the ``fuchsia`` module instead. The ``zircon`` module will be removed
172172
in the 24th release.
173173

174+
- Improved :program:`clang-tidy`'s `cppcoreguidelines-macro-usage` check.
175+
The lint will not warn on macros that starts with ``__attribute__((..))`` keyword,
176+
meaning that the macro body is unlikely a proper expression, so that it stops
177+
suggesting users an impossible rewrite into a template function.
178+
174179
New checks
175180
^^^^^^^^^^
176181

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)