John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 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 | |
| 10 | #include "clang/ARCMigrate/FileRemapper.h" |
| 11 | #include "clang/Frontend/CompilerInvocation.h" |
| 12 | #include "clang/Basic/FileManager.h" |
| 13 | #include "llvm/Support/MemoryBuffer.h" |
| 14 | #include "llvm/Support/Path.h" |
| 15 | #include "llvm/Support/FileSystem.h" |
| 16 | #include "llvm/Support/raw_ostream.h" |
| 17 | #include <fstream> |
| 18 | |
| 19 | using namespace clang; |
| 20 | using namespace arcmt; |
| 21 | |
| 22 | FileRemapper::FileRemapper() { |
| 23 | FileMgr.reset(new FileManager(FileSystemOptions())); |
| 24 | } |
| 25 | |
| 26 | FileRemapper::~FileRemapper() { |
| 27 | clear(); |
| 28 | } |
| 29 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 30 | void FileRemapper::clear(StringRef outputDir) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 31 | for (MappingsTy::iterator |
| 32 | I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) |
| 33 | resetTarget(I->second); |
| 34 | FromToMappings.clear(); |
| 35 | assert(ToFromMappings.empty()); |
| 36 | if (!outputDir.empty()) { |
| 37 | std::string infoFile = getRemapInfoFile(outputDir); |
| 38 | bool existed; |
| 39 | llvm::sys::fs::remove(infoFile, existed); |
| 40 | } |
| 41 | } |
| 42 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 43 | std::string FileRemapper::getRemapInfoFile(StringRef outputDir) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 44 | assert(!outputDir.empty()); |
| 45 | llvm::sys::Path dir(outputDir); |
| 46 | llvm::sys::Path infoFile = dir; |
| 47 | infoFile.appendComponent("remap"); |
| 48 | return infoFile.str(); |
| 49 | } |
| 50 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 51 | bool FileRemapper::initFromDisk(StringRef outputDir, DiagnosticsEngine &Diag, |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 52 | bool ignoreIfFilesChanged) { |
| 53 | assert(FromToMappings.empty() && |
| 54 | "initFromDisk should be called before any remap calls"); |
| 55 | std::string infoFile = getRemapInfoFile(outputDir); |
| 56 | bool fileExists = false; |
| 57 | llvm::sys::fs::exists(infoFile, fileExists); |
| 58 | if (!fileExists) |
| 59 | return false; |
| 60 | |
| 61 | std::vector<std::pair<const FileEntry *, const FileEntry *> > pairs; |
Argyrios Kyrtzidis | 56773de | 2011-08-26 22:40:55 +0000 | [diff] [blame] | 62 | |
| 63 | llvm::OwningPtr<llvm::MemoryBuffer> fileBuf; |
Rafael Espindola | 8d2a701 | 2011-12-25 01:18:52 +0000 | [diff] [blame] | 64 | if (llvm::MemoryBuffer::getFile(infoFile.c_str(), fileBuf)) |
Benjamin Kramer | 6748ae15 | 2011-08-27 00:34:29 +0000 | [diff] [blame] | 65 | return report("Error opening file: " + infoFile, Diag); |
Argyrios Kyrtzidis | 56773de | 2011-08-26 22:40:55 +0000 | [diff] [blame] | 66 | |
| 67 | SmallVector<StringRef, 64> lines; |
| 68 | fileBuf->getBuffer().split(lines, "\n"); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 69 | |
Argyrios Kyrtzidis | d37c67b | 2011-08-26 23:20:23 +0000 | [diff] [blame] | 70 | for (unsigned idx = 0; idx+3 <= lines.size(); idx += 3) { |
Benjamin Kramer | 6748ae15 | 2011-08-27 00:34:29 +0000 | [diff] [blame] | 71 | StringRef fromFilename = lines[idx]; |
Douglas Gregor | 484e2b1 | 2011-08-26 23:57:29 +0000 | [diff] [blame] | 72 | unsigned long long timeModified; |
Argyrios Kyrtzidis | 56773de | 2011-08-26 22:40:55 +0000 | [diff] [blame] | 73 | lines[idx+1].getAsInteger(10, timeModified); |
Benjamin Kramer | 6748ae15 | 2011-08-27 00:34:29 +0000 | [diff] [blame] | 74 | StringRef toFilename = lines[idx+2]; |
Argyrios Kyrtzidis | 56773de | 2011-08-26 22:40:55 +0000 | [diff] [blame] | 75 | |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 76 | const FileEntry *origFE = FileMgr->getFile(fromFilename); |
| 77 | if (!origFE) { |
| 78 | if (ignoreIfFilesChanged) |
| 79 | continue; |
Benjamin Kramer | 6748ae15 | 2011-08-27 00:34:29 +0000 | [diff] [blame] | 80 | return report("File does not exist: " + fromFilename, Diag); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 81 | } |
| 82 | const FileEntry *newFE = FileMgr->getFile(toFilename); |
| 83 | if (!newFE) { |
| 84 | if (ignoreIfFilesChanged) |
| 85 | continue; |
Benjamin Kramer | 6748ae15 | 2011-08-27 00:34:29 +0000 | [diff] [blame] | 86 | return report("File does not exist: " + toFilename, Diag); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | if ((uint64_t)origFE->getModificationTime() != timeModified) { |
| 90 | if (ignoreIfFilesChanged) |
| 91 | continue; |
Benjamin Kramer | 6748ae15 | 2011-08-27 00:34:29 +0000 | [diff] [blame] | 92 | return report("File was modified: " + fromFilename, Diag); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | pairs.push_back(std::make_pair(origFE, newFE)); |
| 96 | } |
| 97 | |
| 98 | for (unsigned i = 0, e = pairs.size(); i != e; ++i) |
| 99 | remap(pairs[i].first, pairs[i].second); |
| 100 | |
| 101 | return false; |
| 102 | } |
| 103 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 104 | bool FileRemapper::flushToDisk(StringRef outputDir, DiagnosticsEngine &Diag) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 105 | using namespace llvm::sys; |
| 106 | |
| 107 | bool existed; |
| 108 | if (fs::create_directory(outputDir, existed) != llvm::errc::success) |
Benjamin Kramer | 6748ae15 | 2011-08-27 00:34:29 +0000 | [diff] [blame] | 109 | return report("Could not create directory: " + outputDir, Diag); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 110 | |
| 111 | std::string errMsg; |
| 112 | std::string infoFile = getRemapInfoFile(outputDir); |
Argyrios Kyrtzidis | b709639 | 2011-07-10 18:57:15 +0000 | [diff] [blame] | 113 | llvm::raw_fd_ostream infoOut(infoFile.c_str(), errMsg, |
| 114 | llvm::raw_fd_ostream::F_Binary); |
Argyrios Kyrtzidis | 55a8a8d | 2011-07-10 18:57:18 +0000 | [diff] [blame] | 115 | if (!errMsg.empty()) |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 116 | return report(errMsg, Diag); |
| 117 | |
| 118 | for (MappingsTy::iterator |
| 119 | I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) { |
| 120 | |
| 121 | const FileEntry *origFE = I->first; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 122 | llvm::SmallString<200> origPath = StringRef(origFE->getName()); |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 123 | fs::make_absolute(origPath); |
| 124 | infoOut << origPath << '\n'; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 125 | infoOut << (uint64_t)origFE->getModificationTime() << '\n'; |
| 126 | |
| 127 | if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 128 | llvm::SmallString<200> newPath = StringRef(FE->getName()); |
Argyrios Kyrtzidis | 69325d5 | 2011-07-09 20:00:58 +0000 | [diff] [blame] | 129 | fs::make_absolute(newPath); |
| 130 | infoOut << newPath << '\n'; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 131 | } else { |
| 132 | |
| 133 | llvm::SmallString<64> tempPath; |
| 134 | tempPath = path::filename(origFE->getName()); |
| 135 | tempPath += "-%%%%%%%%"; |
| 136 | tempPath += path::extension(origFE->getName()); |
| 137 | int fd; |
| 138 | if (fs::unique_file(tempPath.str(), fd, tempPath) != llvm::errc::success) |
Benjamin Kramer | 6748ae15 | 2011-08-27 00:34:29 +0000 | [diff] [blame] | 139 | return report("Could not create file: " + tempPath.str(), Diag); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 140 | |
| 141 | llvm::raw_fd_ostream newOut(fd, /*shouldClose=*/true); |
| 142 | llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>(); |
| 143 | newOut.write(mem->getBufferStart(), mem->getBufferSize()); |
| 144 | newOut.close(); |
| 145 | |
| 146 | const FileEntry *newE = FileMgr->getFile(tempPath); |
| 147 | remap(origFE, newE); |
| 148 | infoOut << newE->getName() << '\n'; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | infoOut.close(); |
| 153 | return false; |
| 154 | } |
| 155 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 156 | bool FileRemapper::overwriteOriginal(DiagnosticsEngine &Diag, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 157 | StringRef outputDir) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 158 | using namespace llvm::sys; |
| 159 | |
| 160 | for (MappingsTy::iterator |
| 161 | I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) { |
| 162 | const FileEntry *origFE = I->first; |
| 163 | if (const FileEntry *newFE = I->second.dyn_cast<const FileEntry *>()) { |
| 164 | if (fs::copy_file(newFE->getName(), origFE->getName(), |
Benjamin Kramer | 6748ae15 | 2011-08-27 00:34:29 +0000 | [diff] [blame] | 165 | fs::copy_option::overwrite_if_exists) != llvm::errc::success) |
| 166 | return report(StringRef("Could not copy file '") + newFE->getName() + |
| 167 | "' to file '" + origFE->getName() + "'", Diag); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 168 | } else { |
| 169 | |
| 170 | bool fileExists = false; |
| 171 | fs::exists(origFE->getName(), fileExists); |
| 172 | if (!fileExists) |
Benjamin Kramer | 6748ae15 | 2011-08-27 00:34:29 +0000 | [diff] [blame] | 173 | return report(StringRef("File does not exist: ") + origFE->getName(), |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 174 | Diag); |
| 175 | |
| 176 | std::string errMsg; |
| 177 | llvm::raw_fd_ostream Out(origFE->getName(), errMsg, |
| 178 | llvm::raw_fd_ostream::F_Binary); |
Argyrios Kyrtzidis | 55a8a8d | 2011-07-10 18:57:18 +0000 | [diff] [blame] | 179 | if (!errMsg.empty()) |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 180 | return report(errMsg, Diag); |
| 181 | |
| 182 | llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>(); |
| 183 | Out.write(mem->getBufferStart(), mem->getBufferSize()); |
| 184 | Out.close(); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | clear(outputDir); |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | void FileRemapper::applyMappings(CompilerInvocation &CI) const { |
| 193 | PreprocessorOptions &PPOpts = CI.getPreprocessorOpts(); |
| 194 | for (MappingsTy::const_iterator |
| 195 | I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) { |
| 196 | if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) { |
| 197 | PPOpts.addRemappedFile(I->first->getName(), FE->getName()); |
| 198 | } else { |
| 199 | llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>(); |
| 200 | PPOpts.addRemappedFile(I->first->getName(), mem); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | PPOpts.RetainRemappedFileBuffers = true; |
| 205 | } |
| 206 | |
| 207 | void FileRemapper::transferMappingsAndClear(CompilerInvocation &CI) { |
| 208 | PreprocessorOptions &PPOpts = CI.getPreprocessorOpts(); |
| 209 | for (MappingsTy::iterator |
| 210 | I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) { |
| 211 | if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) { |
| 212 | PPOpts.addRemappedFile(I->first->getName(), FE->getName()); |
| 213 | } else { |
| 214 | llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>(); |
| 215 | PPOpts.addRemappedFile(I->first->getName(), mem); |
| 216 | } |
| 217 | I->second = Target(); |
| 218 | } |
| 219 | |
| 220 | PPOpts.RetainRemappedFileBuffers = false; |
| 221 | clear(); |
| 222 | } |
| 223 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 224 | void FileRemapper::remap(StringRef filePath, llvm::MemoryBuffer *memBuf) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 225 | remap(getOriginalFile(filePath), memBuf); |
| 226 | } |
| 227 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 228 | void FileRemapper::remap(StringRef filePath, StringRef newPath) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 229 | const FileEntry *file = getOriginalFile(filePath); |
| 230 | const FileEntry *newfile = FileMgr->getFile(newPath); |
| 231 | remap(file, newfile); |
| 232 | } |
| 233 | |
| 234 | void FileRemapper::remap(const FileEntry *file, llvm::MemoryBuffer *memBuf) { |
| 235 | assert(file); |
| 236 | Target &targ = FromToMappings[file]; |
| 237 | resetTarget(targ); |
| 238 | targ = memBuf; |
| 239 | } |
| 240 | |
| 241 | void FileRemapper::remap(const FileEntry *file, const FileEntry *newfile) { |
| 242 | assert(file && newfile); |
| 243 | Target &targ = FromToMappings[file]; |
| 244 | resetTarget(targ); |
| 245 | targ = newfile; |
| 246 | ToFromMappings[newfile] = file; |
| 247 | } |
| 248 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 249 | const FileEntry *FileRemapper::getOriginalFile(StringRef filePath) { |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 250 | const FileEntry *file = FileMgr->getFile(filePath); |
| 251 | // If we are updating a file that overriden an original file, |
| 252 | // actually update the original file. |
| 253 | llvm::DenseMap<const FileEntry *, const FileEntry *>::iterator |
| 254 | I = ToFromMappings.find(file); |
| 255 | if (I != ToFromMappings.end()) { |
| 256 | file = I->second; |
| 257 | assert(FromToMappings.find(file) != FromToMappings.end() && |
| 258 | "Original file not in mappings!"); |
| 259 | } |
| 260 | return file; |
| 261 | } |
| 262 | |
| 263 | void FileRemapper::resetTarget(Target &targ) { |
| 264 | if (!targ) |
| 265 | return; |
| 266 | |
| 267 | if (llvm::MemoryBuffer *oldmem = targ.dyn_cast<llvm::MemoryBuffer *>()) { |
| 268 | delete oldmem; |
| 269 | } else { |
| 270 | const FileEntry *toFE = targ.get<const FileEntry *>(); |
| 271 | llvm::DenseMap<const FileEntry *, const FileEntry *>::iterator |
| 272 | I = ToFromMappings.find(toFE); |
| 273 | if (I != ToFromMappings.end()) |
| 274 | ToFromMappings.erase(I); |
| 275 | } |
| 276 | } |
| 277 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 278 | bool FileRemapper::report(const Twine &err, DiagnosticsEngine &Diag) { |
Benjamin Kramer | 6748ae15 | 2011-08-27 00:34:29 +0000 | [diff] [blame] | 279 | llvm::SmallString<128> buf; |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 280 | unsigned ID = Diag.getDiagnosticIDs()->getCustomDiagID(DiagnosticIDs::Error, |
Benjamin Kramer | 6748ae15 | 2011-08-27 00:34:29 +0000 | [diff] [blame] | 281 | err.toStringRef(buf)); |
John McCall | 8f0e8d2 | 2011-06-15 23:25:17 +0000 | [diff] [blame] | 282 | Diag.Report(ID); |
| 283 | return true; |
| 284 | } |