1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ValueRewriter.h"
6
7 #include <utility>
8
9 using namespace clang::ast_matchers;
10
ListValueCallback(std::string method,std::string replacement,std::set<clang::tooling::Replacement> * replacements)11 ValueRewriter::ListValueCallback::ListValueCallback(
12 std::string method,
13 std::string replacement,
14 std::set<clang::tooling::Replacement>* replacements)
15 : method_(std::move(method)),
16 replacement_(std::move(replacement)),
17 replacements_(replacements) {}
18
run(const MatchFinder::MatchResult & result)19 void ValueRewriter::ListValueCallback::run(
20 const MatchFinder::MatchResult& result) {
21 auto* callExpr = result.Nodes.getNodeAs<clang::CXXMemberCallExpr>(method());
22
23 clang::CharSourceRange call_range =
24 clang::CharSourceRange::getTokenRange(callExpr->getExprLoc());
25 replacements_->emplace(*result.SourceManager, call_range, replacement());
26 }
27
ValueRewriter(std::set<clang::tooling::Replacement> * replacements)28 ValueRewriter::ValueRewriter(
29 std::set<clang::tooling::Replacement>* replacements)
30 : list_value_callbacks_({
31 {"::base::ListValue::Clear", "GetList().clear", replacements},
32 {"::base::ListValue::GetSize", "GetList().size", replacements},
33 {"::base::ListValue::empty", "GetList().empty", replacements},
34 {"::base::ListValue::Reserve", "GetList().reserve", replacements},
35 {"::base::ListValue::AppendBoolean", "GetList().emplace_back",
36 replacements},
37 {"::base::ListValue::AppendInteger", "GetList().emplace_back",
38 replacements},
39 {"::base::ListValue::AppendDouble", "GetList().emplace_back",
40 replacements},
41 {"::base::ListValue::AppendString", "GetList().emplace_back",
42 replacements},
43 }) {}
44
RegisterMatchers(MatchFinder * match_finder)45 void ValueRewriter::RegisterMatchers(MatchFinder* match_finder) {
46 for (auto& callback : list_value_callbacks_) {
47 match_finder->addMatcher(
48 callExpr(callee(functionDecl(hasName(callback.method()))))
49 .bind(callback.method()),
50 &callback);
51 }
52 }
53