blob: e255e40c19c869dc4ef1692485d484924afd68d9 [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
Chris Lattner5f9e2722011-07-23 10:55:15 +000051bool FileRemapper::initFromDisk(StringRef outputDir, Diagnostic &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))
John McCall8f0e8d22011-06-15 23:25:17 +000066 return report(std::string("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 Kyrtzidis56773de2011-08-26 22:40:55 +000071 unsigned idx = 0;
72 while (idx+3 <= lines.size()) {
73 std::string fromFilename = lines[idx];
John McCall8f0e8d22011-06-15 23:25:17 +000074 uint64_t timeModified;
Argyrios Kyrtzidis56773de2011-08-26 22:40:55 +000075 lines[idx+1].getAsInteger(10, timeModified);
76 std::string toFilename = lines[idx+2];
77
John McCall8f0e8d22011-06-15 23:25:17 +000078 const FileEntry *origFE = FileMgr->getFile(fromFilename);
79 if (!origFE) {
80 if (ignoreIfFilesChanged)
81 continue;
82 return report(std::string("File does not exist: ") + fromFilename, Diag);
83 }
84 const FileEntry *newFE = FileMgr->getFile(toFilename);
85 if (!newFE) {
86 if (ignoreIfFilesChanged)
87 continue;
88 return report(std::string("File does not exist: ") + toFilename, Diag);
89 }
90
91 if ((uint64_t)origFE->getModificationTime() != timeModified) {
92 if (ignoreIfFilesChanged)
93 continue;
94 return report(std::string("File was modified: ") + fromFilename, Diag);
95 }
96
97 pairs.push_back(std::make_pair(origFE, newFE));
Argyrios Kyrtzidis56773de2011-08-26 22:40:55 +000098
99 idx += 3;
John McCall8f0e8d22011-06-15 23:25:17 +0000100 }
101
102 for (unsigned i = 0, e = pairs.size(); i != e; ++i)
103 remap(pairs[i].first, pairs[i].second);
104
105 return false;
106}
107
Chris Lattner5f9e2722011-07-23 10:55:15 +0000108bool FileRemapper::flushToDisk(StringRef outputDir, Diagnostic &Diag) {
John McCall8f0e8d22011-06-15 23:25:17 +0000109 using namespace llvm::sys;
110
111 bool existed;
112 if (fs::create_directory(outputDir, existed) != llvm::errc::success)
113 return report(std::string("Could not create directory: ") + outputDir.str(),
114 Diag);
115
116 std::string errMsg;
117 std::string infoFile = getRemapInfoFile(outputDir);
Argyrios Kyrtzidisb7096392011-07-10 18:57:15 +0000118 llvm::raw_fd_ostream infoOut(infoFile.c_str(), errMsg,
119 llvm::raw_fd_ostream::F_Binary);
Argyrios Kyrtzidis55a8a8d2011-07-10 18:57:18 +0000120 if (!errMsg.empty())
John McCall8f0e8d22011-06-15 23:25:17 +0000121 return report(errMsg, Diag);
122
123 for (MappingsTy::iterator
124 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
125
126 const FileEntry *origFE = I->first;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000127 llvm::SmallString<200> origPath = StringRef(origFE->getName());
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000128 fs::make_absolute(origPath);
129 infoOut << origPath << '\n';
John McCall8f0e8d22011-06-15 23:25:17 +0000130 infoOut << (uint64_t)origFE->getModificationTime() << '\n';
131
132 if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000133 llvm::SmallString<200> newPath = StringRef(FE->getName());
Argyrios Kyrtzidis69325d52011-07-09 20:00:58 +0000134 fs::make_absolute(newPath);
135 infoOut << newPath << '\n';
John McCall8f0e8d22011-06-15 23:25:17 +0000136 } else {
137
138 llvm::SmallString<64> tempPath;
139 tempPath = path::filename(origFE->getName());
140 tempPath += "-%%%%%%%%";
141 tempPath += path::extension(origFE->getName());
142 int fd;
143 if (fs::unique_file(tempPath.str(), fd, tempPath) != llvm::errc::success)
144 return report(std::string("Could not create file: ") + tempPath.c_str(),
145 Diag);
146
147 llvm::raw_fd_ostream newOut(fd, /*shouldClose=*/true);
148 llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
149 newOut.write(mem->getBufferStart(), mem->getBufferSize());
150 newOut.close();
151
152 const FileEntry *newE = FileMgr->getFile(tempPath);
153 remap(origFE, newE);
154 infoOut << newE->getName() << '\n';
155 }
156 }
157
158 infoOut.close();
159 return false;
160}
161
162bool FileRemapper::overwriteOriginal(Diagnostic &Diag,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000163 StringRef outputDir) {
John McCall8f0e8d22011-06-15 23:25:17 +0000164 using namespace llvm::sys;
165
166 for (MappingsTy::iterator
167 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
168 const FileEntry *origFE = I->first;
169 if (const FileEntry *newFE = I->second.dyn_cast<const FileEntry *>()) {
170 if (fs::copy_file(newFE->getName(), origFE->getName(),
171 fs::copy_option::overwrite_if_exists) != llvm::errc::success) {
172 std::string err = "Could not copy file '";
173 llvm::raw_string_ostream os(err);
174 os << "Could not copy file '" << newFE->getName() << "' to file '"
175 << origFE->getName() << "'";
176 os.flush();
177 return report(err, Diag);
178 }
179 } else {
180
181 bool fileExists = false;
182 fs::exists(origFE->getName(), fileExists);
183 if (!fileExists)
184 return report(std::string("File does not exist: ") + origFE->getName(),
185 Diag);
186
187 std::string errMsg;
188 llvm::raw_fd_ostream Out(origFE->getName(), errMsg,
189 llvm::raw_fd_ostream::F_Binary);
Argyrios Kyrtzidis55a8a8d2011-07-10 18:57:18 +0000190 if (!errMsg.empty())
John McCall8f0e8d22011-06-15 23:25:17 +0000191 return report(errMsg, Diag);
192
193 llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
194 Out.write(mem->getBufferStart(), mem->getBufferSize());
195 Out.close();
196 }
197 }
198
199 clear(outputDir);
200 return false;
201}
202
203void FileRemapper::applyMappings(CompilerInvocation &CI) const {
204 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
205 for (MappingsTy::const_iterator
206 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
207 if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
208 PPOpts.addRemappedFile(I->first->getName(), FE->getName());
209 } else {
210 llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
211 PPOpts.addRemappedFile(I->first->getName(), mem);
212 }
213 }
214
215 PPOpts.RetainRemappedFileBuffers = true;
216}
217
218void FileRemapper::transferMappingsAndClear(CompilerInvocation &CI) {
219 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
220 for (MappingsTy::iterator
221 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
222 if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
223 PPOpts.addRemappedFile(I->first->getName(), FE->getName());
224 } else {
225 llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
226 PPOpts.addRemappedFile(I->first->getName(), mem);
227 }
228 I->second = Target();
229 }
230
231 PPOpts.RetainRemappedFileBuffers = false;
232 clear();
233}
234
Chris Lattner5f9e2722011-07-23 10:55:15 +0000235void FileRemapper::remap(StringRef filePath, llvm::MemoryBuffer *memBuf) {
John McCall8f0e8d22011-06-15 23:25:17 +0000236 remap(getOriginalFile(filePath), memBuf);
237}
238
Chris Lattner5f9e2722011-07-23 10:55:15 +0000239void FileRemapper::remap(StringRef filePath, StringRef newPath) {
John McCall8f0e8d22011-06-15 23:25:17 +0000240 const FileEntry *file = getOriginalFile(filePath);
241 const FileEntry *newfile = FileMgr->getFile(newPath);
242 remap(file, newfile);
243}
244
245void FileRemapper::remap(const FileEntry *file, llvm::MemoryBuffer *memBuf) {
246 assert(file);
247 Target &targ = FromToMappings[file];
248 resetTarget(targ);
249 targ = memBuf;
250}
251
252void FileRemapper::remap(const FileEntry *file, const FileEntry *newfile) {
253 assert(file && newfile);
254 Target &targ = FromToMappings[file];
255 resetTarget(targ);
256 targ = newfile;
257 ToFromMappings[newfile] = file;
258}
259
Chris Lattner5f9e2722011-07-23 10:55:15 +0000260const FileEntry *FileRemapper::getOriginalFile(StringRef filePath) {
John McCall8f0e8d22011-06-15 23:25:17 +0000261 const FileEntry *file = FileMgr->getFile(filePath);
262 // If we are updating a file that overriden an original file,
263 // actually update the original file.
264 llvm::DenseMap<const FileEntry *, const FileEntry *>::iterator
265 I = ToFromMappings.find(file);
266 if (I != ToFromMappings.end()) {
267 file = I->second;
268 assert(FromToMappings.find(file) != FromToMappings.end() &&
269 "Original file not in mappings!");
270 }
271 return file;
272}
273
274void FileRemapper::resetTarget(Target &targ) {
275 if (!targ)
276 return;
277
278 if (llvm::MemoryBuffer *oldmem = targ.dyn_cast<llvm::MemoryBuffer *>()) {
279 delete oldmem;
280 } else {
281 const FileEntry *toFE = targ.get<const FileEntry *>();
282 llvm::DenseMap<const FileEntry *, const FileEntry *>::iterator
283 I = ToFromMappings.find(toFE);
284 if (I != ToFromMappings.end())
285 ToFromMappings.erase(I);
286 }
287}
288
289bool FileRemapper::report(const std::string &err, Diagnostic &Diag) {
290 unsigned ID = Diag.getDiagnosticIDs()->getCustomDiagID(DiagnosticIDs::Error,
291 err);
292 Diag.Report(ID);
293 return true;
294}