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
15 // TODO it would be better if we were running some kind of nullability analysis here, where we marked
16 // the results of expressions like Reference(..UNO_QUERY) as being nullable, and then looked for
17 // places where we unconditionally dereference the results of that expression.
21 class UnoQuery
: public loplugin::FilteringPlugin
<UnoQuery
>
24 explicit UnoQuery(loplugin::InstantiationData
const& data
)
25 : FilteringPlugin(data
)
29 bool preRun() override
{ return compiler
.getLangOpts().CPlusPlus
; }
35 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
39 bool VisitCXXMemberCallExpr(CXXMemberCallExpr
const*);
42 bool UnoQuery::VisitCXXMemberCallExpr(CXXMemberCallExpr
const* memberCallExpr
)
44 if (ignoreLocation(memberCallExpr
))
47 auto isXInterface
= [](Decl
const* decl
) -> bool {
48 return bool(loplugin::DeclCheck(decl
)
56 if (!loplugin::isDerivedFrom(memberCallExpr
->getRecordDecl(), isXInterface
))
58 auto operatorCallExpr
= dyn_cast
<CXXOperatorCallExpr
>(
59 memberCallExpr
->getImplicitObjectArgument()->IgnoreImplicit());
60 if (!operatorCallExpr
)
63 Expr
const* expr
= operatorCallExpr
->getArg(0)->IgnoreImplicit();
64 // depending on the version of clang, the IgnoreImplicit may or may not look through these nodes
65 if (auto matTemp
= dyn_cast
<MaterializeTemporaryExpr
>(expr
))
66 expr
= matTemp
->getSubExpr();
67 if (auto bindTemp
= dyn_cast
<CXXBindTemporaryExpr
>(expr
))
68 expr
= bindTemp
->getSubExpr();
70 auto temporaryExpr
= dyn_cast
<CXXTemporaryObjectExpr
>(expr
);
73 if (temporaryExpr
->getNumArgs() < 2)
75 auto declRefExpr
= dyn_cast
<DeclRefExpr
>(temporaryExpr
->getArg(1)->IgnoreImplicit());
78 auto enumConstant
= dyn_cast
<EnumConstantDecl
>(declRefExpr
->getDecl());
81 if (enumConstant
->getName() != "UNO_QUERY")
84 report(DiagnosticsEngine::Warning
,
85 "calling UNO_QUERY followed by unconditional method call might result in SIGSEGV, "
86 "rather use UNO_QUERY_THROW",
87 memberCallExpr
->getExprLoc())
88 << memberCallExpr
->getSourceRange();
93 loplugin::Plugin::Registration
<UnoQuery
> unoquery("unoquery");
97 #endif // LO_CLANG_SHARED_PLUGINS
99 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */