10#include "clang/Frontend/CompilerInstance.h"
11#include "clang/Lex/MacroArgs.h"
12#include "clang/Lex/PPCallbacks.h"
13#include "clang/Lex/Preprocessor.h"
14#include "llvm/Support/FormatVariadic.h"
21class ReplaceDisallowCopyAndAssignMacroCallbacks :
public PPCallbacks {
23 explicit ReplaceDisallowCopyAndAssignMacroCallbacks(
24 ReplaceDisallowCopyAndAssignMacroCheck &Check, Preprocessor &PP)
25 : Check(Check), PP(PP) {}
27 void MacroExpands(
const Token &MacroNameTok,
const MacroDefinition &MD,
28 SourceRange
Range,
const MacroArgs *
Args)
override {
29 IdentifierInfo *
Info = MacroNameTok.getIdentifierInfo();
32 if (
Info->getName() != Check.getMacroName())
36 const Token *ClassNameTok =
Args->getUnexpArgument(0);
37 if (
Args->ArgNeedsPreexpansion(ClassNameTok, PP))
41 clang::IdentifierInfo *ClassIdent = ClassNameTok->getIdentifierInfo();
45 std::string Replacement = llvm::formatv(
46 R
"cpp({0}(const {0} &) = delete;
47const {0} &operator=(const {0} &) = delete{1})cpp",
48 ClassIdent->getName(), shouldAppendSemi(Range) ? ";" :
"");
50 Check.diag(MacroNameTok.getLocation(),
51 "prefer deleting copy constructor and assignment operator over "
53 << Check.getMacroName()
54 << FixItHint::CreateReplacement(
55 PP.getSourceManager().getExpansionRange(
Range), Replacement);
61 bool shouldAppendSemi(SourceRange MacroLoc) {
62 std::optional<Token> Next = Lexer::findNextToken(
63 MacroLoc.getEnd(), PP.getSourceManager(), PP.getLangOpts());
64 return !(Next && Next->is(tok::semi));
67 ReplaceDisallowCopyAndAssignMacroCheck &Check;
75 MacroName(Options.get(
"MacroName",
"DISALLOW_COPY_AND_ASSIGN")) {}
78 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
80 ::std::make_unique<ReplaceDisallowCopyAndAssignMacroCallbacks>(
81 *
this, *ModuleExpanderPP));
llvm::SmallString< 256U > Name
CharSourceRange Range
SourceRange for the file 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.
Base class for all clang-tidy checks.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
ReplaceDisallowCopyAndAssignMacroCheck(StringRef Name, ClangTidyContext *Context)
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
llvm::StringMap< ClangTidyValue > OptionMap