Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit 0581659

Browse files
committed
make "ContentCache::Buffer" mutable to avoid a const_cast.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@62403 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 5c26385 commit 0581659

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

include/clang/Basic/SourceManager.h

+12-12
Original file line numberDiff line numberDiff line change
@@ -51,24 +51,24 @@ namespace SrcMgr {
5151
class ContentCache {
5252
/// Buffer - The actual buffer containing the characters from the input
5353
/// file. This is owned by the ContentCache object.
54-
const llvm::MemoryBuffer* Buffer;
54+
mutable const llvm::MemoryBuffer *Buffer;
5555

5656
public:
5757
/// Reference to the file entry. This reference does not own
5858
/// the FileEntry object. It is possible for this to be NULL if
5959
/// the ContentCache encapsulates an imaginary text buffer.
60-
const FileEntry* Entry;
60+
const FileEntry *Entry;
6161

6262
/// SourceLineCache - A new[]'d array of offsets for each source line. This
6363
/// is lazily computed. This is owned by the ContentCache object.
64-
unsigned* SourceLineCache;
64+
unsigned *SourceLineCache;
6565

6666
/// NumLines - The number of lines in this ContentCache. This is only valid
6767
/// if SourceLineCache is non-null.
6868
unsigned NumLines;
6969

7070
/// getBuffer - Returns the memory buffer for the associated content.
71-
const llvm::MemoryBuffer* getBuffer() const;
71+
const llvm::MemoryBuffer *getBuffer() const;
7272

7373
/// getSize - Returns the size of the content encapsulated by this
7474
/// ContentCache. This can be the size of the source file or the size of an
@@ -81,20 +81,20 @@ namespace SrcMgr {
8181
/// instantiated.
8282
unsigned getSizeBytesMapped() const;
8383

84-
void setBuffer(const llvm::MemoryBuffer* B) {
84+
void setBuffer(const llvm::MemoryBuffer *B) {
8585
assert(!Buffer && "MemoryBuffer already set.");
8686
Buffer = B;
8787
}
8888

89-
ContentCache(const FileEntry* e = NULL)
89+
ContentCache(const FileEntry *e = NULL)
9090
: Buffer(NULL), Entry(e), SourceLineCache(NULL), NumLines(0) {}
9191

9292
~ContentCache();
9393

9494
/// The copy ctor does not allow copies where source object has either
9595
/// a non-NULL Buffer or SourceLineCache. Ownership of allocated memory
9696
/// is not transfered, so this is a logical error.
97-
ContentCache(const ContentCache& RHS) : Buffer(NULL),SourceLineCache(NULL) {
97+
ContentCache(const ContentCache &RHS) : Buffer(NULL),SourceLineCache(NULL) {
9898
Entry = RHS.Entry;
9999

100100
assert (RHS.Buffer == NULL && RHS.SourceLineCache == NULL
@@ -104,16 +104,16 @@ namespace SrcMgr {
104104
}
105105

106106
/// Emit - Emit this ContentCache to Bitcode.
107-
void Emit(llvm::Serializer& S) const;
107+
void Emit(llvm::Serializer &S) const;
108108

109109
/// ReadToSourceManager - Reconstitute a ContentCache from Bitcode
110110
// and store it in the specified SourceManager.
111-
static void ReadToSourceManager(llvm::Deserializer& D, SourceManager& SMgr,
112-
FileManager* FMgr, std::vector<char>& Buf);
111+
static void ReadToSourceManager(llvm::Deserializer &D, SourceManager &SM,
112+
FileManager *FMgr, std::vector<char> &Buf);
113113

114114
private:
115115
// Disable assignments.
116-
ContentCache& operator=(const ContentCache& RHS);
116+
ContentCache &operator=(const ContentCache& RHS);
117117
};
118118

119119
/// FileIDInfo - Information about a FileID, basically just the logical file
@@ -150,7 +150,7 @@ namespace SrcMgr {
150150
unsigned FileCharacteristic : 2;
151151

152152
/// Content - Information about the source buffer itself.
153-
const ContentCache* Content;
153+
const ContentCache *Content;
154154

155155
public:
156156
/// get - Return a FileIDInfo object.

lib/Basic/SourceManager.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ const llvm::MemoryBuffer* ContentCache::getBuffer() const {
5656
if (!Buffer && Entry) {
5757
// FIXME: Should we support a way to not have to do this check over
5858
// and over if we cannot open the file?
59-
// FIXME: This const_cast is ugly. Should we make getBuffer() non-const?
60-
const_cast<ContentCache*>(this)->Buffer =
61-
MemoryBuffer::getFile(Entry->getName(), 0, Entry->getSize());
59+
Buffer = MemoryBuffer::getFile(Entry->getName(), 0, Entry->getSize());
6260
}
6361
#endif
6462
return Buffer;

0 commit comments

Comments
 (0)