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 @@ -275,13 +275,13 @@ public: /// Return a FileInfo object. - static FileInfo get(SourceLocation IL, const ContentCache *Con, + static FileInfo get(SourceLocation IL, const ContentCache &Con, CharacteristicKind FileCharacter, StringRef Filename) { FileInfo X; X.IncludeLoc = IL.getRawEncoding(); X.NumCreatedFIDs = 0; X.HasLineDirectives = false; - X.ContentAndKind.setPointer(Con); + X.ContentAndKind.setPointer(&Con); X.ContentAndKind.setInt(FileCharacter); X.Filename = Filename; return X; @@ -291,8 +291,8 @@ return SourceLocation::getFromRawEncoding(IncludeLoc); } - const ContentCache *getContentCache() const { - return ContentAndKind.getPointer(); + const ContentCache &getContentCache() const { + return *ContentAndKind.getPointer(); } /// Return whether this is a system header or not. @@ -973,7 +973,7 @@ llvm::Optional getBufferOrNone(FileID FID, SourceLocation Loc = SourceLocation()) const { if (auto *Entry = getSLocEntryForFile(FID)) - return Entry->getFile().getContentCache()->getBufferOrNone( + return Entry->getFile().getContentCache().getBufferOrNone( Diag, getFileManager(), Loc); return None; } @@ -992,8 +992,7 @@ /// Returns the FileEntry record for the provided FileID. const FileEntry *getFileEntryForID(FileID FID) const { if (auto *Entry = getSLocEntryForFile(FID)) - if (auto *Content = Entry->getFile().getContentCache()) - return Content->OrigEntry; + return Entry->getFile().getContentCache().OrigEntry; return nullptr; } @@ -1006,10 +1005,7 @@ /// Returns the FileEntry record for the provided SLocEntry. const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const { - const SrcMgr::ContentCache *Content = sloc.getFile().getContentCache(); - if (!Content) - return nullptr; - return Content->OrigEntry; + return sloc.getFile().getContentCache().OrigEntry; } /// Return a StringRef to the source buffer data for the @@ -1793,10 +1789,10 @@ /// /// This works regardless of whether the ContentCache corresponds to a /// file or some other input source. - FileID createFileID(const SrcMgr::ContentCache *File, StringRef Filename, - SourceLocation IncludePos, - SrcMgr::CharacteristicKind DirCharacter, int LoadedID, - unsigned LoadedOffset); + FileID createFileIDImpl(const SrcMgr::ContentCache &File, StringRef Filename, + SourceLocation IncludePos, + SrcMgr::CharacteristicKind DirCharacter, int LoadedID, + unsigned LoadedOffset); const SrcMgr::ContentCache * getOrCreateContentCache(const FileEntry *SourceFile, diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp --- a/clang/lib/AST/ASTImporter.cpp +++ b/clang/lib/AST/ASTImporter.cpp @@ -8654,7 +8654,7 @@ } ToID = ToSM.getFileID(MLoc); } else { - const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache(); + const SrcMgr::ContentCache *Cache = &FromSLoc.getFile().getContentCache(); if (!IsBuiltin && !Cache->BufferOverridden) { // Include location of this file. 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 @@ -437,7 +437,7 @@ if (!SLocEntryLoaded[Index]) { // Try to recover; create a SLocEntry so the rest of clang can handle it. LoadedSLocEntryTable[Index] = SLocEntry::get( - 0, FileInfo::get(SourceLocation(), getFakeContentCacheForRecovery(), + 0, FileInfo::get(SourceLocation(), *getFakeContentCacheForRecovery(), SrcMgr::C_User, "")); } } @@ -534,7 +534,7 @@ const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile, isSystem(FileCharacter)); assert(IR && "getOrCreateContentCache() cannot return NULL"); - return createFileID(IR, SourceFile->getName(), IncludePos, FileCharacter, + return createFileIDImpl(*IR, SourceFile->getName(), IncludePos, FileCharacter, LoadedID, LoadedOffset); } @@ -545,8 +545,8 @@ const SrcMgr::ContentCache *IR = getOrCreateContentCache( &SourceFile.getFileEntry(), isSystem(FileCharacter)); assert(IR && "getOrCreateContentCache() cannot return NULL"); - return createFileID(IR, SourceFile.getName(), IncludePos, FileCharacter, - LoadedID, LoadedOffset); + return createFileIDImpl(*IR, SourceFile.getName(), IncludePos, FileCharacter, + LoadedID, LoadedOffset); } /// Create a new FileID that represents the specified memory buffer. @@ -558,8 +558,8 @@ int LoadedID, unsigned LoadedOffset, SourceLocation IncludeLoc) { StringRef Name = Buffer->getBufferIdentifier(); - return createFileID(createMemBufferContentCache(std::move(Buffer)), Name, - IncludeLoc, FileCharacter, LoadedID, LoadedOffset); + return createFileIDImpl(*createMemBufferContentCache(std::move(Buffer)), Name, + IncludeLoc, FileCharacter, LoadedID, LoadedOffset); } /// Create a new FileID that represents the specified memory buffer. @@ -587,10 +587,11 @@ /// 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. -FileID SourceManager::createFileID(const ContentCache *File, StringRef Filename, - SourceLocation IncludePos, - SrcMgr::CharacteristicKind FileCharacter, - int LoadedID, unsigned LoadedOffset) { +FileID SourceManager::createFileIDImpl(const ContentCache &File, + StringRef Filename, + SourceLocation IncludePos, + SrcMgr::CharacteristicKind FileCharacter, + int LoadedID, unsigned LoadedOffset) { if (LoadedID < 0) { assert(LoadedID != -1 && "Loading sentinel FileID"); unsigned Index = unsigned(-LoadedID) - 2; @@ -601,7 +602,7 @@ SLocEntryLoaded[Index] = true; return FileID::get(LoadedID); } - unsigned FileSize = File->getSize(); + unsigned FileSize = File.getSize(); if (!(NextLocalOffset + FileSize + 1 > NextLocalOffset && NextLocalOffset + FileSize + 1 <= CurrentLoadedOffset)) { Diag.Report(IncludePos, diag::err_include_too_large); @@ -728,9 +729,8 @@ Optional SourceManager::getNonBuiltinFilenameForID(FileID FID) const { if (const SrcMgr::SLocEntry *Entry = getSLocEntryForFile(FID)) - if (auto *Content = Entry->getFile().getContentCache()) - if (Content->OrigEntry) - return Entry->getFile().getName(); + if (Entry->getFile().getContentCache().OrigEntry) + return Entry->getFile().getName(); return None; } @@ -744,13 +744,13 @@ llvm::Optional SourceManager::getBufferDataIfLoaded(FileID FID) const { if (const SrcMgr::SLocEntry *Entry = getSLocEntryForFile(FID)) - return Entry->getFile().getContentCache()->getBufferDataIfLoaded(); + return Entry->getFile().getContentCache().getBufferDataIfLoaded(); return None; } llvm::Optional SourceManager::getBufferDataOrNone(FileID FID) const { if (const SrcMgr::SLocEntry *Entry = getSLocEntryForFile(FID)) - if (auto B = Entry->getFile().getContentCache()->getBufferOrNone( + if (auto B = Entry->getFile().getContentCache().getBufferOrNone( Diag, getFileManager(), SourceLocation())) return B->getBuffer(); return None; @@ -1171,8 +1171,8 @@ return "<<<>>>"; } llvm::Optional Buffer = - Entry.getFile().getContentCache()->getBufferOrNone(Diag, getFileManager(), - SourceLocation()); + Entry.getFile().getContentCache().getBufferOrNone(Diag, getFileManager(), + SourceLocation()); if (Invalid) *Invalid = !Buffer; return Buffer ? Buffer->getBufferStart() + LocInfo.second @@ -1327,7 +1327,7 @@ return 1; } - Content = const_cast(Entry.getFile().getContentCache()); + Content = const_cast(&Entry.getFile().getContentCache()); } // If this is the first use of line information for this buffer, compute the @@ -1488,7 +1488,7 @@ return PresumedLoc(); const SrcMgr::FileInfo &FI = Entry.getFile(); - const SrcMgr::ContentCache *C = FI.getContentCache(); + const SrcMgr::ContentCache *C = &FI.getContentCache(); // To get the source name, first consult the FileEntry (if one exists) // before the MemBuffer as this will avoid unnecessarily paging in the @@ -1626,9 +1626,7 @@ return FileID(); if (MainSLoc.isFile()) { - const ContentCache *MainContentCache = - MainSLoc.getFile().getContentCache(); - if (MainContentCache && MainContentCache->OrigEntry == SourceFile) + if (MainSLoc.getFile().getContentCache().OrigEntry == SourceFile) return MainFileID; } } @@ -1637,16 +1635,16 @@ // through all of the local source locations. for (unsigned I = 0, N = local_sloc_entry_size(); I != N; ++I) { const SLocEntry &SLoc = getLocalSLocEntry(I); - if (SLoc.isFile() && SLoc.getFile().getContentCache() && - SLoc.getFile().getContentCache()->OrigEntry == SourceFile) + if (SLoc.isFile() && + SLoc.getFile().getContentCache().OrigEntry == SourceFile) return FileID::get(I); } // If that still didn't help, try the modules. for (unsigned I = 0, N = loaded_sloc_entry_size(); I != N; ++I) { const SLocEntry &SLoc = getLoadedSLocEntry(I); - if (SLoc.isFile() && SLoc.getFile().getContentCache() && - SLoc.getFile().getContentCache()->OrigEntry == SourceFile) + if (SLoc.isFile() && + SLoc.getFile().getContentCache().OrigEntry == SourceFile) return FileID::get(-int(I) - 2); } @@ -1678,10 +1676,8 @@ if (Line == 1 && Col == 1) return FileLoc; - ContentCache *Content - = const_cast(Entry.getFile().getContentCache()); - if (!Content) - return SourceLocation(); + ContentCache *Content = + const_cast(&Entry.getFile().getContentCache()); // If this is the first use of line information for this buffer, compute the // SourceLineCache for it on demand. @@ -2139,16 +2135,15 @@ << ">\n"; if (FI.getIncludeLoc().isValid()) out << " included from " << FI.getIncludeLoc().getOffset() << "\n"; - if (auto *CC = FI.getContentCache()) { - out << " for " << (CC->OrigEntry ? CC->OrigEntry->getName() : "") + auto &CC = FI.getContentCache(); + out << " for " << (CC.OrigEntry ? CC.OrigEntry->getName() : "") + << "\n"; + if (CC.BufferOverridden) + out << " contents overridden\n"; + if (CC.ContentsEntry != CC.OrigEntry) { + out << " contents from " + << (CC.ContentsEntry ? CC.ContentsEntry->getName() : "") << "\n"; - if (CC->BufferOverridden) - out << " contents overridden\n"; - if (CC->ContentsEntry != CC->OrigEntry) { - out << " contents from " - << (CC->ContentsEntry ? CC->ContentsEntry->getName() : "") - << "\n"; - } } } else { auto &EI = Entry.getExpansion(); diff --git a/clang/lib/Lex/ScratchBuffer.cpp b/clang/lib/Lex/ScratchBuffer.cpp --- a/clang/lib/Lex/ScratchBuffer.cpp +++ b/clang/lib/Lex/ScratchBuffer.cpp @@ -38,8 +38,9 @@ // Clear out the source line cache if it's already been computed. // FIXME: Allow this to be incrementally extended. auto *ContentCache = const_cast( - SourceMgr.getSLocEntry(SourceMgr.getFileID(BufferStartLoc)) - .getFile().getContentCache()); + &SourceMgr.getSLocEntry(SourceMgr.getFileID(BufferStartLoc)) + .getFile() + .getContentCache()); ContentCache->SourceLineCache = nullptr; } diff --git a/clang/lib/Rewrite/Rewriter.cpp b/clang/lib/Rewrite/Rewriter.cpp --- a/clang/lib/Rewrite/Rewriter.cpp +++ b/clang/lib/Rewrite/Rewriter.cpp @@ -263,8 +263,8 @@ StringRef MB = SourceMgr->getBufferData(FID); unsigned lineNo = SourceMgr->getLineNumber(FID, StartOffs) - 1; - const SrcMgr::ContentCache * - Content = SourceMgr->getSLocEntry(FID).getFile().getContentCache(); + const SrcMgr::ContentCache *Content = + &SourceMgr->getSLocEntry(FID).getFile().getContentCache(); unsigned lineOffs = Content->SourceLineCache[lineNo]; // Find the whitespace at the start of the line. @@ -367,8 +367,8 @@ unsigned startLineNo = SourceMgr->getLineNumber(FID, StartOff) - 1; unsigned endLineNo = SourceMgr->getLineNumber(FID, EndOff) - 1; - const SrcMgr::ContentCache * - Content = SourceMgr->getSLocEntry(FID).getFile().getContentCache(); + const SrcMgr::ContentCache *Content = + &SourceMgr->getSLocEntry(FID).getFile().getContentCache(); // Find where the lines start. unsigned parentLineOffs = Content->SourceLineCache[parentLineNo]; diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp --- a/clang/lib/Serialization/ASTWriter.cpp +++ b/clang/lib/Serialization/ASTWriter.cpp @@ -1452,7 +1452,7 @@ if (!SLoc->isFile()) continue; const SrcMgr::FileInfo &File = SLoc->getFile(); - const SrcMgr::ContentCache *Cache = File.getContentCache(); + const SrcMgr::ContentCache *Cache = &File.getContentCache(); if (!Cache->OrigEntry) continue; @@ -1952,7 +1952,7 @@ // Figure out which record code to use. unsigned Code; if (SLoc->isFile()) { - const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache(); + const SrcMgr::ContentCache *Cache = &SLoc->getFile().getContentCache(); if (Cache->OrigEntry) { Code = SM_SLOC_FILE_ENTRY; } else @@ -1970,7 +1970,7 @@ Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding Record.push_back(File.hasLineDirectives()); - const SrcMgr::ContentCache *Content = File.getContentCache(); + const SrcMgr::ContentCache *Content = &File.getContentCache(); bool EmitBlob = false; if (Content->OrigEntry) { assert(Content->OrigEntry == Content->ContentsEntry && diff --git a/clang/tools/libclang/CIndexInclusionStack.cpp b/clang/tools/libclang/CIndexInclusionStack.cpp --- a/clang/tools/libclang/CIndexInclusionStack.cpp +++ b/clang/tools/libclang/CIndexInclusionStack.cpp @@ -35,7 +35,7 @@ continue; const SrcMgr::FileInfo &FI = SL.getFile(); - if (!FI.getContentCache()->OrigEntry) + if (!FI.getContentCache().OrigEntry) continue; // If this is the main file, and there is a preamble, skip this SLoc. The @@ -60,7 +60,7 @@ // Callback to the client. // FIXME: We should have a function to construct CXFiles. CB(static_cast( - const_cast(FI.getContentCache()->OrigEntry)), + const_cast(FI.getContentCache().OrigEntry)), InclusionStack.data(), InclusionStack.size(), clientData); } }