@@ -49,50 +49,31 @@ using llvm::MemoryBuffer;
49
49
// SourceManager Helper Classes
50
50
// ===----------------------------------------------------------------------===//
51
51
52
- ContentCache::~ContentCache () {
53
- if (shouldFreeBuffer ())
54
- delete Buffer.getPointer ();
55
- }
56
-
57
52
// / getSizeBytesMapped - Returns the number of bytes actually mapped for this
58
53
// / ContentCache. This can be 0 if the MemBuffer was not actually expanded.
59
54
unsigned ContentCache::getSizeBytesMapped () const {
60
- return Buffer. getPointer () ? Buffer. getPointer () ->getBufferSize () : 0 ;
55
+ return Buffer ? Buffer->getBufferSize () : 0 ;
61
56
}
62
57
63
58
// / Returns the kind of memory used to back the memory buffer for
64
59
// / this content cache. This is used for performance analysis.
65
60
llvm::MemoryBuffer::BufferKind ContentCache::getMemoryBufferKind () const {
66
- assert (Buffer. getPointer () );
61
+ assert (Buffer);
67
62
68
63
// Should be unreachable, but keep for sanity.
69
- if (!Buffer. getPointer () )
64
+ if (!Buffer)
70
65
return llvm::MemoryBuffer::MemoryBuffer_Malloc;
71
66
72
- const llvm::MemoryBuffer *buf = Buffer.getPointer ();
73
- return buf->getBufferKind ();
67
+ return Buffer->getBufferKind ();
74
68
}
75
69
76
70
// / getSize - Returns the size of the content encapsulated by this ContentCache.
77
71
// / This can be the size of the source file or the size of an arbitrary
78
72
// / scratch buffer. If the ContentCache encapsulates a source file, that
79
73
// / file is not lazily brought in from disk to satisfy this query.
80
74
unsigned ContentCache::getSize () const {
81
- return Buffer.getPointer () ? (unsigned ) Buffer.getPointer ()->getBufferSize ()
82
- : (unsigned ) ContentsEntry->getSize ();
83
- }
84
-
85
- void ContentCache::replaceBuffer (const llvm::MemoryBuffer *B, bool DoNotFree) {
86
- if (B && B == Buffer.getPointer ()) {
87
- assert (0 && " Replacing with the same buffer" );
88
- Buffer.setInt (DoNotFree? DoNotFreeFlag : 0 );
89
- return ;
90
- }
91
-
92
- if (shouldFreeBuffer ())
93
- delete Buffer.getPointer ();
94
- Buffer.setPointer (B);
95
- Buffer.setInt ((B && DoNotFree) ? DoNotFreeFlag : 0 );
75
+ return Buffer ? (unsigned )Buffer->getBufferSize ()
76
+ : (unsigned )ContentsEntry->getSize ();
96
77
}
97
78
98
79
const char *ContentCache::getInvalidBOM (StringRef BufStr) {
@@ -125,8 +106,8 @@ ContentCache::getBufferOrNone(DiagnosticsEngine &Diag, FileManager &FM,
125
106
// computed it, just return what we have.
126
107
if (IsBufferInvalid)
127
108
return None;
128
- if (auto *B = Buffer. getPointer () )
129
- return B ->getMemBufferRef ();
109
+ if (Buffer)
110
+ return Buffer ->getMemBufferRef ();
130
111
if (!ContentsEntry)
131
112
return None;
132
113
@@ -168,7 +149,7 @@ ContentCache::getBufferOrNone(DiagnosticsEngine &Diag, FileManager &FM,
168
149
return None;
169
150
}
170
151
171
- Buffer. setPointer ( BufferOrError-> release () );
152
+ Buffer = std::move (* BufferOrError);
172
153
173
154
// Check that the file's size is the same as in the file entry (which may
174
155
// have come from a stat cache).
@@ -187,7 +168,7 @@ ContentCache::getBufferOrNone(DiagnosticsEngine &Diag, FileManager &FM,
187
168
// If the buffer is valid, check to see if it has a UTF Byte Order Mark
188
169
// (BOM). We only support UTF-8 with and without a BOM right now. See
189
170
// http://en.wikipedia.org/wiki/Byte_order_mark for more information.
190
- StringRef BufStr = Buffer. getPointer () ->getBuffer ();
171
+ StringRef BufStr = Buffer->getBuffer ();
191
172
const char *InvalidBOM = getInvalidBOM (BufStr);
192
173
193
174
if (InvalidBOM) {
@@ -197,7 +178,7 @@ ContentCache::getBufferOrNone(DiagnosticsEngine &Diag, FileManager &FM,
197
178
return None;
198
179
}
199
180
200
- return Buffer. getPointer () ->getMemBufferRef ();
181
+ return Buffer->getMemBufferRef ();
201
182
}
202
183
203
184
unsigned LineTableInfo::getLineTableFilenameID (StringRef Name) {
@@ -380,7 +361,7 @@ void SourceManager::initializeForReplay(const SourceManager &Old) {
380
361
Clone->BufferOverridden = Cache->BufferOverridden ;
381
362
Clone->IsFileVolatile = Cache->IsFileVolatile ;
382
363
Clone->IsTransient = Cache->IsTransient ;
383
- Clone->replaceBuffer (Cache->getRawBuffer (), /* DoNotFree */ true );
364
+ Clone->setUnownedBuffer (Cache->getRawBuffer ());
384
365
return Clone;
385
366
};
386
367
@@ -441,7 +422,7 @@ const ContentCache *SourceManager::createMemBufferContentCache(
441
422
ContentCache *Entry = ContentCacheAlloc.Allocate <ContentCache>();
442
423
new (Entry) ContentCache ();
443
424
MemBufferInfos.push_back (Entry);
444
- Entry->replaceBuffer (Buffer. release (), /* DoNotFree= */ false );
425
+ Entry->setBuffer ( std::move (Buffer) );
445
426
return Entry;
446
427
}
447
428
@@ -493,8 +474,7 @@ const SrcMgr::ContentCache *
493
474
SourceManager::getFakeContentCacheForRecovery () const {
494
475
if (!FakeContentCacheForRecovery) {
495
476
FakeContentCacheForRecovery = std::make_unique<SrcMgr::ContentCache>();
496
- FakeContentCacheForRecovery->replaceBuffer (getFakeBufferForRecovery (),
497
- /* DoNotFree=*/ true );
477
+ FakeContentCacheForRecovery->setUnownedBuffer (getFakeBufferForRecovery ());
498
478
}
499
479
return FakeContentCacheForRecovery.get ();
500
480
}
@@ -700,14 +680,14 @@ SourceManager::getMemoryBufferForFileOrNone(const FileEntry *File) {
700
680
return IR->getBufferOrNone (Diag, getFileManager (), SourceLocation ());
701
681
}
702
682
703
- void SourceManager::overrideFileContents (const FileEntry *SourceFile,
704
- llvm::MemoryBuffer * Buffer,
705
- bool DoNotFree) {
706
- const SrcMgr::ContentCache *IR = getOrCreateContentCache (SourceFile);
683
+ void SourceManager::overrideFileContents (
684
+ const FileEntry *SourceFile, std::unique_ptr< llvm::MemoryBuffer> Buffer) {
685
+ auto *IR =
686
+ const_cast < SrcMgr::ContentCache *>( getOrCreateContentCache (SourceFile) );
707
687
assert (IR && " getOrCreateContentCache() cannot return NULL" );
708
688
709
- const_cast <SrcMgr::ContentCache *>(IR)-> replaceBuffer ( Buffer, DoNotFree );
710
- const_cast <SrcMgr::ContentCache *>(IR) ->BufferOverridden = true ;
689
+ IR-> setBuffer ( std::move ( Buffer) );
690
+ IR ->BufferOverridden = true ;
711
691
712
692
getOverriddenFilesInfo ().OverriddenFilesWithBuffer .insert (SourceFile);
713
693
}
0 commit comments