File tree Expand file tree Collapse file tree 3 files changed +18
-2
lines changed Expand file tree Collapse file tree 3 files changed +18
-2
lines changed Original file line number Diff line number Diff line change @@ -82,17 +82,26 @@ void MacroUsageCheck::registerPPCallbacks(const SourceManager &SM,
8282void 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
Original file line number Diff line number Diff 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+
174179New checks
175180^^^^^^^^^^
176181
Original file line number Diff line number Diff line change 4747#define DLLEXPORTS __declspec (dllimport)
4848#endif
4949
50+ #define ATTRIBUTE_MACRO (...) __attribute__(__VA_ARGS__)
51+
5052#endif
You can’t perform that action at this time.
0 commit comments