Chromium Code Reviews| 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 476 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 487 static const char* GetName() { return "expr"; } | 487 static const char* GetName() { return "expr"; } |
| 488 static const char* GetType() { return "UnresolvedMemberExpr"; } | 488 static const char* GetType() { return "UnresolvedMemberExpr"; } |
| 489 }; | 489 }; |
| 490 | 490 |
| 491 template <typename DeclNode, typename TargetNode> | 491 template <typename DeclNode, typename TargetNode> |
| 492 class RewriterBase : public MatchFinder::MatchCallback { | 492 class RewriterBase : public MatchFinder::MatchCallback { |
| 493 public: | 493 public: |
| 494 explicit RewriterBase(Replacements* replacements) | 494 explicit RewriterBase(Replacements* replacements) |
| 495 : replacements_(replacements) {} | 495 : replacements_(replacements) {} |
| 496 | 496 |
| 497 void addReplacementIfNeeded(const Replacement& new_replacement) { | |
| 498 // Ignore the replacement if it is a duplicate. | |
| 499 bool is_same_replacement_already_present = std::any_of( | |
| 500 replacements_->begin(), replacements_->end(), | |
| 501 [&new_replacement](const Replacement& old_replacement) { | |
| 502 return new_replacement.toString() == old_replacement.toString(); | |
|
Łukasz Anforowicz
2016/08/16 22:24:16
Replacement doesn't define ==. And Replacements d
| |
| 503 }); | |
| 504 if (is_same_replacement_already_present) | |
| 505 return; | |
|
Łukasz Anforowicz
2016/08/16 22:24:16
If we didn't check |is_same_replacement_already_pr
| |
| 506 | |
| 507 // Try to add the replacement | |
| 508 if (llvm::Error err = replacements_->add(new_replacement)) { | |
| 509 llvm::outs() << "Replacements::add failed: " | |
| 510 << toString(std::move(err)) << "\n"; | |
| 511 assert(false); // Order-dependent replacements? | |
| 512 return; | |
|
Łukasz Anforowicz
2016/08/16 22:24:16
I am not sure how I should spell a fatal failure..
| |
| 513 } | |
| 514 } | |
| 515 | |
| 497 void run(const MatchFinder::MatchResult& result) override { | 516 void run(const MatchFinder::MatchResult& result) override { |
| 498 const DeclNode* decl = result.Nodes.getNodeAs<DeclNode>("decl"); | 517 const DeclNode* decl = result.Nodes.getNodeAs<DeclNode>("decl"); |
| 499 // If false, there's no name to be renamed. | 518 // If false, there's no name to be renamed. |
| 500 if (!decl->getIdentifier()) | 519 if (!decl->getIdentifier()) |
| 501 return; | 520 return; |
| 502 clang::SourceLocation decl_loc = | 521 clang::SourceLocation decl_loc = |
| 503 TargetNodeTraits<clang::NamedDecl>::GetLoc(*decl); | 522 TargetNodeTraits<clang::NamedDecl>::GetLoc(*decl); |
| 504 if (decl_loc.isMacroID()) { | 523 if (decl_loc.isMacroID()) { |
| 505 // Get the location of the spelling of the declaration. If token pasting | 524 // Get the location of the spelling of the declaration. If token pasting |
| 506 // was used this will be in "scratch space" and we don't know how to get | 525 // was used this will be in "scratch space" and we don't know how to get |
| 507 // from there back to/ the actual macro with the foo##bar text. So just | 526 // from there back to/ the actual macro with the foo##bar text. So just |
| 508 // don't replace in that case. | 527 // don't replace in that case. |
| 509 clang::SourceLocation spell = | 528 clang::SourceLocation spell = |
| 510 result.SourceManager->getSpellingLoc(decl_loc); | 529 result.SourceManager->getSpellingLoc(decl_loc); |
| 511 if (strcmp(result.SourceManager->getBufferName(spell), | 530 if (strcmp(result.SourceManager->getBufferName(spell), |
| 512 "<scratch space>") == 0) | 531 "<scratch space>") == 0) |
| 513 return; | 532 return; |
| 514 } | 533 } |
| 515 clang::ASTContext* context = result.Context; | 534 clang::ASTContext* context = result.Context; |
| 516 std::string new_name; | 535 std::string new_name; |
| 517 if (!GetNameForDecl(*decl, *context, new_name)) | 536 if (!GetNameForDecl(*decl, *context, new_name)) |
| 518 return; // If false, the name was not suitable for renaming. | 537 return; // If false, the name was not suitable for renaming. |
| 519 llvm::StringRef old_name = decl->getName(); | 538 llvm::StringRef old_name = decl->getName(); |
| 520 if (old_name == new_name) | 539 if (old_name == new_name) |
| 521 return; | 540 return; |
| 522 clang::SourceLocation loc = TargetNodeTraits<TargetNode>::GetLoc( | 541 clang::SourceLocation loc = TargetNodeTraits<TargetNode>::GetLoc( |
| 523 *result.Nodes.getNodeAs<TargetNode>( | 542 *result.Nodes.getNodeAs<TargetNode>( |
| 524 TargetNodeTraits<TargetNode>::GetName())); | 543 TargetNodeTraits<TargetNode>::GetName())); |
| 525 clang::CharSourceRange range = clang::CharSourceRange::getTokenRange(loc); | 544 clang::CharSourceRange range = clang::CharSourceRange::getTokenRange(loc); |
| 526 replacements_->emplace(*result.SourceManager, range, new_name); | 545 addReplacementIfNeeded(Replacement(*result.SourceManager, range, new_name)); |
| 527 replacement_names_.emplace(old_name.str(), std::move(new_name)); | 546 replacement_names_.emplace(old_name.str(), std::move(new_name)); |
| 528 } | 547 } |
| 529 | 548 |
| 530 const std::unordered_map<std::string, std::string>& replacement_names() | 549 const std::unordered_map<std::string, std::string>& replacement_names() |
| 531 const { | 550 const { |
| 532 return replacement_names_; | 551 return replacement_names_; |
| 533 } | 552 } |
| 534 | 553 |
| 535 private: | 554 private: |
| 536 Replacements* const replacements_; | 555 Replacements* const replacements_; |
| (...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 880 for (const auto& r : replacements) { | 899 for (const auto& r : replacements) { |
| 881 std::string replacement_text = r.getReplacementText().str(); | 900 std::string replacement_text = r.getReplacementText().str(); |
| 882 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); | 901 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); |
| 883 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() | 902 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() |
| 884 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; | 903 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; |
| 885 } | 904 } |
| 886 llvm::outs() << "==== END EDITS ====\n"; | 905 llvm::outs() << "==== END EDITS ====\n"; |
| 887 | 906 |
| 888 return 0; | 907 return 0; |
| 889 } | 908 } |
| OLD | NEW |