diff options
Diffstat (limited to 'lib/Basic/SourceManager.cpp')
-rw-r--r-- | lib/Basic/SourceManager.cpp | 92 |
1 files changed, 61 insertions, 31 deletions
diff --git a/lib/Basic/SourceManager.cpp b/lib/Basic/SourceManager.cpp index ce8aa5d112b36..12b0305e707c9 100644 --- a/lib/Basic/SourceManager.cpp +++ b/lib/Basic/SourceManager.cpp @@ -1,9 +1,8 @@ //===- SourceManager.cpp - Track and cache source files -------------------===// // -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // @@ -70,7 +69,7 @@ llvm::MemoryBuffer::BufferKind ContentCache::getMemoryBufferKind() const { if (!Buffer.getPointer()) return llvm::MemoryBuffer::MemoryBuffer_Malloc; - llvm::MemoryBuffer *buf = Buffer.getPointer(); + const llvm::MemoryBuffer *buf = Buffer.getPointer(); return buf->getBufferKind(); } @@ -83,7 +82,7 @@ unsigned ContentCache::getSize() const { : (unsigned) ContentsEntry->getSize(); } -void ContentCache::replaceBuffer(llvm::MemoryBuffer *B, bool DoNotFree) { +void ContentCache::replaceBuffer(const llvm::MemoryBuffer *B, bool DoNotFree) { if (B && B == Buffer.getPointer()) { assert(0 && "Replacing with the same buffer"); Buffer.setInt(DoNotFree? DoNotFreeFlag : 0); @@ -96,10 +95,10 @@ void ContentCache::replaceBuffer(llvm::MemoryBuffer *B, bool DoNotFree) { Buffer.setInt((B && DoNotFree) ? DoNotFreeFlag : 0); } -llvm::MemoryBuffer *ContentCache::getBuffer(DiagnosticsEngine &Diag, - const SourceManager &SM, - SourceLocation Loc, - bool *Invalid) const { +const llvm::MemoryBuffer *ContentCache::getBuffer(DiagnosticsEngine &Diag, + const SourceManager &SM, + SourceLocation Loc, + bool *Invalid) const { // Lazily create the Buffer for ContentCaches that wrap files. If we already // computed it, just return what we have. if (Buffer.getPointer() || !ContentsEntry) { @@ -109,6 +108,32 @@ llvm::MemoryBuffer *ContentCache::getBuffer(DiagnosticsEngine &Diag, return Buffer.getPointer(); } + // Check that the file's size fits in an 'unsigned' (with room for a + // past-the-end value). This is deeply regrettable, but various parts of + // Clang (including elsewhere in this file!) use 'unsigned' to represent file + // offsets, line numbers, string literal lengths, and so on, and fail + // miserably on large source files. + if ((uint64_t)ContentsEntry->getSize() >= + std::numeric_limits<unsigned>::max()) { + // We can't make a memory buffer of the required size, so just make a small + // one. We should never hit a situation where we've already parsed to a + // later offset of the file, so it shouldn't matter that the buffer is + // smaller than the file. + Buffer.setPointer( + llvm::MemoryBuffer::getMemBuffer("", ContentsEntry->getName()) + .release()); + if (Diag.isDiagnosticInFlight()) + Diag.SetDelayedDiagnostic(diag::err_file_too_large, + ContentsEntry->getName()); + else + Diag.Report(Loc, diag::err_file_too_large) + << ContentsEntry->getName(); + + Buffer.setInt(Buffer.getInt() | InvalidFlag); + if (Invalid) *Invalid = true; + return Buffer.getPointer(); + } + bool isVolatile = SM.userFilesAreVolatile() && !IsSystemFile; auto BufferOrError = SM.getFileManager().getBufferForFile(ContentsEntry, isVolatile); @@ -168,16 +193,16 @@ llvm::MemoryBuffer *ContentCache::getBuffer(DiagnosticsEngine &Diag, // http://en.wikipedia.org/wiki/Byte_order_mark for more information. StringRef BufStr = Buffer.getPointer()->getBuffer(); const char *InvalidBOM = llvm::StringSwitch<const char *>(BufStr) - .StartsWith("\xFE\xFF", "UTF-16 (BE)") - .StartsWith("\xFF\xFE", "UTF-16 (LE)") .StartsWith(llvm::StringLiteral::withInnerNUL("\x00\x00\xFE\xFF"), "UTF-32 (BE)") .StartsWith(llvm::StringLiteral::withInnerNUL("\xFF\xFE\x00\x00"), "UTF-32 (LE)") + .StartsWith("\xFE\xFF", "UTF-16 (BE)") + .StartsWith("\xFF\xFE", "UTF-16 (LE)") .StartsWith("\x2B\x2F\x76", "UTF-7") .StartsWith("\xF7\x64\x4C", "UTF-1") .StartsWith("\xDD\x73\x66\x73", "UTF-EBCDIC") - .StartsWith("\x0E\xFE\xFF", "SDSU") + .StartsWith("\x0E\xFE\xFF", "SCSU") .StartsWith("\xFB\xEE\x28", "BOCU-1") .StartsWith("\x84\x31\x95\x33", "GB-18030") .Default(nullptr); @@ -253,9 +278,9 @@ const LineEntry *LineTableInfo::FindNearestLineEntry(FileID FID, return &Entries.back(); // Do a binary search to find the maximal element that is still before Offset. - std::vector<LineEntry>::const_iterator I = - std::upper_bound(Entries.begin(), Entries.end(), Offset); - if (I == Entries.begin()) return nullptr; + std::vector<LineEntry>::const_iterator I = llvm::upper_bound(Entries, Offset); + if (I == Entries.begin()) + return nullptr; return &*--I; } @@ -304,7 +329,7 @@ void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo, LineTableInfo &SourceManager::getLineTable() { if (!LineTable) - LineTable = new LineTableInfo(); + LineTable.reset(new LineTableInfo()); return *LineTable; } @@ -320,8 +345,6 @@ SourceManager::SourceManager(DiagnosticsEngine &Diag, FileManager &FileMgr, } SourceManager::~SourceManager() { - delete LineTable; - // Delete FileEntry objects corresponding to content caches. Since the actual // content cache objects are bump pointer allocated, we just have to run the // dtors, but we call the deallocate method for completeness. @@ -424,7 +447,7 @@ SourceManager::getOrCreateContentCache(const FileEntry *FileEnt, /// Create a new ContentCache for the specified memory buffer. /// This does no caching. const ContentCache * -SourceManager::createMemBufferContentCache(llvm::MemoryBuffer *Buffer, +SourceManager::createMemBufferContentCache(const llvm::MemoryBuffer *Buffer, bool DoNotFree) { // Add a new ContentCache to the MemBufferInfos list and return it. ContentCache *Entry = ContentCacheAlloc.Allocate<ContentCache>(); @@ -619,8 +642,8 @@ SourceManager::createExpansionLocImpl(const ExpansionInfo &Info, return SourceLocation::getMacroLoc(NextLocalOffset - (TokLength + 1)); } -llvm::MemoryBuffer *SourceManager::getMemoryBufferForFile(const FileEntry *File, - bool *Invalid) { +const llvm::MemoryBuffer * +SourceManager::getMemoryBufferForFile(const FileEntry *File, bool *Invalid) { const SrcMgr::ContentCache *IR = getOrCreateContentCache(File); assert(IR && "getOrCreateContentCache() cannot return NULL"); return IR->getBuffer(Diag, *this, SourceLocation(), Invalid); @@ -676,7 +699,7 @@ StringRef SourceManager::getBufferData(FileID FID, bool *Invalid) const { return "<<<<<INVALID SOURCE LOCATION>>>>>"; } - llvm::MemoryBuffer *Buf = SLoc.getFile().getContentCache()->getBuffer( + const llvm::MemoryBuffer *Buf = SLoc.getFile().getContentCache()->getBuffer( Diag, *this, SourceLocation(), &MyInvalid); if (Invalid) *Invalid = MyInvalid; @@ -1106,8 +1129,9 @@ const char *SourceManager::getCharacterData(SourceLocation SL, return "<<<<INVALID BUFFER>>>>"; } - llvm::MemoryBuffer *Buffer = Entry.getFile().getContentCache()->getBuffer( - Diag, *this, SourceLocation(), &CharDataInvalid); + const llvm::MemoryBuffer *Buffer = + Entry.getFile().getContentCache()->getBuffer( + Diag, *this, SourceLocation(), &CharDataInvalid); if (Invalid) *Invalid = CharDataInvalid; return Buffer->getBufferStart() + (CharDataInvalid? 0 : LocInfo.second); @@ -1118,7 +1142,7 @@ const char *SourceManager::getCharacterData(SourceLocation SL, unsigned SourceManager::getColumnNumber(FileID FID, unsigned FilePos, bool *Invalid) const { bool MyInvalid = false; - llvm::MemoryBuffer *MemBuf = getBuffer(FID, &MyInvalid); + const llvm::MemoryBuffer *MemBuf = getBuffer(FID, &MyInvalid); if (Invalid) *Invalid = MyInvalid; @@ -1203,7 +1227,8 @@ static void ComputeLineNumbers(DiagnosticsEngine &Diag, ContentCache *FI, llvm::BumpPtrAllocator &Alloc, const SourceManager &SM, bool &Invalid) { // Note that calling 'getBuffer()' may lazily page in the file. - MemoryBuffer *Buffer = FI->getBuffer(Diag, SM, SourceLocation(), &Invalid); + const MemoryBuffer *Buffer = + FI->getBuffer(Diag, SM, SourceLocation(), &Invalid); if (Invalid) return; @@ -1429,6 +1454,7 @@ PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc, // To get the source name, first consult the FileEntry (if one exists) // before the MemBuffer as this will avoid unnecessarily paging in the // MemBuffer. + FileID FID = LocInfo.first; StringRef Filename; if (C->OrigEntry) Filename = C->OrigEntry->getName(); @@ -1452,8 +1478,12 @@ PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc, if (const LineEntry *Entry = LineTable->FindNearestLineEntry(LocInfo.first, LocInfo.second)) { // If the LineEntry indicates a filename, use it. - if (Entry->FilenameID != -1) + if (Entry->FilenameID != -1) { Filename = LineTable->getFilename(Entry->FilenameID); + // The contents of files referenced by #line are not in the + // SourceManager + FID = FileID::get(0); + } // Use the line number specified by the LineEntry. This line number may // be multiple lines down from the line entry. Add the difference in @@ -1472,7 +1502,7 @@ PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc, } } - return PresumedLoc(Filename.data(), LineNo, ColNo, IncludeLoc); + return PresumedLoc(Filename.data(), FID, LineNo, ColNo, IncludeLoc); } /// Returns whether the PresumedLoc for a given SourceLocation is @@ -1581,7 +1611,7 @@ FileID SourceManager::translateFile(const FileEntry *SourceFile) const { if (MainSLoc.isFile()) { const ContentCache *MainContentCache = MainSLoc.getFile().getContentCache(); - if (!MainContentCache) { + if (!MainContentCache || !MainContentCache->OrigEntry) { // Can't do anything } else if (MainContentCache->OrigEntry == SourceFile) { FirstFID = MainFileID; @@ -1721,7 +1751,7 @@ SourceLocation SourceManager::translateLineCol(FileID FID, return FileLoc.getLocWithOffset(Size); } - llvm::MemoryBuffer *Buffer = Content->getBuffer(Diag, *this); + const llvm::MemoryBuffer *Buffer = Content->getBuffer(Diag, *this); unsigned FilePos = Content->SourceLineCache[Line - 1]; const char *Buf = Buffer->getBufferStart() + FilePos; unsigned BufLength = Buffer->getBufferSize() - FilePos; |