10#include "clang/Basic/TokenKinds.h"
11#include "clang/Frontend/CompilerInstance.h"
12#include "clang/Lex/PPCallbacks.h"
13#include "clang/Lex/Preprocessor.h"
14#include "llvm/ADT/STLExtras.h"
15#include "llvm/Support/Regex.h"
22 return llvm::all_of(
Name, [](
const char C) {
23 return std::isupper(
C) || std::isdigit(
C) ||
C ==
'_';
29class MacroUsageCallbacks :
public PPCallbacks {
31 MacroUsageCallbacks(MacroUsageCheck *Check,
const SourceManager &SM,
32 StringRef RegExpStr,
bool CapsOnly,
33 bool IgnoreCommandLine)
34 : Check(Check), SM(SM), RegExp(RegExpStr), CheckCapsOnly(CapsOnly),
35 IgnoreCommandLineMacros(IgnoreCommandLine) {}
36 void MacroDefined(
const Token &MacroNameTok,
37 const MacroDirective *MD)
override {
38 if (SM.isWrittenInBuiltinFile(MD->getLocation()) ||
39 MD->getMacroInfo()->isUsedForHeaderGuard() ||
40 MD->getMacroInfo()->tokens_empty() ||
41 llvm::any_of(MD->getMacroInfo()->tokens(), [](
const Token &T) {
42 return T.isOneOf(tok::TokenKind::hash, tok::TokenKind::hashhash);
46 if (IgnoreCommandLineMacros &&
47 SM.isWrittenInCommandLineFile(MD->getLocation()))
50 StringRef
MacroName = MacroNameTok.getIdentifierInfo()->getName();
51 if (
MacroName ==
"__GCC_HAVE_DWARF2_CFI_ASM")
53 if (!CheckCapsOnly && !RegExp.match(
MacroName))
61 MacroUsageCheck *Check;
62 const SourceManager &SM;
63 const llvm::Regex RegExp;
65 bool IgnoreCommandLineMacros;
72 Options.
store(Opts,
"IgnoreCommandLineMacros", IgnoreCommandLineMacros);
77 Preprocessor *ModuleExpanderPP) {
78 PP->addPPCallbacks(std::make_unique<MacroUsageCallbacks>(
79 this, SM, AllowedRegexp, CheckCapsOnly, IgnoreCommandLineMacros));
83 const MacroInfo *
Info = MD->getMacroInfo();
86 if (llvm::all_of(
Info->tokens(), std::mem_fn(&Token::isLiteral)))
87 Message =
"macro '%0' used to declare a constant; consider using a "
88 "'constexpr' constant";
92 else if (
Info->isVariadic())
93 Message =
"variadic macro '%0' used; consider using a 'constexpr' "
94 "variadic template function";
95 else if (
Info->isFunctionLike())
96 Message =
"function-like macro '%0' used; consider a 'constexpr' template "
105 diag(MD->getLocation(),
"macro definition does not define the macro name "
106 "'%0' using all uppercase characters")
llvm::SmallString< 256U > Name
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void warnMacro(const MacroDirective *MD, StringRef MacroName)
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void warnNaming(const MacroDirective *MD, StringRef MacroName)
static bool isCapsOnly(StringRef Name)
llvm::StringMap< ClangTidyValue > OptionMap