1//===--- ReplaceDisallowCopyAndAssignMacroCheck.cpp - clang-tidy ----------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#include "ReplaceDisallowCopyAndAssignMacroCheck.h"
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"
15#include <optional>
16
17namespace clang::tidy::modernize {
18
19namespace {
20
21class ReplaceDisallowCopyAndAssignMacroCallbacks : public PPCallbacks {
22public:
23 explicit ReplaceDisallowCopyAndAssignMacroCallbacks(
24 ReplaceDisallowCopyAndAssignMacroCheck &Check, Preprocessor &PP)
25 : Check(Check), PP(PP) {}
26
27 void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
28 SourceRange Range, const MacroArgs *Args) override {
29 IdentifierInfo *Info = MacroNameTok.getIdentifierInfo();
30 if (!Info || !Args || Args->getNumMacroArguments() != 1)
31 return;
32 if (Info->getName() != Check.getMacroName())
33 return;
34 // The first argument to the DISALLOW_COPY_AND_ASSIGN macro is expected to
35 // be the class name.
36 const Token *ClassNameTok = Args->getUnexpArgument(Arg: 0);
37 if (Args->ArgNeedsPreexpansion(ArgTok: ClassNameTok, PP))
38 // For now we only support simple argument that don't need to be
39 // pre-expanded.
40 return;
41 clang::IdentifierInfo *ClassIdent = ClassNameTok->getIdentifierInfo();
42 if (!ClassIdent)
43 return;
44
45 std::string Replacement = llvm::formatv(
46 Fmt: R"cpp({0}(const {0} &) = delete;
47const {0} &operator=(const {0} &) = delete{1})cpp",
48 Vals: ClassIdent->getName(), Vals: shouldAppendSemi(MacroLoc: Range) ? ";" : "");
49
50 Check.diag(Loc: MacroNameTok.getLocation(),
51 Description: "prefer deleting copy constructor and assignment operator over "
52 "using macro '%0'")
53 << Check.getMacroName()
54 << FixItHint::CreateReplacement(
55 RemoveRange: PP.getSourceManager().getExpansionRange(Range), Code: Replacement);
56 }
57
58private:
59 /// \returns \c true if the next token after the given \p MacroLoc is \b not a
60 /// semicolon.
61 bool shouldAppendSemi(SourceRange MacroLoc) {
62 std::optional<Token> Next = Lexer::findNextToken(
63 Loc: MacroLoc.getEnd(), SM: PP.getSourceManager(), LangOpts: PP.getLangOpts());
64 return !(Next && Next->is(K: tok::semi));
65 }
66
67 ReplaceDisallowCopyAndAssignMacroCheck &Check;
68 Preprocessor &PP;
69};
70} // namespace
71
72ReplaceDisallowCopyAndAssignMacroCheck::ReplaceDisallowCopyAndAssignMacroCheck(
73 StringRef Name, ClangTidyContext *Context)
74 : ClangTidyCheck(Name, Context),
75 MacroName(Options.get(LocalName: "MacroName", Default: "DISALLOW_COPY_AND_ASSIGN")) {}
76
77void ReplaceDisallowCopyAndAssignMacroCheck::registerPPCallbacks(
78 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
79 PP->addPPCallbacks(
80 C: ::std::make_unique<ReplaceDisallowCopyAndAssignMacroCallbacks>(
81 args&: *this, args&: *ModuleExpanderPP));
82}
83
84void ReplaceDisallowCopyAndAssignMacroCheck::storeOptions(
85 ClangTidyOptions::OptionMap &Opts) {
86 Options.store(Options&: Opts, LocalName: "MacroName", Value: MacroName);
87}
88
89} // namespace clang::tidy::modernize
90

source code of clang-tools-extra/clang-tidy/modernize/ReplaceDisallowCopyAndAssignMacroCheck.cpp