| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // Changes Blink-style names to Chrome-style names. Currently transforms: | 5 // Changes Blink-style names to Chrome-style names. Currently transforms: |
| 6 // fields: | 6 // fields: |
| 7 // int m_operationCount => int operation_count_ | 7 // int m_operationCount => int operation_count_ |
| 8 // variables (including parameters): | 8 // variables (including parameters): |
| 9 // int mySuperVariable => int my_super_variable | 9 // int mySuperVariable => int my_super_variable |
| 10 // constants: | 10 // constants: |
| (...skipping 1196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1207 | 1207 |
| 1208 std::unique_ptr<clang::PPCallbacks> CreatePreprocessorCallbacks() { | 1208 std::unique_ptr<clang::PPCallbacks> CreatePreprocessorCallbacks() { |
| 1209 return llvm::make_unique<GMockMemberRewriter::PPCallbacks>(this); | 1209 return llvm::make_unique<GMockMemberRewriter::PPCallbacks>(this); |
| 1210 } | 1210 } |
| 1211 | 1211 |
| 1212 clang::SourceLocation GetTargetLoc( | 1212 clang::SourceLocation GetTargetLoc( |
| 1213 const MatchFinder::MatchResult& result) override { | 1213 const MatchFinder::MatchResult& result) override { |
| 1214 // Find location of the gmock_##MockedMethod identifier. | 1214 // Find location of the gmock_##MockedMethod identifier. |
| 1215 clang::SourceLocation target_loc = Base::GetTargetLoc(result); | 1215 clang::SourceLocation target_loc = Base::GetTargetLoc(result); |
| 1216 | 1216 |
| 1217 // Find location of EXPECT_CALL macro invocation. | 1217 // Find location of EXPECT_CALL or ON_CALL macro invocation. |
| 1218 clang::SourceLocation macro_call_loc = | 1218 clang::SourceLocation macro_call_loc = |
| 1219 result.SourceManager->getExpansionLoc(target_loc); | 1219 result.SourceManager->getExpansionLoc(target_loc); |
| 1220 | 1220 |
| 1221 // Map |macro_call_loc| to argument location (location of the method name | 1221 // Map |macro_call_loc| to argument location (location of the method name |
| 1222 // that needs renaming). | 1222 // that needs renaming). |
| 1223 auto it = expect_call_to_2nd_arg.find(macro_call_loc); | 1223 auto it = gmock_macro_call_to_2nd_arg.find(macro_call_loc); |
| 1224 if (it == expect_call_to_2nd_arg.end()) | 1224 if (it == gmock_macro_call_to_2nd_arg.end()) |
| 1225 return clang::SourceLocation(); | 1225 return clang::SourceLocation(); |
| 1226 return it->second; | 1226 return it->second; |
| 1227 } | 1227 } |
| 1228 | 1228 |
| 1229 private: | 1229 private: |
| 1230 std::map<clang::SourceLocation, clang::SourceLocation> expect_call_to_2nd_arg; | 1230 std::map<clang::SourceLocation, clang::SourceLocation> |
| 1231 gmock_macro_call_to_2nd_arg; |
| 1231 | 1232 |
| 1232 // Called from PPCallbacks with the locations of EXPECT_CALL macro invocation: | 1233 // Called from PPCallbacks with the locations of EXPECT_CALL and ON_CALL macro |
| 1233 // Example: | 1234 // invocation. Example: |
| 1234 // EXPECT_CALL(my_mock, myMethod(123, 456)); | 1235 // EXPECT_CALL(my_mock, myMethod(123, 456)); |
| 1235 // ^- expansion_loc ^- actual_arg_loc | 1236 // ^- expansion_loc ^- actual_arg_loc |
| 1236 void RecordExpectCallMacroInvocation(clang::SourceLocation expansion_loc, | 1237 void RecordGMockMacroInvocation(clang::SourceLocation expansion_loc, |
| 1237 clang::SourceLocation second_arg_loc) { | 1238 clang::SourceLocation second_arg_loc) { |
| 1238 expect_call_to_2nd_arg[expansion_loc] = second_arg_loc; | 1239 gmock_macro_call_to_2nd_arg[expansion_loc] = second_arg_loc; |
| 1239 } | 1240 } |
| 1240 | 1241 |
| 1241 class PPCallbacks : public clang::PPCallbacks { | 1242 class PPCallbacks : public clang::PPCallbacks { |
| 1242 public: | 1243 public: |
| 1243 explicit PPCallbacks(GMockMemberRewriter* rewriter) : rewriter_(rewriter) {} | 1244 explicit PPCallbacks(GMockMemberRewriter* rewriter) : rewriter_(rewriter) {} |
| 1244 ~PPCallbacks() override {} | 1245 ~PPCallbacks() override {} |
| 1245 void MacroExpands(const clang::Token& name, | 1246 void MacroExpands(const clang::Token& name, |
| 1246 const clang::MacroDefinition& def, | 1247 const clang::MacroDefinition& def, |
| 1247 clang::SourceRange range, | 1248 clang::SourceRange range, |
| 1248 const clang::MacroArgs* args) override { | 1249 const clang::MacroArgs* args) override { |
| 1249 clang::IdentifierInfo* id = name.getIdentifierInfo(); | 1250 clang::IdentifierInfo* id = name.getIdentifierInfo(); |
| 1250 if (!id) | 1251 if (!id) |
| 1251 return; | 1252 return; |
| 1252 | 1253 |
| 1253 if (id->getName() != "EXPECT_CALL") | 1254 if (id->getName() != "EXPECT_CALL" && id->getName() != "ON_CALL") |
| 1254 return; | 1255 return; |
| 1255 | 1256 |
| 1256 if (def.getMacroInfo()->getNumArgs() != 2) | 1257 if (def.getMacroInfo()->getNumArgs() != 2) |
| 1257 return; | 1258 return; |
| 1258 | 1259 |
| 1259 // TODO(lukasza): Should check if def.getMacroInfo()->getDefinitionLoc() | 1260 // TODO(lukasza): Should check if def.getMacroInfo()->getDefinitionLoc() |
| 1260 // is in testing/gmock/include/gmock/gmock-spec-builders.h but I don't | 1261 // is in testing/gmock/include/gmock/gmock-spec-builders.h but I don't |
| 1261 // know how to get clang::SourceManager to call getFileName. | 1262 // know how to get clang::SourceManager to call getFileName. |
| 1262 | 1263 |
| 1263 rewriter_->RecordExpectCallMacroInvocation( | 1264 rewriter_->RecordGMockMacroInvocation( |
| 1264 name.getLocation(), args->getUnexpArgument(1)->getLocation()); | 1265 name.getLocation(), args->getUnexpArgument(1)->getLocation()); |
| 1265 } | 1266 } |
| 1266 | 1267 |
| 1267 private: | 1268 private: |
| 1268 GMockMemberRewriter* rewriter_; | 1269 GMockMemberRewriter* rewriter_; |
| 1269 }; | 1270 }; |
| 1270 }; | 1271 }; |
| 1271 | 1272 |
| 1272 clang::DeclarationName GetUnresolvedName( | 1273 clang::DeclarationName GetUnresolvedName( |
| 1273 const clang::UnresolvedMemberExpr& expr) { | 1274 const clang::UnresolvedMemberExpr& expr) { |
| (...skipping 537 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1811 expr(id("expr", cxxDependentScopeMemberExpr( | 1812 expr(id("expr", cxxDependentScopeMemberExpr( |
| 1812 hasMemberFromType(blink_qual_type_matcher)))); | 1813 hasMemberFromType(blink_qual_type_matcher)))); |
| 1813 CXXDependentScopeMemberExprRewriter cxx_dependent_scope_member_expr_rewriter( | 1814 CXXDependentScopeMemberExprRewriter cxx_dependent_scope_member_expr_rewriter( |
| 1814 &replacements); | 1815 &replacements); |
| 1815 match_finder.addMatcher(cxx_dependent_scope_member_expr_matcher, | 1816 match_finder.addMatcher(cxx_dependent_scope_member_expr_matcher, |
| 1816 &cxx_dependent_scope_member_expr_rewriter); | 1817 &cxx_dependent_scope_member_expr_rewriter); |
| 1817 | 1818 |
| 1818 // GMock calls lookup ======== | 1819 // GMock calls lookup ======== |
| 1819 // Given | 1820 // Given |
| 1820 // EXPECT_CALL(obj, myMethod(...)) | 1821 // EXPECT_CALL(obj, myMethod(...)) |
| 1821 // will match obj.gmock_myMethod(...) call generated by the macro | 1822 // or |
| 1823 // ON_CALL(obj, myMethod(...)) |
| 1824 // will match obj.gmock_myMethod(...) call generated by the macros |
| 1822 // (but only if it mocks a Blink method). | 1825 // (but only if it mocks a Blink method). |
| 1823 auto gmock_member_matcher = | 1826 auto gmock_member_matcher = |
| 1824 id("expr", memberExpr(hasDeclaration( | 1827 id("expr", memberExpr(hasDeclaration( |
| 1825 decl(cxxMethodDecl(mocksMethod(method_decl_matcher)))))); | 1828 decl(cxxMethodDecl(mocksMethod(method_decl_matcher)))))); |
| 1826 GMockMemberRewriter gmock_member_rewriter(&replacements); | 1829 GMockMemberRewriter gmock_member_rewriter(&replacements); |
| 1827 match_finder.addMatcher(gmock_member_matcher, &gmock_member_rewriter); | 1830 match_finder.addMatcher(gmock_member_matcher, &gmock_member_rewriter); |
| 1828 | 1831 |
| 1829 // Prepare and run the tool. | 1832 // Prepare and run the tool. |
| 1830 SourceFileCallbacks source_file_callbacks(&gmock_member_rewriter); | 1833 SourceFileCallbacks source_file_callbacks(&gmock_member_rewriter); |
| 1831 std::unique_ptr<clang::tooling::FrontendActionFactory> factory = | 1834 std::unique_ptr<clang::tooling::FrontendActionFactory> factory = |
| (...skipping 18 matching lines...) Expand all Loading... |
| 1850 for (const auto& r : replacements) { | 1853 for (const auto& r : replacements) { |
| 1851 std::string replacement_text = r.getReplacementText().str(); | 1854 std::string replacement_text = r.getReplacementText().str(); |
| 1852 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); | 1855 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); |
| 1853 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() | 1856 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() |
| 1854 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; | 1857 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; |
| 1855 } | 1858 } |
| 1856 llvm::outs() << "==== END EDITS ====\n"; | 1859 llvm::outs() << "==== END EDITS ====\n"; |
| 1857 | 1860 |
| 1858 return 0; | 1861 return 0; |
| 1859 } | 1862 } |
| OLD | NEW |