fix llabs()/_abs64() on unsigned warnings 23/21123/2
Clément DAVID [Thu, 7 Nov 2019 08:21:40 +0000 (09:21 +0100)]
Change-Id: Ia1d7e89f649a871172c13eb2211d02e4eda793bd

scilab/modules/ast/includes/types/int.hxx
scilab/modules/ast/includes/types/tostring_common.hxx
scilab/modules/ast/src/cpp/types/sparse.cpp

index 95dff8e..12e019d 100644 (file)
@@ -200,16 +200,8 @@ private :
         {
             ostr << L"eye *" << std::endl << std::endl;
             int iWidth = 0;
-            if (isSigned())
-            {
-                getSignedIntFormat(ArrayOf<T>::get(0), &iWidth);
-                addSignedIntValue(&ostr, ArrayOf<T>::get(0), iWidth);
-            }
-            else
-            {
-                getUnsignedIntFormat(ArrayOf<T>::get(0), &iWidth);
-                addUnsignedIntValue(&ostr, ArrayOf<T>::get(0), iWidth);
-            }
+            getIntFormat(ArrayOf<T>::get(0), &iWidth);
+            addIntValue(&ostr, ArrayOf<T>::get(0), iWidth);
             ostr << std::endl;
         }
         else if (GenericType::isScalar())
@@ -220,16 +212,9 @@ private :
             _piDims[1]  = 0;
             int iPos    = ArrayOf<T>::getIndex(_piDims);
 
-            if (isSigned())
-            {
-                getSignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
-                addSignedIntValue(&ostr, ArrayOf<T>::get(iPos), iWidth);
-            }
-            else
-            {
-                getUnsignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
-                addUnsignedIntValue(&ostr, ArrayOf<T>::get(iPos), iWidth);
-            }
+            getIntFormat(ArrayOf<T>::get(iPos), &iWidth);
+            addIntValue(&ostr, ArrayOf<T>::get(iPos), iWidth);
+
             ostr << std::endl;
         }
         else if (GenericType::getCols() == 1)
@@ -244,14 +229,7 @@ private :
                 _piDims[0] = i;
                 int iWidth = 0;
                 int iPos = ArrayOf<T>::getIndex(_piDims);
-                if (isSigned())
-                {
-                    getSignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
-                }
-                else
-                {
-                    getUnsignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
-                }
+                getIntFormat(ArrayOf<T>::get(iPos), &iWidth);
                 iWidthMax = (std::max)(iWidthMax, iWidth);
             }
 
@@ -268,14 +246,7 @@ private :
                 _piDims[0]  = i;
                 int iPos    = ArrayOf<T>::getIndex(_piDims);
 
-                if (isSigned())
-                {
-                    addSignedIntValue(&ostr, ArrayOf<T>::get(iPos), iWidthMax);
-                }
-                else
-                {
-                    addUnsignedIntValue(&ostr, ArrayOf<T>::get(iPos), iWidthMax);
-                }
+                addIntValue(&ostr, ArrayOf<T>::get(iPos), iWidthMax);
                 ostr << std::endl;
             }
         }
@@ -293,14 +264,7 @@ private :
                 _piDims[1]  = i;
                 int iPos    = ArrayOf<T>::getIndex(_piDims);
 
-                if (isSigned())
-                {
-                    getSignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
-                }
-                else
-                {
-                    getUnsignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
-                }
+                getIntFormat(ArrayOf<T>::get(iPos), &iWidth);
 
                 iLen = iWidth + static_cast<int>(ostemp.str().size());
                 if (iLen > iLineLen && iLastVal != i)
@@ -319,14 +283,7 @@ private :
                     iLastVal = i;
                 }
 
-                if (isSigned())
-                {
-                    addSignedIntValue(&ostemp, ArrayOf<T>::get(iPos), iWidth);
-                }
-                else
-                {
-                    addUnsignedIntValue(&ostemp, ArrayOf<T>::get(iPos), iWidth);
-                }
+                addIntValue(&ostemp, ArrayOf<T>::get(iPos), iWidth);
             }
 
             if (iLastVal != 0)
@@ -357,14 +314,7 @@ private :
                     _piDims[1]  = iCols1;
                     int iPos    = ArrayOf<T>::getIndex(_piDims);
 
-                    if (isSigned())
-                    {
-                        getSignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
-                    }
-                    else
-                    {
-                        getUnsignedIntFormat(ArrayOf<T>::get(iPos), &iWidth);
-                    }
+                    getIntFormat(ArrayOf<T>::get(iPos), &iWidth);
                     piSize[iCols1] = (std::max)(piSize[iCols1], iWidth);
                 }
 
@@ -396,14 +346,7 @@ private :
                             _piDims[1]  = iCols2;
                             int iPos    = ArrayOf<T>::getIndex(_piDims);
 
-                            if (isSigned())
-                            {
-                                addSignedIntValue(&ostemp, ArrayOf<T>::get(iPos), piSize[iCols2]);
-                            }
-                            else
-                            {
-                                addUnsignedIntValue(&ostemp, ArrayOf<T>::get(iPos), piSize[iCols2]);
-                            }
+                            addIntValue(&ostemp, ArrayOf<T>::get(iPos), piSize[iCols2]);
                         }
                         ostemp << std::endl;
                     }
@@ -449,14 +392,7 @@ private :
                     _piDims[1]  = iCols2;
                     int iPos    = ArrayOf<T>::getIndex(_piDims);
 
-                    if (isSigned())
-                    {
-                        addSignedIntValue(&ostemp, ArrayOf<T>::get(iPos), piSize[iCols2]);
-                    }
-                    else
-                    {
-                        addUnsignedIntValue(&ostemp, ArrayOf<T>::get(iPos), piSize[iCols2]);
-                    }
+                    addIntValue(&ostemp, ArrayOf<T>::get(iPos), piSize[iCols2]);
                 }
                 ostemp << std::endl;
             }
index d58f6a8..b728bc4 100644 (file)
@@ -16,6 +16,7 @@
 #ifndef __TOSTRING_COMMON_HXX__
 #define __TOSTRING_COMMON_HXX__
 
+#include <type_traits>
 #include <sstream>
 #include "os_string.h"
 #include "dynlib_ast.h"
@@ -77,7 +78,10 @@ EXTERN_AST void printComplexValue(std::wostringstream& ostr, double val_r, doubl
 */
 
 template <typename T>
-void getUnsignedIntFormat(T _TVal, int *_piWidth)
+void getIntFormat(T _TVal, int *_piWidth,
+                  typename std::enable_if <
+                  std::is_unsigned<T>::value
+                  >::type* = 0)
 {
     if (_TVal == 0)
     {
@@ -91,7 +95,10 @@ void getUnsignedIntFormat(T _TVal, int *_piWidth)
 }
 
 template <typename T>
-void getSignedIntFormat(T _TVal, int *_piWidth)
+void getIntFormat(T _TVal, int *_piWidth,
+                  typename std::enable_if <
+                  std::is_signed<T>::value
+                  >::type* = 0)
 {
     if (_TVal == 0)
     {
@@ -109,7 +116,10 @@ void getSignedIntFormat(T _TVal, int *_piWidth)
 }
 
 template <typename T>
-void addUnsignedIntValue(std::wostringstream *_postr, T _TVal, int _iWidth, bool bPrintPlusSign = false, bool bPrintOne = true)
+void addIntValue(std::wostringstream *_postr, T _TVal, int _iWidth, bool bPrintPlusSign = false, bool bPrintOne = true,
+                 typename std::enable_if <
+                 std::is_unsigned<T>::value
+                 >::type* = 0)
 {
     const wchar_t * pwstSign = PLUS_STRING;
     wchar_t pwstFormat[32];
@@ -132,7 +142,10 @@ void addUnsignedIntValue(std::wostringstream *_postr, T _TVal, int _iWidth, bool
 }
 
 template <typename T>
-void addSignedIntValue(std::wostringstream *_postr, T _TVal, int _iWidth, bool bPrintPlusSign = false, bool bPrintOne = true)
+void addIntValue(std::wostringstream *_postr, T _TVal, int _iWidth, bool bPrintPlusSign = false, bool bPrintOne = true,
+                 typename std::enable_if <
+                 std::is_signed<T>::value
+                 >::type* = 0)
 {
     const wchar_t* pwstSign = NULL;
     wchar_t pwstFormat[32];
index cb020c1..d42a3ea 100644 (file)
@@ -133,13 +133,13 @@ template<typename T> std::wstring toString(T const& m, int precision)
 
     int iWidthRows = 0;
     int iWidthCols = 0;
-    getSignedIntFormat(m.rows(), &iWidthRows);
-    getSignedIntFormat(m.cols(), &iWidthCols);
+    getIntFormat(m.rows(), &iWidthRows);
+    getIntFormat(m.cols(), &iWidthCols);
 
     ostr << L"(";
-    addUnsignedIntValue<unsigned long long>(&ostr, m.rows(), iWidthRows);
+    addIntValue<unsigned long long>(&ostr, m.rows(), iWidthRows);
     ostr << ",";
-    addUnsignedIntValue<unsigned long long>(&ostr, m.cols(), iWidthCols);
+    addIntValue<unsigned long long>(&ostr, m.cols(), iWidthCols);
     ostr << L")";
 
     Printer p(precision);
@@ -160,9 +160,9 @@ template<typename T> std::wstring toString(T const& m, int precision)
         for (size_t i = pINbItemByRow[j - 1]; i < pINbItemByRow[j]; i++)
         {
             ostr << L"(";
-            addUnsignedIntValue<unsigned long long>(&ostr, (int)j, iWidthRows);
+            addIntValue<unsigned long long>(&ostr, (int)j, iWidthRows);
             ostr << L",";
-            addUnsignedIntValue<unsigned long long>(&ostr, pIColPos[iPos] + 1, iWidthCols);
+            addIntValue<unsigned long long>(&ostr, pIColPos[iPos] + 1, iWidthCols);
             ostr << L")\t" << p(m.valuePtr()[iPos]) << std::endl;
 
             iPos++;