summaryrefslogtreecommitdiffstats
path: root/lib/Basic/SourceManager.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Basic/SourceManager.cpp')
-rw-r--r--lib/Basic/SourceManager.cpp34
1 files changed, 20 insertions, 14 deletions
diff --git a/lib/Basic/SourceManager.cpp b/lib/Basic/SourceManager.cpp
index e6d9785..633d86c 100644
--- a/lib/Basic/SourceManager.cpp
+++ b/lib/Basic/SourceManager.cpp
@@ -32,7 +32,8 @@ using llvm::MemoryBuffer;
//===----------------------------------------------------------------------===//
ContentCache::~ContentCache() {
- delete Buffer.getPointer();
+ if (shouldFreeBuffer())
+ delete Buffer.getPointer();
}
/// getSizeBytesMapped - Returns the number of bytes actually mapped for
@@ -51,12 +52,14 @@ unsigned ContentCache::getSize() const {
: (unsigned) Entry->getSize();
}
-void ContentCache::replaceBuffer(const llvm::MemoryBuffer *B) {
+void ContentCache::replaceBuffer(const llvm::MemoryBuffer *B,
+ bool DoNotFree) {
assert(B != Buffer.getPointer());
- delete Buffer.getPointer();
+ if (shouldFreeBuffer())
+ delete Buffer.getPointer();
Buffer.setPointer(B);
- Buffer.setInt(false);
+ Buffer.setInt(DoNotFree? DoNotFreeFlag : 0);
}
const llvm::MemoryBuffer *ContentCache::getBuffer(Diagnostic &Diag,
@@ -72,7 +75,6 @@ const llvm::MemoryBuffer *ContentCache::getBuffer(Diagnostic &Diag,
struct stat FileInfo;
Buffer.setPointer(MemoryBuffer::getFile(Entry->getName(), &ErrorStr,
Entry->getSize(), &FileInfo));
- Buffer.setInt(false);
// If we were unable to open the file, then we are in an inconsistent
// situation where the content cache referenced a file which no longer
@@ -99,7 +101,7 @@ const llvm::MemoryBuffer *ContentCache::getBuffer(Diagnostic &Diag,
Diag.Report(FullSourceLoc(Loc, SM), diag::err_cannot_open_file)
<< Entry->getName() << ErrorStr;
- Buffer.setInt(true);
+ Buffer.setInt(Buffer.getInt() | InvalidFlag);
// FIXME: This conditionalization is horrible, but we see spurious failures
// in the test suite due to this warning and no one has had time to hunt it
@@ -119,14 +121,14 @@ const llvm::MemoryBuffer *ContentCache::getBuffer(Diagnostic &Diag,
Diag.Report(FullSourceLoc(Loc, SM), diag::err_file_modified)
<< Entry->getName();
- Buffer.setInt(true);
+ Buffer.setInt(Buffer.getInt() | InvalidFlag);
#endif
}
// If the buffer is valid, check to see if it has a UTF Byte Order Mark
// (BOM). We only support UTF-8 without a BOM right now. See
// http://en.wikipedia.org/wiki/Byte_order_mark for more information.
- if (!Buffer.getInt()) {
+ if (!isBufferInvalid()) {
llvm::StringRef BufStr = Buffer.getPointer()->getBuffer();
const char *BOM = 0;
if (BufStr.startswith("\xFE\xBB\xBF"))
@@ -161,7 +163,7 @@ const llvm::MemoryBuffer *ContentCache::getBuffer(Diagnostic &Diag,
}
if (Invalid)
- *Invalid = Buffer.getInt();
+ *Invalid = isBufferInvalid();
return Buffer.getPointer();
}
@@ -422,9 +424,12 @@ void SourceManager::PreallocateSLocEntries(ExternalSLocEntrySource *Source,
unsigned NextOffset) {
ExternalSLocEntries = Source;
this->NextOffset = NextOffset;
+ unsigned CurPrealloc = SLocEntryLoaded.size();
+ // If we've ever preallocated, we must not count the dummy entry.
+ if (CurPrealloc) --CurPrealloc;
SLocEntryLoaded.resize(NumSLocEntries + 1);
SLocEntryLoaded[0] = true;
- SLocEntryTable.resize(SLocEntryTable.size() + NumSLocEntries);
+ SLocEntryTable.resize(SLocEntryTable.size() + NumSLocEntries - CurPrealloc);
}
void SourceManager::ClearPreallocatedSLocEntries() {
@@ -448,7 +453,7 @@ void SourceManager::ClearPreallocatedSLocEntries() {
// Methods to create new FileID's and instantiations.
//===----------------------------------------------------------------------===//
-/// createFileID - Create a new fileID for the specified ContentCache and
+/// 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,
@@ -521,12 +526,13 @@ SourceManager::getMemoryBufferForFile(const FileEntry *File,
}
bool SourceManager::overrideFileContents(const FileEntry *SourceFile,
- const llvm::MemoryBuffer *Buffer) {
+ const llvm::MemoryBuffer *Buffer,
+ bool DoNotFree) {
const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile);
if (IR == 0)
return true;
- const_cast<SrcMgr::ContentCache *>(IR)->replaceBuffer(Buffer);
+ const_cast<SrcMgr::ContentCache *>(IR)->replaceBuffer(Buffer, DoNotFree);
return false;
}
@@ -1241,7 +1247,7 @@ bool SourceManager::isBeforeInTranslationUnit(SourceLocation LHS,
}
// There is no common ancestor, most probably because one location is in the
- // predefines buffer or a PCH file.
+ // predefines buffer or an AST file.
// FIXME: We should rearrange the external interface so this simply never
// happens; it can't conceptually happen. Also see PR5662.
IsBeforeInTUCache.setQueryFIDs(FileID(), FileID()); // Don't try caching.
OpenPOWER on IntegriCloud