1 //===--- FileRemapper.cpp - File Remapping Helper -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
19 using namespace clang;
20 using namespace arcmt;
22 FileRemapper::FileRemapper() {
23 FileMgr.reset(new FileManager(FileSystemOptions()));
26 FileRemapper::~FileRemapper() {
30 void 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);
39 llvm::sys::fs::remove(infoFile, existed);
43 std::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();
51 bool 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);
61 std::vector<std::pair<const FileEntry *, const FileEntry *> > pairs;
63 std::ifstream fin(infoFile.c_str());
65 return report(std::string("Error opening file: ") + infoFile, Diag);
68 std::string fromFilename, toFilename;
69 uint64_t timeModified;
71 fin >> fromFilename >> timeModified >> toFilename;
75 return report(std::string("Error in format of file: ") + infoFile, Diag);
77 const FileEntry *origFE = FileMgr->getFile(fromFilename);
79 if (ignoreIfFilesChanged)
81 return report(std::string("File does not exist: ") + fromFilename, Diag);
83 const FileEntry *newFE = FileMgr->getFile(toFilename);
85 if (ignoreIfFilesChanged)
87 return report(std::string("File does not exist: ") + toFilename, Diag);
90 if ((uint64_t)origFE->getModificationTime() != timeModified) {
91 if (ignoreIfFilesChanged)
93 return report(std::string("File was modified: ") + fromFilename, Diag);
96 pairs.push_back(std::make_pair(origFE, newFE));
99 for (unsigned i = 0, e = pairs.size(); i != e; ++i)
100 remap(pairs[i].first, pairs[i].second);
105 bool FileRemapper::flushToDisk(llvm::StringRef outputDir, Diagnostic &Diag) {
106 using namespace llvm::sys;
109 if (fs::create_directory(outputDir, existed) != llvm::errc::success)
110 return report(std::string("Could not create directory: ") + outputDir.str(),
114 std::string infoFile = getRemapInfoFile(outputDir);
115 llvm::raw_fd_ostream infoOut(infoFile.c_str(), errMsg,
116 llvm::raw_fd_ostream::F_Binary);
118 return report(errMsg, Diag);
120 for (MappingsTy::iterator
121 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
123 const FileEntry *origFE = I->first;
124 llvm::SmallString<200> origPath = llvm::StringRef(origFE->getName());
125 fs::make_absolute(origPath);
126 infoOut << origPath << '\n';
127 infoOut << (uint64_t)origFE->getModificationTime() << '\n';
129 if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
130 llvm::SmallString<200> newPath = llvm::StringRef(FE->getName());
131 fs::make_absolute(newPath);
132 infoOut << newPath << '\n';
135 llvm::SmallString<64> tempPath;
136 tempPath = path::filename(origFE->getName());
137 tempPath += "-%%%%%%%%";
138 tempPath += path::extension(origFE->getName());
140 if (fs::unique_file(tempPath.str(), fd, tempPath) != llvm::errc::success)
141 return report(std::string("Could not create file: ") + tempPath.c_str(),
144 llvm::raw_fd_ostream newOut(fd, /*shouldClose=*/true);
145 llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
146 newOut.write(mem->getBufferStart(), mem->getBufferSize());
149 const FileEntry *newE = FileMgr->getFile(tempPath);
151 infoOut << newE->getName() << '\n';
159 bool FileRemapper::overwriteOriginal(Diagnostic &Diag,
160 llvm::StringRef outputDir) {
161 using namespace llvm::sys;
163 for (MappingsTy::iterator
164 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
165 const FileEntry *origFE = I->first;
166 if (const FileEntry *newFE = I->second.dyn_cast<const FileEntry *>()) {
167 if (fs::copy_file(newFE->getName(), origFE->getName(),
168 fs::copy_option::overwrite_if_exists) != llvm::errc::success) {
169 std::string err = "Could not copy file '";
170 llvm::raw_string_ostream os(err);
171 os << "Could not copy file '" << newFE->getName() << "' to file '"
172 << origFE->getName() << "'";
174 return report(err, Diag);
178 bool fileExists = false;
179 fs::exists(origFE->getName(), fileExists);
181 return report(std::string("File does not exist: ") + origFE->getName(),
185 llvm::raw_fd_ostream Out(origFE->getName(), errMsg,
186 llvm::raw_fd_ostream::F_Binary);
188 return report(errMsg, Diag);
190 llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
191 Out.write(mem->getBufferStart(), mem->getBufferSize());
200 void FileRemapper::applyMappings(CompilerInvocation &CI) const {
201 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
202 for (MappingsTy::const_iterator
203 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
204 if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
205 PPOpts.addRemappedFile(I->first->getName(), FE->getName());
207 llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
208 PPOpts.addRemappedFile(I->first->getName(), mem);
212 PPOpts.RetainRemappedFileBuffers = true;
215 void FileRemapper::transferMappingsAndClear(CompilerInvocation &CI) {
216 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
217 for (MappingsTy::iterator
218 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
219 if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
220 PPOpts.addRemappedFile(I->first->getName(), FE->getName());
222 llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
223 PPOpts.addRemappedFile(I->first->getName(), mem);
225 I->second = Target();
228 PPOpts.RetainRemappedFileBuffers = false;
232 void FileRemapper::remap(llvm::StringRef filePath, llvm::MemoryBuffer *memBuf) {
233 remap(getOriginalFile(filePath), memBuf);
236 void FileRemapper::remap(llvm::StringRef filePath, llvm::StringRef newPath) {
237 const FileEntry *file = getOriginalFile(filePath);
238 const FileEntry *newfile = FileMgr->getFile(newPath);
239 remap(file, newfile);
242 void FileRemapper::remap(const FileEntry *file, llvm::MemoryBuffer *memBuf) {
244 Target &targ = FromToMappings[file];
249 void FileRemapper::remap(const FileEntry *file, const FileEntry *newfile) {
250 assert(file && newfile);
251 Target &targ = FromToMappings[file];
254 ToFromMappings[newfile] = file;
257 const FileEntry *FileRemapper::getOriginalFile(llvm::StringRef filePath) {
258 const FileEntry *file = FileMgr->getFile(filePath);
259 // If we are updating a file that overriden an original file,
260 // actually update the original file.
261 llvm::DenseMap<const FileEntry *, const FileEntry *>::iterator
262 I = ToFromMappings.find(file);
263 if (I != ToFromMappings.end()) {
265 assert(FromToMappings.find(file) != FromToMappings.end() &&
266 "Original file not in mappings!");
271 void FileRemapper::resetTarget(Target &targ) {
275 if (llvm::MemoryBuffer *oldmem = targ.dyn_cast<llvm::MemoryBuffer *>()) {
278 const FileEntry *toFE = targ.get<const FileEntry *>();
279 llvm::DenseMap<const FileEntry *, const FileEntry *>::iterator
280 I = ToFromMappings.find(toFE);
281 if (I != ToFromMappings.end())
282 ToFromMappings.erase(I);
286 bool FileRemapper::report(const std::string &err, Diagnostic &Diag) {
287 unsigned ID = Diag.getDiagnosticIDs()->getCustomDiagID(DiagnosticIDs::Error,