diff options
Diffstat (limited to 'contrib/llvm/tools/clang/lib/ARCMigrate/FileRemapper.cpp')
-rw-r--r-- | contrib/llvm/tools/clang/lib/ARCMigrate/FileRemapper.cpp | 82 |
1 files changed, 38 insertions, 44 deletions
diff --git a/contrib/llvm/tools/clang/lib/ARCMigrate/FileRemapper.cpp b/contrib/llvm/tools/clang/lib/ARCMigrate/FileRemapper.cpp index db26c29..c6e6ce4 100644 --- a/contrib/llvm/tools/clang/lib/ARCMigrate/FileRemapper.cpp +++ b/contrib/llvm/tools/clang/lib/ARCMigrate/FileRemapper.cpp @@ -27,7 +27,7 @@ FileRemapper::~FileRemapper() { clear(); } -void FileRemapper::clear(llvm::StringRef outputDir) { +void FileRemapper::clear(StringRef outputDir) { for (MappingsTy::iterator I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) resetTarget(I->second); @@ -40,7 +40,7 @@ void FileRemapper::clear(llvm::StringRef outputDir) { } } -std::string FileRemapper::getRemapInfoFile(llvm::StringRef outputDir) { +std::string FileRemapper::getRemapInfoFile(StringRef outputDir) { assert(!outputDir.empty()); llvm::sys::Path dir(outputDir); llvm::sys::Path infoFile = dir; @@ -48,7 +48,7 @@ std::string FileRemapper::getRemapInfoFile(llvm::StringRef outputDir) { return infoFile.str(); } -bool FileRemapper::initFromDisk(llvm::StringRef outputDir, Diagnostic &Diag, +bool FileRemapper::initFromDisk(StringRef outputDir, DiagnosticsEngine &Diag, bool ignoreIfFilesChanged) { assert(FromToMappings.empty() && "initFromDisk should be called before any remap calls"); @@ -59,38 +59,38 @@ bool FileRemapper::initFromDisk(llvm::StringRef outputDir, Diagnostic &Diag, return false; std::vector<std::pair<const FileEntry *, const FileEntry *> > pairs; - - std::ifstream fin(infoFile.c_str()); - if (!fin.good()) - return report(std::string("Error opening file: ") + infoFile, Diag); - - while (true) { - std::string fromFilename, toFilename; - uint64_t timeModified; - - fin >> fromFilename >> timeModified >> toFilename; - if (fin.eof()) - break; - if (!fin.good()) - return report(std::string("Error in format of file: ") + infoFile, Diag); - + + llvm::OwningPtr<llvm::MemoryBuffer> fileBuf; + if (llvm::error_code ec = llvm::MemoryBuffer::getFile(infoFile.c_str(), + fileBuf)) + return report("Error opening file: " + infoFile, Diag); + + SmallVector<StringRef, 64> lines; + fileBuf->getBuffer().split(lines, "\n"); + + for (unsigned idx = 0; idx+3 <= lines.size(); idx += 3) { + StringRef fromFilename = lines[idx]; + unsigned long long timeModified; + lines[idx+1].getAsInteger(10, timeModified); + StringRef toFilename = lines[idx+2]; + const FileEntry *origFE = FileMgr->getFile(fromFilename); if (!origFE) { if (ignoreIfFilesChanged) continue; - return report(std::string("File does not exist: ") + fromFilename, Diag); + return report("File does not exist: " + fromFilename, Diag); } const FileEntry *newFE = FileMgr->getFile(toFilename); if (!newFE) { if (ignoreIfFilesChanged) continue; - return report(std::string("File does not exist: ") + toFilename, Diag); + return report("File does not exist: " + toFilename, Diag); } if ((uint64_t)origFE->getModificationTime() != timeModified) { if (ignoreIfFilesChanged) continue; - return report(std::string("File was modified: ") + fromFilename, Diag); + return report("File was modified: " + fromFilename, Diag); } pairs.push_back(std::make_pair(origFE, newFE)); @@ -102,13 +102,12 @@ bool FileRemapper::initFromDisk(llvm::StringRef outputDir, Diagnostic &Diag, return false; } -bool FileRemapper::flushToDisk(llvm::StringRef outputDir, Diagnostic &Diag) { +bool FileRemapper::flushToDisk(StringRef outputDir, DiagnosticsEngine &Diag) { using namespace llvm::sys; bool existed; if (fs::create_directory(outputDir, existed) != llvm::errc::success) - return report(std::string("Could not create directory: ") + outputDir.str(), - Diag); + return report("Could not create directory: " + outputDir, Diag); std::string errMsg; std::string infoFile = getRemapInfoFile(outputDir); @@ -121,13 +120,13 @@ bool FileRemapper::flushToDisk(llvm::StringRef outputDir, Diagnostic &Diag) { I = FromToMappings.begin(), E = FromToMappings.end(); I != E; ++I) { const FileEntry *origFE = I->first; - llvm::SmallString<200> origPath = llvm::StringRef(origFE->getName()); + llvm::SmallString<200> origPath = StringRef(origFE->getName()); fs::make_absolute(origPath); infoOut << origPath << '\n'; infoOut << (uint64_t)origFE->getModificationTime() << '\n'; if (const FileEntry *FE = I->second.dyn_cast<const FileEntry *>()) { - llvm::SmallString<200> newPath = llvm::StringRef(FE->getName()); + llvm::SmallString<200> newPath = StringRef(FE->getName()); fs::make_absolute(newPath); infoOut << newPath << '\n'; } else { @@ -138,8 +137,7 @@ bool FileRemapper::flushToDisk(llvm::StringRef outputDir, Diagnostic &Diag) { tempPath += path::extension(origFE->getName()); int fd; if (fs::unique_file(tempPath.str(), fd, tempPath) != llvm::errc::success) - return report(std::string("Could not create file: ") + tempPath.c_str(), - Diag); + return report("Could not create file: " + tempPath.str(), Diag); llvm::raw_fd_ostream newOut(fd, /*shouldClose=*/true); llvm::MemoryBuffer *mem = I->second.get<llvm::MemoryBuffer *>(); @@ -156,8 +154,8 @@ bool FileRemapper::flushToDisk(llvm::StringRef outputDir, Diagnostic &Diag) { return false; } -bool FileRemapper::overwriteOriginal(Diagnostic &Diag, - llvm::StringRef outputDir) { +bool FileRemapper::overwriteOriginal(DiagnosticsEngine &Diag, + StringRef outputDir) { using namespace llvm::sys; for (MappingsTy::iterator @@ -165,20 +163,15 @@ bool FileRemapper::overwriteOriginal(Diagnostic &Diag, const FileEntry *origFE = I->first; if (const FileEntry *newFE = I->second.dyn_cast<const FileEntry *>()) { if (fs::copy_file(newFE->getName(), origFE->getName(), - fs::copy_option::overwrite_if_exists) != llvm::errc::success) { - std::string err = "Could not copy file '"; - llvm::raw_string_ostream os(err); - os << "Could not copy file '" << newFE->getName() << "' to file '" - << origFE->getName() << "'"; - os.flush(); - return report(err, Diag); - } + fs::copy_option::overwrite_if_exists) != llvm::errc::success) + return report(StringRef("Could not copy file '") + newFE->getName() + + "' to file '" + origFE->getName() + "'", Diag); } else { bool fileExists = false; fs::exists(origFE->getName(), fileExists); if (!fileExists) - return report(std::string("File does not exist: ") + origFE->getName(), + return report(StringRef("File does not exist: ") + origFE->getName(), Diag); std::string errMsg; @@ -229,11 +222,11 @@ void FileRemapper::transferMappingsAndClear(CompilerInvocation &CI) { clear(); } -void FileRemapper::remap(llvm::StringRef filePath, llvm::MemoryBuffer *memBuf) { +void FileRemapper::remap(StringRef filePath, llvm::MemoryBuffer *memBuf) { remap(getOriginalFile(filePath), memBuf); } -void FileRemapper::remap(llvm::StringRef filePath, llvm::StringRef newPath) { +void FileRemapper::remap(StringRef filePath, StringRef newPath) { const FileEntry *file = getOriginalFile(filePath); const FileEntry *newfile = FileMgr->getFile(newPath); remap(file, newfile); @@ -254,7 +247,7 @@ void FileRemapper::remap(const FileEntry *file, const FileEntry *newfile) { ToFromMappings[newfile] = file; } -const FileEntry *FileRemapper::getOriginalFile(llvm::StringRef filePath) { +const FileEntry *FileRemapper::getOriginalFile(StringRef filePath) { const FileEntry *file = FileMgr->getFile(filePath); // If we are updating a file that overriden an original file, // actually update the original file. @@ -283,9 +276,10 @@ void FileRemapper::resetTarget(Target &targ) { } } -bool FileRemapper::report(const std::string &err, Diagnostic &Diag) { +bool FileRemapper::report(const Twine &err, DiagnosticsEngine &Diag) { + llvm::SmallString<128> buf; unsigned ID = Diag.getDiagnosticIDs()->getCustomDiagID(DiagnosticIDs::Error, - err); + err.toStringRef(buf)); Diag.Report(ID); return true; } |