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>"; | |
705 if (!verified_out_of_scratch_space) | |
706 loc = result.SourceManager->getImmediateMacroCallerLoc(loc); | |
707 } | |
708 if (!verified_out_of_scratch_space) | |
dcheng
2016/12/22 11:35:04
It turns out I implemented something similar, but
Łukasz Anforowicz
2016/12/22 17:59:30
Acknowledged. I considered switching to your way
| |
709 return; | |
710 } | |
711 | |
712 // Minimize the diff - this is important for dealing with toFooBar -> | |
dcheng
2016/12/22 11:35:04
Perhaps mention that if the edit only affect 1 cha
Łukasz Anforowicz
2016/12/22 17:59:30
Ok - done.
| |
713 // ToFooBar method renaming when the method name is built using macro | |
714 // token concatenation like to##macroArgument - in this case we should | |
715 // only rewrite "t" -> "T" and leave "o##macroArgument" untouched. | |
716 llvm::StringRef expected_old_text = old_name; | |
717 llvm::StringRef new_text = new_name; | |
718 if (expected_old_text.substr(1) == new_text.substr(1)) { | |
719 expected_old_text = expected_old_text.substr(0, 1); | |
720 new_text = new_text.substr(0, 1); | |
721 } | |
722 clang::SourceLocation spell = result.SourceManager->getSpellingLoc(loc); | |
723 clang::CharSourceRange range = clang::CharSourceRange::getCharRange( | |
724 spell, spell.getLocWithOffset(expected_old_text.size())); | |
dcheng
2016/12/22 11:35:04
It's probably worth comparing the entire original
Łukasz Anforowicz
2016/12/22 17:59:30
Comparing the entire |old_name| is wrong - it will
dcheng
2016/12/22 21:32:41
I don't think doing this hurts, so we may as well
Łukasz Anforowicz
2016/12/23 00:17:48
I've decided to avoid minimization:
1) it keeps th
| |
725 | |
726 // We need to ensure that |actual_old_text| is the same as | |
727 // |expected_old_text| - it can be different if |actual_old_text| represents | |
728 // a macro argument (see DEFINE_WITH_TOKEN_CONCATENATION2 in | |
729 // macros-original.cc testcase). | |
730 StringRef actual_old_text = clang::Lexer::getSourceText( | |
731 range, *result.SourceManager, result.Context->getLangOpts()); | |
732 if (actual_old_text != expected_old_text) | |
733 return; | |
734 | |
735 replacements_->emplace(*result.SourceManager, range, new_text); | |
697 replacement_names_.emplace(old_name.str(), std::move(new_name)); | 736 replacement_names_.emplace(old_name.str(), std::move(new_name)); |
698 } | 737 } |
699 | 738 |
700 const std::unordered_map<std::string, std::string>& replacement_names() | 739 const std::unordered_map<std::string, std::string>& replacement_names() |
701 const { | 740 const { |
702 return replacement_names_; | 741 return replacement_names_; |
703 } | 742 } |
704 | 743 |
705 private: | 744 private: |
706 std::set<Replacement>* const replacements_; | 745 std::set<Replacement>* const replacements_; |
707 std::unordered_map<std::string, std::string> replacement_names_; | 746 std::unordered_map<std::string, std::string> replacement_names_; |
708 }; | 747 }; |
709 | 748 |
710 template <typename DeclNode, typename TargetNode> | 749 template <typename DeclNode, typename TargetNode> |
711 class DeclRewriterBase : public RewriterBase<TargetNode> { | 750 class DeclRewriterBase : public RewriterBase<TargetNode> { |
712 public: | 751 public: |
713 using Base = RewriterBase<TargetNode>; | 752 using Base = RewriterBase<TargetNode>; |
714 | 753 |
715 explicit DeclRewriterBase(std::set<Replacement>* replacements) | 754 explicit DeclRewriterBase(std::set<Replacement>* replacements) |
716 : Base(replacements) {} | 755 : Base(replacements) {} |
717 | 756 |
718 void run(const MatchFinder::MatchResult& result) override { | 757 void run(const MatchFinder::MatchResult& result) override { |
719 const DeclNode* decl = result.Nodes.getNodeAs<DeclNode>("decl"); | 758 const DeclNode* decl = result.Nodes.getNodeAs<DeclNode>("decl"); |
720 assert(decl); | 759 assert(decl); |
721 // If false, there's no name to be renamed. | 760 // If false, there's no name to be renamed. |
722 if (!decl->getIdentifier()) | 761 if (!decl->getIdentifier()) |
723 return; | 762 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; | 763 clang::ASTContext* context = result.Context; |
737 std::string new_name; | 764 std::string new_name; |
738 if (!GetNameForDecl(*decl, *context, new_name)) | 765 if (!GetNameForDecl(*decl, *context, new_name)) |
739 return; // If false, the name was not suitable for renaming. | 766 return; // If false, the name was not suitable for renaming. |
740 llvm::StringRef old_name = decl->getName(); | 767 llvm::StringRef old_name = decl->getName(); |
741 Base::AddReplacement(result, old_name, std::move(new_name)); | 768 Base::AddReplacement(result, old_name, std::move(new_name)); |
742 } | 769 } |
743 }; | 770 }; |
744 | 771 |
745 using FieldDeclRewriter = DeclRewriterBase<clang::FieldDecl, clang::NamedDecl>; | 772 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) { | 1350 for (const auto& r : replacements) { |
1324 std::string replacement_text = r.getReplacementText().str(); | 1351 std::string replacement_text = r.getReplacementText().str(); |
1325 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); | 1352 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); |
1326 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() | 1353 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() |
1327 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; | 1354 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; |
1328 } | 1355 } |
1329 llvm::outs() << "==== END EDITS ====\n"; | 1356 llvm::outs() << "==== END EDITS ====\n"; |
1330 | 1357 |
1331 return 0; | 1358 return 0; |
1332 } | 1359 } |
OLD | NEW |