From ed5640d8b587fbcfed7dd7967f3de04b37a76f26 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 11:06:44 +0200 Subject: Adding upstream version 4:7.4.7. Signed-off-by: Daniel Baumann --- compilerplugins/clang/unoquery.cxx | 99 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 compilerplugins/clang/unoquery.cxx (limited to 'compilerplugins/clang/unoquery.cxx') diff --git a/compilerplugins/clang/unoquery.cxx b/compilerplugins/clang/unoquery.cxx new file mode 100644 index 000000000..1fe2d4b5b --- /dev/null +++ b/compilerplugins/clang/unoquery.cxx @@ -0,0 +1,99 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#ifndef LO_CLANG_SHARED_PLUGINS + +#include "check.hxx" +#include "plugin.hxx" + +// TODO it would be better if we were running some kind of nullability analysis here, where we marked +// the results of expressions like Reference(..UNO_QUERY) as being nullable, and then looked for +// places where we unconditionally deference the results of that expression. + +namespace +{ +class UnoQuery : public loplugin::FilteringPlugin +{ +public: + explicit UnoQuery(loplugin::InstantiationData const& data) + : FilteringPlugin(data) + { + } + + bool preRun() override { return compiler.getLangOpts().CPlusPlus; } + + void run() override + { + if (preRun()) + { + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + } + } + + bool VisitCXXMemberCallExpr(CXXMemberCallExpr const*); +}; + +bool UnoQuery::VisitCXXMemberCallExpr(CXXMemberCallExpr const* memberCallExpr) +{ + if (ignoreLocation(memberCallExpr)) + return true; + + auto isXInterface = [](Decl const* decl) -> bool { + return bool(loplugin::DeclCheck(decl) + .Class("XInterface") + .Namespace("uno") + .Namespace("star") + .Namespace("sun") + .Namespace("com") + .GlobalNamespace()); + }; + if (!loplugin::isDerivedFrom(memberCallExpr->getRecordDecl(), isXInterface)) + return true; + auto operatorCallExpr = dyn_cast( + memberCallExpr->getImplicitObjectArgument()->IgnoreImplicit()); + if (!operatorCallExpr) + return true; + + Expr const* expr = operatorCallExpr->getArg(0)->IgnoreImplicit(); + // depending on the version of clang, the IgnoreImplicit may or may not look through these nodes + if (auto matTemp = dyn_cast(expr)) + expr = matTemp->getSubExpr(); + if (auto bindTemp = dyn_cast(expr)) + expr = bindTemp->getSubExpr(); + + auto temporaryExpr = dyn_cast(expr); + if (!temporaryExpr) + return true; + if (temporaryExpr->getNumArgs() < 2) + return true; + auto declRefExpr = dyn_cast(temporaryExpr->getArg(1)->IgnoreImplicit()); + if (!declRefExpr) + return true; + auto enumConstant = dyn_cast(declRefExpr->getDecl()); + if (!enumConstant) + return true; + if (enumConstant->getName() != "UNO_QUERY") + return true; + + report(DiagnosticsEngine::Warning, + "calling UNO_QUERY followed by unconditional method call might result in SIGSEGV, " + "rather use UNO_QUERY_THROW", + memberCallExpr->getExprLoc()) + << memberCallExpr->getSourceRange(); + + return true; +} + +loplugin::Plugin::Registration unoquery("unoquery"); + +} // namespace + +#endif // LO_CLANG_SHARED_PLUGINS + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ -- cgit v1.2.3