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 {
17 namespace tidy {
18 namespace modernize {
19
20 static const char SpecialFunction[] = "SpecialFunction";
21 static const char DeletedNotPublic[] = "DeletedNotPublic";
22
storeOptions(ClangTidyOptions::OptionMap & Opts)23 void UseEqualsDeleteCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
24 Options.store(Opts, "IgnoreMacros", IgnoreMacros);
25 }
26
registerMatchers(MatchFinder * Finder)27 void UseEqualsDeleteCheck::registerMatchers(MatchFinder *Finder) {
28 auto PrivateSpecialFn = cxxMethodDecl(
29 isPrivate(),
30 anyOf(cxxConstructorDecl(anyOf(isDefaultConstructor(),
31 isCopyConstructor(), isMoveConstructor())),
32 cxxMethodDecl(
33 anyOf(isCopyAssignmentOperator(), isMoveAssignmentOperator())),
34 cxxDestructorDecl()));
35
36 Finder->addMatcher(
37 cxxMethodDecl(
38 PrivateSpecialFn,
39 unless(anyOf(hasAnyBody(stmt()), isDefaulted(), isDeleted(),
40 ast_matchers::isTemplateInstantiation(),
41 // Ensure that all methods except private special member
42 // functions are defined.
43 hasParent(cxxRecordDecl(hasMethod(unless(
44 anyOf(PrivateSpecialFn, hasAnyBody(stmt()), isPure(),
45 isDefaulted(), isDeleted()))))))))
46 .bind(SpecialFunction),
47 this);
48
49 Finder->addMatcher(
50 cxxMethodDecl(isDeleted(), unless(isPublic())).bind(DeletedNotPublic),
51 this);
52 }
53
check(const MatchFinder::MatchResult & Result)54 void UseEqualsDeleteCheck::check(const MatchFinder::MatchResult &Result) {
55 if (const auto *Func =
56 Result.Nodes.getNodeAs<CXXMethodDecl>(SpecialFunction)) {
57 SourceLocation EndLoc = Lexer::getLocForEndOfToken(
58 Func->getEndLoc(), 0, *Result.SourceManager, getLangOpts());
59
60 if (Func->getLocation().isMacroID() && IgnoreMacros)
61 return;
62 // FIXME: Improve FixItHint to make the method public.
63 diag(Func->getLocation(),
64 "use '= delete' to prohibit calling of a special member function")
65 << FixItHint::CreateInsertion(EndLoc, " = delete");
66 } else if (const auto *Func =
67 Result.Nodes.getNodeAs<CXXMethodDecl>(DeletedNotPublic)) {
68 // Ignore this warning in macros, since it's extremely noisy in code using
69 // DISALLOW_COPY_AND_ASSIGN-style macros and there's no easy way to
70 // automatically fix the warning when macros are in play.
71 if (Func->getLocation().isMacroID() && IgnoreMacros)
72 return;
73 // FIXME: Add FixItHint to make the method public.
74 diag(Func->getLocation(), "deleted member function should be public");
75 }
76 }
77
78 } // namespace modernize
79 } // namespace tidy
80 } // namespace clang
81