[Libreoffice-commits] core.git: basic/source include/o3tl o3tl/CompilerTest_o3tl_temporary.mk o3tl/Module_o3tl.mk o3tl/qa reportdesign/source sal/rtl scaddins/source sc/source slideshow/source sw/source writerfilter/source xmloff/source

Stephan Bergmann sbergman at redhat.com
Thu May 17 19:03:38 UTC 2018


 basic/source/comp/exprnode.cxx                        |   10 ++-----
 basic/source/runtime/methods1.cxx                     |    4 +-
 include/o3tl/temporary.hxx                            |   22 +++++++++++++++
 o3tl/CompilerTest_o3tl_temporary.mk                   |   16 +++++++++++
 o3tl/Module_o3tl.mk                                   |    1 
 o3tl/qa/compile-temporary.cxx                         |   25 ++++++++++++++++++
 reportdesign/source/filter/xml/xmlControlProperty.cxx |    4 +-
 sal/rtl/random.cxx                                    |    3 +-
 sc/source/filter/excel/xepivotxml.cxx                 |    4 +-
 scaddins/source/analysis/financial.cxx                |    4 +-
 slideshow/source/engine/transitions/snakewipe.cxx     |    7 ++---
 sw/source/core/bastyp/calc.cxx                        |    5 ++-
 sw/source/core/fields/flddat.cxx                      |    4 +-
 writerfilter/source/dmapper/DomainMapper_Impl.cxx     |    5 +--
 xmloff/source/forms/propertyimport.cxx                |    5 +--
 15 files changed, 90 insertions(+), 29 deletions(-)

New commits:
commit 649313625b94e6b879848fc19b607b74375100bf
Author: Stephan Bergmann <sbergman at redhat.com>
Date:   Thu May 17 11:28:56 2018 +0200

    New o3tl::temporary to simplify calls of std::modf
    
    ...that ignore the out-parameter integral part
    
    Change-Id: I05f07c1a8909023232f8aecf75ea5541d4eb81ca
    Reviewed-on: https://gerrit.libreoffice.org/54474
    Reviewed-by: Stephan Bergmann <sbergman at redhat.com>
    Tested-by: Stephan Bergmann <sbergman at redhat.com>

diff --git a/basic/source/comp/exprnode.cxx b/basic/source/comp/exprnode.cxx
index 8d6db9bd88d8..aa8f69828c37 100644
--- a/basic/source/comp/exprnode.cxx
+++ b/basic/source/comp/exprnode.cxx
@@ -21,6 +21,7 @@
 #include <math.h>
 #include <algorithm>
 
+#include <o3tl/temporary.hxx>
 #include <rtl/math.hxx>
 #include <codegen.hxx>
 #include <parser.hxx>
@@ -152,8 +153,7 @@ void SbiExprNode::ConvertToIntConstIfPossible()
     {
         if( eType >= SbxINTEGER && eType <= SbxDOUBLE )
         {
-            double n;
-            if( nVal >= SbxMININT && nVal <= SbxMAXINT && modf( nVal, &n ) == 0 )
+            if( nVal >= SbxMININT && nVal <= SbxMAXINT && modf( nVal, &o3tl::temporary(double()) ) == 0 )
             {
                 eType = SbxINTEGER;
             }
@@ -220,9 +220,8 @@ void SbiExprNode::FoldConstants(SbiParser* pParser)
         // Potentially convolve in INTEGER (because of better opcode)?
         if( eType == SbxSINGLE || eType == SbxDOUBLE )
         {
-            double x;
             if( nVal >= SbxMINLNG && nVal <= SbxMAXLNG
-            && !modf( nVal, &x ) )
+            && !modf( nVal, &o3tl::temporary(double()) ) )
                 eType = SbxLONG;
         }
         if( eType == SbxLONG && nVal >= SbxMININT && nVal <= SbxMAXINT )
@@ -463,9 +462,8 @@ void SbiExprNode::FoldConstantsUnaryNode(SbiParser* pParser)
         // Potentially convolve in INTEGER (because of better opcode)?
         if( eType == SbxSINGLE || eType == SbxDOUBLE )
         {
-            double x;
             if( nVal >= SbxMINLNG && nVal <= SbxMAXLNG
-            && !modf( nVal, &x ) )
+            && !modf( nVal, &o3tl::temporary(double()) ) )
                 eType = SbxLONG;
         }
         if( eType == SbxLONG && nVal >= SbxMININT && nVal <= SbxMAXINT )
