FileRemapper.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //===--- FileRemapper.cpp - File Remapping Helper -------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "clang/ARCMigrate/FileRemapper.h"
  10. #include "clang/Basic/Diagnostic.h"
  11. #include "clang/Basic/FileManager.h"
  12. #include "clang/Lex/PreprocessorOptions.h"
  13. #include "llvm/Support/FileSystem.h"
  14. #include "llvm/Support/MemoryBuffer.h"
  15. #include "llvm/Support/Path.h"
  16. #include "llvm/Support/raw_ostream.h"
  17. #include <fstream>
  18. using namespace clang;
  19. using namespace arcmt;
  20. FileRemapper::FileRemapper() {
  21. FileMgr.reset(new FileManager(FileSystemOptions()));
  22. }
  23. FileRemapper::~FileRemapper() {
  24. clear();
  25. }
  26. void FileRemapper::clear(StringRef outputDir) {
  27. for (MappingsTy::iterator
  28. I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I)
  29. resetTarget(I->second);
  30. FromToMappings.clear();
  31. assert(ToFromMappings.empty());
  32. if (!outputDir.empty()) {
  33. std::string infoFile = getRemapInfoFile(outputDir);
  34. llvm::sys::fs::remove(infoFile);
  35. }
  36. }
  37. std::string FileRemapper::getRemapInfoFile(StringRef outputDir) {
  38. assert(!outputDir.empty());
  39. SmallString<128> InfoFile = outputDir;
  40. llvm::sys::path::append(InfoFile, "remap");
  41. return InfoFile.str();
  42. }
  43. bool FileRemapper::initFromDisk(StringRef outputDir, DiagnosticsEngine &Diag,
  44. bool ignoreIfFilesChanged) {
  45. std::string infoFile = getRemapInfoFile(outputDir);
  46. return initFromFile(infoFile, Diag, ignoreIfFilesChanged);
  47. }
  48. bool FileRemapper::initFromFile(StringRef filePath, DiagnosticsEngine &Diag,
  49. bool ignoreIfFilesChanged) {
  50. assert(FromToMappings.empty() &&
  51. "initFromDisk should be called before any remap calls");
  52. std::string infoFile = filePath;
  53. bool fileExists = false;
  54. llvm::sys::fs::exists(infoFile, fileExists);
  55. if (!fileExists)
  56. return false;
  57. std::vector<std::pair<const FileEntry *, const FileEntry *> > pairs;
  58. llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> fileBuf =
  59. llvm::MemoryBuffer::getFile(infoFile.c_str());
  60. if (!fileBuf)
  61. return report("Error opening file: " + infoFile, Diag);
  62. SmallVector<StringRef, 64> lines;
  63. fileBuf.get()->getBuffer().split(lines, "\n");
  64. for (unsigned idx = 0; idx+3 <= lines.size(); idx += 3) {
  65. StringRef fromFilename = lines[idx];
  66. unsigned long long timeModified;
  67. if (lines[idx+1].getAsInteger(10, timeModified))
  68. return report("Invalid file data: '" + lines[idx+1] + "' not a number",
  69. Diag);
  70. StringRef toFilename = lines[idx+2];
  71. const FileEntry *origFE = FileMgr->getFile(fromFilename);
  72. if (!origFE) {
  73. if (ignoreIfFilesChanged)
  74. continue;
  75. return report("File does not exist: " + fromFilename, Diag);
  76. }
  77. const FileEntry *newFE = FileMgr->getFile(toFilename);
  78. if (!newFE) {
  79. if (ignoreIfFilesChanged)
  80. continue;
  81. return report("File does not exist: " + toFilename, Diag);
  82. }
  83. if ((uint64_t)origFE->getModificationTime() != timeModified) {
  84. if (ignoreIfFilesChanged)
  85. continue;
  86. return report("File was modified: " + fromFilename, Diag);
  87. }
  88. pairs.push_back(std::make_pair(origFE, newFE));
  89. }
  90. for (unsigned i = 0, e = pairs.size(); i != e; ++i)
  91. remap(pairs[i].first, pairs[i].second);
  92. return false;
  93. }
  94. bool FileRemapper::flushToDisk(StringRef outputDir, DiagnosticsEngine &Diag) {
  95. using namespace llvm::sys;
  96. if (fs::create_directory(outputDir))
  97. return report("Could not create directory: " + outputDir, Diag);
  98. std::string infoFile = getRemapInfoFile(outputDir);
  99. return flushToFile(infoFile, Diag);
  100. }
  101. bool FileRemapper::flushToFile(StringRef outputPath, DiagnosticsEngine &Diag) {
  102. using namespace llvm::sys;
  103. std::error_code EC;
  104. std::string infoFile = outputPath;
  105. llvm::raw_fd_ostream infoOut(infoFile, EC, llvm::sys::fs::F_None);
  106. if (EC)
  107. return report(EC.message(), Diag);
  108. for (MappingsTy::iterator
  109. I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
  110. const FileEntry *origFE = I->first;
  111. SmallString<200> origPath = StringRef(origFE->getName());
  112. fs::make_absolute(origPath);
  113. infoOut << origPath << '\n';
  114. infoOut << (uint64_t)origFE->getModificationTime() << '\n';
  115. if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
  116. SmallString<200> newPath = StringRef(FE->getName());
  117. fs::make_absolute(newPath);
  118. infoOut << newPath << '\n';
  119. } else {
  120. SmallString<64> tempPath;
  121. int fd;
  122. if (fs::createTemporaryFile(path::filename(origFE->getName()),
  123. path::extension(origFE->getName()), fd,
  124. tempPath))
  125. return report("Could not create file: " + tempPath.str(), Diag);
  126. llvm::raw_fd_ostream newOut(fd, /*shouldClose=*/true);
  127. llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
  128. newOut.write(mem->getBufferStart(), mem->getBufferSize());
  129. newOut.close();
  130. const FileEntry *newE = FileMgr->getFile(tempPath);
  131. remap(origFE, newE);
  132. infoOut << newE->getName() << '\n';
  133. }
  134. }
  135. infoOut.close();
  136. return false;
  137. }
  138. bool FileRemapper::overwriteOriginal(DiagnosticsEngine &Diag,
  139. StringRef outputDir) {
  140. using namespace llvm::sys;
  141. for (MappingsTy::iterator
  142. I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
  143. const FileEntry *origFE = I->first;
  144. assert(I->second.is<llvm::MemoryBuffer *>());
  145. bool fileExists = false;
  146. fs::exists(origFE->getName(), fileExists);
  147. if (!fileExists)
  148. return report(StringRef("File does not exist: ") + origFE->getName(),
  149. Diag);
  150. std::error_code EC;
  151. llvm::raw_fd_ostream Out(origFE->getName(), EC, llvm::sys::fs::F_None);
  152. if (EC)
  153. return report(EC.message(), Diag);
  154. llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
  155. Out.write(mem->getBufferStart(), mem->getBufferSize());
  156. Out.close();
  157. }
  158. clear(outputDir);
  159. return false;
  160. }
  161. void FileRemapper::applyMappings(PreprocessorOptions &PPOpts) const {
  162. for (MappingsTy::const_iterator
  163. I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
  164. if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
  165. PPOpts.addRemappedFile(I->first->getName(), FE->getName());
  166. } else {
  167. llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
  168. PPOpts.addRemappedFile(I->first->getName(), mem);
  169. }
  170. }
  171. PPOpts.RetainRemappedFileBuffers = true;
  172. }
  173. void FileRemapper::remap(StringRef filePath,
  174. std::unique_ptr<llvm::MemoryBuffer> memBuf) {
  175. remap(getOriginalFile(filePath), std::move(memBuf));
  176. }
  177. void FileRemapper::remap(const FileEntry *file,
  178. std::unique_ptr<llvm::MemoryBuffer> memBuf) {
  179. assert(file);
  180. Target &targ = FromToMappings[file];
  181. resetTarget(targ);
  182. targ = memBuf.release();
  183. }
  184. void FileRemapper::remap(const FileEntry *file, const FileEntry *newfile) {
  185. assert(file && newfile);
  186. Target &targ = FromToMappings[file];
  187. resetTarget(targ);
  188. targ = newfile;
  189. ToFromMappings[newfile] = file;
  190. }
  191. const FileEntry *FileRemapper::getOriginalFile(StringRef filePath) {
  192. const FileEntry *file = FileMgr->getFile(filePath);
  193. // If we are updating a file that overriden an original file,
  194. // actually update the original file.
  195. llvm::DenseMap<const FileEntry *, const FileEntry *>::iterator
  196. I = ToFromMappings.find(file);
  197. if (I != ToFromMappings.end()) {
  198. file = I->second;
  199. assert(FromToMappings.find(file) != FromToMappings.end() &&
  200. "Original file not in mappings!");
  201. }
  202. return file;
  203. }
  204. void FileRemapper::resetTarget(Target &targ) {
  205. if (!targ)
  206. return;
  207. if (llvm::MemoryBuffer *oldmem = targ.dyn_cast<llvm::MemoryBuffer *>()) {
  208. delete oldmem;
  209. } else {
  210. const FileEntry *toFE = targ.get<const FileEntry *>();
  211. ToFromMappings.erase(toFE);
  212. }
  213. }
  214. bool FileRemapper::report(const Twine &err, DiagnosticsEngine &Diag) {
  215. Diag.Report(Diag.getCustomDiagID(DiagnosticsEngine::Error, "%0"))
  216. << err.str();
  217. return true;
  218. }