clang-tools  7.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 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())
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 modernize
77 } // namespace tidy
78 } // namespace clang
static const char SpecialFunction[]
static bool isPublic(const clang::AccessSpecifier AS, const clang::Linkage Link)
Definition: Serialize.cpp:174
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static const char DeletedNotPublic[]