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 674 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
685 } | 685 } |
686 | 686 |
687 void AddReplacement(const MatchFinder::MatchResult& result, | 687 void AddReplacement(const MatchFinder::MatchResult& result, |
688 llvm::StringRef old_name, | 688 llvm::StringRef old_name, |
689 std::string new_name) { | 689 std::string new_name) { |
690 if (old_name == new_name) | 690 if (old_name == new_name) |
691 return; | 691 return; |
692 | 692 |
693 clang::SourceLocation loc = | 693 clang::SourceLocation loc = |
694 TargetNodeTraits<TargetNode>::GetLoc(GetTargetNode(result)); | 694 TargetNodeTraits<TargetNode>::GetLoc(GetTargetNode(result)); |
695 clang::CharSourceRange range = clang::CharSourceRange::getTokenRange(loc); | 695 |
696 replacements_->emplace(*result.SourceManager, range, new_name); | 696 if (loc.isMacroID()) { |
697 // Try to jump "above" the scratch buffer if |loc| is inside | |
698 // token##Concatenation. | |
699 const int kMaxJumps = 5; | |
700 bool verified_out_of_scratch_space = false; | |
701 for (int i = 0; i < kMaxJumps && !verified_out_of_scratch_space; i++) { | |
702 clang::SourceLocation spell = result.SourceManager->getSpellingLoc(loc); | |
703 verified_out_of_scratch_space = | |
704 result.SourceManager->getBufferName(spell) != "<scratch space>"; | |
dcheng
2016/12/22 21:32:42
I think I would prefer that we just use the file e
Łukasz Anforowicz
2016/12/23 00:17:48
I can't make it work without actually looking at "
| |
705 if (!verified_out_of_scratch_space) | |
706 loc = result.SourceManager->getImmediateMacroCallerLoc(loc); | |
707 } | |
708 if (!verified_out_of_scratch_space) | |
709 return; | |
710 } | |
711 | |
712 // If the edit affects only the first character of the identifier, then | |
713 // narrow down the edit to only this single character. This is important | |
714 // for dealing with toFooBar -> ToFooBar method renaming when the method | |
715 // name is built using macro token concatenation like to##macroArgument - in | |
716 // this case we should only rewrite "t" -> "T" and leave "o##macroArgument" | |
717 // untouched. | |
718 llvm::StringRef expected_old_text = old_name; | |
719 llvm::StringRef new_text = new_name; | |
720 if (expected_old_text.substr(1) == new_text.substr(1)) { | |
721 expected_old_text = expected_old_text.substr(0, 1); | |
722 new_text = new_text.substr(0, 1); | |
723 } | |
724 clang::SourceLocation spell = result.SourceManager->getSpellingLoc(loc); | |
725 clang::CharSourceRange range = clang::CharSourceRange::getCharRange( | |
726 spell, spell.getLocWithOffset(expected_old_text.size())); | |
727 | |
728 // We need to ensure that |actual_old_text| is the same as | |
729 // |expected_old_text| - it can be different if |actual_old_text| contains | |
730 // a macro argument (see DEFINE_WITH_TOKEN_CONCATENATION2 in | |
731 // macros-original.cc testcase). | |
732 StringRef actual_old_text = clang::Lexer::getSourceText( | |
733 range, *result.SourceManager, result.Context->getLangOpts()); | |
734 if (actual_old_text != expected_old_text) | |
735 return; | |
736 | |
737 replacements_->emplace(*result.SourceManager, range, new_text); | |
697 replacement_names_.emplace(old_name.str(), std::move(new_name)); | 738 replacement_names_.emplace(old_name.str(), std::move(new_name)); |
698 } | 739 } |
699 | 740 |
700 const std::unordered_map<std::string, std::string>& replacement_names() | 741 const std::unordered_map<std::string, std::string>& replacement_names() |
701 const { | 742 const { |
702 return replacement_names_; | 743 return replacement_names_; |
703 } | 744 } |
704 | 745 |
705 private: | 746 private: |
706 std::set<Replacement>* const replacements_; | 747 std::set<Replacement>* const replacements_; |
707 std::unordered_map<std::string, std::string> replacement_names_; | 748 std::unordered_map<std::string, std::string> replacement_names_; |
708 }; | 749 }; |
709 | 750 |
710 template <typename DeclNode, typename TargetNode> | 751 template <typename DeclNode, typename TargetNode> |
711 class DeclRewriterBase : public RewriterBase<TargetNode> { | 752 class DeclRewriterBase : public RewriterBase<TargetNode> { |
712 public: | 753 public: |
713 using Base = RewriterBase<TargetNode>; | 754 using Base = RewriterBase<TargetNode>; |
714 | 755 |
715 explicit DeclRewriterBase(std::set<Replacement>* replacements) | 756 explicit DeclRewriterBase(std::set<Replacement>* replacements) |
716 : Base(replacements) {} | 757 : Base(replacements) {} |
717 | 758 |
718 void run(const MatchFinder::MatchResult& result) override { | 759 void run(const MatchFinder::MatchResult& result) override { |
719 const DeclNode* decl = result.Nodes.getNodeAs<DeclNode>("decl"); | 760 const DeclNode* decl = result.Nodes.getNodeAs<DeclNode>("decl"); |
720 assert(decl); | 761 assert(decl); |
721 // If false, there's no name to be renamed. | 762 // If false, there's no name to be renamed. |
722 if (!decl->getIdentifier()) | 763 if (!decl->getIdentifier()) |
723 return; | 764 return; |
724 clang::SourceLocation decl_loc = | |
725 TargetNodeTraits<clang::NamedDecl>::GetLoc(*decl); | |
726 if (decl_loc.isMacroID()) { | |
727 // Get the location of the spelling of the declaration. If token pasting | |
728 // was used this will be in "scratch space" and we don't know how to get | |
729 // from there back to/ the actual macro with the foo##bar text. So just | |
730 // don't replace in that case. | |
731 clang::SourceLocation spell = | |
732 result.SourceManager->getSpellingLoc(decl_loc); | |
733 if (result.SourceManager->getBufferName(spell) == "<scratch space>") | |
734 return; | |
735 } | |
736 clang::ASTContext* context = result.Context; | 765 clang::ASTContext* context = result.Context; |
737 std::string new_name; | 766 std::string new_name; |
738 if (!GetNameForDecl(*decl, *context, new_name)) | 767 if (!GetNameForDecl(*decl, *context, new_name)) |
739 return; // If false, the name was not suitable for renaming. | 768 return; // If false, the name was not suitable for renaming. |
740 llvm::StringRef old_name = decl->getName(); | 769 llvm::StringRef old_name = decl->getName(); |
741 Base::AddReplacement(result, old_name, std::move(new_name)); | 770 Base::AddReplacement(result, old_name, std::move(new_name)); |
742 } | 771 } |
743 }; | 772 }; |
744 | 773 |
745 using FieldDeclRewriter = DeclRewriterBase<clang::FieldDecl, clang::NamedDecl>; | 774 using FieldDeclRewriter = DeclRewriterBase<clang::FieldDecl, clang::NamedDecl>; |
(...skipping 577 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1323 for (const auto& r : replacements) { | 1352 for (const auto& r : replacements) { |
1324 std::string replacement_text = r.getReplacementText().str(); | 1353 std::string replacement_text = r.getReplacementText().str(); |
1325 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); | 1354 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); |
1326 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() | 1355 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() |
1327 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; | 1356 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; |
1328 } | 1357 } |
1329 llvm::outs() << "==== END EDITS ====\n"; | 1358 llvm::outs() << "==== END EDITS ====\n"; |
1330 | 1359 |
1331 return 0; | 1360 return 0; |
1332 } | 1361 } |
OLD | NEW |