blob: c6e6ce46d173f5e8b4cb0ef65815009110b3292e [file] [log] [blame]
John McCall8f0e8d22011-06-15 23:25:17 +00001//===--- 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
19using namespace clang;
20using namespace arcmt;
21
22FileRemapper::FileRemapper() {
23 FileMgr.reset(new FileManager(FileSystemOptions()));
24}
25
26FileRemapper::~FileRemapper() {
27 clear();
28}
29
Chris Lattner5f9e2722011-07-23 10:55:15 +000030void FileRemapper::clear(StringRef outputDir) {
John McCall8f0e8d22011-06-15 23:25:17 +000031 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 Lattner5f9e2722011-07-23 10:55:15 +000043std::string FileRemapper::getRemapInfoFile(StringRef outputDir) {
John McCall8f0e8d22011-06-15 23:25:17 +000044 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 Blaikied6471f72011-09-25 23:23:43 +000051bool FileRemapper::initFromDisk(StringRef outputDir, DiagnosticsEngine &Diag,
John McCall8f0e8d22011-06-15 23:25:17 +000052 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 Kyrtzidis56773de2011-08-26 22:40:55 +000062
63 llvm::OwningPtr<llvm::MemoryBuffer> fileBuf;
64 if (llvm::error_code ec = llvm::MemoryBuffer::getFile(infoFile.c_str(),
65 fileBuf))
Benjamin Kramer6748ae152011-08-27 00:34:29 +000066 return report("Error opening file: " + infoFile, Diag);
Argyrios Kyrtzidis56773de2011-08-26 22:40:55 +000067
68 SmallVector<StringRef, 64> lines;
69 fileBuf->getBuffer().split(lines, "\n");
John McCall8f0e8d22011-06-15 23:25:17 +000070
Argyrios Kyrtzidisd37c67b2011-08-26 23:20:23 +000071 for (unsigned idx = 0; idx+3 <= lines.size(); idx += 3) {
Benjamin Kramer6748ae152011-08-27 00:34:29 +000072 StringRef fromFilename = lines[idx];
Douglas Gregor484e2b12011-08-26 23:57:29 +000073 unsigned long long timeModified;
Argyrios Kyrtzidis56773de2011-08-26 22:40:55 +000074 lines[idx+1].getAsInteger(10, timeModified);
Benjamin Kramer6748ae152011-08-27 00:34:29 +000075 StringRef toFilename = lines[idx+2];
Argyrios Kyrtzidis56773de2011-08-26 22:40:55 +000076
John McCall8f0e8d22011-06-15 23:25:17 +000077 const FileEntry *origFE = FileMgr->getFile(fromFilename);
78 if (!origFE) {
79 if (ignoreIfFilesChanged)
80 continue;
Benjamin Kramer6748ae152011-08-27 00:34:29 +000081 return report("File does not exist: " + fromFilename, Diag);
John McCall8f0e8d22011-06-15 23:25:17 +000082 }
83 const FileEntry *newFE = FileMgr->getFile(toFilename);
84 if (!newFE) {
85 if (ignoreIfFilesChanged)
86 continue;
Benjamin Kramer6748ae152011-08-27 00:34:29 +000087 return report("File does not exist: " + toFilename, Diag);
John McCall8f0e8d22011-06-15 23:25:17 +000088 }
89
90 if ((uint64_t)origFE->getModificationTime() != timeModified) {
91 if (ignoreIfFilesChanged)
92 continue;
Benjamin Kramer6748ae152011-08-27 00:34:29 +000093 return report("File was modified: " + fromFilename, Diag);
John McCall8f0e8d22011-06-15 23:25:17 +000094 }
95
96 pairs.push_back(std::make_pair(origFE, newFE));
97 }
98
99 for (unsigned i = 0, e = pairs.size(); i != e; ++i)
100 remap(pairs[i].first, pairs[i].second);
101
102 return false;
103}
104
David Blaikied6471f72011-09-25 23:23:43 +0000105bool FileRemapper::flushToDisk(StringRef outputDir, DiagnosticsEngine &Diag) {
John McCall8f0e8d22011-06-15 23:25:17 +0000106 using namespace llvm::sys;
107
108 bool existed;
109 if (fs::create_directory(outputDir, existed) != llvm::errc::success)
Benjamin Kramer6748ae152011-08-27 00:34:29 +0000110 return report("Could not create directory: " + outputDir, Diag);
John McCall8f0e8d22011-06-15 23:25:17 +0000111
112 std::string errMsg;
113 std::string infoFile = getRemapInfoFile(outputDir);
Argyrios Kyrtzidisb7096392011-07-10 18:57:15 +0000114 llvm::raw_fd_ostream infoOut(infoFile.c_str(), errMsg,
115 llvm::raw_fd_ostream::F_Binary);
Argyrios Kyrtzidis55a8a8d2011-07-10 18:57:18 +0000116 if (!errMsg.empty())
John McCall8f0e8d22011-06-15 23:25:17 +0000117 return report(errMsg, Diag);
118
119 for (MappingsTy::iterator
120 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
121
122 const FileEntry *origFE = I->first;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000123 llvm::SmallString<200> origPath = StringRef(origFE->getName());
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000124 fs::make_absolute(origPath);
125 infoOut << origPath << '\n';
John McCall8f0e8d22011-06-15 23:25:17 +0000126 infoOut << (uint64_t)origFE->getModificationTime() << '\n';
127
128 if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000129 llvm::SmallString<200> newPath = StringRef(FE->getName());
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000130 fs::make_absolute(newPath);
131 infoOut << newPath << '\n';
John McCall8f0e8d22011-06-15 23:25:17 +0000132 } else {
133
134 llvm::SmallString<64> tempPath;
135 tempPath = path::filename(origFE->getName());
136 tempPath += "-%%%%%%%%";
137 tempPath += path::extension(origFE->getName());
138 int fd;
139 if (fs::unique_file(tempPath.str(), fd, tempPath) != llvm::errc::success)
Benjamin Kramer6748ae152011-08-27 00:34:29 +0000140 return report("Could not create file: " + tempPath.str(), Diag);
John McCall8f0e8d22011-06-15 23:25:17 +0000141
142 llvm::raw_fd_ostream newOut(fd, /*shouldClose=*/true);
143 llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
144 newOut.write(mem->getBufferStart(), mem->getBufferSize());
145 newOut.close();
146
147 const FileEntry *newE = FileMgr->getFile(tempPath);
148 remap(origFE, newE);
149 infoOut << newE->getName() << '\n';
150 }
151 }
152
153 infoOut.close();
154 return false;
155}
156
David Blaikied6471f72011-09-25 23:23:43 +0000157bool FileRemapper::overwriteOriginal(DiagnosticsEngine &Diag,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000158 StringRef outputDir) {
John McCall8f0e8d22011-06-15 23:25:17 +0000159 using namespace llvm::sys;
160
161 for (MappingsTy::iterator
162 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
163 const FileEntry *origFE = I->first;
164 if (const FileEntry *newFE = I->second.dyn_cast<const FileEntry *>()) {
165 if (fs::copy_file(newFE->getName(), origFE->getName(),
Benjamin Kramer6748ae152011-08-27 00:34:29 +0000166 fs::copy_option::overwrite_if_exists) != llvm::errc::success)
167 return report(StringRef("Could not copy file '") + newFE->getName() +
168 "' to file '" + origFE->getName() + "'", Diag);
John McCall8f0e8d22011-06-15 23:25:17 +0000169 } else {
170
171 bool fileExists = false;
172 fs::exists(origFE->getName(), fileExists);
173 if (!fileExists)
Benjamin Kramer6748ae152011-08-27 00:34:29 +0000174 return report(StringRef("File does not exist: ") + origFE->getName(),
John McCall8f0e8d22011-06-15 23:25:17 +0000175 Diag);
176
177 std::string errMsg;
178 llvm::raw_fd_ostream Out(origFE->getName(), errMsg,
179 llvm::raw_fd_ostream::F_Binary);
Argyrios Kyrtzidis55a8a8d2011-07-10 18:57:18 +0000180 if (!errMsg.empty())
John McCall8f0e8d22011-06-15 23:25:17 +0000181 return report(errMsg, Diag);
182
183 llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
184 Out.write(mem->getBufferStart(), mem->getBufferSize());
185 Out.close();
186 }
187 }
188
189 clear(outputDir);
190 return false;
191}
192
193void FileRemapper::applyMappings(CompilerInvocation &CI) const {
194 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
195 for (MappingsTy::const_iterator
196 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
197 if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
198 PPOpts.addRemappedFile(I->first->getName(), FE->getName());
199 } else {
200 llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
201 PPOpts.addRemappedFile(I->first->getName(), mem);
202 }
203 }
204
205 PPOpts.RetainRemappedFileBuffers = true;
206}
207
208void FileRemapper::transferMappingsAndClear(CompilerInvocation &CI) {
209 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
210 for (MappingsTy::iterator
211 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
212 if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
213 PPOpts.addRemappedFile(I->first->getName(), FE->getName());
214 } else {
215 llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
216 PPOpts.addRemappedFile(I->first->getName(), mem);
217 }
218 I->second = Target();
219 }
220
221 PPOpts.RetainRemappedFileBuffers = false;
222 clear();
223}
224
Chris Lattner5f9e2722011-07-23 10:55:15 +0000225void FileRemapper::remap(StringRef filePath, llvm::MemoryBuffer *memBuf) {
John McCall8f0e8d22011-06-15 23:25:17 +0000226 remap(getOriginalFile(filePath), memBuf);
227}
228
Chris Lattner5f9e2722011-07-23 10:55:15 +0000229void FileRemapper::remap(StringRef filePath, StringRef newPath) {
John McCall8f0e8d22011-06-15 23:25:17 +0000230 const FileEntry *file = getOriginalFile(filePath);
231 const FileEntry *newfile = FileMgr->getFile(newPath);
232 remap(file, newfile);
233}
234
235void FileRemapper::remap(const FileEntry *file, llvm::MemoryBuffer *memBuf) {
236 assert(file);
237 Target &targ = FromToMappings[file];
238 resetTarget(targ);
239 targ = memBuf;
240}
241
242void FileRemapper::remap(const FileEntry *file, const FileEntry *newfile) {
243 assert(file && newfile);
244 Target &targ = FromToMappings[file];
245 resetTarget(targ);
246 targ = newfile;
247 ToFromMappings[newfile] = file;
248}
249
Chris Lattner5f9e2722011-07-23 10:55:15 +0000250const FileEntry *FileRemapper::getOriginalFile(StringRef filePath) {
John McCall8f0e8d22011-06-15 23:25:17 +0000251 const FileEntry *file = FileMgr->getFile(filePath);
252 // If we are updating a file that overriden an original file,
253 // actually update the original file.
254 llvm::DenseMap<const FileEntry *, const FileEntry *>::iterator
255 I = ToFromMappings.find(file);
256 if (I != ToFromMappings.end()) {
257 file = I->second;
258 assert(FromToMappings.find(file) != FromToMappings.end() &&
259 "Original file not in mappings!");
260 }
261 return file;
262}
263
264void FileRemapper::resetTarget(Target &targ) {
265 if (!targ)
266 return;
267
268 if (llvm::MemoryBuffer *oldmem = targ.dyn_cast<llvm::MemoryBuffer *>()) {
269 delete oldmem;
270 } else {
271 const FileEntry *toFE = targ.get<const FileEntry *>();
272 llvm::DenseMap<const FileEntry *, const FileEntry *>::iterator
273 I = ToFromMappings.find(toFE);
274 if (I != ToFromMappings.end())
275 ToFromMappings.erase(I);
276 }
277}
278
David Blaikied6471f72011-09-25 23:23:43 +0000279bool FileRemapper::report(const Twine &err, DiagnosticsEngine &Diag) {
Benjamin Kramer6748ae152011-08-27 00:34:29 +0000280 llvm::SmallString<128> buf;
John McCall8f0e8d22011-06-15 23:25:17 +0000281 unsigned ID = Diag.getDiagnosticIDs()->getCustomDiagID(DiagnosticIDs::Error,
Benjamin Kramer6748ae152011-08-27 00:34:29 +0000282 err.toStringRef(buf));
John McCall8f0e8d22011-06-15 23:25:17 +0000283 Diag.Report(ID);
284 return true;
285}