blob: 159ab79b54377fa04cabfbba070376b60e750ad3 [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"
John McCall8f0e8d22011-06-15 23:25:17 +000017#include "llvm/Support/raw_ostream.h"
18#include <fstream>
19
20using namespace clang;
21using namespace arcmt;
22
23FileRemapper::FileRemapper() {
24 FileMgr.reset(new FileManager(FileSystemOptions()));
25}
26
27FileRemapper::~FileRemapper() {
28 clear();
29}
30
Chris Lattner5f9e2722011-07-23 10:55:15 +000031void FileRemapper::clear(StringRef outputDir) {
John McCall8f0e8d22011-06-15 23:25:17 +000032 for (MappingsTy::iterator
33 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I)
34 resetTarget(I->second);
35 FromToMappings.clear();
36 assert(ToFromMappings.empty());
37 if (!outputDir.empty()) {
38 std::string infoFile = getRemapInfoFile(outputDir);
39 bool existed;
40 llvm::sys::fs::remove(infoFile, existed);
41 }
42}
43
Chris Lattner5f9e2722011-07-23 10:55:15 +000044std::string FileRemapper::getRemapInfoFile(StringRef outputDir) {
John McCall8f0e8d22011-06-15 23:25:17 +000045 assert(!outputDir.empty());
Benjamin Kramer0b214902013-06-13 14:26:04 +000046 SmallString<128> InfoFile = outputDir;
47 llvm::sys::path::append(InfoFile, "remap");
48 return InfoFile.str();
John McCall8f0e8d22011-06-15 23:25:17 +000049}
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) {
Ted Kremenek30660a82012-03-06 20:06:33 +000053 std::string infoFile = getRemapInfoFile(outputDir);
54 return initFromFile(infoFile, Diag, ignoreIfFilesChanged);
55}
56
57bool FileRemapper::initFromFile(StringRef filePath, DiagnosticsEngine &Diag,
58 bool ignoreIfFilesChanged) {
John McCall8f0e8d22011-06-15 23:25:17 +000059 assert(FromToMappings.empty() &&
60 "initFromDisk should be called before any remap calls");
Ted Kremenek30660a82012-03-06 20:06:33 +000061 std::string infoFile = filePath;
John McCall8f0e8d22011-06-15 23:25:17 +000062 bool fileExists = false;
63 llvm::sys::fs::exists(infoFile, fileExists);
64 if (!fileExists)
65 return false;
66
67 std::vector<std::pair<const FileEntry *, const FileEntry *> > pairs;
Argyrios Kyrtzidis56773de2011-08-26 22:40:55 +000068
Dylan Noblesmith6f42b622012-02-05 02:12:40 +000069 OwningPtr<llvm::MemoryBuffer> fileBuf;
Rafael Espindola8d2a7012011-12-25 01:18:52 +000070 if (llvm::MemoryBuffer::getFile(infoFile.c_str(), fileBuf))
Benjamin Kramer6748ae152011-08-27 00:34:29 +000071 return report("Error opening file: " + infoFile, Diag);
Argyrios Kyrtzidis56773de2011-08-26 22:40:55 +000072
73 SmallVector<StringRef, 64> lines;
74 fileBuf->getBuffer().split(lines, "\n");
John McCall8f0e8d22011-06-15 23:25:17 +000075
Argyrios Kyrtzidisd37c67b2011-08-26 23:20:23 +000076 for (unsigned idx = 0; idx+3 <= lines.size(); idx += 3) {
Benjamin Kramer6748ae152011-08-27 00:34:29 +000077 StringRef fromFilename = lines[idx];
Douglas Gregor484e2b12011-08-26 23:57:29 +000078 unsigned long long timeModified;
David Blaikie95109d22012-05-01 02:45:39 +000079 if (lines[idx+1].getAsInteger(10, timeModified))
80 return report("Invalid file data: '" + lines[idx+1] + "' not a number",
81 Diag);
Benjamin Kramer6748ae152011-08-27 00:34:29 +000082 StringRef toFilename = lines[idx+2];
Argyrios Kyrtzidis56773de2011-08-26 22:40:55 +000083
John McCall8f0e8d22011-06-15 23:25:17 +000084 const FileEntry *origFE = FileMgr->getFile(fromFilename);
85 if (!origFE) {
86 if (ignoreIfFilesChanged)
87 continue;
Benjamin Kramer6748ae152011-08-27 00:34:29 +000088 return report("File does not exist: " + fromFilename, Diag);
John McCall8f0e8d22011-06-15 23:25:17 +000089 }
90 const FileEntry *newFE = FileMgr->getFile(toFilename);
91 if (!newFE) {
92 if (ignoreIfFilesChanged)
93 continue;
Benjamin Kramer6748ae152011-08-27 00:34:29 +000094 return report("File does not exist: " + toFilename, Diag);
John McCall8f0e8d22011-06-15 23:25:17 +000095 }
96
97 if ((uint64_t)origFE->getModificationTime() != timeModified) {
98 if (ignoreIfFilesChanged)
99 continue;
Benjamin Kramer6748ae152011-08-27 00:34:29 +0000100 return report("File was modified: " + fromFilename, Diag);
John McCall8f0e8d22011-06-15 23:25:17 +0000101 }
102
103 pairs.push_back(std::make_pair(origFE, newFE));
104 }
105
106 for (unsigned i = 0, e = pairs.size(); i != e; ++i)
107 remap(pairs[i].first, pairs[i].second);
108
109 return false;
110}
111
David Blaikied6471f72011-09-25 23:23:43 +0000112bool FileRemapper::flushToDisk(StringRef outputDir, DiagnosticsEngine &Diag) {
John McCall8f0e8d22011-06-15 23:25:17 +0000113 using namespace llvm::sys;
114
115 bool existed;
116 if (fs::create_directory(outputDir, existed) != llvm::errc::success)
Benjamin Kramer6748ae152011-08-27 00:34:29 +0000117 return report("Could not create directory: " + outputDir, Diag);
John McCall8f0e8d22011-06-15 23:25:17 +0000118
John McCall8f0e8d22011-06-15 23:25:17 +0000119 std::string infoFile = getRemapInfoFile(outputDir);
Ted Kremenek30660a82012-03-06 20:06:33 +0000120 return flushToFile(infoFile, Diag);
121}
122
123bool FileRemapper::flushToFile(StringRef outputPath, DiagnosticsEngine &Diag) {
124 using namespace llvm::sys;
125
126 std::string errMsg;
127 std::string infoFile = outputPath;
Argyrios Kyrtzidisb7096392011-07-10 18:57:15 +0000128 llvm::raw_fd_ostream infoOut(infoFile.c_str(), errMsg,
129 llvm::raw_fd_ostream::F_Binary);
Argyrios Kyrtzidis55a8a8d2011-07-10 18:57:18 +0000130 if (!errMsg.empty())
John McCall8f0e8d22011-06-15 23:25:17 +0000131 return report(errMsg, Diag);
132
133 for (MappingsTy::iterator
134 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
135
136 const FileEntry *origFE = I->first;
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000137 SmallString<200> origPath = StringRef(origFE->getName());
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000138 fs::make_absolute(origPath);
139 infoOut << origPath << '\n';
John McCall8f0e8d22011-06-15 23:25:17 +0000140 infoOut << (uint64_t)origFE->getModificationTime() << '\n';
141
142 if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000143 SmallString<200> newPath = StringRef(FE->getName());
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000144 fs::make_absolute(newPath);
145 infoOut << newPath << '\n';
John McCall8f0e8d22011-06-15 23:25:17 +0000146 } else {
147
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000148 SmallString<64> tempPath;
John McCall8f0e8d22011-06-15 23:25:17 +0000149 int fd;
Rafael Espindola1ec4a862013-07-05 20:00:06 +0000150 if (fs::createTemporaryFile(path::filename(origFE->getName()),
151 path::extension(origFE->getName()), fd,
152 tempPath))
Benjamin Kramer6748ae152011-08-27 00:34:29 +0000153 return report("Could not create file: " + tempPath.str(), Diag);
John McCall8f0e8d22011-06-15 23:25:17 +0000154
155 llvm::raw_fd_ostream newOut(fd, /*shouldClose=*/true);
156 llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
157 newOut.write(mem->getBufferStart(), mem->getBufferSize());
158 newOut.close();
159
160 const FileEntry *newE = FileMgr->getFile(tempPath);
161 remap(origFE, newE);
162 infoOut << newE->getName() << '\n';
163 }
164 }
165
166 infoOut.close();
167 return false;
168}
169
David Blaikied6471f72011-09-25 23:23:43 +0000170bool FileRemapper::overwriteOriginal(DiagnosticsEngine &Diag,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000171 StringRef outputDir) {
John McCall8f0e8d22011-06-15 23:25:17 +0000172 using namespace llvm::sys;
173
174 for (MappingsTy::iterator
175 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
176 const FileEntry *origFE = I->first;
177 if (const FileEntry *newFE = I->second.dyn_cast<const FileEntry *>()) {
178 if (fs::copy_file(newFE->getName(), origFE->getName(),
Benjamin Kramer6748ae152011-08-27 00:34:29 +0000179 fs::copy_option::overwrite_if_exists) != llvm::errc::success)
180 return report(StringRef("Could not copy file '") + newFE->getName() +
181 "' to file '" + origFE->getName() + "'", Diag);
John McCall8f0e8d22011-06-15 23:25:17 +0000182 } else {
183
184 bool fileExists = false;
185 fs::exists(origFE->getName(), fileExists);
186 if (!fileExists)
Benjamin Kramer6748ae152011-08-27 00:34:29 +0000187 return report(StringRef("File does not exist: ") + origFE->getName(),
John McCall8f0e8d22011-06-15 23:25:17 +0000188 Diag);
189
190 std::string errMsg;
191 llvm::raw_fd_ostream Out(origFE->getName(), errMsg,
192 llvm::raw_fd_ostream::F_Binary);
Argyrios Kyrtzidis55a8a8d2011-07-10 18:57:18 +0000193 if (!errMsg.empty())
John McCall8f0e8d22011-06-15 23:25:17 +0000194 return report(errMsg, Diag);
195
196 llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
197 Out.write(mem->getBufferStart(), mem->getBufferSize());
198 Out.close();
199 }
200 }
201
202 clear(outputDir);
203 return false;
204}
205
Ted Kremenek30660a82012-03-06 20:06:33 +0000206void FileRemapper::applyMappings(PreprocessorOptions &PPOpts) const {
John McCall8f0e8d22011-06-15 23:25:17 +0000207 for (MappingsTy::const_iterator
208 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
209 if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
210 PPOpts.addRemappedFile(I->first->getName(), FE->getName());
211 } else {
212 llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
213 PPOpts.addRemappedFile(I->first->getName(), mem);
214 }
215 }
216
217 PPOpts.RetainRemappedFileBuffers = true;
218}
219
Ted Kremenek30660a82012-03-06 20:06:33 +0000220void FileRemapper::transferMappingsAndClear(PreprocessorOptions &PPOpts) {
John McCall8f0e8d22011-06-15 23:25:17 +0000221 for (MappingsTy::iterator
222 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
223 if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
224 PPOpts.addRemappedFile(I->first->getName(), FE->getName());
225 } else {
226 llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
227 PPOpts.addRemappedFile(I->first->getName(), mem);
228 }
229 I->second = Target();
230 }
231
232 PPOpts.RetainRemappedFileBuffers = false;
233 clear();
234}
235
Chris Lattner5f9e2722011-07-23 10:55:15 +0000236void FileRemapper::remap(StringRef filePath, llvm::MemoryBuffer *memBuf) {
John McCall8f0e8d22011-06-15 23:25:17 +0000237 remap(getOriginalFile(filePath), memBuf);
238}
239
Chris Lattner5f9e2722011-07-23 10:55:15 +0000240void FileRemapper::remap(StringRef filePath, StringRef newPath) {
John McCall8f0e8d22011-06-15 23:25:17 +0000241 const FileEntry *file = getOriginalFile(filePath);
242 const FileEntry *newfile = FileMgr->getFile(newPath);
243 remap(file, newfile);
244}
245
246void FileRemapper::remap(const FileEntry *file, llvm::MemoryBuffer *memBuf) {
247 assert(file);
248 Target &targ = FromToMappings[file];
249 resetTarget(targ);
250 targ = memBuf;
251}
252
253void FileRemapper::remap(const FileEntry *file, const FileEntry *newfile) {
254 assert(file && newfile);
255 Target &targ = FromToMappings[file];
256 resetTarget(targ);
257 targ = newfile;
258 ToFromMappings[newfile] = file;
259}
260
Chris Lattner5f9e2722011-07-23 10:55:15 +0000261const FileEntry *FileRemapper::getOriginalFile(StringRef filePath) {
John McCall8f0e8d22011-06-15 23:25:17 +0000262 const FileEntry *file = FileMgr->getFile(filePath);
263 // If we are updating a file that overriden an original file,
264 // actually update the original file.
265 llvm::DenseMap<const FileEntry *, const FileEntry *>::iterator
266 I = ToFromMappings.find(file);
267 if (I != ToFromMappings.end()) {
268 file = I->second;
269 assert(FromToMappings.find(file) != FromToMappings.end() &&
270 "Original file not in mappings!");
271 }
272 return file;
273}
274
275void FileRemapper::resetTarget(Target &targ) {
276 if (!targ)
277 return;
278
279 if (llvm::MemoryBuffer *oldmem = targ.dyn_cast<llvm::MemoryBuffer *>()) {
280 delete oldmem;
281 } else {
282 const FileEntry *toFE = targ.get<const FileEntry *>();
Benjamin Kramer7e423922012-03-24 18:22:12 +0000283 ToFromMappings.erase(toFE);
John McCall8f0e8d22011-06-15 23:25:17 +0000284 }
285}
286
David Blaikied6471f72011-09-25 23:23:43 +0000287bool FileRemapper::report(const Twine &err, DiagnosticsEngine &Diag) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000288 SmallString<128> buf;
John McCall8f0e8d22011-06-15 23:25:17 +0000289 unsigned ID = Diag.getDiagnosticIDs()->getCustomDiagID(DiagnosticIDs::Error,
Benjamin Kramer6748ae152011-08-27 00:34:29 +0000290 err.toStringRef(buf));
John McCall8f0e8d22011-06-15 23:25:17 +0000291 Diag.Report(ID);
292 return true;
293}