LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / compilerplugins / clang / unoquery.cxx
blobfd7715fe1df3c1ec8fbb8bddd7ffd4434dd6e02f
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 // 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.
20 namespace
22 class UnoQuery : public loplugin::FilteringPlugin<UnoQuery>
24 public:
25 explicit UnoQuery(loplugin::InstantiationData const& data)
26 : FilteringPlugin(data)
30 bool preRun() override { return compiler.getLangOpts().CPlusPlus; }
32 void run() override
34 if (preRun())
36 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
40 bool VisitCXXMemberCallExpr(CXXMemberCallExpr const*);
43 bool UnoQuery::VisitCXXMemberCallExpr(CXXMemberCallExpr const* memberCallExpr)
45 if (ignoreLocation(memberCallExpr))
46 return true;
48 auto isXInterface = [](Decl const* decl) -> bool {
49 return bool(loplugin::DeclCheck(decl)
50 .Class("XInterface")
51 .Namespace("uno")
52 .Namespace("star")
53 .Namespace("sun")
54 .Namespace("com")
55 .GlobalNamespace());
57 if (!loplugin::isDerivedFrom(memberCallExpr->getRecordDecl(), isXInterface))
58 return true;
59 auto operatorCallExpr = dyn_cast<CXXOperatorCallExpr>(
60 memberCallExpr->getImplicitObjectArgument()->IgnoreImplicit());
61 if (!operatorCallExpr)
62 return true;
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);
72 if (!temporaryExpr)
73 return true;
74 if (temporaryExpr->getNumArgs() < 2)
75 return true;
76 auto declRefExpr = dyn_cast<DeclRefExpr>(temporaryExpr->getArg(1)->IgnoreImplicit());
77 if (!declRefExpr)
78 return true;
79 auto enumConstant = dyn_cast<EnumConstantDecl>(declRefExpr->getDecl());
80 if (!enumConstant)
81 return true;
82 if (enumConstant->getName() != "UNO_QUERY")
83 return true;
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();
91 return true;
94 loplugin::Plugin::Registration<UnoQuery> unoquery("unoquery");
96 } // namespace
98 #endif // LO_CLANG_SHARED_PLUGINS
100 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */