summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/cpp11-migrate/PassByValue/PassByValueActions.h
blob: 5aeaae4f9fb2078bd3b4f1dde34354109fc76c2d (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
//===-- PassByValueActions.h ------------------------------------*- C++ -*-===//
//
//                     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 declaration of the ASTMatcher callback for the
/// PassByValue transform.
///
//===----------------------------------------------------------------------===//

#ifndef CPP11_MIGRATE_PASS_BY_VALUE_ACTIONS_H
#define CPP11_MIGRATE_PASS_BY_VALUE_ACTIONS_H

#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Tooling/Refactoring.h"

class Transform;
class IncludeDirectives;

/// \brief Callback that replaces const-ref parameters in constructors to use
/// pass-by-value semantic where applicable.
///
/// Modifications done by the callback:
/// - \#include \<utility\> is added if necessary for the definition of
///   \c std::move() to be available.
/// - The parameter type is changed from const-ref to value-type.
/// - In the init-list the parameter is moved.
///
/// Example:
/// \code
/// + #include <utility>
///
/// class Foo(const std::string &S) {
/// public:
///   - Foo(const std::string &S) : S(S) {}
///   + Foo(std::string S) : S(std::move(S)) {}
///
/// private:
///   std::string S;
/// };
/// \endcode
///
/// \note Since an include may be added by this matcher it's necessary to call
/// \c setIncludeDirectives() with an up-to-date \c IncludeDirectives. This is
/// typically done by overloading \c Transform::handleBeginSource().
class ConstructorParamReplacer
    : public clang::ast_matchers::MatchFinder::MatchCallback {
public:
  ConstructorParamReplacer(unsigned &AcceptedChanges, unsigned &RejectedChanges,
                           Transform &Owner)
      : AcceptedChanges(AcceptedChanges), RejectedChanges(RejectedChanges),
        Owner(Owner), IncludeManager(0) {}

  void setIncludeDirectives(IncludeDirectives *Includes) {
    IncludeManager = Includes;
  }

private:
  /// \brief Entry point to the callback called when matches are made.
  virtual void run(const clang::ast_matchers::MatchFinder::MatchResult &Result)
      LLVM_OVERRIDE;

  unsigned &AcceptedChanges;
  unsigned &RejectedChanges;
  Transform &Owner;
  IncludeDirectives *IncludeManager;
};

#endif // CPP11_MIGRATE_PASS_BY_VALUE_ACTIONS_H
OpenPOWER on IntegriCloud