diff --git a/clang/include/clang/Basic/SourceManager.h b/clang/include/clang/Basic/SourceManager.h --- a/clang/include/clang/Basic/SourceManager.h +++ b/clang/include/clang/Basic/SourceManager.h @@ -134,6 +134,8 @@ /// /// It is possible for this to be NULL if the ContentCache encapsulates /// an imaginary text buffer. + /// + /// FIXME: Turn this into a FileEntryRef and remove Filename. const FileEntry *OrigEntry; /// References the file which the contents were actually loaded from. @@ -142,6 +144,11 @@ /// with the contents of another file. const FileEntry *ContentsEntry; + /// The filename that is used to access OrigEntry. + /// + /// FIXME: Remove this once OrigEntry is a FileEntryRef with a stable name. + StringRef Filename; + /// A bump pointer allocated array of offsets for each source line. /// /// This is lazily computed. The lines are owned by the SourceManager @@ -266,7 +273,11 @@ /// from. This information encodes the \#include chain that a token was /// expanded from. The main include file has an invalid IncludeLoc. /// - /// FileInfos contain a "ContentCache *", with the contents of the file. + /// FileInfo should not grow larger than ExpansionInfo. Doing so will + /// cause memory to bloat in compilations with many unloaded macro + /// expansions, since the two data structurs are stored in a union in + /// SLocEntry. Extra fields should instead go in "ContentCache *", which + /// stores file contents and other bits on the side. /// class FileInfo { friend class clang::SourceManager; @@ -291,10 +302,6 @@ llvm::PointerIntPair ContentAndKind; - /// The filename that is used to access the file entry represented by the - /// content cache. - StringRef Filename; - public: /// Return a FileInfo object. static FileInfo get(SourceLocation IL, ContentCache &Con, @@ -305,7 +312,7 @@ X.HasLineDirectives = false; X.ContentAndKind.setPointer(&Con); X.ContentAndKind.setInt(FileCharacter); - X.Filename = Filename; + Con.Filename = Filename; return X; } @@ -333,7 +340,7 @@ /// Returns the name of the file that was used when the file was loaded from /// the underlying file system. - StringRef getName() const { return Filename; } + StringRef getName() const { return getContentCache().Filename; } }; /// Each ExpansionInfo encodes the expansion location - where @@ -454,6 +461,13 @@ } }; + // Assert that the \c FileInfo objects are no bigger than \c ExpansionInfo + // objects. This controls the size of \c SLocEntry, of which we have one for + // each macro expansion. The number of (unloaded) macro expansions can be + // very large. Any other fields needed in FileInfo should go in ContentCache. + static_assert(sizeof(FileInfo) <= sizeof(ExpansionInfo), + "FileInfo must be no larger than ExpansionInfo."); + /// This is a discriminated union of FileInfo and ExpansionInfo. /// /// SourceManager keeps an array of these objects, and they are uniquely diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp --- a/clang/lib/Basic/SourceManager.cpp +++ b/clang/lib/Basic/SourceManager.cpp @@ -1734,7 +1734,7 @@ // Predefined header doesn't have a valid include location in main // file, but any files created by it should still be skipped when // computing macro args expanded in the main file. - (FID == MainFileID && Entry.getFile().Filename == ""); + (FID == MainFileID && Entry.getFile().getName() == ""); if (IncludedInFID) { // Skip the files/macros of the #include'd file, we only care about // macros that lexed macro arguments from our file.