FileRemapper.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 std::string(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 = std::string(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, /*IsText=*/true);
  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 = std::string(outputPath);
  102. llvm::raw_fd_ostream infoOut(infoFile, EC, llvm::sys::fs::OF_Text);
  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(
  120. path::filename(origFE->getName()),
  121. path::extension(origFE->getName()).drop_front(), fd, tempPath,
  122. llvm::sys::fs::OF_Text))
  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. auto newE = FileMgr->getFile(tempPath);
  129. if (newE) {
  130. remap(origFE, *newE);
  131. infoOut << (*newE)->getName() << '\n';
  132. }
  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. if (!fs::exists(origFE->getName()))
  146. return report(StringRef("File does not exist: ") + origFE->getName(),
  147. Diag);
  148. std::error_code EC;
  149. llvm::raw_fd_ostream Out(origFE->getName(), EC, llvm::sys::fs::OF_None);
  150. if (EC)
  151. return report(EC.message(), Diag);
  152. llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
  153. Out.write(mem->getBufferStart(), mem->getBufferSize());
  154. Out.close();
  155. }
  156. clear(outputDir);
  157. return false;
  158. }
  159. void FileRemapper::forEachMapping(
  160. llvm::function_ref<void(StringRef, StringRef)> CaptureFile,
  161. llvm::function_ref<void(StringRef, const llvm::MemoryBufferRef &)>
  162. CaptureBuffer) const {
  163. for (auto &Mapping : FromToMappings) {
  164. if (const FileEntry *FE = Mapping.second.dyn_cast<const FileEntry *>()) {
  165. CaptureFile(Mapping.first->getName(), FE->getName());
  166. continue;
  167. }
  168. CaptureBuffer(
  169. Mapping.first->getName(),
  170. Mapping.second.get<llvm::MemoryBuffer *>()->getMemBufferRef());
  171. }
  172. }
  173. void FileRemapper::applyMappings(PreprocessorOptions &PPOpts) const {
  174. for (MappingsTy::const_iterator
  175. I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
  176. if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
  177. PPOpts.addRemappedFile(I->first->getName(), FE->getName());
  178. } else {
  179. llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
  180. PPOpts.addRemappedFile(I->first->getName(), mem);
  181. }
  182. }
  183. PPOpts.RetainRemappedFileBuffers = true;
  184. }
  185. void FileRemapper::remap(StringRef filePath,
  186. std::unique_ptr<llvm::MemoryBuffer> memBuf) {
  187. remap(getOriginalFile(filePath), std::move(memBuf));
  188. }
  189. void FileRemapper::remap(const FileEntry *file,
  190. std::unique_ptr<llvm::MemoryBuffer> memBuf) {
  191. assert(file);
  192. Target &targ = FromToMappings[file];
  193. resetTarget(targ);
  194. targ = memBuf.release();
  195. }
  196. void FileRemapper::remap(const FileEntry *file, const FileEntry *newfile) {
  197. assert(file && newfile);
  198. Target &targ = FromToMappings[file];
  199. resetTarget(targ);
  200. targ = newfile;
  201. ToFromMappings[newfile] = file;
  202. }
  203. const FileEntry *FileRemapper::getOriginalFile(StringRef filePath) {
  204. const FileEntry *file = nullptr;
  205. if (auto fileOrErr = FileMgr->getFile(filePath))
  206. file = *fileOrErr;
  207. // If we are updating a file that overridden an original file,
  208. // actually update the original file.
  209. llvm::DenseMap<const FileEntry *, const FileEntry *>::iterator
  210. I = ToFromMappings.find(file);
  211. if (I != ToFromMappings.end()) {
  212. file = I->second;
  213. assert(FromToMappings.find(file) != FromToMappings.end() &&
  214. "Original file not in mappings!");
  215. }
  216. return file;
  217. }
  218. void FileRemapper::resetTarget(Target &targ) {
  219. if (!targ)
  220. return;
  221. if (llvm::MemoryBuffer *oldmem = targ.dyn_cast<llvm::MemoryBuffer *>()) {
  222. delete oldmem;
  223. } else {
  224. const FileEntry *toFE = targ.get<const FileEntry *>();
  225. ToFromMappings.erase(toFE);
  226. }
  227. }
  228. bool FileRemapper::report(const Twine &err, DiagnosticsEngine &Diag) {
  229. Diag.Report(Diag.getCustomDiagID(DiagnosticsEngine::Error, "%0"))
  230. << err.str();
  231. return true;
  232. }