summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clang-modernize/PassByValue/PassByValue.cpp
blob: ac6e568defc909ed43fafa36e1c33662362f50f9 (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
//===-- PassByValue.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 provides the implementation of the ReplaceAutoPtrTransform
/// class.
///
//===----------------------------------------------------------------------===//

#include "PassByValue.h"
#include "PassByValueActions.h"
#include "PassByValueMatchers.h"

using namespace clang;
using namespace clang::tooling;
using namespace clang::ast_matchers;

int PassByValueTransform::apply(const tooling::CompilationDatabase &Database,
                                const std::vector<std::string> &SourcePaths) {
  ClangTool Tool(Database, SourcePaths);
  unsigned AcceptedChanges = 0;
  unsigned RejectedChanges = 0;
  MatchFinder Finder;
  ConstructorParamReplacer Replacer(AcceptedChanges, RejectedChanges,
                                    /*Owner=*/ *this);

  Finder.addMatcher(makePassByValueCtorParamMatcher(), &Replacer);

  // make the replacer available to handleBeginSource()
  this->Replacer = &Replacer;

  if (Tool.run(createActionFactory(Finder).get())) {
    llvm::errs() << "Error encountered during translation.\n";
    return 1;
  }

  setAcceptedChanges(AcceptedChanges);
  setRejectedChanges(RejectedChanges);
  return 0;
}

bool PassByValueTransform::handleBeginSource(CompilerInstance &CI,
                                             llvm::StringRef Filename) {
  assert(Replacer && "Replacer not set");
  IncludeManager.reset(new IncludeDirectives(CI));
  Replacer->setIncludeDirectives(IncludeManager.get());
  return Transform::handleBeginSource(CI, Filename);
}

struct PassByValueFactory : TransformFactory {
  PassByValueFactory() {
    // Based on the Replace Auto-Ptr Transform that is also using std::move().
    Since.Clang = Version(3, 0);
    Since.Gcc = Version(4, 6);
    Since.Icc = Version(13);
    Since.Msvc = Version(11);
  }

  Transform *createTransform(const TransformOptions &Opts) override {
    return new PassByValueTransform(Opts);
  }
};

// Register the factory using this statically initialized variable.
static TransformFactoryRegistry::Add<PassByValueFactory>
X("pass-by-value", "Pass parameters by value where possible");

// This anchor is used to force the linker to link in the generated object file
// and thus register the factory.
volatile int PassByValueTransformAnchorSource = 0;
OpenPOWER on IntegriCloud