Skip to content

Commit 61b5340

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 3a34710 commit 61b5340

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,24 @@ 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 PossiblyNotFunctionLike;
86+
if (Info->getNumTokens() > 0) {
87+
const Token &Tok = Info->getReplacementToken(0);
88+
PossiblyNotFunctionLike = Tok.is(tok::kw___attribute);
89+
} else {
90+
PossiblyNotFunctionLike = false;
91+
}
8592

8693
if (llvm::all_of(Info->tokens(), std::mem_fn(&Token::isLiteral)))
8794
Message = "macro '%0' used to declare a constant; consider using a "
8895
"'constexpr' constant";
8996
// A variadic macro is function-like at the same time. Therefore variadic
9097
// macros are checked first and will be excluded for the function-like
9198
// diagnostic.
92-
else if (Info->isVariadic())
99+
else if (Info->isVariadic() && !PossiblyNotFunctionLike)
93100
Message = "variadic macro '%0' used; consider using a 'constexpr' "
94101
"variadic template function";
95-
else if (Info->isFunctionLike())
102+
else if (Info->isFunctionLike() && !PossiblyNotFunctionLike)
96103
Message = "function-like macro '%0' used; consider a 'constexpr' template "
97104
"function";
98105

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)