clang-tools 23.0.0git
UseEqualsDeleteCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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
10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/Lex/Lexer.h"
13
14using namespace clang::ast_matchers;
15
16namespace clang::tidy::modernize {
17
18namespace {
19AST_MATCHER(FunctionDecl, hasAnyDefinition) {
20 if (Node.hasBody() || Node.isPureVirtual() || Node.isDefaulted() ||
21 Node.isDeleted())
22 return true;
23
24 if (const FunctionDecl *Definition = Node.getDefinition())
25 if (Definition->hasBody() || Definition->isPureVirtual() ||
26 Definition->isDefaulted() || Definition->isDeleted())
27 return true;
28
29 return false;
30}
31
32AST_MATCHER(Decl, isUsed) { return Node.isUsed(); }
33
34AST_MATCHER(CXXMethodDecl, isSpecialFunction) {
35 if (const auto *Constructor = dyn_cast<CXXConstructorDecl>(&Node))
36 return Constructor->isDefaultConstructor() ||
37 Constructor->isCopyOrMoveConstructor();
38
39 return isa<CXXDestructorDecl>(Node) || Node.isCopyAssignmentOperator() ||
40 Node.isMoveAssignmentOperator();
41}
42
43AST_MATCHER(CXXMethodDecl, hasPublicOverload) {
44 const DeclContext::lookup_result LookupResult =
45 Node.getParent()->lookup(Node.getNameInfo().getName());
46
47 if (LookupResult.isSingleResult())
48 return false; // No overloads
49
50 static constexpr auto IsPublicOverload = [](const Decl *Overload) {
51 return isa<CXXMethodDecl, FunctionTemplateDecl>(Overload) &&
52 Overload->getAccess() == AS_public;
53 };
54
55 return llvm::any_of(LookupResult, IsPublicOverload);
56}
57} // namespace
58
59static constexpr char SpecialFunction[] = "SpecialFunction";
60static constexpr char DeletedNotPublic[] = "DeletedNotPublic";
61
63 ClangTidyContext *Context)
64 : ClangTidyCheck(Name, Context),
65 IgnoreMacros(Options.get("IgnoreMacros", true)) {}
66
68 Options.store(Opts, "IgnoreMacros", IgnoreMacros);
69}
70
71void UseEqualsDeleteCheck::registerMatchers(MatchFinder *Finder) {
72 auto PrivateSpecialFn = cxxMethodDecl(isPrivate(), isSpecialFunction());
73
74 Finder->addMatcher(
75 cxxMethodDecl(
76 PrivateSpecialFn, unless(hasAnyDefinition()), unless(isUsed()),
77 // Ensure that all methods except private special member functions are
78 // defined.
79 unless(ofClass(hasMethod(cxxMethodDecl(unless(PrivateSpecialFn),
80 unless(hasAnyDefinition()))))))
81 .bind(SpecialFunction),
82 this);
83
84 // Add a matcher for deleted private member functions, with a public overload,
85 // to recommend moving them to the public section.
86 Finder->addMatcher(
87 cxxMethodDecl(isDeleted(), unless(isPublic()),
88 anyOf(hasPublicOverload(), isSpecialFunction()))
89 .bind(DeletedNotPublic),
90 this);
91}
92
93void UseEqualsDeleteCheck::check(const MatchFinder::MatchResult &Result) {
94 if (const auto *Func =
95 Result.Nodes.getNodeAs<CXXMethodDecl>(SpecialFunction)) {
96 const SourceLocation EndLoc = Lexer::getLocForEndOfToken(
97 Func->getEndLoc(), 0, *Result.SourceManager, getLangOpts());
98
99 if (IgnoreMacros && Func->getLocation().isMacroID())
100 return;
101 // FIXME: Improve FixItHint to make the method public.
102 diag(Func->getLocation(),
103 "use '= delete' to prohibit calling of a special member function")
104 << FixItHint::CreateInsertion(EndLoc, " = delete");
105 } else if (const auto *Func =
106 Result.Nodes.getNodeAs<CXXMethodDecl>(DeletedNotPublic)) {
107 // Ignore this warning in macros, since it's extremely noisy in code using
108 // DISALLOW_COPY_AND_ASSIGN-style macros and there's no easy way to
109 // automatically fix the warning when macros are in play.
110 if (IgnoreMacros && Func->getLocation().isMacroID())
111 return;
112 // FIXME: Add FixItHint to make the method public.
113 diag(Func->getLocation(), "deleted member function should be public");
114 }
115}
116
117} // namespace clang::tidy::modernize
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
UseEqualsDeleteCheck(StringRef Name, ClangTidyContext *Context)
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
AST_MATCHER(BinaryOperator, isRelationalOperator)
static constexpr char SpecialFunction[]
static constexpr char DeletedNotPublic[]
llvm::StringMap< ClangTidyValue > OptionMap