File tree Expand file tree Collapse file tree 3 files changed +19
-2
lines changed Expand file tree Collapse file tree 3 files changed +19
-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 @@ -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.
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