Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: tools/clang/BindMigrate/FileRemapper.cpp

Issue 7886056: Clang plugin that rewrites PostTask(_, NewRunnableMethod(...)) to PostTask(_, base::Bind(...)); (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix lifetime issue where BindMigrateAction is deleted before the ASTConsumer is finished. Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tools/clang/BindMigrate/FileRemapper.h ('k') | tools/clang/BindMigrate/Makefile » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 "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
19 using namespace clang;
20 using namespace arcmt_hack;
21
22 FileRemapper::FileRemapper() {
23 FileMgr.reset(new FileManager(FileSystemOptions()));
24 }
25
26 FileRemapper::~FileRemapper() {
27 clear();
28 }
29
30 void FileRemapper::clear(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
43 std::string FileRemapper::getRemapInfoFile(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
51 bool FileRemapper::initFromDisk(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 llvm::OwningPtr<llvm::MemoryBuffer> fileBuf;
64 if (llvm::error_code ec = llvm::MemoryBuffer::getFile(infoFile.c_str(),
65 fileBuf))
66 return report("Error opening file: " + infoFile, Diag);
67
68 SmallVector<StringRef, 64> lines;
69 fileBuf->getBuffer().split(lines, "\n");
70
71 for (unsigned idx = 0; idx+3 <= lines.size(); idx += 3) {
72 StringRef fromFilename = lines[idx];
73 unsigned long long timeModified;
74 lines[idx+1].getAsInteger(10, timeModified);
75 StringRef toFilename = lines[idx+2];
76
77 const FileEntry *origFE = FileMgr->getFile(fromFilename);
78 if (!origFE) {
79 if (ignoreIfFilesChanged)
80 continue;
81 return report("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("File does not exist: " + toFilename, Diag);
88 }
89
90 if ((uint64_t)origFE->getModificationTime() != timeModified) {
91 if (ignoreIfFilesChanged)
92 continue;
93 return report("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
105 bool FileRemapper::flushToDisk(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("Could not create directory: " + outputDir, Diag);
111
112 std::string errMsg;
113 std::string infoFile = getRemapInfoFile(outputDir);
114 llvm::raw_fd_ostream infoOut(infoFile.c_str(), errMsg,
115 llvm::raw_fd_ostream::F_Binary);
116 if (!errMsg.empty())
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;
123 llvm::SmallString<200> origPath = StringRef(origFE->getName());
124 fs::make_absolute(origPath);
125 infoOut << origPath << '\n';
126 infoOut << (uint64_t)origFE->getModificationTime() << '\n';
127
128 if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
129 llvm::SmallString<200> newPath = StringRef(FE->getName());
130 fs::make_absolute(newPath);
131 infoOut << newPath << '\n';
132 } 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("Could not create file: " + tempPath.str(), Diag);
141
142 llvm::raw_fd_ostream newOut(fd, /*shouldClose=*/true);
143 llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
144 newOut.write(mem->getBufferStart(), mem->getBufferSize());
145 newOut.close();
146
147 const FileEntry *newE = FileMgr->getFile(tempPath);
148 remap(origFE, newE);
149 infoOut << newE->getName() << '\n';
150 }
151 }
152
153 infoOut.close();
154 return false;
155 }
156
157 bool FileRemapper::overwriteOriginal(Diagnostic &Diag,
158 StringRef outputDir) {
159 using namespace llvm::sys;
160
161 for (MappingsTy::iterator
162 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
163 const FileEntry *origFE = I->first;
164 if (const FileEntry *newFE = I->second.dyn_cast<const FileEntry *>()) {
165 if (fs::copy_file(newFE->getName(), origFE->getName(),
166 fs::copy_option::overwrite_if_exists) != llvm::errc::success)
167 return report(StringRef("Could not copy file '") + newFE->getName() +
168 "' to file '" + origFE->getName() + "'", Diag);
169 } else {
170
171 bool fileExists = false;
172 fs::exists(origFE->getName(), fileExists);
173 if (!fileExists)
174 return report(StringRef("File does not exist: ") + origFE->getName(),
175 Diag);
176
177 std::string errMsg;
178 llvm::raw_fd_ostream Out(origFE->getName(), errMsg,
179 llvm::raw_fd_ostream::F_Binary);
180 if (!errMsg.empty())
181 return report(errMsg, Diag);
182
183 llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
184 Out.write(mem->getBufferStart(), mem->getBufferSize());
185 Out.close();
186 }
187 }
188
189 clear(outputDir);
190 return false;
191 }
192
193 bool FileRemapper::writeParallelTree(StringRef outputDir,
194 Diagnostic &Diag) {
195 using namespace llvm::sys;
196 bool existed;
197
198 for (MappingsTy::iterator
199 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
200 const FileEntry *origFE = I->first;
201 llvm::SmallString<512> abs_filename;
202 llvm::Twine(origFE->getName()).toVector(abs_filename);
203 llvm::sys::fs::make_absolute(abs_filename);
204 llvm::errs() << "abs_Filename " << abs_filename << "\n";
205
206 std::string new_name = outputDir.str() + abs_filename.c_str();
207 StringRef directory = path::parent_path(new_name);
208 llvm::errs() << "Writing to " << new_name << "\n";
209 if (fs::create_directories(directory, existed) != llvm::errc::success)
210 return report("Could not create directory: " + directory, Diag);
211 if (const FileEntry *newFE = I->second.dyn_cast<const FileEntry *>()) {
212 if (fs::copy_file(newFE->getName(), new_name.c_str(),
213 fs::copy_option::overwrite_if_exists) != llvm::errc::success)
214 return report(StringRef("Could not copy file '") + newFE->getName() +
215 "' to file '" + new_name + "'", Diag);
216 } else {
217 std::string errMsg;
218 llvm::raw_fd_ostream Out(new_name.c_str(), errMsg,
219 llvm::raw_fd_ostream::F_Binary);
220 if (!errMsg.empty())
221 return report(errMsg, Diag);
222
223 llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
224 Out.write(mem->getBufferStart(), mem->getBufferSize());
225 Out.close();
226 }
227 }
228
229 return false;
230 }
231
232 void FileRemapper::applyMappings(CompilerInvocation &CI) const {
233 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
234 for (MappingsTy::const_iterator
235 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
236 if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
237 PPOpts.addRemappedFile(I->first->getName(), FE->getName());
238 } else {
239 llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
240 PPOpts.addRemappedFile(I->first->getName(), mem);
241 }
242 }
243
244 PPOpts.RetainRemappedFileBuffers = true;
245 }
246
247 void FileRemapper::transferMappingsAndClear(CompilerInvocation &CI) {
248 PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
249 for (MappingsTy::iterator
250 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
251 if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) {
252 PPOpts.addRemappedFile(I->first->getName(), FE->getName());
253 } else {
254 llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
255 PPOpts.addRemappedFile(I->first->getName(), mem);
256 }
257 I->second = Target();
258 }
259
260 PPOpts.RetainRemappedFileBuffers = false;
261 clear();
262 }
263
264 void FileRemapper::remap(StringRef filePath, llvm::MemoryBuffer *memBuf) {
265 remap(getOriginalFile(filePath), memBuf);
266 }
267
268 void FileRemapper::remap(StringRef filePath, StringRef newPath) {
269 const FileEntry *file = getOriginalFile(filePath);
270 const FileEntry *newfile = FileMgr->getFile(newPath);
271 remap(file, newfile);
272 }
273
274 void FileRemapper::remap(const FileEntry *file, llvm::MemoryBuffer *memBuf) {
275 assert(file);
276 Target &targ = FromToMappings[file];
277 resetTarget(targ);
278 targ = memBuf;
279 }
280
281 void FileRemapper::remap(const FileEntry *file, const FileEntry *newfile) {
282 assert(file && newfile);
283 Target &targ = FromToMappings[file];
284 resetTarget(targ);
285 targ = newfile;
286 ToFromMappings[newfile] = file;
287 }
288
289 const FileEntry *FileRemapper::getOriginalFile(StringRef filePath) {
290 const FileEntry *file = FileMgr->getFile(filePath);
291 // If we are updating a file that overriden an original file,
292 // actually update the original file.
293 llvm::DenseMap<const FileEntry *, const FileEntry *>::iterator
294 I = ToFromMappings.find(file);
295 if (I != ToFromMappings.end()) {
296 file = I->second;
297 assert(FromToMappings.find(file) != FromToMappings.end() &&
298 "Original file not in mappings!");
299 }
300 return file;
301 }
302
303 void FileRemapper::resetTarget(Target &targ) {
304 if (!targ)
305 return;
306
307 if (llvm::MemoryBuffer *oldmem = targ.dyn_cast<llvm::MemoryBuffer *>()) {
308 delete oldmem;
309 } else {
310 const FileEntry *toFE = targ.get<const FileEntry *>();
311 llvm::DenseMap<const FileEntry *, const FileEntry *>::iterator
312 I = ToFromMappings.find(toFE);
313 if (I != ToFromMappings.end())
314 ToFromMappings.erase(I);
315 }
316 }
317
318 bool FileRemapper::report(const Twine &err, Diagnostic &Diag) {
319 llvm::SmallString<128> buf;
320 unsigned ID = Diag.getDiagnosticIDs()->getCustomDiagID(DiagnosticIDs::Error,
321 err.toStringRef(buf));
322 Diag.Report(ID);
323 return true;
324 }
OLDNEW
« no previous file with comments | « tools/clang/BindMigrate/FileRemapper.h ('k') | tools/clang/BindMigrate/Makefile » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698