compilerplugins/clang/doubleconvert.cxx      |   84 +++++++++++++++++++++++++++
 compilerplugins/clang/test/doubleconvert.cxx |   22 +++++++
 sc/source/core/tool/detfunc.cxx              |    6 -
 sc/source/ui/dbgui/csvgrid.cxx               |   10 +--
 solenv/CompilerTest_compilerplugins_clang.mk |    1 
 5 files changed, 115 insertions(+), 8 deletions(-)

New commits:
commit 64ada386b9e67b2cb5823a333ec2b289781459ba
Author: Noel Grandin <noel.gran...@collabora.co.uk>
Date:   Thu Feb 22 15:39:56 2018 +0200

    new loplugin:doubleconvert to with ColorData work
    
    Change-Id: Ib8f2ef485f548645c4b0e7cf080b72b28c4e278d
    Reviewed-on: https://gerrit.libreoffice.org/50226
    Reviewed-by: Noel Grandin <noel.gran...@collabora.co.uk>
    Tested-by: Noel Grandin <noel.gran...@collabora.co.uk>

diff --git a/compilerplugins/clang/doubleconvert.cxx 
b/compilerplugins/clang/doubleconvert.cxx
new file mode 100644
index 000000000000..a0478e9e5f1f
--- /dev/null
+++ b/compilerplugins/clang/doubleconvert.cxx
@@ -0,0 +1,84 @@
+/* -*- 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 "check.hxx"
+#include "compat.hxx"
+#include "plugin.hxx"
+
+/**
+ * Look for places where we are converting from type A through a conversion 
operator and back to type A,
+ * which is redundant. At the moment only look for Color, to aid my 
ColorData->Color conversion
+ */
+namespace
+{
+class DoubleConvert final : public RecursiveASTVisitor<DoubleConvert>, public 
loplugin::Plugin
+{
+public:
+    explicit DoubleConvert(loplugin::InstantiationData const& data)
+        : Plugin(data)
+    {
+    }
+    void run() override { 
TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
+
+    bool VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr const*);
+};
+
+/**
+ The AST looks like:
+
+ CXXOperatorCallExpr 0x8e5b840 'class Color' lvalue
+|-ImplicitCastExpr 0x8e5b828 'class Color &(*)(class Color &&) noexcept' 
<FunctionToPointerDecay>
+| `-DeclRefExpr 0x8e5b800 'class Color &(class Color &&) noexcept' lvalue 
CXXMethod 0x8e59a08 'operator=' 'class Color &(class Color &&) noexcept'
+|-DeclRefExpr 0x8e5b678 'class Color' lvalue Var 0x8e5b5d0 'col2' 'class Color'
+`-MaterializeTemporaryExpr 0x8e5b7e8 'class Color' xvalue
+  `-CXXConstructExpr 0x8e5b7b0 'class Color' 'void (ColorData)'
+    `-ImplicitCastExpr 0x8e5b798 'ColorData':'unsigned int' <IntegralCast>
+      `-CXXFunctionalCastExpr 0x8e5b770 'sal_Int32':'int' functional cast to 
sal_Int32 <NoOp>
+        `-ImplicitCastExpr 0x8e5b758 'sal_Int32':'int' <UserDefinedConversion>
+          `-CXXMemberCallExpr 0x8e5b730 'sal_Int32':'int'
+            `-MemberExpr 0x8e5b6f8 '<bound member function type>' .operator 
int 0x8e51048
+              `-ImplicitCastExpr 0x8e5b6e0 'const class Color' lvalue <NoOp>
+                `-DeclRefExpr 0x8e5b6b0 'class Color' lvalue Var 0x8e5b518 
'col1' 'class Color'
+*/
+bool DoubleConvert::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr 
const* materializetemp)
+{
+    if (ignoreLocation(materializetemp))
+        return true;
+    auto cxxConstruct
+        = 
dyn_cast<CXXConstructExpr>(materializetemp->GetTemporaryExpr()->IgnoreParenCasts());
+    if (!cxxConstruct)
+        return true;
+    if (cxxConstruct->getNumArgs() == 0)
+        return true;
+    auto cxxMemberCallExpr
+        = 
dyn_cast<CXXMemberCallExpr>(cxxConstruct->getArg(0)->IgnoreParenCasts());
+    if (!cxxMemberCallExpr)
+        return true;
+    if (!isa<CXXConversionDecl>(cxxMemberCallExpr->getMethodDecl()))
+        return true;
+    if (materializetemp->getType().getCanonicalType().getTypePtr()
+        != cxxMemberCallExpr->getImplicitObjectArgument()
+               ->getType()
+               .getCanonicalType()
+               .getTypePtr())
+        return true;
+    if (!loplugin::TypeCheck(materializetemp->getType().getCanonicalType())
+             .Class("Color")
+             .GlobalNamespace())
+        return true;
+
+    report(DiagnosticsEngine::Warning, "redundant double conversion", 
materializetemp->getExprLoc())
+        << materializetemp->getSourceRange();
+    return true;
+}
+
+static loplugin::Plugin::Registration<DoubleConvert> reg("doubleconvert");
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s 
cinkeys+=0=break: */
diff --git a/compilerplugins/clang/test/doubleconvert.cxx 
b/compilerplugins/clang/test/doubleconvert.cxx
new file mode 100644
index 000000000000..d0fd8b787bdb
--- /dev/null
+++ b/compilerplugins/clang/test/doubleconvert.cxx
@@ -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"
+#include "tools/color.hxx"
+
+int main()
+{
+    Color col1;
+    Color col2;
+    col2 = sal_Int32(
+        col1); // expected-error@-1 {{redundant double conversion 
[loplugin:doubleconvert]}}
+    (void)col2;
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s 
cinkeys+=0=break: */
diff --git a/sc/source/core/tool/detfunc.cxx b/sc/source/core/tool/detfunc.cxx
index 0022dea97add..7eebfedd2bcf 100644
--- a/sc/source/core/tool/detfunc.cxx
+++ b/sc/source/core/tool/detfunc.cxx
@@ -1657,9 +1657,9 @@ void ScDetectiveFunc::InitializeColors()
     // may be called several times to update colors from configuration
 
     const svtools::ColorConfig& rColorCfg = SC_MOD()->GetColorConfig();
