blob: 39c9aada4dbfbad72061f5d7cb108a16f41076ce [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;
Rafael Espindola8d2a7012011-12-25 01:18:52 +000064 if (llvm::MemoryBuffer::getFile(infoFile.c_str(), fileBuf))
Benjamin Kramer6748ae152011-08-27 00:34:29 +000065 return report("Error opening file: " + infoFile, Diag);
Argyrios Kyrtzidis56773de2011-08-26 22:40:55 +000066
67 SmallVector<StringRef, 64> lines;
68 fileBuf->getBuffer().split(lines, "\n");
John McCall8f0e8d22011-06-15 23:25:17 +000069
Argyrios Kyrtzidisd37c67b2011-08-26 23:20:23 +000070 for (unsigned idx = 0; idx+3 <= lines.size(); idx += 3) {
Benjamin Kramer6748ae152011-08-27 00:34:29 +000071 StringRef fromFilename = lines[idx];
Douglas Gregor484e2b12011-08-26 23:57:29 +000072 unsigned long long timeModified;
Argyrios Kyrtzidis56773de2011-08-26 22:40:55 +000073 lines[idx+1].getAsInteger(10, timeModified);
Benjamin Kramer6748ae152011-08-27 00:34:29 +000074 StringRef toFilename = lines[idx+2];
Argyrios Kyrtzidis56773de2011-08-26 22:40:55 +000075
John McCall8f0e8d22011-06-15 23:25:17 +000076 const FileEntry *origFE = FileMgr->getFile(fromFilename);
77 if (!origFE) {
78 if (ignoreIfFilesChanged)
79 continue;
Benjamin Kramer6748ae152011-08-27 00:34:29 +000080 return report("File does not exist: " + fromFilename, Diag);
John McCall8f0e8d22011-06-15 23:25:17 +000081 }
82 const FileEntry *newFE = FileMgr->getFile(toFilename);
83 if (!newFE) {
84 if (ignoreIfFilesChanged)
85 continue;
Benjamin Kramer6748ae152011-08-27 00:34:29 +000086 return report("File does not exist: " + toFilename, Diag);
John McCall8f0e8d22011-06-15 23:25:17 +000087 }
88
89 if ((uint64_t)origFE->getModificationTime() != timeModified) {
90 if (ignoreIfFilesChanged)
91 continue;
Benjamin Kramer6748ae152011-08-27 00:34:29 +000092 return report("File was modified: " + fromFilename, Diag);
John McCall8f0e8d22011-06-15 23:25:17 +000093 }
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 Blaikied6471f72011-09-25 23:23:43 +0000104bool FileRemapper::flushToDisk(StringRef outputDir, DiagnosticsEngine &Diag) {
John McCall8f0e8d22011-06-15 23:25:17 +0000105 using namespace llvm::sys;
106
107 bool existed;
108 if (fs::create_directory(outputDir, existed) != llvm::errc::success)
Benjamin Kramer6748ae152011-08-27 00:34:29 +0000109 return report("Could not create directory: " + outputDir, Diag);
John McCall8f0e8d22011-06-15 23:25:17 +0000110
111 std::string errMsg;
112 std::string infoFile = getRemapInfoFile(outputDir);
Argyrios Kyrtzidisb7096392011-07-10 18:57:15 +0000113 llvm::raw_fd_ostream infoOut(infoFile.c_str(), errMsg,
114 llvm::raw_fd_ostream::F_Binary);
Argyrios Kyrtzidis55a8a8d2011-07-10 18:57:18 +0000115 if (!errMsg.empty())
John McCall8f0e8d22011-06-15 23:25:17 +0000116 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 Lattner5f9e2722011-07-23 10:55:15 +0000122 llvm::SmallString<200> origPath = StringRef(origFE->getName());
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000123 fs::make_absolute(origPath);
124 infoOut << origPath << '\n';
John McCall8f0e8d22011-06-15 23:25:17 +0000125 infoOut << (uint64_t)origFE->getModificationTime() << '\n';
126
127 if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000128 llvm::SmallString<200> newPath = StringRef(FE->getName());
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000129 fs::make_absolute(newPath);
130 infoOut << newPath << '\n';
John McCall8f0e8d22011-06-15 23:25:17 +0000131 } 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 Kramer6748ae152011-08-27 00:34:29 +0000139 return report("Could not create file: " + tempPath.str(), Diag);
John McCall8f0e8d22011-06-15 23:25:17 +0000140
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 Blaikied6471f72011-09-25 23:23:43 +0000156bool FileRemapper::overwriteOriginal(DiagnosticsEngine &Diag,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000157 StringRef outputDir) {
John McCall8f0e8d22011-06-15 23:25:17 +0000158 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 Kramer6748ae152011-08-27 00:34:29 +0000165 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 McCall8f0e8d22011-06-15 23:25:17 +0000168 } else {
169
170 bool fileExists = false;
171 fs::exists(origFE->getName(), fileExists);
172 if (!fileExists)
Benjamin Kramer6748ae152011-08-27 00:34:29 +0000173 return report(StringRef("File does not exist: ") + origFE->getName(),
John McCall8f0e8d22011-06-15 23:25:17 +0000174 Diag);
175
176 std::string errMsg;
177 llvm::raw_fd_ostream Out(origFE->getName(), errMsg,
178 llvm::raw_fd_ostream::F_Binary);
Argyrios Kyrtzidis55a8a8d2011-07-10 18:57:18 +0000179 if (!errMsg.empty())
John McCall8f0e8d22011-06-15 23:25:17 +0000180 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
192void 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
207void 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 Lattner5f9e2722011-07-23 10:55:15 +0000224void FileRemapper::remap(StringRef filePath, llvm::MemoryBuffer *memBuf) {
John McCall8f0e8d22011-06-15 23:25:17 +0000225 remap(getOriginalFile(filePath), memBuf);
226}
227
Chris Lattner5f9e2722011-07-23 10:55:15 +0000228void FileRemapper::remap(StringRef filePath, StringRef newPath) {
John McCall8f0e8d22011-06-15 23:25:17 +0000229 const FileEntry *file = getOriginalFile(filePath);
230 const FileEntry *newfile = FileMgr->getFile(newPath);
231 remap(file, newfile);
232}
233
234void FileRemapper::remap(const FileEntry *file, llvm::MemoryBuffer *memBuf) {
235 assert(file);
236 Target &targ = FromToMappings[file];
237 resetTarget(targ);
238 targ = memBuf;
239}
240
241void 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 Lattner5f9e2722011-07-23 10:55:15 +0000249const FileEntry *FileRemapper::getOriginalFile(StringRef filePath) {
John McCall8f0e8d22011-06-15 23:25:17 +0000250 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
263void 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 Blaikied6471f72011-09-25 23:23:43 +0000278bool FileRemapper::report(const Twine &err, DiagnosticsEngine &Diag) {
Benjamin Kramer6748ae152011-08-27 00:34:29 +0000279 llvm::SmallString<128> buf;
John McCall8f0e8d22011-06-15 23:25:17 +0000280 unsigned ID = Diag.getDiagnosticIDs()->getCustomDiagID(DiagnosticIDs::Error,
Benjamin Kramer6748ae152011-08-27 00:34:29 +0000281 err.toStringRef(buf));
John McCall8f0e8d22011-06-15 23:25:17 +0000282 Diag.Report(ID);
283 return true;
284}