clang-tools 17.0.0git
UseEqualsDeleteCheck.cpp
Go to the documentation of this file.
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
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
18static const char SpecialFunction[] = "SpecialFunction";
19static const char DeletedNotPublic[] = "DeletedNotPublic";
20
22 Options.store(Opts, "IgnoreMacros", IgnoreMacros);
23}
24
25void UseEqualsDeleteCheck::registerMatchers(MatchFinder *Finder) {
26 auto PrivateSpecialFn = cxxMethodDecl(
27 isPrivate(),
28 anyOf(cxxConstructorDecl(anyOf(isDefaultConstructor(),
29 isCopyConstructor(), isMoveConstructor())),
30 cxxMethodDecl(
31 anyOf(isCopyAssignmentOperator(), isMoveAssignmentOperator())),
32 cxxDestructorDecl()));
33
34 Finder->addMatcher(
35 cxxMethodDecl(
36 PrivateSpecialFn,
37 unless(anyOf(hasAnyBody(stmt()), isDefaulted(), isDeleted(),
38 ast_matchers::isTemplateInstantiation(),
39 // Ensure that all methods except private special member
40 // functions are defined.
41 hasParent(cxxRecordDecl(hasMethod(unless(
42 anyOf(PrivateSpecialFn, hasAnyBody(stmt()), isPure(),
43 isDefaulted(), isDeleted()))))))))
44 .bind(SpecialFunction),
45 this);
46
47 Finder->addMatcher(
48 cxxMethodDecl(isDeleted(), unless(isPublic())).bind(DeletedNotPublic),
49 this);
50}
51
52void UseEqualsDeleteCheck::check(const MatchFinder::MatchResult &Result) {
53 if (const auto *Func =
54 Result.Nodes.getNodeAs<CXXMethodDecl>(SpecialFunction)) {
55 SourceLocation EndLoc = Lexer::getLocForEndOfToken(
56 Func->getEndLoc(), 0, *Result.SourceManager, getLangOpts());
57
58 if (Func->getLocation().isMacroID() && IgnoreMacros)
59 return;
60 // FIXME: Improve FixItHint to make the method public.
61 diag(Func->getLocation(),
62 "use '= delete' to prohibit calling of a special member function")
63 << FixItHint::CreateInsertion(EndLoc, " = delete");
64 } else if (const auto *Func =
65 Result.Nodes.getNodeAs<CXXMethodDecl>(DeletedNotPublic)) {
66 // Ignore this warning in macros, since it's extremely noisy in code using
67 // DISALLOW_COPY_AND_ASSIGN-style macros and there's no easy way to
68 // automatically fix the warning when macros are in play.
69 if (Func->getLocation().isMacroID() && IgnoreMacros)
70 return;
71 // FIXME: Add FixItHint to make the method public.
72 diag(Func->getLocation(), "deleted member function should be public");
73 }
74}
75
76} // namespace clang::tidy::modernize
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
const LangOptions & getLangOpts() const
Returns the language options from the context.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
static const char DeletedNotPublic[]
static const char SpecialFunction[]
llvm::StringMap< ClangTidyValue > OptionMap