clang-tools 22.0.0git
ReplaceDisallowCopyAndAssignMacroCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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
10#include "../utils/LexerUtils.h"
11#include "clang/Frontend/CompilerInstance.h"
12#include "clang/Lex/MacroArgs.h"
13#include "clang/Lex/PPCallbacks.h"
14#include "clang/Lex/Preprocessor.h"
15#include "llvm/Support/FormatVariadic.h"
16#include <optional>
17
18namespace clang::tidy::modernize {
19
20namespace {
21
22class ReplaceDisallowCopyAndAssignMacroCallbacks : public PPCallbacks {
23public:
24 explicit ReplaceDisallowCopyAndAssignMacroCallbacks(
25 ReplaceDisallowCopyAndAssignMacroCheck &Check, Preprocessor &PP)
26 : Check(Check), PP(PP) {}
27
28 void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
29 SourceRange Range, const MacroArgs *Args) override {
30 const 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 expected 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 const clang::IdentifierInfo *ClassIdent = ClassNameTok->getIdentifierInfo();
43 if (!ClassIdent)
44 return;
45
46 const std::string Replacement = llvm::formatv(
47 R"cpp({0}(const {0} &) = delete;
48const {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
59private:
60 /// \returns \c true if the next token after the given \p MacroLoc is \b not a
61 /// semicolon.
62 bool shouldAppendSemi(SourceRange MacroLoc) {
63 std::optional<Token> Next = utils::lexer::findNextTokenSkippingComments(
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
74 StringRef Name, ClangTidyContext *Context)
75 : ClangTidyCheck(Name, Context),
76 MacroName(Options.get("MacroName", "DISALLOW_COPY_AND_ASSIGN")) {}
77
79 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
80 PP->addPPCallbacks(
81 ::std::make_unique<ReplaceDisallowCopyAndAssignMacroCallbacks>(
82 *this, *ModuleExpanderPP));
83}
84
87 Options.store(Opts, "MacroName", MacroName);
88}
89
90} // namespace clang::tidy::modernize
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
@ Info
An information message.
Definition Protocol.h:738
std::optional< Token > findNextTokenSkippingComments(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts)
Definition LexerUtils.h:101
llvm::StringMap< ClangTidyValue > OptionMap