Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
|
danakj
2016/06/02 20:15:08
2016
dcheng
2016/06/02 20:48:28
Done.
dcheng
2016/06/02 20:48:28
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::ListValue API usage. | |
| 6 // - ListValue::Append(new FundamentalValue(bool)) | |
| 7 // => ListValue::AppendBoolean() | |
| 8 // - ListValue::Append(new FundamentalValue(int)) | |
| 9 // => ListValue::AppendInteger() | |
| 10 // - ListValue::Append(new FundamentalValue(double)) | |
| 11 // => ListValue::AppendDouble() | |
| 12 // - base::ListValue::Append(new base::StringValue(...)) | |
|
danakj
2016/06/02 20:15:08
Why does this one say base:: but the others dont?
dcheng
2016/06/02 20:48:28
Fixed (and below)
| |
| 13 // => base::ListValue::AppendString(...) | |
| 14 | |
| 15 #include "ListValueRewriter.h" | |
| 16 | |
| 17 #include <assert.h> | |
| 18 #include <algorithm> | |
| 19 | |
| 20 #include "clang/AST/ASTContext.h" | |
| 21 #include "clang/ASTMatchers/ASTMatchFinder.h" | |
| 22 #include "clang/ASTMatchers/ASTMatchers.h" | |
| 23 #include "clang/ASTMatchers/ASTMatchersMacros.h" | |
| 24 #include "clang/Basic/CharInfo.h" | |
| 25 #include "clang/Basic/SourceManager.h" | |
| 26 #include "clang/Frontend/FrontendActions.h" | |
| 27 #include "clang/Lex/Lexer.h" | |
| 28 #include "clang/Tooling/Refactoring.h" | |
| 29 #include "llvm/ADT/STLExtras.h" | |
| 30 | |
| 31 using namespace clang::ast_matchers; | |
| 32 using clang::tooling::Replacement; | |
| 33 using clang::tooling::Replacements; | |
| 34 using llvm::StringRef; | |
| 35 | |
| 36 ListValueRewriter::AppendCallback::AppendCallback(Replacements* replacements) | |
| 37 : replacements_(replacements) {} | |
| 38 | |
| 39 void ListValueRewriter::AppendCallback::run( | |
| 40 const MatchFinder::MatchResult& result) { | |
| 41 // Delete `new base::*Value(' and `)'. | |
| 42 auto* newExpr = result.Nodes.getNodeAs<clang::CXXNewExpr>("newExpr"); | |
| 43 auto* argExpr = result.Nodes.getNodeAs<clang::Expr>("argExpr"); | |
| 44 | |
| 45 clang::CharSourceRange pre_arg_range = clang::CharSourceRange::getCharRange( | |
| 46 newExpr->getLocStart(), argExpr->getLocStart()); | |
| 47 replacements_->emplace(*result.SourceManager, pre_arg_range, ""); | |
| 48 | |
| 49 clang::CharSourceRange post_arg_range = | |
| 50 clang::CharSourceRange::getTokenRange(newExpr->getLocEnd()); | |
| 51 replacements_->emplace(*result.SourceManager, post_arg_range, ""); | |
| 52 } | |
| 53 | |
| 54 ListValueRewriter::AppendBooleanCallback::AppendBooleanCallback( | |
| 55 Replacements* replacements) | |
| 56 : AppendCallback(replacements) {} | |
| 57 | |
| 58 void ListValueRewriter::AppendBooleanCallback::run( | |
| 59 const MatchFinder::MatchResult& result) { | |
| 60 // Replace 'Append' with 'AppendBoolean'. | |
| 61 auto* callExpr = result.Nodes.getNodeAs<clang::CXXMemberCallExpr>("callExpr"); | |
| 62 | |
| 63 clang::CharSourceRange call_range = | |
| 64 clang::CharSourceRange::getTokenRange(callExpr->getExprLoc()); | |
| 65 replacements_->emplace(*result.SourceManager, call_range, "AppendBoolean"); | |
| 66 | |
| 67 AppendCallback::run(result); | |
| 68 } | |
| 69 | |
| 70 ListValueRewriter::AppendIntegerCallback::AppendIntegerCallback( | |
| 71 Replacements* replacements) | |
| 72 : AppendCallback(replacements) {} | |
| 73 | |
| 74 void ListValueRewriter::AppendIntegerCallback::run( | |
| 75 const MatchFinder::MatchResult& result) { | |
| 76 // Replace 'Append' with 'AppendInteger'. | |
| 77 auto* callExpr = result.Nodes.getNodeAs<clang::CXXMemberCallExpr>("callExpr"); | |
| 78 | |
| 79 clang::CharSourceRange call_range = | |
| 80 clang::CharSourceRange::getTokenRange(callExpr->getExprLoc()); | |
| 81 replacements_->emplace(*result.SourceManager, call_range, "AppendInteger"); | |
| 82 | |
| 83 AppendCallback::run(result); | |
| 84 } | |
| 85 | |
| 86 ListValueRewriter::AppendDoubleCallback::AppendDoubleCallback( | |
| 87 Replacements* replacements) | |
| 88 : AppendCallback(replacements) {} | |
| 89 | |
| 90 void ListValueRewriter::AppendDoubleCallback::run( | |
| 91 const MatchFinder::MatchResult& result) { | |
| 92 // Replace 'Append' with 'AppendDouble'. | |
| 93 auto* callExpr = result.Nodes.getNodeAs<clang::CXXMemberCallExpr>("callExpr"); | |
| 94 | |
| 95 clang::CharSourceRange call_range = | |
| 96 clang::CharSourceRange::getTokenRange(callExpr->getExprLoc()); | |
| 97 replacements_->emplace(*result.SourceManager, call_range, "AppendDouble"); | |
| 98 | |
| 99 AppendCallback::run(result); | |
| 100 } | |
| 101 | |
| 102 ListValueRewriter::AppendStringCallback::AppendStringCallback( | |
| 103 Replacements* replacements) | |
| 104 : AppendCallback(replacements) {} | |
| 105 | |
| 106 void ListValueRewriter::AppendStringCallback::run( | |
| 107 const MatchFinder::MatchResult& result) { | |
| 108 // Replace 'Append' with 'AppendString'. | |
| 109 auto* callExpr = result.Nodes.getNodeAs<clang::CXXMemberCallExpr>("callExpr"); | |
| 110 | |
| 111 clang::CharSourceRange call_range = | |
| 112 clang::CharSourceRange::getTokenRange(callExpr->getExprLoc()); | |
| 113 replacements_->emplace(*result.SourceManager, call_range, "AppendString"); | |
| 114 | |
| 115 AppendCallback::run(result); | |
| 116 } | |
| 117 | |
| 118 ListValueRewriter::ListValueRewriter(Replacements* replacements) | |
| 119 : append_boolean_callback_(replacements), | |
| 120 append_integer_callback_(replacements), | |
| 121 append_double_callback_(replacements), | |
| 122 append_string_callback_(replacements) {} | |
| 123 | |
| 124 void ListValueRewriter::RegisterMatchers(MatchFinder* match_finder) { | |
| 125 auto is_list_append = | |
| 126 allOf(callee(cxxMethodDecl(hasName("::base::ListValue::Append"))), | |
| 127 argumentCountIs(1)); | |
| 128 | |
| 129 // ListValue::Append(new FundamentalValue(bool)) | |
| 130 // => ListValue::AppendBoolean() | |
| 131 match_finder->addMatcher( | |
| 132 id("callExpr", | |
| 133 cxxMemberCallExpr( | |
| 134 is_list_append, | |
| 135 hasArgument( | |
| 136 0, ignoringParenImpCasts(id( | |
| 137 "newExpr", | |
| 138 cxxNewExpr(has(cxxConstructExpr( | |
| 139 hasDeclaration(cxxMethodDecl(hasName( | |
| 140 "::base::FundamentalValue::FundamentalValue"))), | |
| 141 argumentCountIs(1), | |
| 142 hasArgument( | |
| 143 0, id("argExpr", | |
| 144 expr(hasType(booleanType())))))))))))), | |
| 145 &append_boolean_callback_); | |
| 146 | |
| 147 // ListValue::Append(new FundamentalValue(int)) | |
| 148 // => ListValue::AppendInteger() | |
| 149 match_finder->addMatcher( | |
| 150 id("callExpr", | |
| 151 cxxMemberCallExpr( | |
| 152 is_list_append, | |
| 153 hasArgument( | |
| 154 0, | |
| 155 ignoringParenImpCasts(id( | |
| 156 "newExpr", | |
| 157 cxxNewExpr(has(cxxConstructExpr( | |
| 158 hasDeclaration(cxxMethodDecl(hasName( | |
| 159 "::base::FundamentalValue::FundamentalValue"))), | |
| 160 argumentCountIs(1), | |
| 161 hasArgument(0, id("argExpr", | |
| 162 expr(hasType(isInteger()), | |
| 163 unless(hasType( | |
| 164 booleanType()))))))))))))), | |
| 165 &append_integer_callback_); | |
| 166 | |
| 167 // ListValue::Append(new FundamentalValue(double)) | |
| 168 // => ListValue::AppendDouble() | |
| 169 match_finder->addMatcher( | |
| 170 id("callExpr", | |
| 171 cxxMemberCallExpr( | |
| 172 is_list_append, | |
| 173 hasArgument( | |
| 174 0, ignoringParenImpCasts(id( | |
| 175 "newExpr", | |
| 176 cxxNewExpr(has(cxxConstructExpr( | |
| 177 hasDeclaration(cxxMethodDecl(hasName( | |
| 178 "::base::FundamentalValue::FundamentalValue"))), | |
| 179 argumentCountIs(1), | |
| 180 hasArgument( | |
| 181 0, id("argExpr", | |
| 182 expr(hasType( | |
| 183 realFloatingPointType())))))))))))), | |
| 184 &append_double_callback_); | |
| 185 | |
| 186 // ListValue::Append(new StringValue(...)) | |
| 187 // => ListValue::AppendString() | |
| 188 match_finder->addMatcher( | |
| 189 id("callExpr", | |
| 190 cxxMemberCallExpr( | |
| 191 is_list_append, | |
| 192 hasArgument( | |
| 193 0, ignoringParenImpCasts(id( | |
| 194 "newExpr", | |
| 195 cxxNewExpr(has(cxxConstructExpr( | |
| 196 hasDeclaration(cxxMethodDecl( | |
| 197 hasName("::base::StringValue::StringValue"))), | |
| 198 argumentCountIs(1), | |
| 199 hasArgument(0, id("argExpr", expr())))))))))), | |
| 200 &append_string_callback_); | |
| 201 } | |
| OLD | NEW |