bump product version to 7.2.5.1
[LibreOffice.git] / compilerplugins / clang / doubleconvert.cxx
blob6a08ae376a956e437891c62f787405642b9ace87
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 VisitCXXConstructExpr(CXXConstructExpr 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::VisitCXXConstructExpr(CXXConstructExpr const* cxxConstruct)
53 if (ignoreLocation(cxxConstruct))
54 return true;
55 if (cxxConstruct->getNumArgs() == 0)
56 return true;
57 auto cxxMemberCallExpr
58 = dyn_cast<CXXMemberCallExpr>(cxxConstruct->getArg(0)->IgnoreParenCasts());
59 if (!cxxMemberCallExpr)
60 return true;
61 if (!compat::isa_and_nonnull<CXXConversionDecl>(cxxMemberCallExpr->getMethodDecl()))
62 return true;
63 if (cxxConstruct->getType().getCanonicalType().getTypePtr()
64 != cxxMemberCallExpr->getImplicitObjectArgument()
65 ->getType()
66 .getCanonicalType()
67 .getTypePtr())
68 return true;
69 if (!loplugin::TypeCheck(cxxConstruct->getType().getCanonicalType())
70 .Class("Color")
71 .GlobalNamespace())
72 return true;
74 report(DiagnosticsEngine::Warning, "redundant double conversion", cxxConstruct->getExprLoc())
75 << cxxConstruct->getSourceRange();
76 return true;
79 static loplugin::Plugin::Registration<DoubleConvert> doubleconvert("doubleconvert");
82 #endif // LO_CLANG_SHARED_PLUGINS
84 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */