summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clang-modernize/PassByValue/PassByValueMatchers.cpp
blob: 32095e167b7633196a1d71be50be5f91f45b59ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//===-- PassByValueMatchers.cpp -------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// \brief This file contains the definitions for matcher-generating functions
/// and names for bound nodes found by AST matchers.
///
//===----------------------------------------------------------------------===//

#include "PassByValueMatchers.h"

const char *PassByValueCtorId = "Ctor";
const char *PassByValueParamId = "Param";
const char *PassByValueInitializerId = "Initializer";

namespace clang {
namespace ast_matchers {

/// \brief Matches move constructible classes.
///
/// Given
/// \code
///   // POD types are trivially move constructible
///   struct Foo { int a; };
///
///   struct Bar {
///     Bar(Bar &&) = deleted;
///     int a;
///   };
/// \endcode
/// recordDecl(isMoveConstructible())
///   matches "Foo".
AST_MATCHER(CXXRecordDecl, isMoveConstructible) {
  for (CXXRecordDecl::ctor_iterator I = Node.ctor_begin(), E = Node.ctor_end(); I != E; ++I) {
    const CXXConstructorDecl *Ctor = *I;
    if (Ctor->isMoveConstructor() && !Ctor->isDeleted())
      return true;
  }
  return false;
}

/// \brief Matches non-deleted copy constructors.
///
/// Given
/// \code
///   struct Foo { Foo(const Foo &) = default; };
///   struct Bar { Bar(const Bar &) = deleted; };
/// \endcode
/// constructorDecl(isNonDeletedCopyConstructor())
///   matches "Foo(const Foo &)".
AST_MATCHER(CXXConstructorDecl, isNonDeletedCopyConstructor) {
  return Node.isCopyConstructor() && !Node.isDeleted();
}
} // namespace ast_matchers
} // namespace clang

using namespace clang;
using namespace clang::ast_matchers;

DeclarationMatcher makePassByValueCtorParamMatcher() {
  return constructorDecl(
      forEachConstructorInitializer(ctorInitializer(
          // Clang builds a CXXConstructExpr only when it knowns which
          // constructor will be called. In dependent contexts a ParenListExpr
          // is generated instead of a CXXConstructExpr, filtering out templates
          // automatically for us.
          withInitializer(constructExpr(
              has(declRefExpr(to(parmVarDecl().bind(PassByValueParamId)))),
              hasDeclaration(constructorDecl(
                  isNonDeletedCopyConstructor(),
                  hasDeclContext(recordDecl(isMoveConstructible())))))))
                                        .bind(PassByValueInitializerId)))
      .bind(PassByValueCtorId);
}
OpenPOWER on IntegriCloud