blob: 14fa179d4bdd940720ed3360c0e05ea3de2462d5 [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"
Ted Kremenek30660a82012-03-06 20:06:33 +000011#include "clang/Basic/Diagnostic.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000012#include "clang/Basic/FileManager.h"
13#include "clang/Lex/PreprocessorOptions.h"
14#include "llvm/Support/FileSystem.h"
John McCall8f0e8d22011-06-15 23:25:17 +000015#include "llvm/Support/MemoryBuffer.h"
16#include "llvm/Support/Path.h"
Rafael Espindola34392372013-06-11 19:59:07 +000017#include "llvm/Support/PathV1.h"
John McCall8f0e8d22011-06-15 23:25:17 +000018#include "llvm/Support/raw_ostream.h"
19#include <fstream>
20
21using namespace clang;
22using namespace arcmt;
23
24FileRemapper::FileRemapper() {
25 FileMgr.reset(new FileManager(FileSystemOptions()));
26}
27
28FileRemapper::~FileRemapper() {
29 clear();
30}
31
Chris Lattner5f9e2722011-07-23 10:55:15 +000032void FileRemapper::clear(StringRef outputDir) {
John McCall8f0e8d22011-06-15 23:25:17 +000033 for (MappingsTy::iterator
34 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I)
35 resetTarget(I->second);
36 FromToMappings.clear();
37 assert(ToFromMappings.empty());
38 if (!outputDir.empty()) {
39 std::string infoFile = getRemapInfoFile(outputDir);
40 bool existed;
41 llvm::sys::fs::remove(infoFile, existed);
42 }
43}
44
Chris Lattner5f9e2722011-07-23 10:55:15 +000045std::string FileRemapper::getRemapInfoFile(StringRef outputDir) {
John McCall8f0e8d22011-06-15 23:25:17 +000046 assert(!outputDir.empty());
47 llvm::sys::Path dir(outputDir);
48 llvm::sys::Path infoFile = dir;
49 infoFile.appendComponent("remap");
50 return infoFile.str();
51}
52
David Blaikied6471f72011-09-25 23:23:43 +000053bool FileRemapper::initFromDisk(StringRef outputDir, DiagnosticsEngine &Diag,
John McCall8f0e8d22011-06-15 23:25:17 +000054 bool ignoreIfFilesChanged) {
Ted Kremenek30660a82012-03-06 20:06:33 +000055 std::string infoFile = getRemapInfoFile(outputDir);
56 return initFromFile(infoFile, Diag, ignoreIfFilesChanged);
57}
58
59bool FileRemapper::initFromFile(StringRef filePath, DiagnosticsEngine &Diag,
60 bool ignoreIfFilesChanged) {
John McCall8f0e8d22011-06-15 23:25:17 +000061 assert(FromToMappings.empty() &&
62 "initFromDisk should be called before any remap calls");
Ted Kremenek30660a82012-03-06 20:06:33 +000063 std::string infoFile = filePath;
John McCall8f0e8d22011-06-15 23:25:17 +000064 bool fileExists = false;
65 llvm::sys::fs::exists(infoFile, fileExists);
66 if (!fileExists)
67 return false;
68
69 std::vector<std::pair<const FileEntry *, const FileEntry *> > pairs;
Argyrios Kyrtzidis56773de2011-08-26 22:40:55 +000070
Dylan Noblesmith6f42b622012-02-05 02:12:40 +000071 OwningPtr<llvm::MemoryBuffer> fileBuf;
Rafael Espindola8d2a7012011-12-25 01:18:52 +000072 if (llvm::MemoryBuffer::getFile(infoFile.c_str(), fileBuf))
Benjamin Kramer6748ae152011-08-27 00:34:29 +000073 return report("Error opening file: " + infoFile, Diag);
Argyrios Kyrtzidis56773de2011-08-26 22:40:55 +000074
75 SmallVector<StringRef, 64> lines;
76 fileBuf->getBuffer().split(lines, "\n");
John McCall8f0e8d22011-06-15 23:25:17 +000077
Argyrios Kyrtzidisd37c67b2011-08-26 23:20:23 +000078 for (unsigned idx = 0; idx+3 <= lines.size(); idx += 3) {
Benjamin Kramer6748ae152011-08-27 00:34:29 +000079 StringRef fromFilename = lines[idx];
Douglas Gregor484e2b12011-08-26 23:57:29 +000080 unsigned long long timeModified;
David Blaikie95109d22012-05-01 02:45:39 +000081 if (lines[idx+1].getAsInteger(10, timeModified))
82 return report("Invalid file data: '" + lines[idx+1] + "' not a number",
83 Diag);
Benjamin Kramer6748ae152011-08-27 00:34:29 +000084 StringRef toFilename = lines[idx+2];
Argyrios Kyrtzidis56773de2011-08-26 22:40:55 +000085
John McCall8f0e8d22011-06-15 23:25:17 +000086 const FileEntry *origFE = FileMgr->getFile(fromFilename);
87 if (!origFE) {
88 if (ignoreIfFilesChanged)
89 continue;
Benjamin Kramer6748ae152011-08-27 00:34:29 +000090 return report("File does not exist: " + fromFilename, Diag);
John McCall8f0e8d22011-06-15 23:25:17 +000091 }
92 const FileEntry *newFE = FileMgr->getFile(toFilename);
93 if (!newFE) {
94 if (ignoreIfFilesChanged)
95 continue;
Benjamin Kramer6748ae152011-08-27 00:34:29 +000096 return report("File does not exist: " + toFilename, Diag);
John McCall8f0e8d22011-06-15 23:25:17 +000097 }
98
99 if ((uint64_t)origFE->getModificationTime() != timeModified) {
100 if (ignoreIfFilesChanged)
101 continue;
Benjamin Kramer6748ae152011-08-27 00:34:29 +0000102 return report("File was modified: " + fromFilename, Diag);
John McCall8f0e8d22011-06-15 23:25:17 +0000103 }
104
105 pairs.push_back(std::make_pair(origFE, newFE));
106 }
107
108 for (unsigned i = 0, e = pairs.size(); i != e; ++i)
109 remap(pairs[i].first, pairs[i].second);
110
111 return false;
112}
113
David Blaikied6471f72011-09-25 23:23:43 +0000114bool FileRemapper::flushToDisk(StringRef outputDir, DiagnosticsEngine &Diag) {
John McCall8f0e8d22011-06-15 23:25:17 +0000115 using namespace llvm::sys;
116
117 bool existed;
118 if (fs::create_directory(outputDir, existed) != llvm::errc::success)
Benjamin Kramer6748ae152011-08-27 00:34:29 +0000119 return report("Could not create directory: " + outputDir, Diag);
John McCall8f0e8d22011-06-15 23:25:17 +0000120
John McCall8f0e8d22011-06-15 23:25:17 +0000121 std::string infoFile = getRemapInfoFile(outputDir);
Ted Kremenek30660a82012-03-06 20:06:33 +0000122 return flushToFile(infoFile, Diag);
123}
124
125bool FileRemapper::flushToFile(StringRef outputPath, DiagnosticsEngine &Diag) {
126 using namespace llvm::sys;
127
128 std::string errMsg;
129 std::string infoFile = outputPath;
Argyrios Kyrtzidisb7096392011-07-10 18:57:15 +0000130 llvm::raw_fd_ostream infoOut(infoFile.c_str(), errMsg,
131 llvm::raw_fd_ostream::F_Binary);
Argyrios Kyrtzidis55a8a8d2011-07-10 18:57:18 +0000132 if (!errMsg.empty())
John McCall8f0e8d22011-06-15 23:25:17 +0000133 return report(errMsg, Diag);
134
135 for (MappingsTy::iterator
136 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
137
138 const FileEntry *origFE = I->first;
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000139 SmallString<200> origPath = StringRef(origFE->getName());
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000140 fs::make_absolute(origPath);
141 infoOut << origPath << '\n';
John McCall8f0e8d22011-06-15 23:25:17 +0000142 infoOut << (uint64_t)origFE->getModificationTime() << '\n';
143
144 if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000145 SmallString<200> newPath = StringRef(FE->getName());
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000146 fs::make_absolute(newPath);
147 infoOut << newPath << '\n';
John McCall8f0e8d22011-06-15 23:25:17 +0000148 } else {
149
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000150 SmallString<64> tempPath;
John McCall8f0e8d22011-06-15 23:25:17 +0000151 tempPath = path::filename(origFE->getName());
152 tempPath += "-%%%%%%%%";
153 tempPath += path::extension(origFE->getName());
154 int fd;
155 if (fs::unique_file(tempPath.str(), fd, tempPath) != llvm::errc::success)
Benjamin Kramer6748ae152011-08-27 00:34:29 +0000156 return report("Could not create file: " + tempPath.str(), Diag);
John McCall8f0e8d22011-06-15 23:25:17 +0000157
158 llvm::raw_fd_ostream newOut(fd, /*shouldClose=*/true);
159 llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
160 newOut.write(mem->getBufferStart(), mem->getBufferSize());
161 newOut.close();
162
163 const FileEntry *newE = FileMgr->getFile(tempPath);
164 remap(origFE, newE);
165 infoOut << newE->getName() << '\n';
166 }
167 }
168
169 infoOut.close();
170 return false;
171}
172
David Blaikied6471f72011-09-25 23:23:43 +0000173bool FileRemapper::overwriteOriginal(DiagnosticsEngine &Diag,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000174 StringRef outputDir) {
John McCall8f0e8d22011-06-15 23:25:17 +0000175 using namespace llvm::sys;
176
177 for (MappingsTy::iterator
178 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
179 const FileEntry *origFE = I->first;
180 if (const FileEntry *newFE = I->second.dyn_cast<const FileEntry *>()) {
181 if (fs::copy_file(newFE->getName(), origFE->getName(),
Benjamin Kramer6748ae152011-08-27 00:34:29 +0000182 fs::copy_option::overwrite_if_exists) != llvm::errc::success)
183 return report(StringRef("Could not copy file '") + newFE->getName() +
184 "' to file '" + origFE->getName() + "'", Diag);
John McCall8f0e8d22011-06-15 23:25:17 +0000185 } else {
186
187 bool fileExists = false;
188 fs::exists(origFE->getName(), fileExists);
189 if (!fileExists)
Benjamin Kramer6748ae152011-08-27 00:34:29 +0000190 return report(StringRef("File does not exist: ") + origFE->getName(),
John McCall8f0e8d22011-06-15 23:25:17 +0000191 Diag);
192
193 std::string errMsg;
194 llvm::raw_fd_ostream Out(origFE->getName(), errMsg,
195 llvm::raw_fd_ostream::F_Binary);
Argyrios Kyrtzidis55a8a8d2011-07-10 18:57:18 +0000196 if (!errMsg.empty())
John McCall8f0e8d22011-06-15 23:25:17 +0000197 return report(errMsg, Diag);
198
199 llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
200 Out.write(mem->getBufferStart(), mem->getBufferSize());
201 Out.close();
202 }
203 }
204
205 clear(outputDir);
206 return false;
207}
208
Ted Kremenek30660a82012-03-06 20:06:33 +0000209void FileRemapper::applyMappings(PreprocessorOptions &PPOpts) const {
John McCall8f0e8d22011-06-15 23:25:17 +0000210 for (MappingsTy::const_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 }
219
220 PPOpts.RetainRemappedFileBuffers = true;
221}
222
Ted Kremenek30660a82012-03-06 20:06:33 +0000223void FileRemapper::transferMappingsAndClear(PreprocessorOptions &PPOpts) {
John McCall8f0e8d22011-06-15 23:25:17 +0000224 for (MappingsTy::iterator
225 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
226 if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
227 PPOpts.addRemappedFile(I->first->getName(), FE->getName());
228 } else {
229 llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
230 PPOpts.addRemappedFile(I->first->getName(), mem);
231 }
232 I->second = Target();
233 }
234
235 PPOpts.RetainRemappedFileBuffers = false;
236 clear();
237}
238
Chris Lattner5f9e2722011-07-23 10:55:15 +0000239void FileRemapper::remap(StringRef filePath, llvm::MemoryBuffer *memBuf) {
John McCall8f0e8d22011-06-15 23:25:17 +0000240 remap(getOriginalFile(filePath), memBuf);
241}
242
Chris Lattner5f9e2722011-07-23 10:55:15 +0000243void FileRemapper::remap(StringRef filePath, StringRef newPath) {
John McCall8f0e8d22011-06-15 23:25:17 +0000244 const FileEntry *file = getOriginalFile(filePath);
245 const FileEntry *newfile = FileMgr->getFile(newPath);
246 remap(file, newfile);
247}
248
249void FileRemapper::remap(const FileEntry *file, llvm::MemoryBuffer *memBuf) {
250 assert(file);
251 Target &targ = FromToMappings[file];
252 resetTarget(targ);
253 targ = memBuf;
254}
255
256void FileRemapper::remap(const FileEntry *file, const FileEntry *newfile) {
257 assert(file && newfile);
258 Target &targ = FromToMappings[file];
259 resetTarget(targ);
260 targ = newfile;
261 ToFromMappings[newfile] = file;
262}
263
Chris Lattner5f9e2722011-07-23 10:55:15 +0000264const FileEntry *FileRemapper::getOriginalFile(StringRef filePath) {
John McCall8f0e8d22011-06-15 23:25:17 +0000265 const FileEntry *file = FileMgr->getFile(filePath);
266 // If we are updating a file that overriden an original file,
267 // actually update the original file.
268 llvm::DenseMap<const FileEntry *, const FileEntry *>::iterator
269 I = ToFromMappings.find(file);
270 if (I != ToFromMappings.end()) {
271 file = I->second;
272 assert(FromToMappings.find(file) != FromToMappings.end() &&
273 "Original file not in mappings!");
274 }
275 return file;
276}
277
278void FileRemapper::resetTarget(Target &targ) {
279 if (!targ)
280 return;
281
282 if (llvm::MemoryBuffer *oldmem = targ.dyn_cast<llvm::MemoryBuffer *>()) {
283 delete oldmem;
284 } else {
285 const FileEntry *toFE = targ.get<const FileEntry *>();
Benjamin Kramer7e423922012-03-24 18:22:12 +0000286 ToFromMappings.erase(toFE);
John McCall8f0e8d22011-06-15 23:25:17 +0000287 }
288}
289
David Blaikied6471f72011-09-25 23:23:43 +0000290bool FileRemapper::report(const Twine &err, DiagnosticsEngine &Diag) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000291 SmallString<128> buf;
John McCall8f0e8d22011-06-15 23:25:17 +0000292 unsigned ID = Diag.getDiagnosticIDs()->getCustomDiagID(DiagnosticIDs::Error,
Benjamin Kramer6748ae152011-08-27 00:34:29 +0000293 err.toStringRef(buf));
John McCall8f0e8d22011-06-15 23:25:17 +0000294 Diag.Report(ID);
295 return true;
296}