Skip to content

Commit 9ee76ab

Browse files
committed
raw_ostream - fix static analyzer warnings. NFCI.
- uninitialized variables - make BufferKind a scoped enum class
1 parent c8f0bb4 commit 9ee76ab

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

llvm/include/llvm/Support/raw_ostream.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class raw_ostream {
6464
/// this buffer.
6565
char *OutBufStart, *OutBufEnd, *OutBufCur;
6666

67-
enum BufferKind {
67+
enum class BufferKind {
6868
Unbuffered = 0,
6969
InternalBuffer,
7070
ExternalBuffer
@@ -97,7 +97,8 @@ class raw_ostream {
9797
static const Colors RESET = Colors::RESET;
9898

9999
explicit raw_ostream(bool unbuffered = false)
100-
: BufferMode(unbuffered ? Unbuffered : InternalBuffer) {
100+
: BufferMode(unbuffered ? BufferKind::Unbuffered
101+
: BufferKind::InternalBuffer) {
101102
// Start out ready to flush.
102103
OutBufStart = OutBufEnd = OutBufCur = nullptr;
103104
}
@@ -121,13 +122,13 @@ class raw_ostream {
121122
/// Set the stream to be buffered, using the specified buffer size.
122123
void SetBufferSize(size_t Size) {
123124
flush();
124-
SetBufferAndMode(new char[Size], Size, InternalBuffer);
125+
SetBufferAndMode(new char[Size], Size, BufferKind::InternalBuffer);
125126
}
126127

127128
size_t GetBufferSize() const {
128129
// If we're supposed to be buffered but haven't actually gotten around
129130
// to allocating the buffer yet, return the value that would be used.
130-
if (BufferMode != Unbuffered && OutBufStart == nullptr)
131+
if (BufferMode != BufferKind::Unbuffered && OutBufStart == nullptr)
131132
return preferred_buffer_size();
132133

133134
// Otherwise just return the size of the allocated buffer.
@@ -139,7 +140,7 @@ class raw_ostream {
139140
/// when the stream is being set to unbuffered.
140141
void SetUnbuffered() {
141142
flush();
142-
SetBufferAndMode(nullptr, 0, Unbuffered);
143+
SetBufferAndMode(nullptr, 0, BufferKind::Unbuffered);
143144
}
144145

145146
size_t GetNumBytesInBuffer() const {
@@ -325,7 +326,7 @@ class raw_ostream {
325326
/// use only by subclasses which can arrange for the output to go directly
326327
/// into the desired output buffer, instead of being copied on each flush.
327328
void SetBuffer(char *BufferStart, size_t Size) {
328-
SetBufferAndMode(BufferStart, Size, ExternalBuffer);
329+
SetBufferAndMode(BufferStart, Size, BufferKind::ExternalBuffer);
329330
}
330331

331332
/// Return an efficient buffer size for the underlying output mechanism.
@@ -384,7 +385,7 @@ class raw_pwrite_stream : public raw_ostream {
384385
class raw_fd_ostream : public raw_pwrite_stream {
385386
int FD;
386387
bool ShouldClose;
387-
bool SupportsSeeking;
388+
bool SupportsSeeking = false;
388389
bool ColorEnabled = true;
389390

390391
#ifdef _WIN32
@@ -395,7 +396,7 @@ class raw_fd_ostream : public raw_pwrite_stream {
395396

396397
std::error_code EC;
397398

398-
uint64_t pos;
399+
uint64_t pos = 0;
399400

400401
/// See raw_ostream::write_impl.
401402
void write_impl(const char *Ptr, size_t Size) override;

llvm/lib/Support/raw_ostream.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ raw_ostream::~raw_ostream() {
8282
assert(OutBufCur == OutBufStart &&
8383
"raw_ostream destructor called with non-empty buffer!");
8484

85-
if (BufferMode == InternalBuffer)
85+
if (BufferMode == BufferKind::InternalBuffer)
8686
delete [] OutBufStart;
8787
}
8888

@@ -102,14 +102,14 @@ void raw_ostream::SetBuffered() {
102102

103103
void raw_ostream::SetBufferAndMode(char *BufferStart, size_t Size,
104104
BufferKind Mode) {
105-
assert(((Mode == Unbuffered && !BufferStart && Size == 0) ||
106-
(Mode != Unbuffered && BufferStart && Size != 0)) &&
105+
assert(((Mode == BufferKind::Unbuffered && !BufferStart && Size == 0) ||
106+
(Mode != BufferKind::Unbuffered && BufferStart && Size != 0)) &&
107107
"stream must be unbuffered or have at least one byte");
108108
// Make sure the current buffer is free of content (we can't flush here; the
109109
// child buffer management logic will be in write_impl).
110110
assert(GetNumBytesInBuffer() == 0 && "Current buffer is non-empty!");
111111

112-
if (BufferMode == InternalBuffer)
112+
if (BufferMode == BufferKind::InternalBuffer)
113113
delete [] OutBufStart;
114114
OutBufStart = BufferStart;
115115
OutBufEnd = OutBufStart+Size;
@@ -223,7 +223,7 @@ raw_ostream &raw_ostream::write(unsigned char C) {
223223
// Group exceptional cases into a single branch.
224224
if (LLVM_UNLIKELY(OutBufCur >= OutBufEnd)) {
225225
if (LLVM_UNLIKELY(!OutBufStart)) {
226-
if (BufferMode == Unbuffered) {
226+
if (BufferMode == BufferKind::Unbuffered) {
227227
write_impl(reinterpret_cast<char*>(&C), 1);
228228
return *this;
229229
}
@@ -243,7 +243,7 @@ raw_ostream &raw_ostream::write(const char *Ptr, size_t Size) {
243243
// Group exceptional cases into a single branch.
244244
if (LLVM_UNLIKELY(size_t(OutBufEnd - OutBufCur) < Size)) {
245245
if (LLVM_UNLIKELY(!OutBufStart)) {
246-
if (BufferMode == Unbuffered) {
246+
if (BufferMode == BufferKind::Unbuffered) {
247247
write_impl(Ptr, Size);
248248
return *this;
249249
}

0 commit comments

Comments
 (0)