raw_ostream - fix static analyzer warnings. NFCI.

- uninitialized variables
 - make BufferKind a scoped enum class
This commit is contained in:
Simon Pilgrim 2019-11-08 15:06:33 +00:00
parent c8f0bb4021
commit 9ee76ab37f
2 changed files with 15 additions and 14 deletions

View File

@ -64,7 +64,7 @@ private:
/// this buffer. /// this buffer.
char *OutBufStart, *OutBufEnd, *OutBufCur; char *OutBufStart, *OutBufEnd, *OutBufCur;
enum BufferKind { enum class BufferKind {
Unbuffered = 0, Unbuffered = 0,
InternalBuffer, InternalBuffer,
ExternalBuffer ExternalBuffer
@ -97,7 +97,8 @@ public:
static const Colors RESET = Colors::RESET; static const Colors RESET = Colors::RESET;
explicit raw_ostream(bool unbuffered = false) explicit raw_ostream(bool unbuffered = false)
: BufferMode(unbuffered ? Unbuffered : InternalBuffer) { : BufferMode(unbuffered ? BufferKind::Unbuffered
: BufferKind::InternalBuffer) {
// Start out ready to flush. // Start out ready to flush.
OutBufStart = OutBufEnd = OutBufCur = nullptr; OutBufStart = OutBufEnd = OutBufCur = nullptr;
} }
@ -121,13 +122,13 @@ public:
/// Set the stream to be buffered, using the specified buffer size. /// Set the stream to be buffered, using the specified buffer size.
void SetBufferSize(size_t Size) { void SetBufferSize(size_t Size) {
flush(); flush();
SetBufferAndMode(new char[Size], Size, InternalBuffer); SetBufferAndMode(new char[Size], Size, BufferKind::InternalBuffer);
} }
size_t GetBufferSize() const { size_t GetBufferSize() const {
// If we're supposed to be buffered but haven't actually gotten around // If we're supposed to be buffered but haven't actually gotten around
// to allocating the buffer yet, return the value that would be used. // to allocating the buffer yet, return the value that would be used.
if (BufferMode != Unbuffered && OutBufStart == nullptr) if (BufferMode != BufferKind::Unbuffered && OutBufStart == nullptr)
return preferred_buffer_size(); return preferred_buffer_size();
// Otherwise just return the size of the allocated buffer. // Otherwise just return the size of the allocated buffer.
@ -139,7 +140,7 @@ public:
/// when the stream is being set to unbuffered. /// when the stream is being set to unbuffered.
void SetUnbuffered() { void SetUnbuffered() {
flush(); flush();
SetBufferAndMode(nullptr, 0, Unbuffered); SetBufferAndMode(nullptr, 0, BufferKind::Unbuffered);
} }
size_t GetNumBytesInBuffer() const { size_t GetNumBytesInBuffer() const {
@ -325,7 +326,7 @@ protected:
/// use only by subclasses which can arrange for the output to go directly /// use only by subclasses which can arrange for the output to go directly
/// into the desired output buffer, instead of being copied on each flush. /// into the desired output buffer, instead of being copied on each flush.
void SetBuffer(char *BufferStart, size_t Size) { void SetBuffer(char *BufferStart, size_t Size) {
SetBufferAndMode(BufferStart, Size, ExternalBuffer); SetBufferAndMode(BufferStart, Size, BufferKind::ExternalBuffer);
} }
/// Return an efficient buffer size for the underlying output mechanism. /// Return an efficient buffer size for the underlying output mechanism.
@ -384,7 +385,7 @@ public:
class raw_fd_ostream : public raw_pwrite_stream { class raw_fd_ostream : public raw_pwrite_stream {
int FD; int FD;
bool ShouldClose; bool ShouldClose;
bool SupportsSeeking; bool SupportsSeeking = false;
bool ColorEnabled = true; bool ColorEnabled = true;
#ifdef _WIN32 #ifdef _WIN32
@ -395,7 +396,7 @@ class raw_fd_ostream : public raw_pwrite_stream {
std::error_code EC; std::error_code EC;
uint64_t pos; uint64_t pos = 0;
/// See raw_ostream::write_impl. /// See raw_ostream::write_impl.
void write_impl(const char *Ptr, size_t Size) override; void write_impl(const char *Ptr, size_t Size) override;

View File

@ -82,7 +82,7 @@ raw_ostream::~raw_ostream() {
assert(OutBufCur == OutBufStart && assert(OutBufCur == OutBufStart &&
"raw_ostream destructor called with non-empty buffer!"); "raw_ostream destructor called with non-empty buffer!");
if (BufferMode == InternalBuffer) if (BufferMode == BufferKind::InternalBuffer)
delete [] OutBufStart; delete [] OutBufStart;
} }
@ -102,14 +102,14 @@ void raw_ostream::SetBuffered() {
void raw_ostream::SetBufferAndMode(char *BufferStart, size_t Size, void raw_ostream::SetBufferAndMode(char *BufferStart, size_t Size,
BufferKind Mode) { BufferKind Mode) {
assert(((Mode == Unbuffered && !BufferStart && Size == 0) || assert(((Mode == BufferKind::Unbuffered && !BufferStart && Size == 0) ||
(Mode != Unbuffered && BufferStart && Size != 0)) && (Mode != BufferKind::Unbuffered && BufferStart && Size != 0)) &&
"stream must be unbuffered or have at least one byte"); "stream must be unbuffered or have at least one byte");
// Make sure the current buffer is free of content (we can't flush here; the // Make sure the current buffer is free of content (we can't flush here; the
// child buffer management logic will be in write_impl). // child buffer management logic will be in write_impl).
assert(GetNumBytesInBuffer() == 0 && "Current buffer is non-empty!"); assert(GetNumBytesInBuffer() == 0 && "Current buffer is non-empty!");
if (BufferMode == InternalBuffer) if (BufferMode == BufferKind::InternalBuffer)
delete [] OutBufStart; delete [] OutBufStart;
OutBufStart = BufferStart; OutBufStart = BufferStart;
OutBufEnd = OutBufStart+Size; OutBufEnd = OutBufStart+Size;
@ -223,7 +223,7 @@ raw_ostream &raw_ostream::write(unsigned char C) {
// Group exceptional cases into a single branch. // Group exceptional cases into a single branch.
if (LLVM_UNLIKELY(OutBufCur >= OutBufEnd)) { if (LLVM_UNLIKELY(OutBufCur >= OutBufEnd)) {
if (LLVM_UNLIKELY(!OutBufStart)) { if (LLVM_UNLIKELY(!OutBufStart)) {
if (BufferMode == Unbuffered) { if (BufferMode == BufferKind::Unbuffered) {
write_impl(reinterpret_cast<char*>(&C), 1); write_impl(reinterpret_cast<char*>(&C), 1);
return *this; return *this;
} }
@ -243,7 +243,7 @@ raw_ostream &raw_ostream::write(const char *Ptr, size_t Size) {
// Group exceptional cases into a single branch. // Group exceptional cases into a single branch.
if (LLVM_UNLIKELY(size_t(OutBufEnd - OutBufCur) < Size)) { if (LLVM_UNLIKELY(size_t(OutBufEnd - OutBufCur) < Size)) {
if (LLVM_UNLIKELY(!OutBufStart)) { if (LLVM_UNLIKELY(!OutBufStart)) {
if (BufferMode == Unbuffered) { if (BufferMode == BufferKind::Unbuffered) {
write_impl(Ptr, Size); write_impl(Ptr, Size);
return *this; return *this;
} }