11 #include "clang/Frontend/CompilerInstance.h" 12 #include "clang/Lex/PPCallbacks.h" 13 #include "llvm/ADT/STLExtras.h" 14 #include "llvm/Support/Regex.h" 20 namespace cppcoreguidelines {
24 bool isCapsOnly(StringRef
Name) {
25 return std::all_of(Name.begin(), Name.end(), [](
const char c) {
26 if (std::isupper(c) || std::isdigit(c) || c ==
'_')
34 MacroUsageCallbacks(MacroUsageCheck *Check,
const SourceManager &SM,
35 StringRef RegExp,
bool CapsOnly,
bool IgnoreCommandLine)
36 : Check(Check), SM(SM), RegExp(RegExp), CheckCapsOnly(CapsOnly),
37 IgnoreCommandLineMacros(IgnoreCommandLine) {}
38 void MacroDefined(
const Token &MacroNameTok,
39 const MacroDirective *
MD)
override {
40 if (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);
72 if (!getLangOpts().CPlusPlus11)
75 Compiler.getPreprocessor().addPPCallbacks(
76 llvm::make_unique<MacroUsageCallbacks>(
this, Compiler.getSourceManager(),
77 AllowedRegexp, CheckCapsOnly,
78 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)
static const StringRef Message
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.")
void registerPPCallbacks(CompilerInstance &Compiler) override
Override this to register PPCallbacks with Compiler.