blob: c1dbe92ffb8d575e06f9a5e1658342aab7590eb2 [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"
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
30void FileRemapper::clear(llvm::StringRef outputDir) {
31 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
43std::string FileRemapper::getRemapInfoFile(llvm::StringRef outputDir) {
44 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
51bool FileRemapper::initFromDisk(llvm::StringRef outputDir, Diagnostic &Diag,
52 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;
62
63 std::ifstream fin(infoFile.c_str());
64 if (!fin.good())
65 return report(std::string("Error opening file: ") + infoFile, Diag);
66
67 while (true) {
68 std::string fromFilename, toFilename;
69 uint64_t timeModified;
70
71 fin >> fromFilename >> timeModified >> toFilename;
72 if (fin.eof())
73 break;
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +000074 if (!fin.good())
John McCalld70fb982011-06-15 23:25:17 +000075 return report(std::string("Error in format of file: ") + infoFile, Diag);
John McCalld70fb982011-06-15 23:25:17 +000076
77 const FileEntry *origFE = FileMgr->getFile(fromFilename);
78 if (!origFE) {
79 if (ignoreIfFilesChanged)
80 continue;
81 return report(std::string("File does not exist: ") + fromFilename, Diag);
82 }
83 const FileEntry *newFE = FileMgr->getFile(toFilename);
84 if (!newFE) {
85 if (ignoreIfFilesChanged)
86 continue;
87 return report(std::string("File does not exist: ") + toFilename, Diag);
88 }
89
90 if ((uint64_t)origFE->getModificationTime() != timeModified) {
91 if (ignoreIfFilesChanged)
92 continue;
93 return report(std::string("File was modified: ") + fromFilename, Diag);
94 }
95
96 pairs.push_back(std::make_pair(origFE, newFE));
97 }
98
99 for (unsigned i = 0, e = pairs.size(); i != e; ++i)
100 remap(pairs[i].first, pairs[i].second);
101
102 return false;
103}
104
105bool FileRemapper::flushToDisk(llvm::StringRef outputDir, Diagnostic &Diag) {
106 using namespace llvm::sys;
107
108 bool existed;
109 if (fs::create_directory(outputDir, existed) != llvm::errc::success)
110 return report(std::string("Could not create directory: ") + outputDir.str(),
111 Diag);
112
113 std::string errMsg;
114 std::string infoFile = getRemapInfoFile(outputDir);
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000115 llvm::raw_fd_ostream infoOut(infoFile.c_str(), errMsg);
John McCalld70fb982011-06-15 23:25:17 +0000116 if (!errMsg.empty() || infoOut.has_error())
117 return report(errMsg, Diag);
118
119 for (MappingsTy::iterator
120 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
121
122 const FileEntry *origFE = I->first;
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000123 llvm::SmallString<200> origPath = llvm::StringRef(origFE->getName());
124 fs::make_absolute(origPath);
125 infoOut << origPath << '\n';
John McCalld70fb982011-06-15 23:25:17 +0000126 infoOut << (uint64_t)origFE->getModificationTime() << '\n';
127
128 if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
Argyrios Kyrtzidis7fbd97f2011-07-09 20:00:58 +0000129 llvm::SmallString<200> newPath = llvm::StringRef(FE->getName());
130 fs::make_absolute(newPath);
131 infoOut << newPath << '\n';
John McCalld70fb982011-06-15 23:25:17 +0000132 } else {
133
134 llvm::SmallString<64> tempPath;
135 tempPath = path::filename(origFE->getName());
136 tempPath += "-%%%%%%%%";
137 tempPath += path::extension(origFE->getName());
138 int fd;
139 if (fs::unique_file(tempPath.str(), fd, tempPath) != llvm::errc::success)
140 return report(std::string("Could not create file: ") + tempPath.c_str(),
141 Diag);
142
143 llvm::raw_fd_ostream newOut(fd, /*shouldClose=*/true);
144 llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
145 newOut.write(mem->getBufferStart(), mem->getBufferSize());
146 newOut.close();
147
148 const FileEntry *newE = FileMgr->getFile(tempPath);
149 remap(origFE, newE);
150 infoOut << newE->getName() << '\n';
151 }
152 }
153
154 infoOut.close();
155 return false;
156}
157
158bool FileRemapper::overwriteOriginal(Diagnostic &Diag,
159 llvm::StringRef outputDir) {
160 using namespace llvm::sys;
161
162 for (MappingsTy::iterator
163 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
164 const FileEntry *origFE = I->first;
165 if (const FileEntry *newFE = I->second.dyn_cast<const FileEntry *>()) {
166 if (fs::copy_file(newFE->getName(), origFE->getName(),
167 fs::copy_option::overwrite_if_exists) != llvm::errc::success) {
168 std::string err = "Could not copy file '";
169 llvm::raw_string_ostream os(err);
170 os << "Could not copy file '" << newFE->getName() << "' to file '"
171 << origFE->getName() << "'";
172 os.flush();
173 return report(err, Diag);
174 }
175 } else {
176
177 bool fileExists = false;
178 fs::exists(origFE->getName(), fileExists);
179 if (!fileExists)
180 return report(std::string("File does not exist: ") + origFE->getName(),
181 Diag);
182
183 std::string errMsg;
184 llvm::raw_fd_ostream Out(origFE->getName(), errMsg,
185 llvm::raw_fd_ostream::F_Binary);
186 if (!errMsg.empty() || Out.has_error())
187 return report(errMsg, Diag);
188
189 llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
190 Out.write(mem->getBufferStart(), mem->getBufferSize());
191 Out.close();
192 }
193 }
194
195 clear(outputDir);
196 return false;
197}
198
199void FileRemapper::applyMappings(CompilerInvocation &CI) const {
200 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
201 for (MappingsTy::const_iterator
202 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
203 if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
204 PPOpts.addRemappedFile(I->first->getName(), FE->getName());
205 } else {
206 llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
207 PPOpts.addRemappedFile(I->first->getName(), mem);
208 }
209 }
210
211 PPOpts.RetainRemappedFileBuffers = true;
212}
213
214void FileRemapper::transferMappingsAndClear(CompilerInvocation &CI) {
215 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
216 for (MappingsTy::iterator
217 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
218 if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
219 PPOpts.addRemappedFile(I->first->getName(), FE->getName());
220 } else {
221 llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
222 PPOpts.addRemappedFile(I->first->getName(), mem);
223 }
224 I->second = Target();
225 }
226
227 PPOpts.RetainRemappedFileBuffers = false;
228 clear();
229}
230
231void FileRemapper::remap(llvm::StringRef filePath, llvm::MemoryBuffer *memBuf) {
232 remap(getOriginalFile(filePath), memBuf);
233}
234
235void FileRemapper::remap(llvm::StringRef filePath, llvm::StringRef newPath) {
236 const FileEntry *file = getOriginalFile(filePath);
237 const FileEntry *newfile = FileMgr->getFile(newPath);
238 remap(file, newfile);
239}
240
241void FileRemapper::remap(const FileEntry *file, llvm::MemoryBuffer *memBuf) {
242 assert(file);
243 Target &targ = FromToMappings[file];
244 resetTarget(targ);
245 targ = memBuf;
246}
247
248void FileRemapper::remap(const FileEntry *file, const FileEntry *newfile) {
249 assert(file && newfile);
250 Target &targ = FromToMappings[file];
251 resetTarget(targ);
252 targ = newfile;
253 ToFromMappings[newfile] = file;
254}
255
256const FileEntry *FileRemapper::getOriginalFile(llvm::StringRef filePath) {
257 const FileEntry *file = FileMgr->getFile(filePath);
258 // If we are updating a file that overriden an original file,
259 // actually update the original file.
260 llvm::DenseMap<const FileEntry *, const FileEntry *>::iterator
261 I = ToFromMappings.find(file);
262 if (I != ToFromMappings.end()) {
263 file = I->second;
264 assert(FromToMappings.find(file) != FromToMappings.end() &&
265 "Original file not in mappings!");
266 }
267 return file;
268}
269
270void FileRemapper::resetTarget(Target &targ) {
271 if (!targ)
272 return;
273
274 if (llvm::MemoryBuffer *oldmem = targ.dyn_cast<llvm::MemoryBuffer *>()) {
275 delete oldmem;
276 } else {
277 const FileEntry *toFE = targ.get<const FileEntry *>();
278 llvm::DenseMap<const FileEntry *, const FileEntry *>::iterator
279 I = ToFromMappings.find(toFE);
280 if (I != ToFromMappings.end())
281 ToFromMappings.erase(I);
282 }
283}
284
285bool FileRemapper::report(const std::string &err, Diagnostic &Diag) {
286 unsigned ID = Diag.getDiagnosticIDs()->getCustomDiagID(DiagnosticIDs::Error,
287 err);
288 Diag.Report(ID);
289 return true;
290}