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/Basic/Diagnostic.h"
12 #include "clang/Basic/FileManager.h"
13 #include "clang/Lex/PreprocessorOptions.h"
14 #include "llvm/Support/FileSystem.h"
15 #include "llvm/Support/MemoryBuffer.h"
16 #include "llvm/Support/Path.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include <fstream>
19 
20 using namespace clang;
21 using namespace arcmt;
22 
23 FileRemapper::FileRemapper() {
24   FileMgr.reset(new FileManager(FileSystemOptions()));
25 }
26 
27 FileRemapper::~FileRemapper() {
28   clear();
29 }
30 
31 void 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     llvm::sys::fs::remove(infoFile);
40   }
41 }
42 
43 std::string FileRemapper::getRemapInfoFile(StringRef outputDir) {
44   assert(!outputDir.empty());
45   SmallString<128> InfoFile = outputDir;
46   llvm::sys::path::append(InfoFile, "remap");
47   return InfoFile.str();
48 }
49 
50 bool FileRemapper::initFromDisk(StringRef outputDir, DiagnosticsEngine &Diag,
51                                 bool ignoreIfFilesChanged) {
52   std::string infoFile = getRemapInfoFile(outputDir);
53   return initFromFile(infoFile, Diag, ignoreIfFilesChanged);
54 }
55 
56 bool FileRemapper::initFromFile(StringRef filePath, DiagnosticsEngine &Diag,
57                                 bool ignoreIfFilesChanged) {
58   assert(FromToMappings.empty() &&
59          "initFromDisk should be called before any remap calls");
60   std::string infoFile = filePath;
61   bool fileExists = false;
62   llvm::sys::fs::exists(infoFile, fileExists);
63   if (!fileExists)
64     return false;
65 
66   std::vector<std::pair<const FileEntry *, const FileEntry *> > pairs;
67 
68   OwningPtr<llvm::MemoryBuffer> fileBuf;
69   if (llvm::MemoryBuffer::getFile(infoFile.c_str(), fileBuf))
70     return report("Error opening file: " + infoFile, Diag);
71 
72   SmallVector<StringRef, 64> lines;
73   fileBuf->getBuffer().split(lines, "\n");
74 
75   for (unsigned idx = 0; idx+3 <= lines.size(); idx += 3) {
76     StringRef fromFilename = lines[idx];
77     unsigned long long timeModified;
78     if (lines[idx+1].getAsInteger(10, timeModified))
79       return report("Invalid file data: '" + lines[idx+1] + "' not a number",
80                     Diag);
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 
111 bool FileRemapper::flushToDisk(StringRef outputDir, DiagnosticsEngine &Diag) {
112   using namespace llvm::sys;
113 
114   if (fs::create_directory(outputDir) != llvm::errc::success)
115     return report("Could not create directory: " + outputDir, Diag);
116 
117   std::string infoFile = getRemapInfoFile(outputDir);
118   return flushToFile(infoFile, Diag);
119 }
120 
121 bool FileRemapper::flushToFile(StringRef outputPath, DiagnosticsEngine &Diag) {
122   using namespace llvm::sys;
123 
124   std::string errMsg;
125   std::string infoFile = outputPath;
126   llvm::raw_fd_ostream infoOut(infoFile.c_str(), errMsg,
127                                llvm::sys::fs::F_Binary);
128   if (!errMsg.empty())
129     return report(errMsg, Diag);
130 
131   for (MappingsTy::iterator
132          I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
133 
134     const FileEntry *origFE = I->first;
135     SmallString<200> origPath = StringRef(origFE->getName());
136     fs::make_absolute(origPath);
137     infoOut << origPath << '\n';
138     infoOut << (uint64_t)origFE->getModificationTime() << '\n';
139 
140     if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
141       SmallString<200> newPath = StringRef(FE->getName());
142       fs::make_absolute(newPath);
143       infoOut << newPath << '\n';
144     } else {
145 
146       SmallString<64> tempPath;
147       int fd;
148       if (fs::createTemporaryFile(path::filename(origFE->getName()),
149                                   path::extension(origFE->getName()), fd,
150                                   tempPath))
151         return report("Could not create file: " + tempPath.str(), Diag);
152 
153       llvm::raw_fd_ostream newOut(fd, /*shouldClose=*/true);
154       llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
155       newOut.write(mem->getBufferStart(), mem->getBufferSize());
156       newOut.close();
157 
158       const FileEntry *newE = FileMgr->getFile(tempPath);
159       remap(origFE, newE);
160       infoOut << newE->getName() << '\n';
161     }
162   }
163 
164   infoOut.close();
165   return false;
166 }
167 
168 bool FileRemapper::overwriteOriginal(DiagnosticsEngine &Diag,
169                                      StringRef outputDir) {
170   using namespace llvm::sys;
171 
172   for (MappingsTy::iterator
173          I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
174     const FileEntry *origFE = I->first;
175     assert(I->second.is<llvm::MemoryBuffer *>());
176     bool fileExists = false;
177     fs::exists(origFE->getName(), fileExists);
178     if (!fileExists)
179       return report(StringRef("File does not exist: ") + origFE->getName(),
180                     Diag);
181 
182     std::string errMsg;
183     llvm::raw_fd_ostream Out(origFE->getName(), errMsg,
184                              llvm::sys::fs::F_Binary);
185     if (!errMsg.empty())
186       return report(errMsg, Diag);
187 
188     llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
189     Out.write(mem->getBufferStart(), mem->getBufferSize());
190     Out.close();
191   }
192 
193   clear(outputDir);
194   return false;
195 }
196 
197 void FileRemapper::applyMappings(PreprocessorOptions &PPOpts) const {
198   for (MappingsTy::const_iterator
199          I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
200     if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
201       PPOpts.addRemappedFile(I->first->getName(), FE->getName());
202     } else {
203       llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
204       PPOpts.addRemappedFile(I->first->getName(), mem);
205     }
206   }
207 
208   PPOpts.RetainRemappedFileBuffers = true;
209 }
210 
211 void FileRemapper::transferMappingsAndClear(PreprocessorOptions &PPOpts) {
212   for (MappingsTy::iterator
213          I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
214     if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
215       PPOpts.addRemappedFile(I->first->getName(), FE->getName());
216     } else {
217       llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
218       PPOpts.addRemappedFile(I->first->getName(), mem);
219     }
220     I->second = Target();
221   }
222 
223   PPOpts.RetainRemappedFileBuffers = false;
224   clear();
225 }
226 
227 void FileRemapper::remap(StringRef filePath, llvm::MemoryBuffer *memBuf) {
228   remap(getOriginalFile(filePath), memBuf);
229 }
230 
231 void FileRemapper::remap(const FileEntry *file, llvm::MemoryBuffer *memBuf) {
232   assert(file);
233   Target &targ = FromToMappings[file];
234   resetTarget(targ);
235   targ = memBuf;
236 }
237 
238 void FileRemapper::remap(const FileEntry *file, const FileEntry *newfile) {
239   assert(file && newfile);
240   Target &targ = FromToMappings[file];
241   resetTarget(targ);
242   targ = newfile;
243   ToFromMappings[newfile] = file;
244 }
245 
246 const FileEntry *FileRemapper::getOriginalFile(StringRef filePath) {
247   const FileEntry *file = FileMgr->getFile(filePath);
248   // If we are updating a file that overriden an original file,
249   // actually update the original file.
250   llvm::DenseMap<const FileEntry *, const FileEntry *>::iterator
251     I = ToFromMappings.find(file);
252   if (I != ToFromMappings.end()) {
253     file = I->second;
254     assert(FromToMappings.find(file) != FromToMappings.end() &&
255            "Original file not in mappings!");
256   }
257   return file;
258 }
259 
260 void FileRemapper::resetTarget(Target &targ) {
261   if (!targ)
262     return;
263 
264   if (llvm::MemoryBuffer *oldmem = targ.dyn_cast<llvm::MemoryBuffer *>()) {
265     delete oldmem;
266   } else {
267     const FileEntry *toFE = targ.get<const FileEntry *>();
268     ToFromMappings.erase(toFE);
269   }
270 }
271 
272 bool FileRemapper::report(const Twine &err, DiagnosticsEngine &Diag) {
273   Diag.Report(Diag.getCustomDiagID(DiagnosticsEngine::Error, "%0"))
274       << err.str();
275   return true;
276 }
277