lok: getSlideShowInfo: interactions: check that properties are available
[LibreOffice.git] / compilerplugins / clang / unoquery.cxx
blobac24b8bd7bbf64b30fff14a9a7e1b5e8ba0b0c4e
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 // 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.
19 namespace
21 class UnoQuery : public loplugin::FilteringPlugin<UnoQuery>
23 public:
24 explicit UnoQuery(loplugin::InstantiationData const& data)
25 : FilteringPlugin(data)
29 bool preRun() override { return compiler.getLangOpts().CPlusPlus; }
31 void run() override
33 if (preRun())
35 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
39 bool VisitCXXMemberCallExpr(CXXMemberCallExpr const*);
42 bool UnoQuery::VisitCXXMemberCallExpr(CXXMemberCallExpr const* memberCallExpr)
44 if (ignoreLocation(memberCallExpr))
45 return true;
47 auto isXInterface = [](Decl const* decl) -> bool {
48 return bool(loplugin::DeclCheck(decl)
49 .Class("XInterface")
50 .Namespace("uno")
51 .Namespace("star")
52 .Namespace("sun")
53 .Namespace("com")
54 .GlobalNamespace());
56 if (!loplugin::isDerivedFrom(memberCallExpr->getRecordDecl(), isXInterface))
57 return true;
58 auto operatorCallExpr = dyn_cast<CXXOperatorCallExpr>(
59 memberCallExpr->getImplicitObjectArgument()->IgnoreImplicit());
60 if (!operatorCallExpr)
61 return true;
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);
71 if (!temporaryExpr)
72 return true;
73 if (temporaryExpr->getNumArgs() < 2)
74 return true;
75 auto declRefExpr = dyn_cast<DeclRefExpr>(temporaryExpr->getArg(1)->IgnoreImplicit());
76 if (!declRefExpr)
77 return true;
78 auto enumConstant = dyn_cast<EnumConstantDecl>(declRefExpr->getDecl());
79 if (!enumConstant)
80 return true;
81 if (enumConstant->getName() != "UNO_QUERY")
82 return true;
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();
90 return true;
93 loplugin::Plugin::Registration<UnoQuery> unoquery("unoquery");
95 } // namespace
97 #endif // LO_CLANG_SHARED_PLUGINS
99 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */