Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / compilerplugins / clang / doubleconvert.cxx
bloba0478e9e5f1f1be26e48153f6c892e0bd4c5c196
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 #include "check.hxx"
11 #include "compat.hxx"
12 #include "plugin.hxx"
14 /**
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
18 namespace
20 class DoubleConvert final : public RecursiveASTVisitor<DoubleConvert>, public loplugin::Plugin
22 public:
23 explicit DoubleConvert(loplugin::InstantiationData const& data)
24 : Plugin(data)
27 void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
29 bool VisitMaterializeTemporaryExpr(MaterializeTemporaryExpr const*);
32 /**
33 The AST looks like:
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))
52 return true;
53 auto cxxConstruct
54 = dyn_cast<CXXConstructExpr>(materializetemp->GetTemporaryExpr()->IgnoreParenCasts());
55 if (!cxxConstruct)
56 return true;
57 if (cxxConstruct->getNumArgs() == 0)
58 return true;
59 auto cxxMemberCallExpr
60 = dyn_cast<CXXMemberCallExpr>(cxxConstruct->getArg(0)->IgnoreParenCasts());
61 if (!cxxMemberCallExpr)
62 return true;
63 if (!isa<CXXConversionDecl>(cxxMemberCallExpr->getMethodDecl()))
64 return true;
65 if (materializetemp->getType().getCanonicalType().getTypePtr()
66 != cxxMemberCallExpr->getImplicitObjectArgument()
67 ->getType()
68 .getCanonicalType()
69 .getTypePtr())
70 return true;
71 if (!loplugin::TypeCheck(materializetemp->getType().getCanonicalType())
72 .Class("Color")
73 .GlobalNamespace())
74 return true;
76 report(DiagnosticsEngine::Warning, "redundant double conversion", materializetemp->getExprLoc())
77 << materializetemp->getSourceRange();
78 return true;
81 static loplugin::Plugin::Registration<DoubleConvert> reg("doubleconvert");
84 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */