OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
danakj
2016/06/02 18:33:02
2016
dcheng
2016/06/02 19:51:33
Done
| |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 // | |
5 // Performs simple cleanups of base::Value API usage. | |
6 // - base::ListValue::Append(new base::StringValue(...)) | |
7 // => base::ListValue::AppendString(...) | |
8 | |
9 #include <assert.h> | |
10 #include <algorithm> | |
11 #include <memory> | |
12 #include <string> | |
13 #include <unordered_map> | |
14 | |
15 #include "clang/AST/ASTContext.h" | |
16 #include "clang/ASTMatchers/ASTMatchFinder.h" | |
17 #include "clang/ASTMatchers/ASTMatchers.h" | |
18 #include "clang/ASTMatchers/ASTMatchersMacros.h" | |
19 #include "clang/Basic/CharInfo.h" | |
20 #include "clang/Basic/SourceManager.h" | |
21 #include "clang/Frontend/FrontendActions.h" | |
22 #include "clang/Lex/Lexer.h" | |
23 #include "clang/Tooling/CommonOptionsParser.h" | |
24 #include "clang/Tooling/Refactoring.h" | |
25 #include "clang/Tooling/Tooling.h" | |
26 #include "llvm/Support/CommandLine.h" | |
27 #include "llvm/Support/TargetSelect.h" | |
28 | |
29 using namespace clang::ast_matchers; | |
30 using clang::tooling::CommonOptionsParser; | |
31 using clang::tooling::Replacement; | |
32 using clang::tooling::Replacements; | |
33 using llvm::StringRef; | |
34 | |
35 namespace { | |
36 | |
37 class AppendStringCallback : public MatchFinder::MatchCallback { | |
38 public: | |
39 explicit AppendStringCallback(Replacements* replacements) | |
40 : replacements_(replacements) {} | |
41 | |
42 void run(const MatchFinder::MatchResult& result) override { | |
43 // Replace 'Append' with 'AppendString'. | |
44 auto* callExpr = | |
45 result.Nodes.getNodeAs<clang::CXXMemberCallExpr>("callExpr"); | |
46 | |
47 clang::CharSourceRange call_range = | |
48 clang::CharSourceRange::getTokenRange(callExpr->getExprLoc()); | |
49 replacements_->emplace(*result.SourceManager, call_range, "AppendString"); | |
50 | |
51 // Delete `new base::StringValue(' and `)'. | |
52 auto* newExpr = result.Nodes.getNodeAs<clang::CXXNewExpr>("newExpr"); | |
53 auto* argExpr = result.Nodes.getNodeAs<clang::Expr>("argExpr"); | |
54 | |
55 clang::CharSourceRange pre_arg_range = clang::CharSourceRange::getCharRange( | |
56 newExpr->getLocStart(), argExpr->getLocStart()); | |
57 replacements_->emplace(*result.SourceManager, pre_arg_range, ""); | |
58 | |
59 clang::CharSourceRange post_arg_range = | |
60 clang::CharSourceRange::getTokenRange(newExpr->getLocEnd()); | |
61 replacements_->emplace(*result.SourceManager, post_arg_range, ""); | |
62 } | |
63 | |
64 private: | |
65 Replacements* const replacements_; | |
66 }; | |
67 | |
68 } // namespace | |
69 | |
70 static llvm::cl::extrahelp common_help(CommonOptionsParser::HelpMessage); | |
71 | |
72 int main(int argc, const char* argv[]) { | |
73 // TODO(dcheng): Clang tooling should do this itself. | |
74 // http://llvm.org/bugs/show_bug.cgi?id=21627 | |
75 llvm::InitializeNativeTarget(); | |
76 llvm::InitializeNativeTargetAsmParser(); | |
77 llvm::cl::OptionCategory category( | |
78 "value_cleanup: Cleanup deprecated base::Value APIs."); | |
79 CommonOptionsParser options(argc, argv, category); | |
80 clang::tooling::ClangTool tool(options.getCompilations(), | |
81 options.getSourcePathList()); | |
82 | |
83 MatchFinder match_finder; | |
84 Replacements replacements; | |
85 | |
86 auto append_with_raw_string_value = id( | |
87 "callExpr", | |
88 cxxMemberCallExpr( | |
89 callee(cxxMethodDecl(hasName("::base::ListValue::Append"))), | |
90 argumentCountIs(1), | |
91 hasArgument(0, ignoringParenImpCasts(id( | |
92 "newExpr", | |
93 cxxNewExpr(has(cxxConstructExpr( | |
94 hasDeclaration(cxxMethodDecl(hasName( | |
95 "::base::StringValue::StringValue"))), | |
96 argumentCountIs(1), | |
97 hasArgument(0, id("argExpr", expr())))))))))); | |
98 | |
99 AppendStringCallback append_string_cb(&replacements); | |
100 match_finder.addMatcher(append_with_raw_string_value, &append_string_cb); | |
101 | |
102 std::unique_ptr<clang::tooling::FrontendActionFactory> factory = | |
103 clang::tooling::newFrontendActionFactory(&match_finder); | |
104 int result = tool.run(factory.get()); | |
105 if (result != 0) | |
106 return result; | |
107 | |
108 // Serialization format is documented in tools/clang/scripts/run_tool.py | |
109 llvm::outs() << "==== BEGIN EDITS ====\n"; | |
110 for (const auto& r : replacements) { | |
111 std::string replacement_text = r.getReplacementText().str(); | |
112 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); | |
113 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() | |
114 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; | |
115 } | |
116 llvm::outs() << "==== END EDITS ====\n"; | |
117 | |
118 return 0; | |
119 } | |
OLD | NEW |