-    nArrowColor   = 
sal_Int32(rColorCfg.GetColorValue(svtools::CALCDETECTIVE).nColor);
-    nErrorColor   = 
sal_Int32(rColorCfg.GetColorValue(svtools::CALCDETECTIVEERROR).nColor);
-    nCommentColor = 
sal_Int32(rColorCfg.GetColorValue(svtools::CALCNOTESBACKGROUND).nColor);
+    nArrowColor   = rColorCfg.GetColorValue(svtools::CALCDETECTIVE).nColor;
+    nErrorColor   = 
rColorCfg.GetColorValue(svtools::CALCDETECTIVEERROR).nColor;
+    nCommentColor = 
rColorCfg.GetColorValue(svtools::CALCNOTESBACKGROUND).nColor;
 
     bColorsInitialized = true;
 }
diff --git a/sc/source/ui/dbgui/csvgrid.cxx b/sc/source/ui/dbgui/csvgrid.cxx
index ed47160ea08e..4b7ffb4aabe6 100644
--- a/sc/source/ui/dbgui/csvgrid.cxx
+++ b/sc/source/ui/dbgui/csvgrid.cxx
@@ -213,11 +213,11 @@ void ScCsvGrid::InitColors()
     OSL_PRECOND(mpColorConfig, "the object hasn't been initialized properly");
     if ( !mpColorConfig )
         return;
-    maBackColor = static_cast< sal_uInt32 >( mpColorConfig->GetColorValue( 
::svtools::DOCCOLOR ).nColor );
-    maGridColor = static_cast< sal_uInt32 >( mpColorConfig->GetColorValue( 
::svtools::CALCGRID ).nColor );
-    maGridPBColor = static_cast< sal_uInt32 >( mpColorConfig->GetColorValue( 
::svtools::CALCPAGEBREAK ).nColor );
-    maAppBackColor = static_cast< sal_uInt32 >( mpColorConfig->GetColorValue( 
::svtools::APPBACKGROUND ).nColor );
-    maTextColor = static_cast< sal_uInt32 >( mpColorConfig->GetColorValue( 
::svtools::FONTCOLOR ).nColor );
+    maBackColor = mpColorConfig->GetColorValue( ::svtools::DOCCOLOR ).nColor;
+    maGridColor = mpColorConfig->GetColorValue( ::svtools::CALCGRID ).nColor;
+    maGridPBColor = mpColorConfig->GetColorValue( ::svtools::CALCPAGEBREAK 
).nColor;
+    maAppBackColor = mpColorConfig->GetColorValue( ::svtools::APPBACKGROUND 
).nColor;
+    maTextColor = mpColorConfig->GetColorValue( ::svtools::FONTCOLOR ).nColor;
 
     const StyleSettings& rSett = GetSettings().GetStyleSettings();
     maHeaderBackColor = rSett.GetFaceColor();
diff --git a/solenv/CompilerTest_compilerplugins_clang.mk 
b/solenv/CompilerTest_compilerplugins_clang.mk
index bc4fbdc3ae0c..d417702d19fe 100644
--- a/solenv/CompilerTest_compilerplugins_clang.mk
+++ b/solenv/CompilerTest_compilerplugins_clang.mk
@@ -20,6 +20,7 @@ $(eval $(call 
gb_CompilerTest_add_exception_objects,compilerplugins_clang, \
     compilerplugins/clang/test/cstylecast \
     compilerplugins/clang/test/datamembershadow \
     compilerplugins/clang/test/dodgyswitch \
+    compilerplugins/clang/test/doubleconvert \
     compilerplugins/clang/test/emptyif \
     compilerplugins/clang/test/externvar \
     compilerplugins/clang/test/expressionalwayszero \
_______________________________________________
Libreoffice-commits mailing list
libreoffice-comm...@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits

Reply via email to