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 3 // found in the LICENSE file. 4 5 #include "EditTracker.h" 6 7 #include <assert.h> 8 #include <stdio.h> 9 #include "llvm/Support/Path.h" 10 #include "llvm/Support/raw_ostream.h" 11 Add(const clang::SourceManager & source_manager,clang::SourceLocation location,llvm::StringRef original_text,llvm::StringRef new_text)12void EditTracker::Add(const clang::SourceManager& source_manager, 13 clang::SourceLocation location, 14 llvm::StringRef original_text, 15 llvm::StringRef new_text) { 16 llvm::StringRef filename; 17 for (int i = 0; i < 10; i++) { 18 filename = source_manager.getFilename(location); 19 if (!filename.empty() || !location.isMacroID()) 20 break; 21 // Otherwise, no filename and the SourceLocation is a macro ID. Look one 22 // level up the stack... 23 location = source_manager.getImmediateMacroCallerLoc(location); 24 } 25 assert(!filename.empty() && "Can't track edit with no filename!"); 26 auto result = tracked_edits_.try_emplace(original_text); 27 if (result.second) { 28 result.first->getValue().new_text = new_text; 29 } 30 result.first->getValue().filenames.try_emplace(filename); 31 } 32 SerializeTo(llvm::StringRef tag,llvm::raw_ostream & output) const33void EditTracker::SerializeTo(llvm::StringRef tag, 34 llvm::raw_ostream& output) const { 35 for (const auto& edit : tracked_edits_) { 36 for (const auto& filename : edit.getValue().filenames) { 37 output << filename.getKey() << ":" << tag << ":" << edit.getKey() << ":" 38 << edit.getValue().new_text << "\n"; 39 } 40 } 41 } 42