File: PassToMove.cpp

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (117 lines) | stat: -rw-r--r-- 4,147 bytes parent folder | download | duplicates (11)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Clang tool to change calls to scoper::Pass() to just use std::move().

#include <memory>
#include <string>

#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/ASTMatchersMacros.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Lex/Lexer.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Refactoring.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/TargetSelect.h"

using namespace clang::ast_matchers;
using clang::tooling::CommonOptionsParser;
using clang::tooling::Replacement;
using clang::tooling::Replacements;
using llvm::StringRef;

namespace {

class RewriterCallback : public MatchFinder::MatchCallback {
 public:
  explicit RewriterCallback(Replacements* replacements)
      : replacements_(replacements) {}
  virtual void run(const MatchFinder::MatchResult& result) override;

 private:
  Replacements* const replacements_;
};

void RewriterCallback::run(const MatchFinder::MatchResult& result) {
  const clang::CXXMemberCallExpr* call_expr =
      result.Nodes.getNodeAs<clang::CXXMemberCallExpr>("expr");
  const clang::MemberExpr* callee =
      clang::dyn_cast<clang::MemberExpr>(call_expr->getCallee());
  const bool is_arrow = callee->isArrow();
  const clang::Expr* arg = result.Nodes.getNodeAs<clang::Expr>("arg");

  const char kMoveRefText[] = "std::move(";
  const char kMovePtrText[] = "std::move(*";

  auto err = replacements_->add(
      Replacement(*result.SourceManager,
                  result.SourceManager->getSpellingLoc(arg->getLocStart()), 0,
                  is_arrow ? kMovePtrText : kMoveRefText));
  assert(!err);

  // Delete everything but the closing parentheses from the original call to
  // Pass(): the closing parantheses is left to match up with the parantheses
  // just inserted with std::move.
  err = replacements_->add(Replacement(
      *result.SourceManager,
      clang::CharSourceRange::getCharRange(
          result.SourceManager->getSpellingLoc(callee->getOperatorLoc()),
          result.SourceManager->getSpellingLoc(call_expr->getRParenLoc())),
      ""));
  assert(!err);
}

}  // namespace

static llvm::cl::extrahelp common_help(CommonOptionsParser::HelpMessage);

int main(int argc, const char* argv[]) {
  // TODO(dcheng): Clang tooling should do this itself.
  // http://llvm.org/bugs/show_bug.cgi?id=21627
  llvm::InitializeNativeTarget();
  llvm::InitializeNativeTargetAsmParser();
  llvm::cl::OptionCategory category(
      "C++11 modernization: change scoped::Pass() to std::move()");
  CommonOptionsParser options(argc, argv, category);
  clang::tooling::ClangTool tool(options.getCompilations(),
                                 options.getSourcePathList());

  MatchFinder match_finder;
  Replacements replacements;

  auto pass_matcher = id(
      "expr",
      cxxMemberCallExpr(
          argumentCountIs(0),
          callee(functionDecl(hasName("Pass"), returns(rValueReferenceType()))),
          on(id("arg", expr()))));
  RewriterCallback callback(&replacements);
  match_finder.addMatcher(pass_matcher, &callback);

  std::unique_ptr<clang::tooling::FrontendActionFactory> factory =
      clang::tooling::newFrontendActionFactory(&match_finder);
  int result = tool.run(factory.get());
  if (result != 0)
    return result;

  if (replacements.empty())
    return 0;

  // Serialization format is documented in tools/clang/scripts/run_tool.py
  llvm::outs() << "==== BEGIN EDITS ====\n";
  for (const auto& r : replacements) {
    std::string replacement_text = r.getReplacementText().str();
    std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0');
    llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset()
                 << ":::" << r.getLength() << ":::" << replacement_text << "\n";
  }
  llvm::outs() << "==== END EDITS ====\n";

  return 0;
}