| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #include "EditTracker.h" | 5 #include "EditTracker.h" |
| 6 | 6 |
| 7 #include <assert.h> | 7 #include <assert.h> |
| 8 #include <stdio.h> | 8 #include <stdio.h> |
| 9 #include "llvm/Support/Path.h" | 9 #include "llvm/Support/Path.h" |
| 10 #include "llvm/Support/raw_ostream.h" | 10 #include "llvm/Support/raw_ostream.h" |
| 11 | 11 |
| 12 namespace { |
| 13 |
| 14 const char* GetTag(RenameCategory category) { |
| 15 switch (category) { |
| 16 case RenameCategory::kEnumValue: |
| 17 return "enum"; |
| 18 case RenameCategory::kField: |
| 19 return "var"; |
| 20 case RenameCategory::kFunction: |
| 21 return "func"; |
| 22 case RenameCategory::kUnresolved: |
| 23 return "unresolved"; |
| 24 case RenameCategory::kVariable: |
| 25 return "var"; |
| 26 } |
| 27 } |
| 28 |
| 29 } // namespace |
| 30 |
| 31 EditTracker::EditTracker(RenameCategory category) : category_(category) {} |
| 32 |
| 12 void EditTracker::Add(const clang::SourceManager& source_manager, | 33 void EditTracker::Add(const clang::SourceManager& source_manager, |
| 13 clang::SourceLocation location, | 34 clang::SourceLocation location, |
| 14 llvm::StringRef original_text, | 35 llvm::StringRef original_text, |
| 15 llvm::StringRef new_text) { | 36 llvm::StringRef new_text) { |
| 16 llvm::StringRef filename; | 37 llvm::StringRef filename; |
| 17 for (int i = 0; i < 10; i++) { | 38 for (int i = 0; i < 10; i++) { |
| 18 filename = source_manager.getFilename(location); | 39 filename = source_manager.getFilename(location); |
| 19 if (!filename.empty() || !location.isMacroID()) | 40 if (!filename.empty() || !location.isMacroID()) |
| 20 break; | 41 break; |
| 21 // Otherwise, no filename and the SourceLocation is a macro ID. Look one | 42 // Otherwise, no filename and the SourceLocation is a macro ID. Look one |
| 22 // level up the stack... | 43 // level up the stack... |
| 23 location = source_manager.getImmediateMacroCallerLoc(location); | 44 location = source_manager.getImmediateMacroCallerLoc(location); |
| 24 } | 45 } |
| 25 assert(!filename.empty() && "Can't track edit with no filename!"); | 46 assert(!filename.empty() && "Can't track edit with no filename!"); |
| 26 auto result = tracked_edits_.try_emplace(original_text); | 47 auto result = tracked_edits_.try_emplace(original_text); |
| 27 if (result.second) { | 48 if (result.second) { |
| 28 result.first->getValue().new_text = new_text; | 49 result.first->getValue().new_text = new_text; |
| 29 } | 50 } |
| 30 result.first->getValue().filenames.try_emplace(filename); | 51 result.first->getValue().filenames.try_emplace(filename); |
| 31 } | 52 } |
| 32 | 53 |
| 33 void EditTracker::SerializeTo(llvm::StringRef tag, | 54 void EditTracker::SerializeTo(llvm::raw_ostream& output) const { |
| 34 llvm::raw_ostream& output) const { | 55 const char* tag = GetTag(category_); |
| 35 for (const auto& edit : tracked_edits_) { | 56 for (const auto& edit : tracked_edits_) { |
| 36 for (const auto& filename : edit.getValue().filenames) { | 57 for (const auto& filename : edit.getValue().filenames) { |
| 37 output << filename.getKey() << ":" << tag << ":" << edit.getKey() << ":" | 58 output << filename.getKey() << ":" << tag << ":" << edit.getKey() << ":" |
| 38 << edit.getValue().new_text << "\n"; | 59 << edit.getValue().new_text << "\n"; |
| 39 } | 60 } |
| 40 } | 61 } |
| 41 } | 62 } |
| OLD | NEW |