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/simplifydynamiccast.cxx | 114 ++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 compilerplugins/clang/simplifydynamiccast.cxx (limited to 'compilerplugins/clang/simplifydynamiccast.cxx') diff --git a/compilerplugins/clang/simplifydynamiccast.cxx b/compilerplugins/clang/simplifydynamiccast.cxx new file mode 100644 index 000000000..ffb81658d --- /dev/null +++ b/compilerplugins/clang/simplifydynamiccast.cxx @@ -0,0 +1,114 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * 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/. + */ + +#include +#include +#include +#include +#include + +#include +#include "plugin.hxx" + +namespace +{ +class SimplifyDynamicCast : public loplugin::FilteringPlugin +{ +public: + explicit SimplifyDynamicCast(loplugin::InstantiationData const& data) + : FilteringPlugin(data) + { + } + + virtual void run() override + { + // StringRef fn(handler.getMainFileName()); + // if (loplugin::isSamePathname(fn, WORKDIR "/YaccTarget/unoidl/source/sourceprovider-parser.cxx")) + // return; + + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + } + + bool TraverseIfStmt(IfStmt*); + bool VisitCXXStaticCastExpr(CXXStaticCastExpr const*); + +private: + std::vector dynamicCastVec; + std::vector dynamicCastSubExprVec; + std::vector ifVec; +}; + +bool SimplifyDynamicCast::TraverseIfStmt(IfStmt* ifStmt) +{ + auto condExpr = ifStmt->getCond()->IgnoreParenImpCasts(); + auto dynamicCastExpr = dyn_cast(condExpr); + if (!dynamicCastExpr) + { + if (auto binaryOp = dyn_cast(condExpr)) + { + if (binaryOp->getOpcode() == BO_NE) + dynamicCastExpr + = dyn_cast(binaryOp->getLHS()->IgnoreParenImpCasts()); + } + } + Decl const* subExprDecl = nullptr; + if (dynamicCastExpr) + { + auto subExprDeclRefExpr + = dyn_cast(dynamicCastExpr->getSubExpr()->IgnoreParenImpCasts()); + if (!subExprDeclRefExpr) + dynamicCastExpr = nullptr; + else + subExprDecl = subExprDeclRefExpr->getDecl(); + } + if (dynamicCastExpr) + { + auto qt = dynamicCastExpr->getTypeAsWritten(); + dynamicCastVec.push_back(qt); + dynamicCastSubExprVec.push_back(subExprDecl); + ifVec.push_back(ifStmt); + } + bool ret = RecursiveASTVisitor::TraverseIfStmt(ifStmt); + if (dynamicCastExpr) + { + dynamicCastVec.pop_back(); + dynamicCastSubExprVec.pop_back(); + ifVec.pop_back(); + } + return ret; +} + +bool SimplifyDynamicCast::VisitCXXStaticCastExpr(CXXStaticCastExpr const* staticCastExpr) +{ + if (ignoreLocation(staticCastExpr)) + return true; + if (dynamicCastVec.empty()) + return true; + + auto qt = staticCastExpr->getTypeAsWritten(); + auto it = std::find(dynamicCastVec.begin(), dynamicCastVec.end(), qt); + if (it == dynamicCastVec.end()) + return true; + int idx = it - dynamicCastVec.begin(); + auto subExprDecl = dyn_cast(staticCastExpr->getSubExpr()->IgnoreParenImpCasts()); + if (!subExprDecl) + return true; + if (dynamicCastSubExprVec[idx] != subExprDecl->getDecl()) + return true; + report(DiagnosticsEngine::Warning, "simplify, use var in if", staticCastExpr->getBeginLoc()) + << staticCastExpr->getSourceRange(); + auto ifStmt = ifVec[idx]; + report(DiagnosticsEngine::Note, "if here", ifStmt->getBeginLoc()) << ifStmt->getSourceRange(); + return true; +} + +loplugin::Plugin::Registration X("simplifydynamiccast", true); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit v1.2.3