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