FileRemapper.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. if (!llvm::sys::fs::exists(infoFile))
  54. return false;
  55. std::vector<std::pair<const FileEntry *, const FileEntry *> > pairs;
  56. llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> fileBuf =
  57. llvm::MemoryBuffer::getFile(infoFile.c_str());
  58. if (!fileBuf)
  59. return report("Error opening file: " + infoFile, Diag);
  60. SmallVector<StringRef, 64> lines;
  61. fileBuf.get()->getBuffer().split(lines, "\n");
  62. for (unsigned idx = 0; idx+3 <= lines.size(); idx += 3) {
  63. StringRef fromFilename = lines[idx];
  64. unsigned long long timeModified;
  65. if (lines[idx+1].getAsInteger(10, timeModified))
  66. return report("Invalid file data: '" + lines[idx+1] + "' not a number",
  67. Diag);
  68. StringRef toFilename = lines[idx+2];
  69. const FileEntry *origFE = FileMgr->getFile(fromFilename);
  70. if (!origFE) {
  71. if (ignoreIfFilesChanged)
  72. continue;
  73. return report("File does not exist: " + fromFilename, Diag);
  74. }
  75. const FileEntry *newFE = FileMgr->getFile(toFilename);
  76. if (!newFE) {
  77. if (ignoreIfFilesChanged)
  78. continue;
  79. return report("File does not exist: " + toFilename, Diag);
  80. }
  81. if ((uint64_t)origFE->getModificationTime() != timeModified) {
  82. if (ignoreIfFilesChanged)
  83. continue;
  84. return report("File was modified: " + fromFilename, Diag);
  85. }
  86. pairs.push_back(std::make_pair(origFE, newFE));
  87. }
  88. for (unsigned i = 0, e = pairs.size(); i != e; ++i)
  89. remap(pairs[i].first, pairs[i].second);
  90. return false;
  91. }
  92. bool FileRemapper::flushToDisk(StringRef outputDir, DiagnosticsEngine &Diag) {
  93. using namespace llvm::sys;
  94. if (fs::create_directory(outputDir))
  95. return report("Could not create directory: " + outputDir, Diag);
  96. std::string infoFile = getRemapInfoFile(outputDir);
  97. return flushToFile(infoFile, Diag);
  98. }
  99. bool FileRemapper::flushToFile(StringRef outputPath, DiagnosticsEngine &Diag) {
  100. using namespace llvm::sys;
  101. std::error_code EC;
  102. std::string infoFile = outputPath;
  103. llvm::raw_fd_ostream infoOut(infoFile, EC, llvm::sys::fs::F_None);
  104. if (EC)
  105. return report(EC.message(), Diag);
  106. for (MappingsTy::iterator
  107. I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
  108. const FileEntry *origFE = I->first;
  109. SmallString<200> origPath = StringRef(origFE->getName());
  110. fs::make_absolute(origPath);
  111. infoOut << origPath << '\n';
  112. infoOut << (uint64_t)origFE->getModificationTime() << '\n';
  113. if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
  114. SmallString<200> newPath = StringRef(FE->getName());
  115. fs::make_absolute(newPath);
  116. infoOut << newPath << '\n';
  117. } else {
  118. SmallString<64> tempPath;
  119. int fd;
  120. if (fs::createTemporaryFile(path::filename(origFE->getName()),
  121. path::extension(origFE->getName()), fd,
  122. tempPath))
  123. return report("Could not create file: " + tempPath.str(), Diag);
  124. llvm::raw_fd_ostream newOut(fd, /*shouldClose=*/true);
  125. llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
  126. newOut.write(mem->getBufferStart(), mem->getBufferSize());
  127. newOut.close();
  128. const FileEntry *newE = FileMgr->getFile(tempPath);
  129. remap(origFE, newE);
  130. infoOut << newE->getName() << '\n';
  131. }
  132. }
  133. infoOut.close();
  134. return false;
  135. }
  136. bool FileRemapper::overwriteOriginal(DiagnosticsEngine &Diag,
  137. StringRef outputDir) {
  138. using namespace llvm::sys;
  139. for (MappingsTy::iterator
  140. I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
  141. const FileEntry *origFE = I->first;
  142. assert(I->second.is<llvm::MemoryBuffer *>());
  143. if (!fs::exists(origFE->getName()))
  144. return report(StringRef("File does not exist: ") + origFE->getName(),
  145. Diag);
  146. std::error_code EC;
  147. llvm::raw_fd_ostream Out(origFE->getName(), EC, llvm::sys::fs::F_None);
  148. if (EC)
  149. return report(EC.message(), Diag);
  150. llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
  151. Out.write(mem->getBufferStart(), mem->getBufferSize());
  152. Out.close();
  153. }
  154. clear(outputDir);
  155. return false;
  156. }
  157. void FileRemapper::applyMappings(PreprocessorOptions &PPOpts) const {
  158. for (MappingsTy::const_iterator
  159. I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
  160. if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
  161. PPOpts.addRemappedFile(I->first->getName(), FE->getName());
  162. } else {
  163. llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
  164. PPOpts.addRemappedFile(I->first->getName(), mem);
  165. }
  166. }
  167. PPOpts.RetainRemappedFileBuffers = true;
  168. }
  169. void FileRemapper::remap(StringRef filePath,
  170. std::unique_ptr<llvm::MemoryBuffer> memBuf) {
  171. remap(getOriginalFile(filePath), std::move(memBuf));
  172. }
  173. void FileRemapper::remap(const FileEntry *file,
  174. std::unique_ptr<llvm::MemoryBuffer> memBuf) {
  175. assert(file);
  176. Target &targ = FromToMappings[file];
  177. resetTarget(targ);
  178. targ = memBuf.release();
  179. }
  180. void FileRemapper::remap(const FileEntry *file, const FileEntry *newfile) {
  181. assert(file && newfile);
  182. Target &targ = FromToMappings[file];
  183. resetTarget(targ);
  184. targ = newfile;
  185. ToFromMappings[newfile] = file;
  186. }
  187. const FileEntry *FileRemapper::getOriginalFile(StringRef filePath) {
  188. const FileEntry *file = FileMgr->getFile(filePath);
  189. // If we are updating a file that overriden an original file,
  190. // actually update the original file.
  191. llvm::DenseMap<const FileEntry *, const FileEntry *>::iterator
  192. I = ToFromMappings.find(file);
  193. if (I != ToFromMappings.end()) {
  194. file = I->second;
  195. assert(FromToMappings.find(file) != FromToMappings.end() &&
  196. "Original file not in mappings!");
  197. }
  198. return file;
  199. }
  200. void FileRemapper::resetTarget(Target &targ) {
  201. if (!targ)
  202. return;
  203. if (llvm::MemoryBuffer *oldmem = targ.dyn_cast<llvm::MemoryBuffer *>()) {
  204. delete oldmem;
  205. } else {
  206. const FileEntry *toFE = targ.get<const FileEntry *>();
  207. ToFromMappings.erase(toFE);
  208. }
  209. }
  210. bool FileRemapper::report(const Twine &err, DiagnosticsEngine &Diag) {
  211. Diag.Report(Diag.getCustomDiagID(DiagnosticsEngine::Error, "%0"))
  212. << err.str();
  213. return true;
  214. }