1//===--- 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/PreprocessorOptions.h"
12#include "clang/Basic/FileManager.h"
13#include "clang/Basic/Diagnostic.h"
14#include "llvm/Support/MemoryBuffer.h"
15#include "llvm/Support/Path.h"
16#include "llvm/Support/FileSystem.h"
17#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
31void FileRemapper::clear(StringRef outputDir) {
32  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
44std::string FileRemapper::getRemapInfoFile(StringRef outputDir) {
45  assert(!outputDir.empty());
46  llvm::sys::Path dir(outputDir);
47  llvm::sys::Path infoFile = dir;
48  infoFile.appendComponent("remap");
49  return infoFile.str();
50}
51
52bool FileRemapper::initFromDisk(StringRef outputDir, DiagnosticsEngine &Diag,
53                                bool ignoreIfFilesChanged) {
54  std::string infoFile = getRemapInfoFile(outputDir);
55  return initFromFile(infoFile, Diag, ignoreIfFilesChanged);
56}
57
58bool FileRemapper::initFromFile(StringRef filePath, DiagnosticsEngine &Diag,
59                                bool ignoreIfFilesChanged) {
60  assert(FromToMappings.empty() &&
61         "initFromDisk should be called before any remap calls");
62  std::string infoFile = filePath;
63  bool fileExists = false;
64  llvm::sys::fs::exists(infoFile, fileExists);
65  if (!fileExists)
66    return false;
67
68  std::vector<std::pair<const FileEntry *, const FileEntry *> > pairs;
69
70  OwningPtr<llvm::MemoryBuffer> fileBuf;
71  if (llvm::MemoryBuffer::getFile(infoFile.c_str(), fileBuf))
72    return report("Error opening file: " + infoFile, Diag);
73
74  SmallVector<StringRef, 64> lines;
75  fileBuf->getBuffer().split(lines, "\n");
76
77  for (unsigned idx = 0; idx+3 <= lines.size(); idx += 3) {
78    StringRef fromFilename = lines[idx];
79    unsigned long long timeModified;
80    lines[idx+1].getAsInteger(10, timeModified);
81    StringRef toFilename = lines[idx+2];
82
83    const FileEntry *origFE = FileMgr->getFile(fromFilename);
84    if (!origFE) {
85      if (ignoreIfFilesChanged)
86        continue;
87      return report("File does not exist: " + fromFilename, Diag);
88    }
89    const FileEntry *newFE = FileMgr->getFile(toFilename);
90    if (!newFE) {
91      if (ignoreIfFilesChanged)
92        continue;
93      return report("File does not exist: " + toFilename, Diag);
94    }
95
96    if ((uint64_t)origFE->getModificationTime() != timeModified) {
97      if (ignoreIfFilesChanged)
98        continue;
99      return report("File was modified: " + fromFilename, Diag);
100    }
101
102    pairs.push_back(std::make_pair(origFE, newFE));
103  }
104
105  for (unsigned i = 0, e = pairs.size(); i != e; ++i)
106    remap(pairs[i].first, pairs[i].second);
107
108  return false;
109}
110
111bool FileRemapper::flushToDisk(StringRef outputDir, DiagnosticsEngine &Diag) {
112  using namespace llvm::sys;
113
114  bool existed;
115  if (fs::create_directory(outputDir, existed) != llvm::errc::success)
116    return report("Could not create directory: " + outputDir, Diag);
117
118  std::string infoFile = getRemapInfoFile(outputDir);
119  return flushToFile(infoFile, Diag);
120}
121
122bool FileRemapper::flushToFile(StringRef outputPath, DiagnosticsEngine &Diag) {
123  using namespace llvm::sys;
124
125  std::string errMsg;
126  std::string infoFile = outputPath;
127  llvm::raw_fd_ostream infoOut(infoFile.c_str(), errMsg,
128                               llvm::raw_fd_ostream::F_Binary);
129  if (!errMsg.empty())
130    return report(errMsg, Diag);
131
132  for (MappingsTy::iterator
133         I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
134
135    const FileEntry *origFE = I->first;
136    SmallString<200> origPath = StringRef(origFE->getName());
137    fs::make_absolute(origPath);
138    infoOut << origPath << '\n';
139    infoOut << (uint64_t)origFE->getModificationTime() << '\n';
140
141    if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
142      SmallString<200> newPath = StringRef(FE->getName());
143      fs::make_absolute(newPath);
144      infoOut << newPath << '\n';
145    } else {
146
147      SmallString<64> tempPath;
148      tempPath = path::filename(origFE->getName());
149      tempPath += "-%%%%%%%%";
150      tempPath += path::extension(origFE->getName());
151      int fd;
152      if (fs::unique_file(tempPath.str(), fd, tempPath) != llvm::errc::success)
153        return report("Could not create file: " + tempPath.str(), Diag);
154
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
170bool FileRemapper::overwriteOriginal(DiagnosticsEngine &Diag,
171                                     StringRef outputDir) {
172  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(),
179                 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);
182    } else {
183
184      bool fileExists = false;
185      fs::exists(origFE->getName(), fileExists);
186      if (!fileExists)
187        return report(StringRef("File does not exist: ") + origFE->getName(),
188                      Diag);
189
190      std::string errMsg;
191      llvm::raw_fd_ostream Out(origFE->getName(), errMsg,
192                               llvm::raw_fd_ostream::F_Binary);
193      if (!errMsg.empty())
194        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
206void FileRemapper::applyMappings(PreprocessorOptions &PPOpts) const {
207  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
220void FileRemapper::transferMappingsAndClear(PreprocessorOptions &PPOpts) {
221  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
236void FileRemapper::remap(StringRef filePath, llvm::MemoryBuffer *memBuf) {
237  remap(getOriginalFile(filePath), memBuf);
238}
239
240void FileRemapper::remap(StringRef filePath, StringRef newPath) {
241  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
261const FileEntry *FileRemapper::getOriginalFile(StringRef filePath) {
262  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 *>();
283    ToFromMappings.erase(toFE);
284  }
285}
286
287bool FileRemapper::report(const Twine &err, DiagnosticsEngine &Diag) {
288  SmallString<128> buf;
289  unsigned ID = Diag.getDiagnosticIDs()->getCustomDiagID(DiagnosticIDs::Error,
290                                                         err.toStringRef(buf));
291  Diag.Report(ID);
292  return true;
293}
294