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
16 // TODO it would be better if we were running some kind of nullability analysis here, where we marked
17 // the results of expressions like Reference(..UNO_QUERY) as being nullable, and then looked for
18 // places where we unconditionally deference the results of that expression.
22 class UnoQuery
: public loplugin::FilteringPlugin
<UnoQuery
>
25 explicit UnoQuery(loplugin::InstantiationData
const& data
)
26 : FilteringPlugin(data
)
30 bool preRun() override
{ return compiler
.getLangOpts().CPlusPlus
; }
36 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
40 bool VisitCXXMemberCallExpr(CXXMemberCallExpr
const*);
43 bool UnoQuery::VisitCXXMemberCallExpr(CXXMemberCallExpr
const* memberCallExpr
)
45 if (ignoreLocation(memberCallExpr
))
48 auto isXInterface
= [](Decl
const* decl
) -> bool {
49 return bool(loplugin::DeclCheck(decl
)
57 if (!loplugin::isDerivedFrom(memberCallExpr
->getRecordDecl(), isXInterface
))
59 auto operatorCallExpr
= dyn_cast
<CXXOperatorCallExpr
>(
60 memberCallExpr
->getImplicitObjectArgument()->IgnoreImplicit());
61 if (!operatorCallExpr
)
64 Expr
const* expr
= operatorCallExpr
->getArg(0)->IgnoreImplicit();
65 // depending on the version of clang, the IgnoreImplicit may or may not look through these nodes
66 if (auto matTemp
= dyn_cast
<MaterializeTemporaryExpr
>(expr
))
67 expr
= compat::getSubExpr(matTemp
);
68 if (auto bindTemp
= dyn_cast
<CXXBindTemporaryExpr
>(expr
))
69 expr
= bindTemp
->getSubExpr();
71 auto temporaryExpr
= dyn_cast
<CXXTemporaryObjectExpr
>(expr
);
74 if (temporaryExpr
->getNumArgs() < 2)
76 auto declRefExpr
= dyn_cast
<DeclRefExpr
>(temporaryExpr
->getArg(1)->IgnoreImplicit());
79 auto enumConstant
= dyn_cast
<EnumConstantDecl
>(declRefExpr
->getDecl());
82 if (enumConstant
->getName() != "UNO_QUERY")
85 report(DiagnosticsEngine::Warning
,
86 "calling UNO_QUERY followed by unconditional method call might result in SIGSEGV, "
87 "rather use UNO_QUERY_THROW",
88 memberCallExpr
->getExprLoc())
89 << memberCallExpr
->getSourceRange();
94 loplugin::Plugin::Registration
<UnoQuery
> unoquery("unoquery");
98 #endif // LO_CLANG_SHARED_PLUGINS
100 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */