bump product version to 6.3.0.0.beta1
[LibreOffice.git] / compilerplugins / clang / doubleconvert.cxx
blob95565301b7516266c048c3da1436452bec16345e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
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/.
8 */
10 #ifndef LO_CLANG_SHARED_PLUGINS
12 #include "check.hxx"
13 #include "compat.hxx"
14 #include "plugin.hxx"
16 /**
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
20 namespace
22 class DoubleConvert final : public loplugin::FilteringPlugin<DoubleConvert>
24 public:
25 explicit DoubleConvert(loplugin::InstantiationData const& data)
26 : FilteringPlugin(data)
29 void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
31 bool VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr const*);
34 /**
35 The AST looks like:
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))
54 return true;
55 auto cxxConstruct
56 = dyn_cast<CXXConstructExpr>(materializetemp->GetTemporaryExpr()->IgnoreParenCasts());
57 if (!cxxConstruct)
58 return true;
59 if (cxxConstruct->getNumArgs() == 0)
60 return true;
61 auto cxxMemberCallExpr
62 = dyn_cast<CXXMemberCallExpr>(cxxConstruct->getArg(0)->IgnoreParenCasts());
63 if (!cxxMemberCallExpr)
64 return true;
65 if (!isa<CXXConversionDecl>(cxxMemberCallExpr->getMethodDecl()))
66 return true;
67 if (materializetemp->getType().getCanonicalType().getTypePtr()
68 != cxxMemberCallExpr->getImplicitObjectArgument()
69 ->getType()
70 .getCanonicalType()
71 .getTypePtr())
72 return true;
73 if (!loplugin::TypeCheck(materializetemp->getType().getCanonicalType())
74 .Class("Color")
75 .GlobalNamespace())
76 return true;
78 report(DiagnosticsEngine::Warning, "redundant double conversion", materializetemp->getExprLoc())
79 << materializetemp->getSourceRange();
80 return true;
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: */