C++: fix portability issue with MSVC 2017

Visual Studio issues a C4146 warning on '-static_cast<unsigned>(rhs)'.
The code is weird, probably to cope with INT_MIN.  Let's go back to
using std::max (whose header is still included in position.hh...) like
originally, but with the needed casts.

Reported by 長田偉伸, and with help from Rici Lake.

See also
http://lists.gnu.org/archive/html/bug-bison/2013-02/msg00000.html
and commit 75ae829984.

* data/location.cc (position::add_): Take min as an int.
Use std::max.
While here, get rid of a couple of useless inlines.
This commit is contained in:
Akim Demaille
2018-08-18 16:37:47 +02:00
parent e866c476fd
commit f348522005
2 changed files with 6 additions and 7 deletions

1
THANKS
View File

@@ -172,6 +172,7 @@ Wolfram Wagner ww@mpi-sb.mpg.de
Wwp subscript@free.fr Wwp subscript@free.fr
xolodho xolodho@gmail.com xolodho xolodho@gmail.com
Zack Weinberg zack@codesourcery.com Zack Weinberg zack@codesourcery.com
長田偉伸 cbh34680@iret.co.jp
Many people are not named here because we lost track of them. We Many people are not named here because we lost track of them. We
thank them! Please, help us keeping this list up to date. thank them! Please, help us keeping this list up to date.

View File

@@ -73,12 +73,10 @@ m4_define([b4_position_define],
unsigned column; unsigned column;
private: private:
/// Compute max(min, lhs+rhs) (provided min <= lhs). /// Compute max(min, lhs+rhs).
static unsigned add_ (unsigned lhs, int rhs, unsigned min) static unsigned add_ (unsigned lhs, int rhs, int min)
{ {
return (0 < rhs || -static_cast<unsigned>(rhs) < lhs return static_cast<unsigned>(std::max(min, static_cast<int>(lhs) + rhs));
? rhs + lhs
: min);
} }
}; };
@@ -134,7 +132,7 @@ m4_define([b4_position_define],
** \param pos a reference to the position to redirect ** \param pos a reference to the position to redirect
*/ */
template <typename YYChar> template <typename YYChar>
inline std::basic_ostream<YYChar>& std::basic_ostream<YYChar>&
operator<< (std::basic_ostream<YYChar>& ostr, const position& pos) operator<< (std::basic_ostream<YYChar>& ostr, const position& pos)
{ {
if (pos.filename) if (pos.filename)
@@ -271,7 +269,7 @@ m4_define([b4_location_define],
** Avoid duplicate information. ** Avoid duplicate information.
*/ */
template <typename YYChar> template <typename YYChar>
inline std::basic_ostream<YYChar>& std::basic_ostream<YYChar>&
operator<< (std::basic_ostream<YYChar>& ostr, const location& loc) operator<< (std::basic_ostream<YYChar>& ostr, const location& loc)
{ {
unsigned end_col = 0 < loc.end.column ? loc.end.column - 1 : 0; unsigned end_col = 0 < loc.end.column ? loc.end.column - 1 : 0;