diff options
author | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2010-11-03 22:45:23 +0000 |
---|---|---|
committer | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2010-11-03 22:45:23 +0000 |
commit | 389db16c63eec6ecfa9b235155252d8da766e94e (patch) | |
tree | fee8c776b8591a09c1387bcc7dd3bd1bccb14e7e /include/clang/Basic | |
parent | f1410802d1c9e7ff72b2818ad91fd85283abc6bf (diff) |
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
Diffstat (limited to 'include/clang/Basic')
-rw-r--r-- | include/clang/Basic/FileManager.h | 63 | ||||
-rw-r--r-- | include/clang/Basic/FileSystemOptions.h | 29 | ||||
-rw-r--r-- | include/clang/Basic/SourceManager.h | 15 |
3 files changed, 94 insertions, 13 deletions
diff --git a/include/clang/Basic/FileManager.h b/include/clang/Basic/FileManager.h index e71f51a0e7..44a7079edd 100644 --- a/include/clang/Basic/FileManager.h +++ b/include/clang/Basic/FileManager.h @@ -24,8 +24,16 @@ #include <sys/types.h> #include <sys/stat.h> +namespace llvm { +class MemoryBuffer; +namespace sys { +class Path; +} +} + namespace clang { class FileManager; +class FileSystemOptions; /// DirectoryEntry - Cached information about one directory on the disk. /// @@ -162,9 +170,8 @@ class FileManager { // Caching. llvm::OwningPtr<StatSysCallCache> StatCache; - int stat_cached(const char* path, struct stat* buf) { - return StatCache.get() ? StatCache->stat(path, buf) : stat(path, buf); - } + int stat_cached(const char* path, struct stat* buf, + const FileSystemOptions &FileSystemOpts); public: FileManager(); @@ -189,25 +196,61 @@ public: /// getDirectory - Lookup, cache, and verify the specified directory. This /// returns null if the directory doesn't exist. /// - const DirectoryEntry *getDirectory(llvm::StringRef Filename) { - return getDirectory(Filename.begin(), Filename.end()); + const DirectoryEntry *getDirectory(llvm::StringRef Filename, + const FileSystemOptions &FileSystemOpts) { + return getDirectory(Filename.begin(), Filename.end(), FileSystemOpts); } - const DirectoryEntry *getDirectory(const char *FileStart,const char *FileEnd); + const DirectoryEntry *getDirectory(const char *FileStart,const char *FileEnd, + const FileSystemOptions &FileSystemOpts); /// getFile - Lookup, cache, and verify the specified file. This returns null /// if the file doesn't exist. /// - const FileEntry *getFile(llvm::StringRef Filename) { - return getFile(Filename.begin(), Filename.end()); + const FileEntry *getFile(llvm::StringRef Filename, + const FileSystemOptions &FileSystemOpts) { + return getFile(Filename.begin(), Filename.end(), FileSystemOpts); } const FileEntry *getFile(const char *FilenameStart, - const char *FilenameEnd); + const char *FilenameEnd, + const FileSystemOptions &FileSystemOpts); /// \brief Retrieve a file entry for a "virtual" file that acts as /// if there were a file with the given name on disk. The file /// itself is not accessed. const FileEntry *getVirtualFile(llvm::StringRef Filename, off_t Size, - time_t ModificationTime); + time_t ModificationTime, + const FileSystemOptions &FileSystemOpts); + + /// \brief Open the specified file as a MemoryBuffer, returning a new + /// MemoryBuffer if successful, otherwise returning null. + llvm::MemoryBuffer *getBufferForFile(const FileEntry *Entry, + const FileSystemOptions &FileSystemOpts, + std::string *ErrorStr = 0, + struct stat *FileInfo = 0) { + return getBufferForFile(Entry->getName(), FileSystemOpts, + ErrorStr, Entry->getSize(), FileInfo); + } + llvm::MemoryBuffer *getBufferForFile(llvm::StringRef Filename, + const FileSystemOptions &FileSystemOpts, + std::string *ErrorStr = 0, + int64_t FileSize = -1, + struct stat *FileInfo = 0) { + return getBufferForFile(Filename.begin(), Filename.end(), FileSystemOpts, + ErrorStr, FileSize, FileInfo); + } + llvm::MemoryBuffer *getBufferForFile(const char *FilenameStart, + const char *FilenameEnd, + const FileSystemOptions &FileSystemOpts, + std::string *ErrorStr = 0, + int64_t FileSize = -1, + struct stat *FileInfo = 0); + + /// \brief If path is not absolute and FileSystemOptions set the working + /// directory, the path is modified to be relative to the given + /// working directory. + static void FixupRelativePath(llvm::sys::Path &path, + const FileSystemOptions &FSOpts); + void PrintStats() const; }; diff --git a/include/clang/Basic/FileSystemOptions.h b/include/clang/Basic/FileSystemOptions.h new file mode 100644 index 0000000000..4f3c2ae03d --- /dev/null +++ b/include/clang/Basic/FileSystemOptions.h @@ -0,0 +1,29 @@ +//===--- FileSystemOptions.h - File System Options --------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines the FileSystemOptions interface. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_BASIC_FILESYSTEMOPTIONS_H +#define LLVM_CLANG_BASIC_FILESYSTEMOPTIONS_H + +namespace clang { + +/// \brief Keeps track of options that affect how file operations are performed. +class FileSystemOptions { +public: + /// \brief If set, paths are resolved as if the working directory was + /// set to the value of WorkingDir. + std::string WorkingDir; +}; + +} // end namespace clang + +#endif diff --git a/include/clang/Basic/SourceManager.h b/include/clang/Basic/SourceManager.h index 2098698400..4f5c17344d 100644 --- a/include/clang/Basic/SourceManager.h +++ b/include/clang/Basic/SourceManager.h @@ -33,6 +33,7 @@ namespace clang { class Diagnostic; class SourceManager; class FileManager; +class FileSystemOptions; class FileEntry; class LineTableInfo; @@ -369,7 +370,10 @@ public: class SourceManager { /// \brief Diagnostic object. Diagnostic &Diag; - + + FileManager &FileMgr; + const FileSystemOptions &FileSystemOpts; + mutable llvm::BumpPtrAllocator ContentCacheAlloc; /// FileInfos - Memoized information about all of the files tracked by this @@ -427,8 +431,10 @@ class SourceManager { explicit SourceManager(const SourceManager&); void operator=(const SourceManager&); public: - SourceManager(Diagnostic &Diag) - : Diag(Diag), ExternalSLocEntries(0), LineTable(0), NumLinearScans(0), + SourceManager(Diagnostic &Diag, FileManager &FileMgr, + const FileSystemOptions &FSOpts) + : Diag(Diag), FileMgr(FileMgr), FileSystemOpts(FSOpts), + ExternalSLocEntries(0), LineTable(0), NumLinearScans(0), NumBinaryProbes(0) { clearIDTables(); } @@ -438,6 +444,9 @@ public: Diagnostic &getDiagnostics() const { return Diag; } + FileManager &getFileManager() const { return FileMgr; } + const FileSystemOptions &getFileSystemOpts() const { return FileSystemOpts; } + //===--------------------------------------------------------------------===// // MainFileID creation and querying methods. //===--------------------------------------------------------------------===// |