10 #include "clang/Frontend/CompilerInstance.h" 11 #include "clang/Lex/PPCallbacks.h" 12 #include "llvm/ADT/STLExtras.h" 13 #include "llvm/Support/Regex.h" 19 namespace cppcoreguidelines {
23 bool isCapsOnly(StringRef
Name) {
24 return std::all_of(Name.begin(), Name.end(), [](
const char c) {
25 if (std::isupper(c) || std::isdigit(c) || c ==
'_')
33 MacroUsageCallbacks(MacroUsageCheck *Check,
const SourceManager &SM,
34 StringRef RegExp,
bool CapsOnly,
bool IgnoreCommandLine)
35 : Check(Check), SM(SM), RegExp(RegExp), CheckCapsOnly(CapsOnly),
36 IgnoreCommandLineMacros(IgnoreCommandLine) {}
37 void MacroDefined(
const Token &MacroNameTok,
38 const MacroDirective *
MD)
override {
39 if (SM.isWrittenInBuiltinFile(MD->getLocation()) ||
40 MD->getMacroInfo()->isUsedForHeaderGuard() ||
41 MD->getMacroInfo()->getNumTokens() == 0)
44 if (IgnoreCommandLineMacros &&
45 SM.isWrittenInCommandLineFile(MD->getLocation()))
48 StringRef MacroName = MacroNameTok.getIdentifierInfo()->getName();
49 if (!CheckCapsOnly && !llvm::Regex(RegExp).
match(MacroName))
50 Check->warnMacro(MD, MacroName);
52 if (CheckCapsOnly && !isCapsOnly(MacroName))
53 Check->warnNaming(MD, MacroName);
57 MacroUsageCheck *Check;
58 const SourceManager &SM;
61 bool IgnoreCommandLineMacros;
66 Options.store(Opts,
"AllowedRegexp", AllowedRegexp);
67 Options.store(Opts,
"CheckCapsOnly", CheckCapsOnly);
68 Options.store(Opts,
"IgnoreCommandLineMacros", IgnoreCommandLineMacros);
73 Preprocessor *ModuleExpanderPP) {
74 if (!getLangOpts().CPlusPlus11)
77 PP->addPPCallbacks(std::make_unique<MacroUsageCallbacks>(
78 this, SM, AllowedRegexp, CheckCapsOnly, IgnoreCommandLineMacros));
83 "macro '%0' used to declare a constant; consider using a 'constexpr' " 89 if (MD->getMacroInfo()->isVariadic())
90 Message =
"variadic macro '%0' used; consider using a 'constexpr' " 91 "variadic template function";
92 else if (MD->getMacroInfo()->isFunctionLike())
93 Message =
"function-like macro '%0' used; consider a 'constexpr' template " 96 diag(MD->getLocation(),
Message) << MacroName;
100 StringRef MacroName) {
101 diag(MD->getLocation(),
"macro definition does not define the macro name " 102 "'%0' using all uppercase characters")
void warnNaming(const MacroDirective *MD, StringRef MacroName)
void warnMacro(const MacroDirective *MD, StringRef MacroName)
constexpr llvm::StringLiteral Message
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
std::vector< std::string > match(const SymbolIndex &I, const FuzzyFindRequest &Req, bool *Incomplete)
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static GeneratorRegistry::Add< MDGenerator > MD(MDGenerator::Format, "Generator for MD output.")