[Patch] More redundant NULL pointer checks

Gawain Bolton gbolton@free.fr
Sat Apr 19 20:52:00 GMT 2003


These patches to add "const" make me wonder if the compiler couldn't 
help us.

I thinking about a warning for variables which the compiler can see are 
not modified.   Something like:

    Warning: Variable x should be const.

Is this possible?  Could this be useful?


Gawain

Paolo Carlini wrote:

> Hi,
>
> the below, which I consider almost obvious in the light of
> the good acceptance of the previous one, completes the work
> for stringstreams. Some constification in this case too.
>
> Will wait one day, then commit.
>
> Paolo.
>
> //////////
>
>------------------------------------------------------------------------
>
>2003-04-19  Paolo Carlini  <pcarlini@unitus.it>
>
>	* include/bits/sstream.tcc (pbackfail): Remove redundant
>	NULL pointer check from test involving _M_in_*.
>	(overflow, seekoff, seekpos): Const qualify bool variables.
>	* include/std/std_sstream.h (underflow): Remove redundant
>	NULL pointer check from test involving _M_in_*.
>	(_M_really_sync): Const qualify bool variables.
>
>	* include/std/std_streambuf.h (sgetc): Remove redundant
>	variable.
>
>------------------------------------------------------------------------
>
>diff -urN libstdc++-v3-orig/include/bits/sstream.tcc libstdc++-v3/include/bits/sstream.tcc
>--- libstdc++-v3-orig/include/bits/sstream.tcc	2003-04-12 21:21:34.000000000 +0200
>+++ libstdc++-v3/include/bits/sstream.tcc	2003-04-19 18:40:17.000000000 +0200
>@@ -47,8 +47,9 @@
>     pbackfail(int_type __c)
>     {
>       int_type __ret = traits_type::eof();
>-      bool __testeof = traits_type::eq_int_type(__c, traits_type::eof());
>-      bool __testpos = this->_M_in_cur && this->_M_in_beg < this->_M_in_cur; 
>+      const bool __testeof = 
>+	traits_type::eq_int_type(__c, traits_type::eof());
>+      const bool __testpos = this->_M_in_beg < this->_M_in_cur; 
>       
>       // Try to put back __c into input sequence in one of three ways.
>       // Order these tests done in is unspecified by the standard.
>@@ -80,11 +81,12 @@
>     basic_stringbuf<_CharT, _Traits, _Alloc>::
>     overflow(int_type __c)
>     {
>-      bool __testout = this->_M_mode & ios_base::out;
>+      const bool __testout = this->_M_mode & ios_base::out;
>       if (__builtin_expect(!__testout, false))
> 	return traits_type::eof();
> 
>-      bool __testeof = traits_type::eq_int_type(__c, traits_type::eof());
>+      const bool __testeof =
>+	traits_type::eq_int_type(__c, traits_type::eof());
>       if (__builtin_expect(__testeof, false))
> 	return traits_type::not_eof(__c);
> 
>@@ -94,7 +96,7 @@
>       // suit particular needs.
>       __size_type __len = std::max(__size_type(_M_string.capacity() + 1),
> 				   __size_type(512));
>-      bool __testput = this->_M_out_cur < this->_M_out_end;
>+      const bool __testput = this->_M_out_cur < this->_M_out_end;
>       if (__builtin_expect(!__testput && __len > _M_string.max_size(), false))
> 	return traits_type::eof();
> 
>@@ -123,7 +125,7 @@
>       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;
>-      bool __testboth = __testin && __testout && __way != ios_base::cur;
>+      const bool __testboth = __testin && __testout && __way != ios_base::cur;
>       __testin &= !(__mode & ios_base::out);
>       __testout &= !(__mode & ios_base::in);
> 
>@@ -189,8 +191,8 @@
> 	  off_type __pos = __sp; // Use streamoff operator to do conversion.
> 	  char_type* __beg = NULL;
> 	  char_type* __end = NULL;
>-	  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;
> 	  
> 	  // NB: Ordered.
> 	  bool __testposi = false;
>diff -urN libstdc++-v3-orig/include/std/std_sstream.h libstdc++-v3/include/std/std_sstream.h
>--- libstdc++-v3-orig/include/std/std_sstream.h	2003-04-12 21:21:34.000000000 +0200
>+++ libstdc++-v3/include/std/std_sstream.h	2003-04-19 18:10:45.000000000 +0200
>@@ -190,7 +190,7 @@
>       virtual int_type
>       underflow()
>       {
>-	if (this->_M_in_cur && this->_M_in_cur < this->_M_in_end)
>+	if (this->_M_in_cur < this->_M_in_end)
> 	  return traits_type::to_int_type(*gptr());
> 	else
> 	  return traits_type::eof();
>@@ -260,8 +260,8 @@
>       virtual void
>       _M_really_sync(char_type* __base, __size_type __i, __size_type __o)
>       {
>-	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;
> 	__size_type __len = _M_string.size();
> 
> 	this->_M_buf = __base;
>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-19 13:28:45.000000000 +0200
>+++ libstdc++-v3/include/std/std_streambuf.h	2003-04-19 18:23:25.000000000 +0200
>@@ -452,12 +452,10 @@
>       int_type 
>       sgetc()
>       {
>-	int_type __ret;
> 	if (_M_in_cur < _M_in_end)
>-	  __ret = traits_type::to_int_type(*(this->gptr()));
>+	  return traits_type::to_int_type(*(this->gptr()));
> 	else 
>-	  __ret = this->underflow();
>-	return __ret;
>+	  return this->underflow();
>       }
> 
>       /**
>  
>




More information about the Libstdc++ mailing list