diff options
author | Reid Kleckner <rnk@google.com> | 2020-02-29 09:10:42 -0800 |
---|---|---|
committer | Reid Kleckner <rnk@google.com> | 2020-03-11 13:53:12 -0700 |
commit | e08464fb450456881733c885267b32dc7339cf11 (patch) | |
tree | 1a11e18cf258b651cdb87814626417a70271ea71 /clang/lib/Basic/SourceManager.cpp | |
parent | 526a4f2ac365a5babbc80e0f7c17be310728a538 (diff) | |
download | llvm-e08464fb450456881733c885267b32dc7339cf11.zip llvm-e08464fb450456881733c885267b32dc7339cf11.tar.gz llvm-e08464fb450456881733c885267b32dc7339cf11.tar.bz2 |
Avoid including FileManager.h from SourceManager.h
Most clients of SourceManager.h need to do things like turning source
locations into file & line number pairs, but this doesn't require
bringing in FileManager.h and LLVM's FS headers.
The main code change here is to sink SM::createFileID into the cpp file.
I reason that this is not performance critical because it doesn't happen
on the diagnostic path, it happens along the paths of macro expansion
(could be hot) and new includes (less hot).
Saves some includes:
309 - /usr/local/google/home/rnk/llvm-project/clang/include/clang/Basic/FileManager.h
272 - /usr/local/google/home/rnk/llvm-project/clang/include/clang/Basic/FileSystemOptions.h
271 - /usr/local/google/home/rnk/llvm-project/llvm/include/llvm/Support/VirtualFileSystem.h
267 - /usr/local/google/home/rnk/llvm-project/llvm/include/llvm/Support/FileSystem.h
266 - /usr/local/google/home/rnk/llvm-project/llvm/include/llvm/Support/Chrono.h
Differential Revision: https://reviews.llvm.org/D75406
Diffstat (limited to 'clang/lib/Basic/SourceManager.cpp')
-rw-r--r-- | clang/lib/Basic/SourceManager.cpp | 87 |
1 files changed, 85 insertions, 2 deletions
diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp index 187c33a..8ba5815 100644 --- a/clang/lib/Basic/SourceManager.cpp +++ b/clang/lib/Basic/SourceManager.cpp @@ -17,12 +17,12 @@ #include "clang/Basic/SourceLocation.h" #include "clang/Basic/SourceManagerInternals.h" #include "llvm/ADT/DenseMap.h" -#include "llvm/ADT/Optional.h" #include "llvm/ADT/None.h" +#include "llvm/ADT/Optional.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallVector.h" -#include "llvm/ADT/StringSwitch.h" #include "llvm/ADT/StringRef.h" +#include "llvm/ADT/StringSwitch.h" #include "llvm/Support/Allocator.h" #include "llvm/Support/Capacity.h" #include "llvm/Support/Compiler.h" @@ -560,6 +560,70 @@ FileID SourceManager::getNextFileID(FileID FID) const { // Methods to create new FileID's and macro expansions. //===----------------------------------------------------------------------===// +/// Create a new FileID that represents the specified file +/// being \#included from the specified IncludePosition. +/// +/// This translates NULL into standard input. +FileID SourceManager::createFileID(const FileEntry *SourceFile, + SourceLocation IncludePos, + SrcMgr::CharacteristicKind FileCharacter, + int LoadedID, unsigned LoadedOffset) { + assert(SourceFile && "Null source file!"); + const SrcMgr::ContentCache *IR = + getOrCreateContentCache(SourceFile, isSystem(FileCharacter)); + assert(IR && "getOrCreateContentCache() cannot return NULL"); + return createFileID(IR, SourceFile->getName(), IncludePos, FileCharacter, + LoadedID, LoadedOffset); +} + +FileID SourceManager::createFileID(FileEntryRef SourceFile, + SourceLocation IncludePos, + SrcMgr::CharacteristicKind FileCharacter, + int LoadedID, unsigned LoadedOffset) { + const SrcMgr::ContentCache *IR = getOrCreateContentCache( + &SourceFile.getFileEntry(), isSystem(FileCharacter)); + assert(IR && "getOrCreateContentCache() cannot return NULL"); + return createFileID(IR, SourceFile.getName(), IncludePos, FileCharacter, + LoadedID, LoadedOffset); +} + +/// Create a new FileID that represents the specified memory buffer. +/// +/// This does no caching of the buffer and takes ownership of the +/// MemoryBuffer, so only pass a MemoryBuffer to this once. +FileID SourceManager::createFileID(std::unique_ptr<llvm::MemoryBuffer> Buffer, + SrcMgr::CharacteristicKind FileCharacter, + int LoadedID, unsigned LoadedOffset, + SourceLocation IncludeLoc) { + StringRef Name = Buffer->getBufferIdentifier(); + return createFileID( + createMemBufferContentCache(Buffer.release(), /*DoNotFree*/ false), + Name, IncludeLoc, FileCharacter, LoadedID, LoadedOffset); +} + +/// Create a new FileID that represents the specified memory buffer. +/// +/// This does not take ownership of the MemoryBuffer. The memory buffer must +/// outlive the SourceManager. +FileID SourceManager::createFileID(UnownedTag, const llvm::MemoryBuffer *Buffer, + SrcMgr::CharacteristicKind FileCharacter, + int LoadedID, unsigned LoadedOffset, + SourceLocation IncludeLoc) { + return createFileID(createMemBufferContentCache(Buffer, /*DoNotFree*/ true), + Buffer->getBufferIdentifier(), IncludeLoc, + FileCharacter, LoadedID, LoadedOffset); +} + +/// Get the FileID for \p SourceFile if it exists. Otherwise, create a +/// new FileID for the \p SourceFile. +FileID +SourceManager::getOrCreateFileID(const FileEntry *SourceFile, + SrcMgr::CharacteristicKind FileCharacter) { + FileID ID = translateFile(SourceFile); + return ID.isValid() ? ID : createFileID(SourceFile, SourceLocation(), + FileCharacter); +} + /// createFileID - Create a new FileID for the specified ContentCache and /// include position. This works regardless of whether the ContentCache /// corresponds to a file or some other input source. @@ -701,6 +765,18 @@ void SourceManager::setFileIsTransient(const FileEntry *File) { const_cast<SrcMgr::ContentCache *>(CC)->IsTransient = true; } +Optional<FileEntryRef> SourceManager::getFileEntryRefForID(FileID FID) const { + bool Invalid = false; + const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); + if (Invalid || !Entry.isFile()) + return None; + + const SrcMgr::ContentCache *Content = Entry.getFile().getContentCache(); + if (!Content || !Content->OrigEntry) + return None; + return FileEntryRef(Entry.getFile().getName(), *Content->OrigEntry); +} + StringRef SourceManager::getBufferData(FileID FID, bool *Invalid) const { bool MyInvalid = false; const SLocEntry &SLoc = getSLocEntry(FID, &MyInvalid); @@ -992,6 +1068,13 @@ SourceLocation SourceManager::getImmediateSpellingLoc(SourceLocation Loc) const{ return Loc.getLocWithOffset(LocInfo.second); } +/// Return the filename of the file containing a SourceLocation. +StringRef SourceManager::getFilename(SourceLocation SpellingLoc) const { + if (const FileEntry *F = getFileEntryForID(getFileID(SpellingLoc))) + return F->getName(); + return StringRef(); +} + /// getImmediateExpansionRange - Loc is required to be an expansion location. /// Return the start/end of the expansion information. CharSourceRange |