diff --git a/basic/source/runtime/methods1.cxx b/basic/source/runtime/methods1.cxx
index 3a1964783ff5..8c8c3dcbcd7d 100644
--- a/basic/source/runtime/methods1.cxx
+++ b/basic/source/runtime/methods1.cxx
@@ -35,6 +35,7 @@
 #include <svl/zforlist.hxx>
 #include <tools/urlobj.hxx>
 #include <tools/fract.hxx>
+#include <o3tl/temporary.hxx>
 #include <osl/file.hxx>
 #include <vcl/jobset.hxx>
 #include <sbobjmod.hxx>
@@ -2354,8 +2355,7 @@ void SbRtl_FormatDateTime(StarBASIC *, SbxArray & rPar, bool)
         // ShortTime: Display a time using the 24-hour format (hh:mm).
         // 11:24
     case 4:
-        double n;
-        double dTime = modf( dDate, &n );
+        double dTime = modf( dDate, &o3tl::temporary(double()) );
         pSbxVar->PutDate( dTime );
         if( nNamedFormat == 3 )
         {
diff --git a/include/o3tl/temporary.hxx b/include/o3tl/temporary.hxx
new file mode 100644
index 000000000000..476e934cb44a
--- /dev/null
+++ b/include/o3tl/temporary.hxx
@@ -0,0 +1,22 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include <sal/config.h>
+
+namespace o3tl
+{
+// Cast an rvalue to an lvalue.  Can be useful if a function parameter is a pointer/reference to T,
+// and some call site doesn't need the value beyond the call itself (e.g., in a call like
+// std::modf(x, &o3tl::temporary(double())) to obtain the fractional part of x, ignoring the
+// integral part).
+template <typename T> constexpr T& temporary(T&& x) { return x; }
+template <typename T> constexpr T& temporary(T&) = delete;
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
diff --git a/o3tl/CompilerTest_o3tl_temporary.mk b/o3tl/CompilerTest_o3tl_temporary.mk
new file mode 100644
index 000000000000..fe8a830e03b0
--- /dev/null
+++ b/o3tl/CompilerTest_o3tl_temporary.mk
@@ -0,0 +1,16 @@
+# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t; fill-column: 100 -*-
+#
+# This file is part of the LibreOffice project.
+#
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+#
+
+$(eval $(call gb_CompilerTest_CompilerTest,o3tl_temporary))
+
+$(eval $(call gb_CompilerTest_add_exception_objects,o3tl_temporary, \
+    o3tl/qa/compile-temporary \
+))
+
+# vim: set noet sw=4 ts=4:
diff --git a/o3tl/Module_o3tl.mk b/o3tl/Module_o3tl.mk
index b692ca5de633..779b37ef56af 100644
--- a/o3tl/Module_o3tl.mk
+++ b/o3tl/Module_o3tl.mk
@@ -23,6 +23,7 @@ $(eval $(call gb_Module_add_targets,o3tl,\
 
 $(eval $(call gb_Module_add_check_targets,o3tl,\
 	CppunitTest_o3tl_tests \
+	$(if $(COM_IS_CLANG),CompilerTest_o3tl_temporary) \
 ))
 
 # vim: set noet sw=4:
diff --git a/o3tl/qa/compile-temporary.cxx b/o3tl/qa/compile-temporary.cxx
new file mode 100644
index 000000000000..1d8d84bd5fb5
--- /dev/null
+++ b/o3tl/qa/compile-temporary.cxx
@@ -0,0 +1,25 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include <sal/config.h>
+
+#include <o3tl/temporary.hxx>
+
+void f(int*);
+
+int g();
+
+void h(int n)
+{
+    f(&o3tl::temporary(int()));
+    f(&o3tl::temporary(g()));
+    f(&o3tl::temporary(n)); // expected-error {{}} expected-note at o3tl/temporary.hxx:* 0+ {{}}
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
diff --git a/reportdesign/source/filter/xml/xmlControlProperty.cxx b/reportdesign/source/filter/xml/xmlControlProperty.cxx
index e14b86e0e6dc..a4f0741448b3 100644
--- a/reportdesign/source/filter/xml/xmlControlProperty.cxx
+++ b/reportdesign/source/filter/xml/xmlControlProperty.cxx
@@ -24,6 +24,7 @@
 
 #include "xmlControlProperty.hxx"
 
+#include <o3tl/temporary.hxx>
 #include <rtl/strbuf.hxx>
 #include <sax/tools/converter.hxx>
 #include "xmlfilter.hxx"
@@ -272,8 +273,7 @@ Any OXMLControlProperty::convertString(const css::uno::Type& _rExpectedType, con
                     {
                         case TYPE_DATE:
                         {
-                            double dummy;
-                            OSL_ENSURE(std::modf(nValue, &dummy) == 0,
+                            OSL_ENSURE(std::modf(nValue, &o3tl::temporary(double())) == 0,
                                 "OPropertyImport::convertString: a Date value with a fractional part?");
                             (void)dummy;
                             aReturn <<= implGetDate(nValue);
diff --git a/sal/rtl/random.cxx b/sal/rtl/random.cxx
index c9269abb837b..224a345a3094 100644
--- a/sal/rtl/random.cxx
+++ b/sal/rtl/random.cxx
@@ -22,6 +22,7 @@
 #include <cmath>
 
 #include <sal/types.h>
+#include <o3tl/temporary.hxx>
 #include <osl/thread.h>
 #include <osl/thread.hxx>
 #include <osl/time.h>
@@ -85,7 +86,7 @@ static double data(RandomData_Impl *pImpl)
               (static_cast<double>(pImpl->m_nY) / 30269.0) +
               (static_cast<double>(pImpl->m_nZ) / 30307.0)   );
 
-    return std::modf(random, &random);
+    return std::modf(random, &o3tl::temporary(double()));
 }
 
 static bool initPool(RandomPool_Impl *pImpl)
diff --git a/sc/source/filter/excel/xepivotxml.cxx b/sc/source/filter/excel/xepivotxml.cxx
index ffa2eb724f84..004f9938a72a 100644
--- a/sc/source/filter/excel/xepivotxml.cxx
+++ b/sc/source/filter/excel/xepivotxml.cxx
@@ -15,6 +15,7 @@
 #include <document.hxx>
 #include <generalfunction.hxx>
 
+#include <o3tl/temporary.hxx>
 #include <oox/export/utils.hxx>
 #include <oox/token/namespaces.hxx>
 #include <sax/tools/converter.hxx>
@@ -263,7 +264,6 @@ void XclExpXmlPivotCaches::SavePivotCacheXml( XclExpXmlStream& rStrm, const Entr
         double fMin = std::numeric_limits<double>::infinity(), fMax = -std::numeric_limits<double>::infinity();
         bool isValueInteger = true;
         bool isContainsDate = rCache.IsDateDimension(i);
-        double intpart;
         for (; it != itEnd; ++it)
         {
             ScDPItemData::Type eType = it->GetType();
@@ -275,7 +275,7 @@ void XclExpXmlPivotCaches::SavePivotCacheXml( XclExpXmlStream& rStrm, const Entr
                 fMax = std::max(fMax, fVal);
 
                 // Check if all values are integers
-                if (isValueInteger && (modf(fVal, &intpart) != 0.0))
+                if (isValueInteger && (modf(fVal, &o3tl::temporary(double())) != 0.0))
                 {
                     isValueInteger = false;
                 }
diff --git a/scaddins/source/analysis/financial.cxx b/scaddins/source/analysis/financial.cxx
index ccbf0cd02b69..0b66bd873c96 100644
--- a/scaddins/source/analysis/financial.cxx
+++ b/scaddins/source/analysis/financial.cxx
@@ -19,6 +19,7 @@
 
 #include "analysis.hxx"
 #include "analysishelper.hxx"
+#include <o3tl/temporary.hxx>
 #include <rtl/math.hxx>
 
 using namespace sca::analysis;
@@ -366,8 +367,7 @@ double SAL_CALL AnalysisAddIn::getTbillprice( const css::uno::Reference< css::be
 
     double  fFraction = GetYearFrac( xOpt, nSettle, nMat, 0 );  // method: USA 30/360
 
-    double  fDummy;
-    if( modf( fFraction, &fDummy ) == 0.0 )
+    if( modf( fFraction, &o3tl::temporary(double()) ) == 0.0 )
         throw css::lang::IllegalArgumentException();
 
     double fRet = 100.0 * ( 1.0 - fDisc * fFraction );
diff --git a/slideshow/source/engine/transitions/snakewipe.cxx b/slideshow/source/engine/transitions/snakewipe.cxx
index b484f36cb8d7..2fd53e121494 100644
--- a/slideshow/source/engine/transitions/snakewipe.cxx
+++ b/slideshow/source/engine/transitions/snakewipe.cxx
@@ -21,6 +21,7 @@
 
 #include <cmath>
 
+#include <o3tl/temporary.hxx>
 #include <osl/diagnose.h>
 #include <basegfx/matrix/b2dhommatrix.hxx>
 #include <basegfx/point/b2dpoint.hxx>
@@ -102,8 +103,7 @@ SnakeWipe::SnakeWipe( sal_Int32 nElements, bool diagonal, bool flipOnYAxis )
             res.append(poly);
         }
         const double a = (M_SQRT1_2 / m_sqrtElements);
-        double dummy;
-        const double d = std::modf(sqrtArea2, &dummy);
+        const double d = std::modf(sqrtArea2, &o3tl::temporary(double()));
         const double len = (t * M_SQRT2 * d);
         const double height = ::basegfx::pruneScaleValue( M_SQRT1_2 / m_sqrtElements );
         poly.clear();
@@ -147,8 +147,7 @@ SnakeWipe::SnakeWipe( sal_Int32 nElements, bool diagonal, bool flipOnYAxis )
             res.append(poly);
         }
         const double a = (M_SQRT1_2 / m_sqrtElements);
-        double dummy;
-        const double d = std::modf(sqrtArea2, &dummy);
+        const double d = std::modf(sqrtArea2, &o3tl::temporary(double()));
         const double len = ((1.0 - t) * M_SQRT2 * d);
         const double height = ::basegfx::pruneScaleValue( M_SQRT1_2 / m_sqrtElements );
         poly.clear();
diff --git a/sw/source/core/bastyp/calc.cxx b/sw/source/core/bastyp/calc.cxx
index c1cbfdfb841d..30a1ca83a5ab 100644
--- a/sw/source/core/bastyp/calc.cxx
+++ b/sw/source/core/bastyp/calc.cxx
@@ -38,6 +38,7 @@
 #include <editeng/unolingu.hxx>
 #include <expfld.hxx>
 #include <hintids.hxx>
+#include <o3tl/temporary.hxx>
 #include <osl/diagnose.hxx>
 #include <rtl/math.hxx>
 #include <shellres.hxx>
@@ -1195,8 +1196,8 @@ SwSbxValue SwCalc::Prim()
         GetToken();
         double right = Prim().GetDouble();
 
-        double fraction, integer;
-        fraction = modf( right, &integer );
+        double fraction;
+        fraction = modf( right, &o3tl::temporary(double()) );
         if( ( dleft < 0.0 && 0.0 != fraction ) ||
             ( 0.0 == dleft && right < 0.0 ) )
         {
diff --git a/sw/source/core/fields/flddat.cxx b/sw/source/core/fields/flddat.cxx
index aac0b4def6aa..6cb2f930b492 100644
--- a/sw/source/core/fields/flddat.cxx
+++ b/sw/source/core/fields/flddat.cxx
@@ -20,6 +20,7 @@
 #include <sal/config.h>
 
 #include <o3tl/any.hxx>
+#include <o3tl/temporary.hxx>
 #include <tools/datetime.hxx>
 #include <svl/zforlist.hxx>
 #include <com/sun/star/util/DateTime.hpp>
@@ -150,8 +151,7 @@ Date SwDateTimeField::GetDate() const
 
 tools::Time SwDateTimeField::GetTime() const
 {
-    double fDummy;
-    double fFract = modf(GetValue(), &fDummy);
+    double fFract = modf(GetValue(), &o3tl::temporary(double()));
     DateTime aDT( DateTime::EMPTY );
     aDT.AddTime(fFract);
     return static_cast<tools::Time>(aDT);
diff --git a/writerfilter/source/dmapper/DomainMapper_Impl.cxx b/writerfilter/source/dmapper/DomainMapper_Impl.cxx
index 7194879807fd..409c7083ad7e 100644
--- a/writerfilter/source/dmapper/DomainMapper_Impl.cxx
+++ b/writerfilter/source/dmapper/DomainMapper_Impl.cxx
@@ -70,7 +70,7 @@
 #include <com/sun/star/text/XTextColumns.hpp>
 #include <com/sun/star/awt/CharSet.hpp>
 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
-
+#include <o3tl/temporary.hxx>
 #include <oox/mathml/import.hxx>
 #include <rtl/uri.hxx>
 #include "GraphicHelpers.hxx"
@@ -4587,8 +4587,7 @@ static util::DateTime lcl_dateTimeFromSerial(const double& dSerial)
     DateTime d(Date(30, 12, 1899));
     d.AddDays( static_cast<sal_Int32>(dSerial) );
 
-    double dummy;
-    double frac = std::modf(dSerial, &dummy);
+    double frac = std::modf(dSerial, &o3tl::temporary(double()));
     sal_uInt32 seconds = frac * secondsPerDay;
 
     util::DateTime date;
diff --git a/xmloff/source/forms/propertyimport.cxx b/xmloff/source/forms/propertyimport.cxx
index 3b15dcd69f40..d133e01a1d63 100644
--- a/xmloff/source/forms/propertyimport.cxx
+++ b/xmloff/source/forms/propertyimport.cxx
@@ -28,6 +28,7 @@
 #include <xmloff/xmlimp.hxx>
 #include <xmloff/xmluconv.hxx>
 #include <xmloff/nmspmap.hxx>
+#include <o3tl/temporary.hxx>
 #include <osl/diagnose.h>
 #include <comphelper/extract.hxx>
 #include "callbacks.hxx"
@@ -186,9 +187,7 @@ Any PropertyConversion::convertString( const css::uno::Type& _rExpectedType,
                 {
                     case TYPE_DATE:
                     {
-                        double dummy;
-                        (void) dummy; // avoid warning C4101 with some over-eager MSVC
-                        OSL_ENSURE(std::modf(nValue, &dummy) == 0,
+                        OSL_ENSURE(std::modf(nValue, &o3tl::temporary(double())) == 0,
                             "PropertyConversion::convertString: a Date value with a fractional part?");
                         aReturn <<= lcl_getDate(nValue);
                     }


More information about the Libreoffice-commits mailing list