xref: /llvm-project/clang-tools-extra/clang-tidy/modernize/UseEqualsDeleteCheck.cpp (revision 208fa9acc0ffe5a460bcd504229c24a8d3f87180)
1 //===--- UseEqualsDeleteCheck.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 "UseEqualsDeleteCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/Lex/Lexer.h"
13 
14 using namespace clang::ast_matchers;
15 
16 namespace clang::tidy::modernize {
17 
18 namespace {
19 AST_MATCHER(FunctionDecl, hasAnyDefinition) {
20   if (Node.hasBody() || Node.isPure() || Node.isDefaulted() || Node.isDeleted())
21     return true;
22 
23   if (const FunctionDecl *Definition = Node.getDefinition())
24     if (Definition->hasBody() || Definition->isPure() ||
25         Definition->isDefaulted() || Definition->isDeleted())
26       return true;
27 
28   return false;
29 }
30 
31 AST_MATCHER(Decl, isUsed) { return Node.isUsed(); }
32 
33 AST_MATCHER(CXXMethodDecl, isSpecialFunction) {
34   if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(&Node))
35     return Constructor->isDefaultConstructor() ||
36            Constructor->isCopyOrMoveConstructor();
37 
38   return isa<CXXDestructorDecl>(Node) || Node.isCopyAssignmentOperator() ||
39          Node.isMoveAssignmentOperator();
40 }
41 } // namespace
42 
43 static const char SpecialFunction[] = "SpecialFunction";
44 static const char DeletedNotPublic[] = "DeletedNotPublic";
45 
46 UseEqualsDeleteCheck::UseEqualsDeleteCheck(StringRef Name,
47                                            ClangTidyContext *Context)
48     : ClangTidyCheck(Name, Context),
49       IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", true)) {}
50 
51 void UseEqualsDeleteCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
52   Options.store(Opts, "IgnoreMacros", IgnoreMacros);
53 }
54 
55 void UseEqualsDeleteCheck::registerMatchers(MatchFinder *Finder) {
56   auto PrivateSpecialFn = cxxMethodDecl(isPrivate(), isSpecialFunction());
57 
58   Finder->addMatcher(
59       cxxMethodDecl(
60           PrivateSpecialFn, unless(hasAnyDefinition()), unless(isUsed()),
61           // Ensure that all methods except private special member functions are
62           // defined.
63           unless(ofClass(hasMethod(cxxMethodDecl(unless(PrivateSpecialFn),
64                                                  unless(hasAnyDefinition()))))))
65           .bind(SpecialFunction),
66       this);
67 
68   Finder->addMatcher(
69       cxxMethodDecl(isDeleted(), unless(isPublic())).bind(DeletedNotPublic),
70       this);
71 }
72 
73 void UseEqualsDeleteCheck::check(const MatchFinder::MatchResult &Result) {
74   if (const auto *Func =
75           Result.Nodes.getNodeAs<CXXMethodDecl>(SpecialFunction)) {
76     SourceLocation EndLoc = Lexer::getLocForEndOfToken(
77         Func->getEndLoc(), 0, *Result.SourceManager, getLangOpts());
78 
79     if (IgnoreMacros && Func->getLocation().isMacroID())
80       return;
81     // FIXME: Improve FixItHint to make the method public.
82     diag(Func->getLocation(),
83          "use '= delete' to prohibit calling of a special member function")
84         << FixItHint::CreateInsertion(EndLoc, " = delete");
85   } else if (const auto *Func =
86                  Result.Nodes.getNodeAs<CXXMethodDecl>(DeletedNotPublic)) {
87     // Ignore this warning in macros, since it's extremely noisy in code using
88     // DISALLOW_COPY_AND_ASSIGN-style macros and there's no easy way to
89     // automatically fix the warning when macros are in play.
90     if (IgnoreMacros && Func->getLocation().isMacroID())
91       return;
92     // FIXME: Add FixItHint to make the method public.
93     diag(Func->getLocation(), "deleted member function should be public");
94   }
95 }
96 
97 } // namespace clang::tidy::modernize
98