Move setting of LD_LIBRARY_PATH closer to invocation of cppunittester
[LibreOffice.git] / compilerplugins / clang / store / doubleconvert.cxx
blob6f9cc88df7423f0f75adfdc2d520090cc0a0ac89
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 "plugin.hxx"
15 /**
16 * Look for places where we are converting from type A through a conversion operator and back to type A,
17 * which is redundant. At the moment only look for Color, to aid my ColorData->Color conversion
19 namespace
21 class DoubleConvert final : public loplugin::FilteringPlugin<DoubleConvert>
23 public:
24 explicit DoubleConvert(loplugin::InstantiationData const& data)
25 : FilteringPlugin(data)
28 void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); }
30 bool VisitCXXConstructExpr(CXXConstructExpr const*);
33 /**
34 The AST looks like:
36 CXXOperatorCallExpr 0x8e5b840 'class Color' lvalue
37 |-ImplicitCastExpr 0x8e5b828 'class Color &(*)(class Color &&) noexcept' <FunctionToPointerDecay>
38 | `-DeclRefExpr 0x8e5b800 'class Color &(class Color &&) noexcept' lvalue CXXMethod 0x8e59a08 'operator=' 'class Color &(class Color &&) noexcept'
39 |-DeclRefExpr 0x8e5b678 'class Color' lvalue Var 0x8e5b5d0 'col2' 'class Color'
40 `-MaterializeTemporaryExpr 0x8e5b7e8 'class Color' xvalue
41 `-CXXConstructExpr 0x8e5b7b0 'class Color' 'void (ColorData)'
42 `-ImplicitCastExpr 0x8e5b798 'ColorData':'unsigned int' <IntegralCast>
43 `-CXXFunctionalCastExpr 0x8e5b770 'sal_Int32':'int' functional cast to sal_Int32 <NoOp>
44 `-ImplicitCastExpr 0x8e5b758 'sal_Int32':'int' <UserDefinedConversion>
45 `-CXXMemberCallExpr 0x8e5b730 'sal_Int32':'int'
46 `-MemberExpr 0x8e5b6f8 '<bound member function type>' .operator int 0x8e51048
47 `-ImplicitCastExpr 0x8e5b6e0 'const class Color' lvalue <NoOp>
48 `-DeclRefExpr 0x8e5b6b0 'class Color' lvalue Var 0x8e5b518 'col1' 'class Color'
50 bool DoubleConvert::VisitCXXConstructExpr(CXXConstructExpr const* cxxConstruct)
52 if (ignoreLocation(cxxConstruct))
53 return true;
54 if (cxxConstruct->getNumArgs() == 0)
55 return true;
56 auto cxxMemberCallExpr
57 = dyn_cast<CXXMemberCallExpr>(cxxConstruct->getArg(0)->IgnoreParenCasts());
58 if (!cxxMemberCallExpr)
59 return true;
60 if (!isa_and_nonnull<CXXConversionDecl>(cxxMemberCallExpr->getMethodDecl()))
61 return true;
62 if (cxxConstruct->getType().getCanonicalType().getTypePtr()
63 != cxxMemberCallExpr->getImplicitObjectArgument()
64 ->getType()
65 .getCanonicalType()
66 .getTypePtr())
67 return true;
68 if (!loplugin::TypeCheck(cxxConstruct->getType().getCanonicalType())
69 .Class("Color")
70 .GlobalNamespace())
71 return true;
73 report(DiagnosticsEngine::Warning, "redundant double conversion", cxxConstruct->getExprLoc())
74 << cxxConstruct->getSourceRange();
75 return true;
78 static loplugin::Plugin::Registration<DoubleConvert> doubleconvert("doubleconvert");
81 #endif // LO_CLANG_SHARED_PLUGINS
83 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */