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