1 //===--- MacroUsageCheck.cpp - clang-tidy----------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "MacroUsageCheck.h"
10 #include "clang/Frontend/CompilerInstance.h"
11 #include "clang/Lex/PPCallbacks.h"
12 #include "clang/Lex/Preprocessor.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/Support/Regex.h"
21 namespace cppcoreguidelines
{
23 static bool isCapsOnly(StringRef Name
) {
24 return llvm::all_of(Name
, [](const char C
) {
25 return std::isupper(C
) || std::isdigit(C
) || C
== '_';
31 class MacroUsageCallbacks
: public PPCallbacks
{
33 MacroUsageCallbacks(MacroUsageCheck
*Check
, const SourceManager
&SM
,
34 StringRef RegExpStr
, bool CapsOnly
,
35 bool IgnoreCommandLine
)
36 : Check(Check
), SM(SM
), RegExp(RegExpStr
), CheckCapsOnly(CapsOnly
),
37 IgnoreCommandLineMacros(IgnoreCommandLine
) {}
38 void MacroDefined(const Token
&MacroNameTok
,
39 const MacroDirective
*MD
) override
{
40 if (SM
.isWrittenInBuiltinFile(MD
->getLocation()) ||
41 MD
->getMacroInfo()->isUsedForHeaderGuard() ||
42 MD
->getMacroInfo()->getNumTokens() == 0)
45 if (IgnoreCommandLineMacros
&&
46 SM
.isWrittenInCommandLineFile(MD
->getLocation()))
49 StringRef MacroName
= MacroNameTok
.getIdentifierInfo()->getName();
50 if (MacroName
== "__GCC_HAVE_DWARF2_CFI_ASM")
52 if (!CheckCapsOnly
&& !RegExp
.match(MacroName
))
53 Check
->warnMacro(MD
, MacroName
);
55 if (CheckCapsOnly
&& !isCapsOnly(MacroName
))
56 Check
->warnNaming(MD
, MacroName
);
60 MacroUsageCheck
*Check
;
61 const SourceManager
&SM
;
62 const llvm::Regex RegExp
;
64 bool IgnoreCommandLineMacros
;
68 void MacroUsageCheck::storeOptions(ClangTidyOptions::OptionMap
&Opts
) {
69 Options
.store(Opts
, "AllowedRegexp", AllowedRegexp
);
70 Options
.store(Opts
, "CheckCapsOnly", CheckCapsOnly
);
71 Options
.store(Opts
, "IgnoreCommandLineMacros", IgnoreCommandLineMacros
);
74 void MacroUsageCheck::registerPPCallbacks(const SourceManager
&SM
,
76 Preprocessor
*ModuleExpanderPP
) {
77 PP
->addPPCallbacks(std::make_unique
<MacroUsageCallbacks
>(
78 this, SM
, AllowedRegexp
, CheckCapsOnly
, IgnoreCommandLineMacros
));
81 void MacroUsageCheck::warnMacro(const MacroDirective
*MD
, StringRef MacroName
) {
82 const MacroInfo
*Info
= MD
->getMacroInfo();
85 if (llvm::all_of(Info
->tokens(), std::mem_fn(&Token::isLiteral
)))
86 Message
= "macro '%0' used to declare a constant; consider using a "
87 "'constexpr' constant";
88 // A variadic macro is function-like at the same time. Therefore variadic
89 // macros are checked first and will be excluded for the function-like
91 else if (Info
->isVariadic())
92 Message
= "variadic macro '%0' used; consider using a 'constexpr' "
93 "variadic template function";
94 else if (Info
->isFunctionLike())
95 Message
= "function-like macro '%0' used; consider a 'constexpr' template "
99 diag(MD
->getLocation(), Message
) << MacroName
;
102 void MacroUsageCheck::warnNaming(const MacroDirective
*MD
,
103 StringRef MacroName
) {
104 diag(MD
->getLocation(), "macro definition does not define the macro name "
105 "'%0' using all uppercase characters")
109 } // namespace cppcoreguidelines