clang-tools  4.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::registerMatchers(MatchFinder *Finder) {
25  if (!getLangOpts().CPlusPlus)
26  return;
27 
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(hasBody(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, hasBody(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 
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->getLocEnd(), 0, *Result.SourceManager, getLangOpts());
59 
60  // FIXME: Improve FixItHint to make 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  // FIXME: Add FixItHint to make method public
67  diag(Func->getLocation(), "deleted member function should be public");
68  }
69 }
70 
71 } // namespace modernize
72 } // namespace tidy
73 } // namespace clang
static const char SpecialFunction[]
std::unique_ptr< ast_matchers::MatchFinder > Finder
Definition: ClangTidy.cpp:262
static const char DeletedNotPublic[]
const NamedDecl * Result
Definition: USRFinder.cpp:162