FileRemapper.cpp 8.2 KB

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