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