This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
[Patch] Remove some redundant NULL pointer checks.
- From: Paolo Carlini <pcarlini at unitus dot it>
- To: libstdc++ at gcc dot gnu dot org
- Cc: bkoz <bkoz at redhat dot com>
- Date: Thu, 17 Apr 2003 18:04:51 +0200
- Subject: [Patch] Remove some redundant NULL pointer checks.
Hi,
the below, blessed by Nathan (see his original message
concerning in_avail()), removes many redundant NULL pointer
checks from iostream functions.
Basically, the point is that if any of _M_out_* (_M_in_*)
is NULL all the others are NULL too.
I took the occasion, to also const-qualify
some boolean variables.
Tested x86-linux.
Benjamin, do you like it for trunk?
Thanks,
Paolo.
////////
2003-04-17 Paolo Carlini <pcarlini at unitus dot it>
According to 5.9 para 2 (second bullet) for pointers p, q
pointing to the same type, with p == 0 and q == 0, (p < q)
is false.
* include/bits/fstream.tcc (close, overflow, _M_really_overflow,
seekoff): Remove redundant NULL pointer checks from tests
involving _M_out_* and _M_in_*, const qualify bool variables.
(showmanyc, pbackfail, _M_convert_to_external, imbue): Const
qualify bool variables.
* include/bits/streambuf.tcc (sbumpc, sputbackc, sungetc, sputc):
Remove redundant NULL pointer checks from tests involving
_M_out_* and _M_in_*, const qualify bool variables.
* include/std/std_fstream.h (sync): Likewise.
(_M_is_indeterminate): Const qualify bool variables.
* include/std/std_streambuf.h (sgetc, uflow): Remove redundant
NULL pointer checks from tests involving _M_out_* and _M_in_*,
const qualify bool variables.
(_M_in_cur_move, _M_out_cur_move, uflow): Const qualify bool
variables.
diff -urN libstdc++-v3-orig/include/bits/fstream.tcc libstdc++-v3/include/bits/fstream.tcc
--- libstdc++-v3-orig/include/bits/fstream.tcc 2003-04-14 19:57:48.000000000 +0200
+++ libstdc++-v3/include/bits/fstream.tcc 2003-04-17 17:07:09.000000000 +0200
@@ -116,8 +116,8 @@
{
bool __testfail = false;
const int_type __eof = traits_type::eof();
- bool __testput = this->_M_out_cur
- && this->_M_out_beg < this->_M_out_lim;
+ const bool __testput = this->_M_out_beg < this->_M_out_lim;
+
if (__testput
&& traits_type::eq_int_type(_M_really_overflow(__eof), __eof))
__testfail = true;
@@ -152,11 +152,11 @@
showmanyc()
{
streamsize __ret = -1;
- bool __testin = this->_M_mode & ios_base::in;
+ const bool __testin = this->_M_mode & ios_base::in;
const locale __loc = this->getloc();
const __codecvt_type& __cvt = use_facet<__codecvt_type>(__loc);
// Sync with stdio.
- bool __sync = this->_M_buf_size <= 1;
+ const bool __sync = this->_M_buf_size <= 1;
if (__testin && this->is_open())
{
@@ -178,18 +178,18 @@
pbackfail(int_type __i)
{
int_type __ret = traits_type::eof();
- bool __testin = this->_M_mode & ios_base::in;
+ const bool __testin = this->_M_mode & ios_base::in;
if (__testin)
{
- bool __testpb = this->_M_in_beg < this->_M_in_cur;
+ const bool __testpb = this->_M_in_beg < this->_M_in_cur;
char_type __c = traits_type::to_char_type(__i);
- bool __testeof = traits_type::eq_int_type(__i, __ret);
+ const bool __testeof = traits_type::eq_int_type(__i, __ret);
if (__testpb)
{
- bool __testout = this->_M_mode & ios_base::out;
- bool __testeq = traits_type::eq(__c, this->gptr()[-1]);
+ const bool __testout = this->_M_mode & ios_base::out;
+ const bool __testeq = traits_type::eq(__c, this->gptr()[-1]);
// Try to put back __c into input sequence in one of three ways.
// Order these tests done in is unspecified by the standard.
@@ -251,9 +251,8 @@
overflow(int_type __c)
{
int_type __ret = traits_type::eof();
- bool __testput =
- this->_M_out_cur && this->_M_out_cur < this->_M_out_end;
- bool __testout = this->_M_mode & ios_base::out;
+ const bool __testput = this->_M_out_cur < this->_M_out_end;
+ const bool __testout = this->_M_mode & ios_base::out;
if (__testout)
{
@@ -282,7 +281,7 @@
const locale __loc = this->getloc();
const __codecvt_type& __cvt = use_facet<__codecvt_type>(__loc);
// Sync with stdio.
- bool __sync = this->_M_buf_size <= 1;
+ const bool __sync = this->_M_buf_size <= 1;
if (__cvt.always_noconv() && __ilen)
{
@@ -352,10 +351,10 @@
_M_really_overflow(int_type __c)
{
int_type __ret = traits_type::eof();
- bool __testput = this->_M_out_cur && this->_M_out_beg < this->_M_out_lim;
- bool __testunbuffered = _M_file.is_open() && !this->_M_buf_size;
+ const bool __testput = this->_M_out_beg < this->_M_out_lim;
+ const bool __testunbuffered = _M_file.is_open() && !this->_M_buf_size;
// Sync with stdio.
- bool __sync = this->_M_buf_size <= 1;
+ const bool __sync = this->_M_buf_size <= 1;
if (__testput || __testunbuffered)
{
@@ -440,16 +439,16 @@
seekoff(off_type __off, ios_base::seekdir __way, ios_base::openmode __mode)
{
pos_type __ret = pos_type(off_type(-1));
- bool __testin = (ios_base::in & this->_M_mode & __mode) != 0;
- bool __testout = (ios_base::out & this->_M_mode & __mode) != 0;
+ const bool __testin = (ios_base::in & this->_M_mode & __mode) != 0;
+ const bool __testout = (ios_base::out & this->_M_mode & __mode) != 0;
// Sync with stdio.
- bool __sync = this->_M_buf_size <= 1;
+ const bool __sync = this->_M_buf_size <= 1;
// Should probably do has_facet checks here.
int __width = use_facet<__codecvt_type>(this->_M_buf_locale).encoding();
if (__width < 0)
__width = 0;
- bool __testfail = __off != 0 && __width <= 0;
+ const bool __testfail = __off != 0 && __width <= 0;
if (this->is_open() && !__testfail && (__testin || __testout))
{
@@ -460,10 +459,8 @@
{
off_type __computed_off = __width * __off;
- bool __testget = this->_M_in_cur
- && this->_M_in_beg < this->_M_in_end;
- bool __testput = this->_M_out_cur
- && this->_M_out_beg < this->_M_out_lim;
+ const bool __testget = this->_M_in_beg < this->_M_in_end;
+ const bool __testput = this->_M_out_beg < this->_M_out_lim;
// Sync the internal and external streams.
// out
if (__testput || _M_last_overflowed)
@@ -523,7 +520,7 @@
basic_filebuf<_CharT, _Traits>::
imbue(const locale& __loc)
{
- bool __testbeg = gptr() == eback() && pptr() == pbase();
+ const bool __testbeg = gptr() == eback() && pptr() == pbase();
if (__testbeg && this->_M_buf_locale != __loc)
this->_M_buf_locale = __loc;
diff -urN libstdc++-v3-orig/include/bits/streambuf.tcc libstdc++-v3/include/bits/streambuf.tcc
--- libstdc++-v3-orig/include/bits/streambuf.tcc 2003-04-12 21:21:34.000000000 +0200
+++ libstdc++-v3/include/bits/streambuf.tcc 2003-04-17 17:08:08.000000000 +0200
@@ -49,7 +49,7 @@
sbumpc()
{
int_type __ret;
- if (_M_in_cur && _M_in_cur < _M_in_end)
+ if (_M_in_cur < _M_in_end)
{
char_type __c = *(this->gptr());
_M_in_cur_move(1);
@@ -66,7 +66,7 @@
sputbackc(char_type __c)
{
int_type __ret;
- bool __testpos = _M_in_cur && _M_in_beg < _M_in_cur;
+ const bool __testpos = _M_in_beg < _M_in_cur;
if (!__testpos || !traits_type::eq(__c, this->gptr()[-1]))
__ret = this->pbackfail(traits_type::to_int_type(__c));
else
@@ -83,7 +83,7 @@
sungetc()
{
int_type __ret;
- if (_M_in_cur && _M_in_beg < _M_in_cur)
+ if (_M_in_beg < _M_in_cur)
{
_M_in_cur_move(-1);
__ret = traits_type::to_int_type(*_M_in_cur);
@@ -99,7 +99,7 @@
sputc(char_type __c)
{
int_type __ret;
- if (_M_out_cur && _M_out_cur < _M_out_end)
+ if (_M_out_cur < _M_out_end)
{
*_M_out_cur = __c;
_M_out_cur_move(1);
diff -urN libstdc++-v3-orig/include/std/std_fstream.h libstdc++-v3/include/std/std_fstream.h
--- libstdc++-v3-orig/include/std/std_fstream.h 2003-04-10 09:15:25.000000000 +0200
+++ libstdc++-v3/include/std/std_fstream.h 2003-04-17 17:09:22.000000000 +0200
@@ -312,10 +312,9 @@
sync()
{
int __ret = 0;
- bool __testput = this->_M_out_cur
- && this->_M_out_beg < this->_M_out_lim;
+ const bool __testput = this->_M_out_beg < this->_M_out_lim;
// Sync with stdio.
- bool __sync = this->_M_buf_size <= 1;
+ const bool __sync = this->_M_buf_size <= 1;
// Make sure that the internal buffer resyncs its idea of
// the file position with the external file.
@@ -404,8 +403,8 @@
void
_M_set_determinate(off_type __off)
{
- bool __testin = this->_M_mode & ios_base::in;
- bool __testout = this->_M_mode & ios_base::out;
+ const bool __testin = this->_M_mode & ios_base::in;
+ const bool __testout = this->_M_mode & ios_base::out;
if (__testin)
this->setg(this->_M_buf, this->_M_buf, this->_M_buf + __off);
if (__testout)
@@ -424,8 +423,8 @@
bool
_M_is_indeterminate(void)
{
- bool __testin = this->_M_mode & ios_base::in;
- bool __testout = this->_M_mode & ios_base::out;
+ const bool __testin = this->_M_mode & ios_base::in;
+ const bool __testout = this->_M_mode & ios_base::out;
bool __ret = false;
// Don't return true if unbuffered.
if (this->_M_buf)
diff -urN libstdc++-v3-orig/include/std/std_streambuf.h libstdc++-v3/include/std/std_streambuf.h
--- libstdc++-v3-orig/include/std/std_streambuf.h 2003-04-17 13:26:33.000000000 +0200
+++ libstdc++-v3/include/std/std_streambuf.h 2003-04-17 17:10:28.000000000 +0200
@@ -303,7 +303,7 @@
void
_M_in_cur_move(off_type __n) // argument needs to be +-
{
- bool __testout = _M_out_cur;
+ const bool __testout = _M_out_cur;
_M_in_cur += __n;
if (__testout && _M_buf_unified)
_M_out_cur += __n;
@@ -320,7 +320,7 @@
void
_M_out_cur_move(off_type __n) // argument needs to be +-
{
- bool __testin = _M_in_cur;
+ const bool __testin = _M_in_cur;
_M_out_cur += __n;
if (__testin && _M_buf_unified)
@@ -453,7 +453,7 @@
sgetc()
{
int_type __ret;
- if (_M_in_cur && _M_in_cur < _M_in_end)
+ if (_M_in_cur < _M_in_end)
__ret = traits_type::to_int_type(*(this->gptr()));
else
__ret = this->underflow();
@@ -787,8 +787,9 @@
uflow()
{
int_type __ret = traits_type::eof();
- bool __testeof = traits_type::eq_int_type(this->underflow(), __ret);
- bool __testpending = _M_in_cur && _M_in_cur < _M_in_end;
+ const bool __testeof =
+ traits_type::eq_int_type(this->underflow(), __ret);
+ const bool __testpending = _M_in_cur < _M_in_end;
if (!__testeof && __testpending)
{
__ret = traits_type::to_int_type(*_M_in_cur);