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