Implement -working-directory.
When -working-directory is passed in command line, file paths are resolved relative to the specified directory.
This helps both when using libclang (where we can't require the user to actually change the working directory)
and to help reproduce test cases when the reproduction work comes along.
--FileSystemOptions is introduced which controls how file system operations are performed (currently it just contains
the working directory value if set).
--FileSystemOptions are passed around to various interfaces that perform file operations.
--Opening & reading the content of files should be done only through FileManager. This is useful in general since
file operations will be abstracted in the future for the reproduction mechanism.
FileSystemOptions is independent of FileManager so that we can have multiple translation units sharing the same
FileManager but with different FileSystemOptions.
Addresses rdar://8583824.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@118203 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Lex/HeaderMap.cpp b/lib/Lex/HeaderMap.cpp
index 4010d61..5d9b218 100644
--- a/lib/Lex/HeaderMap.cpp
+++ b/lib/Lex/HeaderMap.cpp
@@ -75,13 +75,14 @@
/// map. If it doesn't look like a HeaderMap, it gives up and returns null.
/// If it looks like a HeaderMap but is obviously corrupted, it puts a reason
/// into the string error argument and returns null.
-const HeaderMap *HeaderMap::Create(const FileEntry *FE) {
+const HeaderMap *HeaderMap::Create(const FileEntry *FE, FileManager &FM,
+ const FileSystemOptions &FSOpts) {
// If the file is too small to be a header map, ignore it.
unsigned FileSize = FE->getSize();
if (FileSize <= sizeof(HMapHeader)) return 0;
llvm::OwningPtr<const llvm::MemoryBuffer> FileBuffer(
- llvm::MemoryBuffer::getFile(FE->getName(), 0, FE->getSize()));
+ FM.getBufferForFile(FE, FSOpts));
if (FileBuffer == 0) return 0; // Unreadable file?
const char *FileStart = FileBuffer->getBufferStart();
@@ -200,7 +201,8 @@
/// LookupFile - Check to see if the specified relative filename is located in
/// this HeaderMap. If so, open it and return its FileEntry.
const FileEntry *HeaderMap::LookupFile(llvm::StringRef Filename,
- FileManager &FM) const {
+ FileManager &FM,
+ const FileSystemOptions &FileSystemOpts) const {
const HMapHeader &Hdr = getHeader();
unsigned NumBuckets = getEndianAdjustedWord(Hdr.NumBuckets);
@@ -223,6 +225,6 @@
llvm::SmallString<1024> DestPath;
DestPath += getString(B.Prefix);
DestPath += getString(B.Suffix);
- return FM.getFile(DestPath.begin(), DestPath.end());
+ return FM.getFile(DestPath.begin(), DestPath.end(), FileSystemOpts);
}
}
diff --git a/lib/Lex/HeaderSearch.cpp b/lib/Lex/HeaderSearch.cpp
index 4554aba..5a11652 100644
--- a/lib/Lex/HeaderSearch.cpp
+++ b/lib/Lex/HeaderSearch.cpp
@@ -32,7 +32,8 @@
return ControllingMacro;
}
-HeaderSearch::HeaderSearch(FileManager &FM) : FileMgr(FM), FrameworkMap(64) {
+HeaderSearch::HeaderSearch(FileManager &FM, const FileSystemOptions &FSOpts)
+ : FileMgr(FM), FileSystemOpts(FSOpts), FrameworkMap(64) {
SystemDirIdx = 0;
NoCurDirSearch = false;
@@ -83,7 +84,7 @@
return HeaderMaps[i].second;
}
- if (const HeaderMap *HM = HeaderMap::Create(FE)) {
+ if (const HeaderMap *HM = HeaderMap::Create(FE, FileMgr, FileSystemOpts)) {
HeaderMaps.push_back(std::make_pair(FE, HM));
return HM;
}
@@ -118,14 +119,16 @@
TmpDir += getDir()->getName();
TmpDir.push_back('/');
TmpDir.append(Filename.begin(), Filename.end());
- return HS.getFileMgr().getFile(TmpDir.begin(), TmpDir.end());
+ return HS.getFileMgr().getFile(TmpDir.begin(), TmpDir.end(),
+ HS.getFileSystemOpts());
}
if (isFramework())
return DoFrameworkLookup(Filename, HS);
assert(isHeaderMap() && "Unknown directory lookup");
- return getHeaderMap()->LookupFile(Filename, HS.getFileMgr());
+ return getHeaderMap()->LookupFile(Filename, HS.getFileMgr(),
+ HS.getFileSystemOpts());
}
@@ -134,6 +137,7 @@
const FileEntry *DirectoryLookup::DoFrameworkLookup(llvm::StringRef Filename,
HeaderSearch &HS) const {
FileManager &FileMgr = HS.getFileMgr();
+ const FileSystemOptions &FileSystemOpts = HS.getFileSystemOpts();
// Framework names must have a '/' in the filename.
size_t SlashPos = Filename.find('/');
@@ -184,7 +188,8 @@
FrameworkName += "Headers/";
FrameworkName.append(Filename.begin()+SlashPos+1, Filename.end());
if (const FileEntry *FE = FileMgr.getFile(FrameworkName.begin(),
- FrameworkName.end())) {
+ FrameworkName.end(),
+ FileSystemOpts)) {
return FE;
}
@@ -192,7 +197,8 @@
const char *Private = "Private";
FrameworkName.insert(FrameworkName.begin()+OrigSize, Private,
Private+strlen(Private));
- return FileMgr.getFile(FrameworkName.begin(), FrameworkName.end());
+ return FileMgr.getFile(FrameworkName.begin(), FrameworkName.end(),
+ FileSystemOpts);
}
@@ -219,7 +225,7 @@
if (FromDir) return 0;
// Otherwise, just return the file.
- return FileMgr.getFile(Filename);
+ return FileMgr.getFile(Filename, FileSystemOpts);
}
// Step #0, unless disabled, check to see if the file is in the #includer's
@@ -234,7 +240,7 @@
TmpDir += CurFileEnt->getDir()->getName();
TmpDir.push_back('/');
TmpDir.append(Filename.begin(), Filename.end());
- if (const FileEntry *FE = FileMgr.getFile(TmpDir.str())) {
+ if (const FileEntry *FE = FileMgr.getFile(TmpDir.str(), FileSystemOpts)) {
// Leave CurDir unset.
// This file is a system header or C++ unfriendly if the old file is.
//
@@ -344,7 +350,8 @@
// If the framework dir doesn't exist, we fail.
const DirectoryEntry *Dir = FileMgr.getDirectory(FrameworkName.begin(),
- FrameworkName.end());
+ FrameworkName.end(),
+ FileSystemOpts);
if (Dir == 0) return 0;
// Otherwise, if it does, remember that this is the right direntry for this
@@ -359,13 +366,14 @@
HeadersFilename += "Headers/";
HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
if (!(FE = FileMgr.getFile(HeadersFilename.begin(),
- HeadersFilename.end()))) {
+ HeadersFilename.end(), FileSystemOpts))) {
// Check ".../Frameworks/HIToolbox.framework/PrivateHeaders/HIToolbox.h"
HeadersFilename = FrameworkName;
HeadersFilename += "PrivateHeaders/";
HeadersFilename.append(Filename.begin()+SlashPos+1, Filename.end());
- if (!(FE = FileMgr.getFile(HeadersFilename.begin(), HeadersFilename.end())))
+ if (!(FE = FileMgr.getFile(HeadersFilename.begin(), HeadersFilename.end(),
+ FileSystemOpts)))
return 0;
}
diff --git a/lib/Lex/PTHLexer.cpp b/lib/Lex/PTHLexer.cpp
index 63b4823..3d52159 100644
--- a/lib/Lex/PTHLexer.cpp
+++ b/lib/Lex/PTHLexer.cpp
@@ -434,10 +434,12 @@
Diags.Report(Diags.getCustomDiagID(Diagnostic::Error, Msg));
}
-PTHManager* PTHManager::Create(const std::string& file, Diagnostic &Diags) {
+PTHManager* PTHManager::Create(const std::string& file, FileManager &FileMgr,
+ const FileSystemOptions &FSOpts,
+ Diagnostic &Diags) {
// Memory map the PTH file.
llvm::OwningPtr<llvm::MemoryBuffer>
- File(llvm::MemoryBuffer::getFile(file.c_str()));
+ File(FileMgr.getBufferForFile(file, FSOpts));
if (!File) {
Diags.Report(diag::err_invalid_pth_file) << file;
diff --git a/lib/Lex/Preprocessor.cpp b/lib/Lex/Preprocessor.cpp
index 1be22df..ff18c3f 100644
--- a/lib/Lex/Preprocessor.cpp
+++ b/lib/Lex/Preprocessor.cpp
@@ -53,7 +53,8 @@
IdentifierInfoLookup* IILookup,
bool OwnsHeaders)
: Diags(&diags), Features(opts), Target(target),FileMgr(Headers.getFileMgr()),
- SourceMgr(SM), HeaderInfo(Headers), ExternalSource(0),
+ FileSystemOpts(Headers.getFileSystemOpts()), SourceMgr(SM),
+ HeaderInfo(Headers), ExternalSource(0),
Identifiers(opts, IILookup), BuiltinInfo(Target), CodeComplete(0),
CodeCompletionFile(0), SkipMainFilePreamble(0, true), CurPPLexer(0),
CurDirLookup(0), Callbacks(0), MacroArgCache(0), Record(0), MIChainHead(0),