1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
15 * Look for places where we are converting from type A through a conversion operator and back to type A,
16 * which is redundant. At the moment only look for Color, to aid my ColorData->Color conversion
20 class DoubleConvert final
: public RecursiveASTVisitor
<DoubleConvert
>, public loplugin::Plugin
23 explicit DoubleConvert(loplugin::InstantiationData
const& data
)
27 void run() override
{ TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl()); }
29 bool VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr
const*);
35 CXXOperatorCallExpr 0x8e5b840 'class Color' lvalue
36 |-ImplicitCastExpr 0x8e5b828 'class Color &(*)(class Color &&) noexcept' <FunctionToPointerDecay>
37 | `-DeclRefExpr 0x8e5b800 'class Color &(class Color &&) noexcept' lvalue CXXMethod 0x8e59a08 'operator=' 'class Color &(class Color &&) noexcept'
38 |-DeclRefExpr 0x8e5b678 'class Color' lvalue Var 0x8e5b5d0 'col2' 'class Color'
39 `-MaterializeTemporaryExpr 0x8e5b7e8 'class Color' xvalue
40 `-CXXConstructExpr 0x8e5b7b0 'class Color' 'void (ColorData)'
41 `-ImplicitCastExpr 0x8e5b798 'ColorData':'unsigned int' <IntegralCast>
42 `-CXXFunctionalCastExpr 0x8e5b770 'sal_Int32':'int' functional cast to sal_Int32 <NoOp>
43 `-ImplicitCastExpr 0x8e5b758 'sal_Int32':'int' <UserDefinedConversion>
44 `-CXXMemberCallExpr 0x8e5b730 'sal_Int32':'int'
45 `-MemberExpr 0x8e5b6f8 '<bound member function type>' .operator int 0x8e51048
46 `-ImplicitCastExpr 0x8e5b6e0 'const class Color' lvalue <NoOp>
47 `-DeclRefExpr 0x8e5b6b0 'class Color' lvalue Var 0x8e5b518 'col1' 'class Color'
49 bool DoubleConvert::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr
const* materializetemp
)
51 if (ignoreLocation(materializetemp
))
54 = dyn_cast
<CXXConstructExpr
>(materializetemp
->GetTemporaryExpr()->IgnoreParenCasts());
57 if (cxxConstruct
->getNumArgs() == 0)
59 auto cxxMemberCallExpr
60 = dyn_cast
<CXXMemberCallExpr
>(cxxConstruct
->getArg(0)->IgnoreParenCasts());
61 if (!cxxMemberCallExpr
)
63 if (!isa
<CXXConversionDecl
>(cxxMemberCallExpr
->getMethodDecl()))
65 if (materializetemp
->getType().getCanonicalType().getTypePtr()
66 != cxxMemberCallExpr
->getImplicitObjectArgument()
71 if (!loplugin::TypeCheck(materializetemp
->getType().getCanonicalType())
76 report(DiagnosticsEngine::Warning
, "redundant double conversion", materializetemp
->getExprLoc())
77 << materializetemp
->getSourceRange();
81 static loplugin::Plugin::Registration
<DoubleConvert
> reg("doubleconvert");
84 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */