13 #include "llvm/Support/FileSystem.h" 14 #include "llvm/Support/MemoryBuffer.h" 15 #include "llvm/Support/Path.h" 16 #include "llvm/Support/raw_ostream.h" 19 using namespace clang;
20 using namespace arcmt;
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);
42 std::string FileRemapper::getRemapInfoFile(StringRef outputDir) {
43 assert(!outputDir.empty());
45 llvm::sys::path::append(InfoFile,
"remap");
46 return InfoFile.str();
50 bool ignoreIfFilesChanged) {
51 std::string infoFile = getRemapInfoFile(outputDir);
52 return initFromFile(infoFile, Diag, ignoreIfFilesChanged);
56 bool ignoreIfFilesChanged) {
57 assert(FromToMappings.empty() &&
58 "initFromDisk should be called before any remap calls");
59 std::string infoFile = filePath;
60 if (!llvm::sys::fs::exists(infoFile))
63 std::vector<std::pair<const FileEntry *, const FileEntry *> > pairs;
65 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> fileBuf =
66 llvm::MemoryBuffer::getFile(infoFile);
68 return report(
"Error opening file: " + infoFile, Diag);
71 fileBuf.get()->getBuffer().split(lines,
"\n");
73 for (
unsigned idx = 0; idx+3 <= lines.size(); idx += 3) {
74 StringRef fromFilename = lines[idx];
75 unsigned long long timeModified;
76 if (lines[idx+1].getAsInteger(10, timeModified))
77 return report(
"Invalid file data: '" + lines[idx+1] +
"' not a number",
79 StringRef toFilename = lines[idx+2];
81 const FileEntry *origFE = FileMgr->getFile(fromFilename);
83 if (ignoreIfFilesChanged)
85 return report(
"File does not exist: " + fromFilename, Diag);
87 const FileEntry *newFE = FileMgr->getFile(toFilename);
89 if (ignoreIfFilesChanged)
91 return report(
"File does not exist: " + toFilename, Diag);
95 if (ignoreIfFilesChanged)
97 return report(
"File was modified: " + fromFilename, Diag);
100 pairs.push_back(std::make_pair(origFE, newFE));
103 for (
unsigned i = 0, e = pairs.size();
i != e; ++
i)
104 remap(pairs[
i].first, pairs[
i].second);
112 if (fs::create_directory(outputDir))
113 return report(
"Could not create directory: " + outputDir, Diag);
115 std::string infoFile = getRemapInfoFile(outputDir);
123 std::string infoFile = outputPath;
124 llvm::raw_fd_ostream infoOut(infoFile, EC, llvm::sys::fs::F_None);
126 return report(EC.message(),
Diag);
128 for (MappingsTy::iterator
129 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
133 fs::make_absolute(origPath);
134 infoOut << origPath <<
'\n';
139 fs::make_absolute(newPath);
140 infoOut << newPath <<
'\n';
145 if (fs::createTemporaryFile(path::filename(origFE->
getName()),
146 path::extension(origFE->
getName()).drop_front(), fd,
148 return report(
"Could not create file: " + tempPath.str(),
Diag);
150 llvm::raw_fd_ostream newOut(fd,
true);
151 llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
152 newOut.write(mem->getBufferStart(), mem->getBufferSize());
155 const FileEntry *newE = FileMgr->getFile(tempPath);
157 infoOut << newE->
getName() <<
'\n';
166 StringRef outputDir) {
169 for (MappingsTy::iterator
170 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
172 assert(I->second.is<llvm::MemoryBuffer *>());
173 if (!fs::exists(origFE->
getName()))
174 return report(StringRef(
"File does not exist: ") + origFE->
getName(),
178 llvm::raw_fd_ostream Out(origFE->
getName(), EC, llvm::sys::fs::F_None);
180 return report(EC.message(),
Diag);
182 llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
183 Out.write(mem->getBufferStart(), mem->getBufferSize());
192 for (MappingsTy::const_iterator
193 I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) {
197 llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>();
206 std::unique_ptr<llvm::MemoryBuffer> memBuf) {
207 remap(getOriginalFile(filePath), std::move(memBuf));
211 std::unique_ptr<llvm::MemoryBuffer> memBuf) {
213 Target &targ = FromToMappings[file];
215 targ = memBuf.release();
219 assert(file && newfile);
220 Target &targ = FromToMappings[file];
223 ToFromMappings[newfile] = file;
226 const FileEntry *FileRemapper::getOriginalFile(StringRef filePath) {
227 const FileEntry *file = FileMgr->getFile(filePath);
230 llvm::DenseMap<const FileEntry *, const FileEntry *>::iterator
231 I = ToFromMappings.find(file);
232 if (I != ToFromMappings.end()) {
234 assert(FromToMappings.find(file) != FromToMappings.end() &&
235 "Original file not in mappings!");
240 void FileRemapper::resetTarget(Target &targ) {
244 if (llvm::MemoryBuffer *oldmem = targ.dyn_cast<llvm::MemoryBuffer *>()) {
248 ToFromMappings.erase(toFE);
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
Implements support for file system lookup, file system caching, and directory search management...
Defines the clang::FileManager interface and associated types.
time_t getModificationTime() const
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
void applyMappings(PreprocessorOptions &PPOpts) const
void addRemappedFile(StringRef From, StringRef To)
void remap(StringRef filePath, std::unique_ptr< llvm::MemoryBuffer > memBuf)
Concrete class used by the front-end to report problems and issues.
Defines the Diagnostic-related interfaces.
bool RetainRemappedFileBuffers
Whether the compiler instance should retain (i.e., not free) the buffers associated with remapped fil...
bool flushToFile(StringRef outputPath, DiagnosticsEngine &Diag)
bool initFromFile(StringRef filePath, DiagnosticsEngine &Diag, bool ignoreIfFilesChanged)
bool overwriteOriginal(DiagnosticsEngine &Diag, StringRef outputDir=StringRef())
StringRef getName() const
bool flushToDisk(StringRef outputDir, DiagnosticsEngine &Diag)
Cached information about one file (either on disk or in the virtual file system). ...
unsigned getCustomDiagID(Level L, const char(&FormatString)[N])
Return an ID for a diagnostic with the specified format string and level.
Dataflow Directional Tag Classes.
Keeps track of options that affect how file operations are performed.
bool initFromDisk(StringRef outputDir, DiagnosticsEngine &Diag, bool ignoreIfFilesChanged)
void clear(StringRef outputDir=StringRef())