Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 //===---- tools/extra/ToolTemplate.cpp - Template for refactoring tool ----===// | |
| 2 // | |
| 3 // The LLVM Compiler Infrastructure | |
| 4 // | |
| 5 // This file is distributed under the University of Illinois Open Source | |
| 6 // License. See LICENSE.TXT for details. | |
| 7 // | |
| 8 //===----------------------------------------------------------------------===// | |
| 9 // | |
| 10 // This file implements a simple clang tool for automatically converting | |
| 11 // instantiations of std::string with an empty string literal to simply call | |
| 12 // the default constructor. The goal is to provide a simple example for writing | |
| 13 // a clang tool for rewriting Chromium code. | |
| 14 // | |
| 15 // Usage: | |
| 16 // tool-template -p <path-to-build-dir> <file1> <file2> ... | |
| 17 // | |
| 18 // See <wiki page that is yet to be finished>. | |
| 19 // TODO(dcheng): Figure out copyright blobs... | |
|
Nico
2013/03/27 16:35:29
Is there any reason this can't just have the regul
dcheng
2013/03/28 00:10:17
Done.
| |
| 20 // | |
| 21 //===----------------------------------------------------------------------===// | |
| 22 | |
| 23 #include "clang/ASTMatchers/ASTMatchers.h" | |
| 24 #include "clang/ASTMatchers/ASTMatchFinder.h" | |
| 25 #include "clang/Basic/SourceManager.h" | |
| 26 #include "clang/Frontend/FrontendActions.h" | |
| 27 #include "clang/Tooling/CommonOptionsParser.h" | |
| 28 #include "clang/Tooling/Refactoring.h" | |
| 29 #include "clang/Tooling/Tooling.h" | |
| 30 #include "llvm/Support/CommandLine.h" | |
| 31 | |
| 32 using clang::ast_matchers::MatchFinder; | |
| 33 using clang::ast_matchers::argumentCountIs; | |
| 34 using clang::ast_matchers::bindTemporaryExpr; | |
| 35 using clang::ast_matchers::constructorDecl; | |
| 36 using clang::ast_matchers::constructExpr; | |
| 37 using clang::ast_matchers::defaultArgExpr; | |
| 38 using clang::ast_matchers::forEach; | |
| 39 using clang::ast_matchers::has; | |
| 40 using clang::ast_matchers::hasArgument; | |
| 41 using clang::ast_matchers::hasDeclaration; | |
| 42 using clang::ast_matchers::hasName; | |
| 43 using clang::ast_matchers::id; | |
| 44 using clang::ast_matchers::methodDecl; | |
| 45 using clang::ast_matchers::newExpr; | |
| 46 using clang::ast_matchers::stringLiteral; | |
| 47 using clang::ast_matchers::varDecl; | |
| 48 using clang::tooling::CommonOptionsParser; | |
| 49 using clang::tooling::Replacement; | |
| 50 using clang::tooling::Replacements; | |
| 51 | |
| 52 namespace { | |
| 53 | |
| 54 enum ReplacementMode { | |
| 55 kConstructorMode, | |
| 56 kInitializerMode, | |
| 57 kTemporaryMode, | |
| 58 }; | |
| 59 | |
| 60 class EmptyStringConverterCallback : public MatchFinder::MatchCallback { | |
| 61 public: | |
| 62 EmptyStringConverterCallback(ReplacementMode mode, Replacements* replacements) | |
| 63 : mode_(mode), replacements_(replacements) {} | |
| 64 | |
| 65 virtual void run(const MatchFinder::MatchResult& result); | |
| 66 | |
| 67 private: | |
| 68 const ReplacementMode mode_; | |
| 69 Replacements* const replacements_; | |
| 70 }; | |
| 71 | |
| 72 class EmptyStringConverter { | |
| 73 public: | |
| 74 explicit EmptyStringConverter(Replacements* replacements) | |
| 75 : constructor_callback_(kConstructorMode, replacements), | |
| 76 initializer_callback_(kInitializerMode, replacements), | |
| 77 temporary_callback_(kTemporaryMode, replacements) {} | |
| 78 | |
| 79 void SetupMatchers(MatchFinder* match_finder); | |
| 80 | |
| 81 private: | |
| 82 EmptyStringConverterCallback constructor_callback_; | |
| 83 EmptyStringConverterCallback initializer_callback_; | |
| 84 EmptyStringConverterCallback temporary_callback_; | |
| 85 }; | |
| 86 | |
| 87 void EmptyStringConverter::SetupMatchers(MatchFinder* match_finder) { | |
| 88 // TODO(dcheng): This doesn't match std::wstring... but hopefully you're not | |
| 89 // using that, right? | |
| 90 const char kStringConstructorCall[] = | |
| 91 "::std::basic_string<char, std::char_traits<char>, std::allocator<char> >" | |
| 92 "::basic_string"; | |
| 93 const auto& constructor_call = id( | |
| 94 "call", | |
| 95 constructExpr(hasDeclaration(methodDecl(hasName(kStringConstructorCall))), | |
| 96 argumentCountIs(2), | |
| 97 hasArgument(0, id("literal", stringLiteral())), | |
| 98 hasArgument(1, defaultArgExpr()))); | |
| 99 | |
| 100 match_finder->addMatcher(varDecl(forEach(constructor_call)), | |
| 101 &constructor_callback_); | |
| 102 match_finder->addMatcher(newExpr(has(constructor_call)), | |
| 103 &constructor_callback_); | |
| 104 match_finder->addMatcher(constructorDecl(forEach(constructor_call)), | |
| 105 &initializer_callback_); | |
| 106 match_finder->addMatcher(bindTemporaryExpr(has(constructor_call)), | |
| 107 &temporary_callback_); | |
| 108 } | |
| 109 | |
| 110 void EmptyStringConverterCallback::run(const MatchFinder::MatchResult& result) { | |
| 111 const clang::StringLiteral* literal = | |
| 112 result.Nodes.getNodeAs<clang::StringLiteral>("literal"); | |
| 113 if (literal->getLength() > 0) | |
| 114 return; | |
| 115 | |
| 116 const clang::CXXConstructExpr* call = | |
| 117 result.Nodes.getNodeAs<clang::CXXConstructExpr>("call"); | |
| 118 | |
| 119 switch (mode_) { | |
| 120 case kConstructorMode: { | |
| 121 clang::CharSourceRange range = | |
| 122 clang::CharSourceRange::getTokenRange(call->getParenRange()); | |
| 123 replacements_->insert(Replacement(*result.SourceManager, range, "")); | |
| 124 break; | |
| 125 } | |
| 126 case kInitializerMode: { | |
| 127 // TODO(dcheng): Implement. | |
| 128 break; | |
| 129 } | |
| 130 case kTemporaryMode: { | |
| 131 // A replacement for an explicit call to std::string("") won't end up | |
| 132 // replacing the closing parenthesis. Instead, detect explicit calls and | |
| 133 // handle them differently. | |
| 134 clang::SourceRange range = call->getParenRange(); | |
| 135 if (range.isValid()) { | |
| 136 replacements_->insert(Replacement(*result.SourceManager, literal, "")); | |
| 137 } else { | |
| 138 replacements_->insert( | |
| 139 Replacement(*result.SourceManager, call, "std::string()")); | |
| 140 } | |
| 141 break; | |
| 142 } | |
| 143 } | |
| 144 } | |
| 145 | |
| 146 } // namespace | |
| 147 | |
| 148 static llvm::cl::extrahelp common_help(CommonOptionsParser::HelpMessage); | |
| 149 | |
| 150 int main(int argc, const char* argv[]) { | |
| 151 CommonOptionsParser options(argc, argv); | |
| 152 | |
| 153 clang::tooling::RefactoringTool tool(options.getCompilations(), | |
| 154 options.getSourcePathList()); | |
| 155 | |
| 156 EmptyStringConverter converter(&tool.getReplacements()); | |
| 157 MatchFinder match_finder; | |
| 158 converter.SetupMatchers(&match_finder); | |
| 159 | |
| 160 // Intentionally avoid using runAndSave(). Instead, please run this tool using | |
| 161 // tools/clang/scripts/run_tool.py. See notes in run_tool.py for more | |
| 162 // information about why. | |
| 163 int result = | |
| 164 tool.run(clang::tooling::newFrontendActionFactory(&match_finder)); | |
| 165 if (result != 0) | |
| 166 return result; | |
| 167 | |
| 168 // Each replacement line should have the following format: | |
| 169 // r:<file path>:<offset>:<length>:<replacement text> | |
| 170 // Only the <replacement text> field can contain embedded ":" characters. | |
| 171 // TODO(dcheng): Use a more clever serialization. | |
| 172 llvm::outs() << "==== BEGIN EDITS ====\n"; | |
| 173 for (const Replacement& r : tool.getReplacements()) { | |
| 174 llvm::outs() << "r:" | |
| 175 << r.getFilePath() << ":" | |
| 176 << r.getOffset() << ":" | |
| 177 << r.getLength() << ":" | |
| 178 << r.getReplacementText() << "\n"; | |
| 179 } | |
| 180 llvm::outs() << "==== END EDITS ====\n"; | |
| 181 | |
| 182 return 0; | |
| 183 } | |
| OLD | NEW |