blob: 4dedac88f982dfe463b7a6e9d15363c91b30106e [file] [log] [blame]
John McCalld70fb982011-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 Kremenekf7639e12012-03-06 20:06:33 +000011#include "clang/Basic/Diagnostic.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000012#include "clang/Basic/FileManager.h"
13#include "clang/Lex/PreprocessorOptions.h"
14#include "llvm/Support/FileSystem.h"
John McCalld70fb982011-06-15 23:25:17 +000015#include "llvm/Support/MemoryBuffer.h"
16#include "llvm/Support/Path.h"
John McCalld70fb982011-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 Lattner0e62c1c2011-07-23 10:55:15 +000031void FileRemapper::clear(StringRef outputDir) {
John McCalld70fb982011-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);
Rafael Espindola2a008782014-01-10 21:32:14 +000039 llvm::sys::fs::remove(infoFile);
John McCalld70fb982011-06-15 23:25:17 +000040 }
41}
42
Chris Lattner0e62c1c2011-07-23 10:55:15 +000043std::string FileRemapper::getRemapInfoFile(StringRef outputDir) {
John McCalld70fb982011-06-15 23:25:17 +000044 assert(!outputDir.empty());
Benjamin Kramer33d43302013-06-13 14:26:04 +000045 SmallString<128> InfoFile = outputDir;
46 llvm::sys::path::append(InfoFile, "remap");
47 return InfoFile.str();
John McCalld70fb982011-06-15 23:25:17 +000048}
49
David Blaikie9c902b52011-09-25 23:23:43 +000050bool FileRemapper::initFromDisk(StringRef outputDir, DiagnosticsEngine &Diag,
John McCalld70fb982011-06-15 23:25:17 +000051 bool ignoreIfFilesChanged) {
Ted Kremenekf7639e12012-03-06 20:06:33 +000052 std::string infoFile = getRemapInfoFile(outputDir);
53 return initFromFile(infoFile, Diag, ignoreIfFilesChanged);
54}
55
56bool FileRemapper::initFromFile(StringRef filePath, DiagnosticsEngine &Diag,
57 bool ignoreIfFilesChanged) {
John McCalld70fb982011-06-15 23:25:17 +000058 assert(FromToMappings.empty() &&
59 "initFromDisk should be called before any remap calls");
Ted Kremenekf7639e12012-03-06 20:06:33 +000060 std::string infoFile = filePath;
Rafael Espindola611505f2014-09-11 18:10:13 +000061 if (!llvm::sys::fs::exists(infoFile))
John McCalld70fb982011-06-15 23:25:17 +000062 return false;
63
64 std::vector<std::pair<const FileEntry *, const FileEntry *> > pairs;
Ahmed Charlesb8984322014-03-07 20:03:18 +000065
Rafael Espindola2d2b4202014-07-06 17:43:24 +000066 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> fileBuf =
Malcolm Parsonsf76f6502016-11-02 10:39:27 +000067 llvm::MemoryBuffer::getFile(infoFile);
Rafael Espindola2d2b4202014-07-06 17:43:24 +000068 if (!fileBuf)
Benjamin Kramer79cfba12011-08-27 00:34:29 +000069 return report("Error opening file: " + infoFile, Diag);
Argyrios Kyrtzidis3da6bc12011-08-26 22:40:55 +000070
71 SmallVector<StringRef, 64> lines;
Rafael Espindola2d2b4202014-07-06 17:43:24 +000072 fileBuf.get()->getBuffer().split(lines, "\n");
John McCalld70fb982011-06-15 23:25:17 +000073
Argyrios Kyrtzidis10454302011-08-26 23:20:23 +000074 for (unsigned idx = 0; idx+3 <= lines.size(); idx += 3) {
Benjamin Kramer79cfba12011-08-27 00:34:29 +000075 StringRef fromFilename = lines[idx];
Douglas Gregor5be7a1c2011-08-26 23:57:29 +000076 unsigned long long timeModified;
David Blaikiedabed222012-05-01 02:45:39 +000077 if (lines[idx+1].getAsInteger(10, timeModified))
78 return report("Invalid file data: '" + lines[idx+1] + "' not a number",
79 Diag);
Benjamin Kramer79cfba12011-08-27 00:34:29 +000080 StringRef toFilename = lines[idx+2];
Argyrios Kyrtzidis3da6bc12011-08-26 22:40:55 +000081
John McCalld70fb982011-06-15 23:25:17 +000082 const FileEntry *origFE = FileMgr->getFile(fromFilename);
83 if (!origFE) {
84 if (ignoreIfFilesChanged)
85 continue;
Benjamin Kramer79cfba12011-08-27 00:34:29 +000086 return report("File does not exist: " + fromFilename, Diag);
John McCalld70fb982011-06-15 23:25:17 +000087 }
88 const FileEntry *newFE = FileMgr->getFile(toFilename);
89 if (!newFE) {
90 if (ignoreIfFilesChanged)
91 continue;
Benjamin Kramer79cfba12011-08-27 00:34:29 +000092 return report("File does not exist: " + toFilename, Diag);
John McCalld70fb982011-06-15 23:25:17 +000093 }
94
95 if ((uint64_t)origFE->getModificationTime() != timeModified) {
96 if (ignoreIfFilesChanged)
97 continue;
Benjamin Kramer79cfba12011-08-27 00:34:29 +000098 return report("File was modified: " + fromFilename, Diag);
John McCalld70fb982011-06-15 23:25:17 +000099 }
100
101 pairs.push_back(std::make_pair(origFE, newFE));
102 }
103
104 for (unsigned i = 0, e = pairs.size(); i != e; ++i)
105 remap(pairs[i].first, pairs[i].second);
106
107 return false;
108}
109
David Blaikie9c902b52011-09-25 23:23:43 +0000110bool FileRemapper::flushToDisk(StringRef outputDir, DiagnosticsEngine &Diag) {
John McCalld70fb982011-06-15 23:25:17 +0000111 using namespace llvm::sys;
112
Rafael Espindola3ae06202014-05-31 03:20:52 +0000113 if (fs::create_directory(outputDir))
Benjamin Kramer79cfba12011-08-27 00:34:29 +0000114 return report("Could not create directory: " + outputDir, Diag);
John McCalld70fb982011-06-15 23:25:17 +0000115
John McCalld70fb982011-06-15 23:25:17 +0000116 std::string infoFile = getRemapInfoFile(outputDir);
Ted Kremenekf7639e12012-03-06 20:06:33 +0000117 return flushToFile(infoFile, Diag);
118}
119
120bool FileRemapper::flushToFile(StringRef outputPath, DiagnosticsEngine &Diag) {
121 using namespace llvm::sys;
122
Rafael Espindoladae941a2014-08-25 18:17:04 +0000123 std::error_code EC;
Ted Kremenekf7639e12012-03-06 20:06:33 +0000124 std::string infoFile = outputPath;
Rafael Espindoladae941a2014-08-25 18:17:04 +0000125 llvm::raw_fd_ostream infoOut(infoFile, EC, llvm::sys::fs::F_None);
126 if (EC)
127 return report(EC.message(), Diag);
John McCalld70fb982011-06-15 23:25:17 +0000128
129 for (MappingsTy::iterator
130 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
131
132 const FileEntry *origFE = I->first;
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000133 SmallString<200> origPath = StringRef(origFE->getName());
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000134 fs::make_absolute(origPath);
135 infoOut << origPath << '\n';
John McCalld70fb982011-06-15 23:25:17 +0000136 infoOut << (uint64_t)origFE->getModificationTime() << '\n';
137
138 if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000139 SmallString<200> newPath = StringRef(FE->getName());
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000140 fs::make_absolute(newPath);
141 infoOut << newPath << '\n';
John McCalld70fb982011-06-15 23:25:17 +0000142 } else {
143
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000144 SmallString<64> tempPath;
John McCalld70fb982011-06-15 23:25:17 +0000145 int fd;
Rafael Espindolaa36e78e2013-07-05 20:00:06 +0000146 if (fs::createTemporaryFile(path::filename(origFE->getName()),
Argyrios Kyrtzidis203e9232015-09-09 16:48:47 +0000147 path::extension(origFE->getName()).drop_front(), fd,
Rafael Espindolaa36e78e2013-07-05 20:00:06 +0000148 tempPath))
Benjamin Kramer79cfba12011-08-27 00:34:29 +0000149 return report("Could not create file: " + tempPath.str(), Diag);
John McCalld70fb982011-06-15 23:25:17 +0000150
151 llvm::raw_fd_ostream newOut(fd, /*shouldClose=*/true);
152 llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
153 newOut.write(mem->getBufferStart(), mem->getBufferSize());
154 newOut.close();
155
156 const FileEntry *newE = FileMgr->getFile(tempPath);
157 remap(origFE, newE);
158 infoOut << newE->getName() << '\n';
159 }
160 }
161
162 infoOut.close();
163 return false;
164}
165
David Blaikie9c902b52011-09-25 23:23:43 +0000166bool FileRemapper::overwriteOriginal(DiagnosticsEngine &Diag,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000167 StringRef outputDir) {
John McCalld70fb982011-06-15 23:25:17 +0000168 using namespace llvm::sys;
169
170 for (MappingsTy::iterator
171 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
172 const FileEntry *origFE = I->first;
Argyrios Kyrtzidis884bee82013-07-16 22:24:06 +0000173 assert(I->second.is<llvm::MemoryBuffer *>());
Rafael Espindola611505f2014-09-11 18:10:13 +0000174 if (!fs::exists(origFE->getName()))
Argyrios Kyrtzidis884bee82013-07-16 22:24:06 +0000175 return report(StringRef("File does not exist: ") + origFE->getName(),
176 Diag);
John McCalld70fb982011-06-15 23:25:17 +0000177
Rafael Espindoladae941a2014-08-25 18:17:04 +0000178 std::error_code EC;
179 llvm::raw_fd_ostream Out(origFE->getName(), EC, llvm::sys::fs::F_None);
180 if (EC)
181 return report(EC.message(), Diag);
John McCalld70fb982011-06-15 23:25:17 +0000182
Argyrios Kyrtzidis884bee82013-07-16 22:24:06 +0000183 llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
184 Out.write(mem->getBufferStart(), mem->getBufferSize());
185 Out.close();
John McCalld70fb982011-06-15 23:25:17 +0000186 }
187
188 clear(outputDir);
189 return false;
190}
191
Ted Kremenekf7639e12012-03-06 20:06:33 +0000192void FileRemapper::applyMappings(PreprocessorOptions &PPOpts) const {
John McCalld70fb982011-06-15 23:25:17 +0000193 for (MappingsTy::const_iterator
194 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
195 if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
196 PPOpts.addRemappedFile(I->first->getName(), FE->getName());
197 } else {
198 llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
199 PPOpts.addRemappedFile(I->first->getName(), mem);
200 }
201 }
202
203 PPOpts.RetainRemappedFileBuffers = true;
204}
205
Rafael Espindola1a1b1562014-08-17 23:12:27 +0000206void FileRemapper::remap(StringRef filePath,
207 std::unique_ptr<llvm::MemoryBuffer> memBuf) {
208 remap(getOriginalFile(filePath), std::move(memBuf));
John McCalld70fb982011-06-15 23:25:17 +0000209}
210
Rafael Espindola1a1b1562014-08-17 23:12:27 +0000211void FileRemapper::remap(const FileEntry *file,
212 std::unique_ptr<llvm::MemoryBuffer> memBuf) {
John McCalld70fb982011-06-15 23:25:17 +0000213 assert(file);
214 Target &targ = FromToMappings[file];
215 resetTarget(targ);
Rafael Espindola1a1b1562014-08-17 23:12:27 +0000216 targ = memBuf.release();
John McCalld70fb982011-06-15 23:25:17 +0000217}
218
219void FileRemapper::remap(const FileEntry *file, const FileEntry *newfile) {
220 assert(file && newfile);
221 Target &targ = FromToMappings[file];
222 resetTarget(targ);
223 targ = newfile;
224 ToFromMappings[newfile] = file;
225}
226
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000227const FileEntry *FileRemapper::getOriginalFile(StringRef filePath) {
John McCalld70fb982011-06-15 23:25:17 +0000228 const FileEntry *file = FileMgr->getFile(filePath);
229 // If we are updating a file that overriden an original file,
230 // actually update the original file.
231 llvm::DenseMap<const FileEntry *, const FileEntry *>::iterator
232 I = ToFromMappings.find(file);
233 if (I != ToFromMappings.end()) {
234 file = I->second;
235 assert(FromToMappings.find(file) != FromToMappings.end() &&
236 "Original file not in mappings!");
237 }
238 return file;
239}
240
241void FileRemapper::resetTarget(Target &targ) {
242 if (!targ)
243 return;
244
245 if (llvm::MemoryBuffer *oldmem = targ.dyn_cast<llvm::MemoryBuffer *>()) {
246 delete oldmem;
247 } else {
248 const FileEntry *toFE = targ.get<const FileEntry *>();
Benjamin Kramere894e092012-03-24 18:22:12 +0000249 ToFromMappings.erase(toFE);
John McCalld70fb982011-06-15 23:25:17 +0000250 }
251}
252
David Blaikie9c902b52011-09-25 23:23:43 +0000253bool FileRemapper::report(const Twine &err, DiagnosticsEngine &Diag) {
Alp Toker29cb66b2014-01-26 06:17:37 +0000254 Diag.Report(Diag.getCustomDiagID(DiagnosticsEngine::Error, "%0"))
255 << err.str();
John McCalld70fb982011-06-15 23:25:17 +0000256 return true;
257}