xref: /llvm-project/clang-tools-extra/clang-tidy/modernize/UseEqualsDeleteCheck.cpp (revision e293eab46f958227b7d186f925043e7e3ed946f2)
1 //===--- UseEqualsDeleteCheck.cpp - clang-tidy-----------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "UseEqualsDeleteCheck.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/Lex/Lexer.h"
14 
15 using namespace clang::ast_matchers;
16 
17 namespace clang {
18 namespace tidy {
19 namespace modernize {
20 
21 static const char SpecialFunction[] = "SpecialFunction";
22 static const char DeletedNotPublic[] = "DeletedNotPublic";
23 
24 void UseEqualsDeleteCheck::registerMatchers(MatchFinder *Finder) {
25   auto PrivateSpecialFn = cxxMethodDecl(
26       isPrivate(),
27       anyOf(cxxConstructorDecl(anyOf(isDefaultConstructor(),
28                                      isCopyConstructor(), isMoveConstructor())),
29             cxxMethodDecl(
30                 anyOf(isCopyAssignmentOperator(), isMoveAssignmentOperator())),
31             cxxDestructorDecl()));
32 
33   Finder->addMatcher(
34       cxxMethodDecl(
35           PrivateSpecialFn,
36           unless(anyOf(hasBody(stmt()), isDefaulted(), isDeleted(),
37                        // Ensure that all methods except private special member
38                        // functions are defined.
39                        hasParent(cxxRecordDecl(hasMethod(unless(
40                            anyOf(PrivateSpecialFn, hasBody(stmt()), isPure(),
41                                  isDefaulted(), isDeleted()))))))))
42           .bind(SpecialFunction),
43       this);
44 
45   Finder->addMatcher(
46       cxxMethodDecl(isDeleted(), unless(isPublic())).bind(DeletedNotPublic),
47       this);
48 }
49 
50 void UseEqualsDeleteCheck::check(const MatchFinder::MatchResult &Result) {
51   if (const auto *Func =
52           Result.Nodes.getNodeAs<CXXMethodDecl>(SpecialFunction)) {
53     SourceLocation EndLoc = Lexer::getLocForEndOfToken(
54         Func->getLocEnd(), 0, *Result.SourceManager, getLangOpts());
55 
56     // FIXME: Improve FixItHint to make method public
57     diag(Func->getLocation(),
58          "use '= delete' to prohibit calling of a special member function")
59         << FixItHint::CreateInsertion(EndLoc, " = delete");
60   } else if (const auto *Func =
61                  Result.Nodes.getNodeAs<CXXMethodDecl>(DeletedNotPublic)) {
62     // FIXME: Add FixItHint to make method public
63     diag(Func->getLocation(), "deleted member function should be public");
64   }
65 }
66 
67 } // namespace modernize
68 } // namespace tidy
69 } // namespace clang
70