aboutsummaryrefslogtreecommitdiff
path: root/clang-tools-extra/clang-tidy/modernize/ReplaceDisallowCopyAndAssignMacroCheck.cpp
diff options
context:
space:
mode:
authorKonrad Kleine <kkleine@redhat.com>2020-06-03 16:53:43 -0400
committerKonrad Kleine <kkleine@redhat.com>2020-06-03 16:56:03 -0400
commite636e6b79ac06b13059e46b49acb4d9de204c75b (patch)
treecf636be146798e8579ca19259cd5994a5ccd929c /clang-tools-extra/clang-tidy/modernize/ReplaceDisallowCopyAndAssignMacroCheck.cpp
parent66251f7e1de79a7c1620659b7f58352b8c8e892e (diff)
downloadllvm-e636e6b79ac06b13059e46b49acb4d9de204c75b.zip
llvm-e636e6b79ac06b13059e46b49acb4d9de204c75b.tar.gz
llvm-e636e6b79ac06b13059e46b49acb4d9de204c75b.tar.bz2
[clang-tidy]: Added modernize-replace-disallow-copy-and-assign-macro
Summary: This check finds macro expansions of `DISALLOW_COPY_AND_ASSIGN(Type)` and replaces them with a deleted copy constructor and a deleted assignment operator. Before the `delete` keyword was introduced in C++11 it was common practice to declare a copy constructor and an assignment operator as a private members. This effectively makes them unusable to the public API of a class. With the advent of the `delete` keyword in C++11 we can abandon the `private` access of the copy constructor and the assignment operator and delete the methods entirely. Migration example: ``` lang=dif class Foo { private: - DISALLOW_COPY_AND_ASSIGN(Foo); + Foo(const Foo &) = delete; + const Foo &operator=(const Foo &) = delete; }; ``` Reviewers: alexfh, hokein, aaron.ballman, njames93 Reviewed By: njames93 Subscribers: Eugene.Zelenko, mgorny, xazax.hun, cfe-commits Tags: #clang, #clang-tools-extra Differential Revision: https://reviews.llvm.org/D80531
Diffstat (limited to 'clang-tools-extra/clang-tidy/modernize/ReplaceDisallowCopyAndAssignMacroCheck.cpp')
-rw-r--r--clang-tools-extra/clang-tidy/modernize/ReplaceDisallowCopyAndAssignMacroCheck.cpp90
1 files changed, 90 insertions, 0 deletions
diff --git a/clang-tools-extra/clang-tidy/modernize/ReplaceDisallowCopyAndAssignMacroCheck.cpp b/clang-tools-extra/clang-tidy/modernize/ReplaceDisallowCopyAndAssignMacroCheck.cpp
new file mode 100644
index 0000000..2219a3c
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/modernize/ReplaceDisallowCopyAndAssignMacroCheck.cpp
@@ -0,0 +1,90 @@
+//===--- ReplaceDisallowCopyAndAssignMacroCheck.cpp - clang-tidy ----------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "ReplaceDisallowCopyAndAssignMacroCheck.h"
+#include "clang/Frontend/CompilerInstance.h"
+#include "clang/Lex/MacroArgs.h"
+#include "llvm/Support/FormatVariadic.h"
+
+namespace clang {
+namespace tidy {
+namespace modernize {
+
+namespace {
+
+class ReplaceDisallowCopyAndAssignMacroCallbacks : public PPCallbacks {
+public:
+ explicit ReplaceDisallowCopyAndAssignMacroCallbacks(
+ ReplaceDisallowCopyAndAssignMacroCheck &Check, Preprocessor &PP)
+ : Check(Check), PP(PP) {}
+
+ void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
+ SourceRange Range, const MacroArgs *Args) override {
+ IdentifierInfo *Info = MacroNameTok.getIdentifierInfo();
+ if (!Info || !Args || Args->getNumMacroArguments() != 1)
+ return;
+ if (Info->getName() != Check.getMacroName())
+ return;
+ // The first argument to the DISALLOW_COPY_AND_ASSIGN macro is exptected to
+ // be the class name.
+ const Token *ClassNameTok = Args->getUnexpArgument(0);
+ if (Args->ArgNeedsPreexpansion(ClassNameTok, PP))
+ // For now we only support simple argument that don't need to be
+ // pre-expanded.
+ return;
+ clang::IdentifierInfo *ClassIdent = ClassNameTok->getIdentifierInfo();
+ if (!ClassIdent)
+ return;
+
+ std::string Replacement = llvm::formatv(
+ R"cpp({0}(const {0} &) = delete;
+const {0} &operator=(const {0} &) = delete{1})cpp",
+ ClassIdent->getName(), shouldAppendSemi(Range) ? ";" : "");
+
+ Check.diag(MacroNameTok.getLocation(),
+ "prefer deleting copy constructor and assignment operator over "
+ "using macro '%0'")
+ << Check.getMacroName()
+ << FixItHint::CreateReplacement(
+ PP.getSourceManager().getExpansionRange(Range), Replacement);
+ }
+
+private:
+ /// \returns \c true if the next token after the given \p MacroLoc is \b not a
+ /// semicolon.
+ bool shouldAppendSemi(SourceRange MacroLoc) {
+ llvm::Optional<Token> Next = Lexer::findNextToken(
+ MacroLoc.getEnd(), PP.getSourceManager(), PP.getLangOpts());
+ return !(Next && Next->is(tok::semi));
+ }
+
+ ReplaceDisallowCopyAndAssignMacroCheck &Check;
+ Preprocessor &PP;
+};
+} // namespace
+
+ReplaceDisallowCopyAndAssignMacroCheck::ReplaceDisallowCopyAndAssignMacroCheck(
+ StringRef Name, ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context),
+ MacroName(Options.get("MacroName", "DISALLOW_COPY_AND_ASSIGN")) {}
+
+void ReplaceDisallowCopyAndAssignMacroCheck::registerPPCallbacks(
+ const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
+ PP->addPPCallbacks(
+ ::std::make_unique<ReplaceDisallowCopyAndAssignMacroCallbacks>(
+ *this, *ModuleExpanderPP));
+}
+
+void ReplaceDisallowCopyAndAssignMacroCheck::storeOptions(
+ ClangTidyOptions::OptionMap &Opts) {
+ Options.store(Opts, "MacroName", MacroName);
+}
+
+} // namespace modernize
+} // namespace tidy
+} // namespace clang