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/.
10 #ifndef LO_CLANG_SHARED_PLUGINS
17 * Look for places where we are converting from type A through a conversion operator and back to type A,
18 * which is redundant. At the moment only look for Color, to aid my ColorData->Color conversion
22 class DoubleConvert final
: public loplugin::FilteringPlugin
<DoubleConvert
>
25 explicit DoubleConvert(loplugin::InstantiationData
const& data
)
26 : FilteringPlugin(data
)
29 void run() override
{ TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl()); }
31 bool VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr
const*);
37 CXXOperatorCallExpr 0x8e5b840 'class Color' lvalue
38 |-ImplicitCastExpr 0x8e5b828 'class Color &(*)(class Color &&) noexcept' <FunctionToPointerDecay>
39 | `-DeclRefExpr 0x8e5b800 'class Color &(class Color &&) noexcept' lvalue CXXMethod 0x8e59a08 'operator=' 'class Color &(class Color &&) noexcept'
40 |-DeclRefExpr 0x8e5b678 'class Color' lvalue Var 0x8e5b5d0 'col2' 'class Color'
41 `-MaterializeTemporaryExpr 0x8e5b7e8 'class Color' xvalue
42 `-CXXConstructExpr 0x8e5b7b0 'class Color' 'void (ColorData)'
43 `-ImplicitCastExpr 0x8e5b798 'ColorData':'unsigned int' <IntegralCast>
44 `-CXXFunctionalCastExpr 0x8e5b770 'sal_Int32':'int' functional cast to sal_Int32 <NoOp>
45 `-ImplicitCastExpr 0x8e5b758 'sal_Int32':'int' <UserDefinedConversion>
46 `-CXXMemberCallExpr 0x8e5b730 'sal_Int32':'int'
47 `-MemberExpr 0x8e5b6f8 '<bound member function type>' .operator int 0x8e51048
48 `-ImplicitCastExpr 0x8e5b6e0 'const class Color' lvalue <NoOp>
49 `-DeclRefExpr 0x8e5b6b0 'class Color' lvalue Var 0x8e5b518 'col1' 'class Color'
51 bool DoubleConvert::VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr
const* materializetemp
)
53 if (ignoreLocation(materializetemp
))
56 = dyn_cast
<CXXConstructExpr
>(materializetemp
->GetTemporaryExpr()->IgnoreParenCasts());
59 if (cxxConstruct
->getNumArgs() == 0)
61 auto cxxMemberCallExpr
62 = dyn_cast
<CXXMemberCallExpr
>(cxxConstruct
->getArg(0)->IgnoreParenCasts());
63 if (!cxxMemberCallExpr
)
65 if (!isa
<CXXConversionDecl
>(cxxMemberCallExpr
->getMethodDecl()))
67 if (materializetemp
->getType().getCanonicalType().getTypePtr()
68 != cxxMemberCallExpr
->getImplicitObjectArgument()
73 if (!loplugin::TypeCheck(materializetemp
->getType().getCanonicalType())
78 report(DiagnosticsEngine::Warning
, "redundant double conversion", materializetemp
->getExprLoc())
79 << materializetemp
->getSourceRange();
83 static loplugin::Plugin::Registration
<DoubleConvert
> doubleconvert("doubleconvert");
86 #endif // LO_CLANG_SHARED_PLUGINS
88 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */