diff options
author | Noel Grandin <noel.grandin@collabora.co.uk> | 2022-07-06 10:35:59 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2022-07-06 15:48:29 +0200 |
commit | c6923103a2d952aecb154223663688a085065b05 (patch) | |
tree | 99fd8629f47ce50b0dd5ac016b8cfdb88be0174f /compilerplugins/clang/store | |
parent | d5bf3d8dfe51aa6791fd88e62fc542d888aa05c7 (diff) |
move old/unused plugins to store
noting that I have only plugins that I wrote or worked on extensively
Change-Id: Ic4931a6ac2df7902cac3968900330a7ce4eb2d57
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/136841
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'compilerplugins/clang/store')
27 files changed, 6513 insertions, 0 deletions
diff --git a/compilerplugins/clang/store/checkunusedparams.cxx b/compilerplugins/clang/store/checkunusedparams.cxx new file mode 100644 index 000000000000..2f45049632d3 --- /dev/null +++ b/compilerplugins/clang/store/checkunusedparams.cxx @@ -0,0 +1,502 @@ +/* -*- 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 <cassert> +#include <string> +#include <set> +#include <iostream> + +#include "config_clang.h" + +#include "plugin.hxx" + +/** +Find parameters that have no name, i.e. they are unused and we're worked around the "unused parameter" warning. + +Most of these can be removed. + +TODO look for places where we are working around the warning by doing + (void) param1; + */ +namespace { + +class CheckUnusedParams: public loplugin::FilteringPlugin<CheckUnusedParams> { +public: + explicit CheckUnusedParams(loplugin::InstantiationData const & data): + FilteringPlugin(data) {} + void run() override; + bool VisitFunctionDecl(FunctionDecl const *); + bool VisitUnaryOperator(UnaryOperator const *); + bool VisitInitListExpr(InitListExpr const *); + bool VisitCallExpr(CallExpr const *); + bool VisitBinaryOperator(BinaryOperator const *); + bool VisitCXXConstructExpr(CXXConstructExpr const *); +private: + void checkForFunctionDecl(Expr const *, bool bCheckOnly = false); + std::set<FunctionDecl const *> m_addressOfSet; + enum class PluginPhase { FindAddressOf, Warning }; + PluginPhase m_phase; +}; + +void CheckUnusedParams::run() +{ + StringRef fn(handler.getMainFileName()); + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sal/")) + return; + // Taking pointer to function + if (loplugin::isSamePathname(fn, SRCDIR "/l10ntools/source/xmlparse.cxx")) + return; + // macro magic which declares something needed by an external library + if (loplugin::isSamePathname(fn, SRCDIR "/svl/source/misc/gridprinter.cxx")) + return; + + // valid test/qa code + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/compilerplugins/clang/test/")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/cppu/qa/test_reference.cxx")) + return; + + // leave this alone for now + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/libreofficekit/")) + return; + // this has a certain pattern to its code which appears to include lots of unused params + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/xmloff/")) + return; + // I believe someone is busy working on this chunk of code + if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/ui/docshell/dataprovider.cxx")) + return; + // I think erack is working on stuff here + if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/filter/excel/xiformula.cxx")) + return; + // lots of callbacks here + if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/filter/lotus/op.cxx")) + return; + // template magic + if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/filter/html/htmlpars.cxx")) + return; + + m_phase = PluginPhase::FindAddressOf; + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + m_phase = PluginPhase::Warning; + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); +} + +bool CheckUnusedParams::VisitUnaryOperator(UnaryOperator const * op) { + if (op->getOpcode() != UO_AddrOf) { + return true; + } + if (m_phase != PluginPhase::FindAddressOf) + return true; + checkForFunctionDecl(op->getSubExpr()); + return true; +} + +bool CheckUnusedParams::VisitBinaryOperator(BinaryOperator const * binaryOperator) { + if (binaryOperator->getOpcode() != BO_Assign) { + return true; + } + if (m_phase != PluginPhase::FindAddressOf) + return true; + checkForFunctionDecl(binaryOperator->getRHS()); + return true; +} + +bool CheckUnusedParams::VisitCallExpr(CallExpr const * callExpr) { + if (m_phase != PluginPhase::FindAddressOf) + return true; + for (auto arg : callExpr->arguments()) + checkForFunctionDecl(arg); + return true; +} + +bool CheckUnusedParams::VisitCXXConstructExpr(CXXConstructExpr const * constructExpr) { + if (m_phase != PluginPhase::FindAddressOf) + return true; + for (auto arg : constructExpr->arguments()) + checkForFunctionDecl(arg); + return true; +} + +bool CheckUnusedParams::VisitInitListExpr(InitListExpr const * initListExpr) { + if (m_phase != PluginPhase::FindAddressOf) + return true; + for (auto subStmt : *initListExpr) + checkForFunctionDecl(dyn_cast<Expr>(subStmt)); + return true; +} + +void CheckUnusedParams::checkForFunctionDecl(Expr const * expr, bool bCheckOnly) { + auto e1 = expr->IgnoreParenCasts(); + auto declRef = dyn_cast<DeclRefExpr>(e1); + if (!declRef) + return; + auto functionDecl = dyn_cast<FunctionDecl>(declRef->getDecl()); + if (!functionDecl) + return; + if (bCheckOnly) + getParentStmt(expr)->dump(); + else + m_addressOfSet.insert(functionDecl->getCanonicalDecl()); +} + +static int noFieldsInRecord(RecordType const * recordType) { + auto recordDecl = recordType->getDecl(); + // if it's complicated, lets just assume it has fields + if (isa<ClassTemplateSpecializationDecl>(recordDecl)) + return 1; + return std::distance(recordDecl->field_begin(), recordDecl->field_end()); +} +static bool startswith(const std::string& rStr, const char* pSubStr) { + return rStr.compare(0, strlen(pSubStr), pSubStr) == 0; +} +static bool endswith(const std::string& rStr, const char* pSubStr) { + auto len = strlen(pSubStr); + if (len > rStr.size()) + return false; + return rStr.compare(rStr.size() - len, rStr.size(), pSubStr) == 0; +} + +bool CheckUnusedParams::VisitFunctionDecl(FunctionDecl const * decl) { + if (m_phase != PluginPhase::Warning) + return true; + if (m_addressOfSet.find(decl->getCanonicalDecl()) != m_addressOfSet.end()) + return true; + if (ignoreLocation(decl)) + return true; + if (isInUnoIncludeFile(compiler.getSourceManager().getSpellingLoc(decl->getLocation()))) + return true; + + auto cxxMethodDecl = dyn_cast<CXXMethodDecl>(decl); + if (cxxMethodDecl) { + if (cxxMethodDecl->isVirtual()) + return true; + auto cxxConstructorDecl = dyn_cast<CXXConstructorDecl>(cxxMethodDecl); + if (cxxConstructorDecl && cxxConstructorDecl->isCopyOrMoveConstructor()) + return true; + } + if (!decl->isThisDeclarationADefinition()) + return true; + if (decl->isFunctionTemplateSpecialization()) + return true; + if (decl->isDeleted()) + return true; + if (decl->getTemplatedKind() != clang::FunctionDecl::TK_NonTemplate) + return true; + if (decl->isOverloadedOperator()) + return true; + if (decl->isExternC()) + return true; + + //TODO, filtering out any functions relating to class templates for now: + CXXRecordDecl const * r = dyn_cast<CXXRecordDecl>(decl->getDeclContext()); + if (r != nullptr + && (r->getTemplateSpecializationKind() != TSK_Undeclared + || r->isDependentContext())) + { + return true; + } + FunctionDecl const * canon = decl->getCanonicalDecl(); + std::string fqn = canon->getQualifiedNameAsString(); // because sometimes clang returns nonsense for the filename of canon + if (ignoreLocation(canon)) + return true; + if (isInUnoIncludeFile(compiler.getSourceManager().getSpellingLoc(canon->getLocation()))) + return true; + StringRef fn = getFilenameOfLocation(compiler.getSourceManager().getSpellingLoc(canon->getBeginLoc())); + // Some backwards compat magic. + // TODO Can probably be removed, but need to do some checking + if (loplugin::isSamePathname(fn, SRCDIR "/include/sax/fshelper.hxx")) + return true; + // Platform-specific code + if (loplugin::isSamePathname(fn, SRCDIR "/include/svl/svdde.hxx")) + return true; + if (loplugin::isSamePathname(fn, SRCDIR "/include/vcl/svmain.hxx")) + return true; + // passing pointer to function + if (loplugin::isSamePathname(fn, SRCDIR "/include/vcl/BitmapReadAccess.hxx")) + return true; + if (loplugin::isSamePathname(fn, SRCDIR "/vcl/inc/unx/gtk/gtkobject.hxx")) + return true; + if (loplugin::isSamePathname(fn, SRCDIR "/vcl/inc/unx/gtk/gtksalframe.hxx")) + return true; + if (loplugin::isSamePathname(fn, SRCDIR "/vcl/inc/unx/gtk/gtkframe.hxx")) + return true; + if (loplugin::isSamePathname(fn, SRCDIR "/vcl/unx/gtk/fpicker/SalGtkFilePicker.hxx")) + return true; + if (loplugin::isSamePathname(fn, SRCDIR "/extensions/source/propctrlr/propertyeditor.hxx")) + return true; + if (loplugin::isSamePathname(fn, SRCDIR "/forms/source/solar/inc/navtoolbar.hxx")) + return true; + if (loplugin::isSamePathname(fn, SRCDIR "/hwpfilter/source/grammar.cxx")) + return true; + if (loplugin::isSamePathname(fn, SRCDIR "/hwpfilter/source/lexer.cxx")) + return true; + // marked with a TODO/FIXME + if (loplugin::isSamePathname(fn, SRCDIR "/vcl/inc/sallayout.hxx")) + return true; + if (loplugin::isSamePathname(fn, SRCDIR "/accessibility/inc/standard/vclxaccessiblelist.hxx")) + return true; + // these are "extern C" but clang doesn't seem to report that accurately + if (loplugin::isSamePathname(fn, SRCDIR "/sax/source/fastparser/fastparser.cxx")) + return true; + // these all follow the same pattern, seems a pity to break that + if (loplugin::isSamePathname(fn, SRCDIR "/include/vcl/graphicfilter.hxx")) + return true; + // looks like work in progress + if (loplugin::isSamePathname(fn, SRCDIR "/vcl/source/filter/ipdf/pdfdocument.cxx")) + return true; + // macro magic + if (loplugin::isSamePathname(fn, SRCDIR "/basctl/source/inc/basidesh.hxx")) + return true; + // template magic + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/canvas/")) + return true; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/include/canvas/")) + return true; + if (loplugin::isSamePathname(fn, SRCDIR "/include/comphelper/unwrapargs.hxx")) + return true; + // this looks like vaguely useful code (ParseError) that I'm loathe to remove + if (loplugin::isSamePathname(fn, SRCDIR "/connectivity/source/inc/RowFunctionParser.hxx")) + return true; + if (loplugin::isSamePathname(fn, SRCDIR "/include/svx/EnhancedCustomShapeFunctionParser.hxx")) + return true; + // TODO marker parameter in constructor, should probably be using an enum + if (loplugin::isSamePathname(fn, SRCDIR "/framework/inc/uielement/uicommanddescription.hxx")) + return true; + if (loplugin::isSamePathname(fn, SRCDIR "/sd/source/ui/inc/SlideTransitionPane.hxx")) + return true; + if (loplugin::isSamePathname(fn, SRCDIR "/sd/source/ui/animations/CustomAnimationPane.hxx")) + return true; + if (loplugin::isSamePathname(fn, SRCDIR "/sd/source/ui/table/TableDesignPane.hxx")) + return true; + // debug stuff + if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/core/data/column2.cxx")) + return true; + // weird stuff + if (loplugin::isSamePathname(fn, SRCDIR "/scaddins/source/analysis/analysishelper.hxx")) + return true; + // SFX_DECL_CHILDWINDOWCONTEXT macro stuff + if (loplugin::isSamePathname(fn, SRCDIR "/sd/source/ui/inc/NavigatorChildWindow.hxx")) + return true; + // TODO, need to remove this from the .sdi file too + if (loplugin::isSamePathname(fn, SRCDIR "/sd/source/ui/inc/SlideSorterViewShell.hxx")) + return true; + if (loplugin::isSamePathname(fn, SRCDIR "/sd/source/ui/inc/OutlineViewShell.hxx")) + return true; + // SFX_DECL_INTERFACE macro stuff + if (loplugin::isSamePathname(fn, SRCDIR "/sd/source/ui/inc/ViewShellBase.hxx")) + return true; + // debug stuff + if (loplugin::isSamePathname(fn, SRCDIR "/sd/source/filter/ppt/pptinanimations.hxx")) + return true; + // takes pointer to fn + if (loplugin::isSamePathname(fn, SRCDIR "/include/sfx2/shell.hxx")) + return true; + // TODO, need to remove this from the .sdi file too + if (fqn == "SfxObjectShell::StateView_Impl") + return true; + // SFX_DECL_CHILDWINDOW_WITHID macro + if (loplugin::isSamePathname(fn, SRCDIR "/include/sfx2/infobar.hxx")) + return true; + // this looks like vaguely useful code (ParseError) that I'm loathe to remove + if (loplugin::isSamePathname(fn, SRCDIR "/slideshow/source/inc/slideshowexceptions.hxx")) + return true; + // SFX_DECL_VIEWFACTORY macro + if (loplugin::isSamePathname(fn, SRCDIR "/starmath/inc/view.hxx")) + return true; + // debugging + if (fqn == "BrowseBox::DoShowCursor" || fqn == "BrowseBox::DoHideCursor") + return true; + // if I change this one, it then overrides a superclass virtual method + if (fqn == "GalleryBrowser2::KeyInput") + return true; + // takes pointer to function + if (fqn == "cmis::AuthProvider::onedriveAuthCodeFallback" || fqn == "cmis::AuthProvider::gdriveAuthCodeFallback") + return true; + if (fqn == "ooo_mount_operation_ask_password") + return true; + // TODO tricky to remove because of default params + if (fqn == "xmloff::OAttribute2Property::addBooleanProperty") + return true; + // taking pointer to function + if (fqn == "sw::DocumentContentOperationsManager::DeleteAndJoinWithRedlineImpl" + || fqn == "sw::DocumentContentOperationsManager::DeleteRangeImpl" + || fqn == "SwTableFormula::GetFormulaBoxes" + || fqn == "SwFEShell::Drag" + || fqn == "GetASCWriter" || fqn == "GetHTMLWriter" || fqn == "GetXMLWriter" + || fqn == "SwWrtShell::UpdateLayoutFrame" || fqn == "SwWrtShell::DefaultDrag" + || fqn == "SwWrtShell::DefaultEndDrag" + || startswith(fqn, "SwWW8ImplReader::Read_")) + return true; + // WIN32 only + if (fqn == "SwFntObj::GuessLeading") + return true; + // SFX_DECL_CHILDWINDOW_WITHID macro + if (fqn == "SwSpellDialogChildWindow::SwSpellDialogChildWindow" + || fqn == "SwFieldDlgWrapper::SwFieldDlgWrapper" + || fqn == "SwInputChild::SwInputChild") + return true; + // SFX_DECL_VIEWFACTORY macro + if (fqn == "SwSrcView::SwSrcView") + return true; + // Serves to disambiguate two very similar methods + if (fqn == "MSWordStyles::BuildGetSlot") + return true; + // TODO there are just too many default params to make this worth fixing right now + if (fqn == "ScDocument::CopyMultiRangeFromClip") + return true; + // TODO looks like this needs fixing? + if (fqn == "ScTable::ExtendPrintArea") + return true; + // there is a FIXME in the code + if (fqn == "ScRangeUtil::IsAbsTabArea") + return true; + // SFX_DECL_CHILDWINDOW_WITHID + if (fqn == "ScInputWindowWrapper::ScInputWindowWrapper" + || fqn == "sc::SearchResultsDlgWrapper::SearchResultsDlgWrapper") + return true; + // ExecMethod in .sdi file + if (fqn == "ScChartShell::ExecuteExportAsGraphic") + return true; + // bool marker parameter + if (fqn == "SvxIconReplacementDialog::SvxIconReplacementDialog") + return true; + // used as pointer to fn + if (endswith(fqn, "_createInstance")) + return true; + // callback + if (startswith(fqn, "SbRtl_")) + return true; + // takes pointer to fn + if (fqn == "migration::BasicMigration_create" || fqn == "migration::WordbookMigration_create" + || fqn == "comp_CBlankNode::_create" || fqn == "comp_CURI::_create" + || fqn == "comp_CLiteral::_create" || fqn == "CDocumentBuilder::_getInstance" + || fqn == "DOM::CDocumentBuilder::_getInstance" + || fqn == "xml_security::serial_number_adapter::create" + || fqn == "desktop::splash::create" || fqn == "ScannerManager_CreateInstance" + || fqn == "formula::FormulaOpCodeMapperObj::create" + || fqn == "(anonymous namespace)::createInstance" + || fqn == "x_error_handler" + || fqn == "warning_func" + || fqn == "error_func" + || fqn == "ScaDateAddIn_CreateInstance" + || fqn == "ScaPricingAddIn_CreateInstance" + || fqn == "(anonymous namespace)::PDFSigningPKCS7PasswordCallback" + || fqn == "ContextMenuEventLink" + || fqn == "DelayedCloseEventLink" + || fqn == "GDIMetaFile::ImplColMonoFnc" + || fqn == "vcl::getGlyph0" + || fqn == "vcl::getGlyph6" + || fqn == "vcl::getGlyph12" + || fqn == "setPasswordCallback" + || fqn == "VCLExceptionSignal_impl" + || fqn == "getFontTable" + || fqn == "textconversiondlgs::ChineseTranslation_UnoDialog::create" + || fqn == "pcr::DefaultHelpProvider::Create" + || fqn == "pcr::DefaultFormComponentInspectorModel::Create" + || fqn == "pcr::ObjectInspectorModel::Create" + || fqn == "GraphicExportFilter::GraphicExportFilter" + || fqn == "CertificateContainer::CertificateContainer" + || startswith(fqn, "ParseCSS1_") + ) + return true; + // TODO + if (fqn == "FontSubsetInfo::CreateFontSubsetFromType1") + return true; + // used in template magic + if (fqn == "MtfRenderer::MtfRenderer" || fqn == "shell::sessioninstall::SyncDbusSessionHelper::SyncDbusSessionHelper" + || fqn == "dp_gui::LicenseDialog::LicenseDialog" + || fqn == "(anonymous namespace)::OGLTransitionFactoryImpl::OGLTransitionFactoryImpl") + return true; + // FIXME + if (fqn == "GtkSalDisplay::filterGdkEvent" || fqn == "SvXMLEmbeddedObjectHelper::ImplReadObject" + || fqn == "chart::CachedDataSequence::CachedDataSequence") + return true; + // used via macro + if (fqn == "framework::MediaTypeDetectionHelper::MediaTypeDetectionHelper" + || fqn == "framework::UriAbbreviation::UriAbbreviation" + || fqn == "framework::DispatchDisabler::DispatchDisabler" + || fqn == "framework::DispatchRecorderSupplier::DispatchRecorderSupplier") + return true; + // TODO Armin Le Grand is still working on this + if (fqn == "svx::frame::CreateDiagFrameBorderPrimitives" + || fqn == "svx::frame::CreateBorderPrimitives") + return true; + // marked with a TODO + if (fqn == "pcr::FormLinkDialog::getExistingRelation" + || fqn == "ooo::vba::DebugHelper::basicexception" + || fqn == "ScPrintFunc::DrawToDev") + return true; + // macros at work + if (fqn == "msfilter::lcl_PrintDigest") + return true; + // TODO something wrong here, the method that calls this (Normal::GenSlidingWindowFunction) cannot be correct + if (fqn == "sc::opencl::OpBase::Gen") + return true; + // Can't change this without conflicting with another constructor with the same signature + if (fqn == "XclExpSupbook::XclExpSupbook") + return true; + // ignore the LINK macros from include/tools/link.hxx + if (decl->getLocation().isMacroID()) + return true; + // debug code in sw/ + if (fqn == "lcl_dbg_out") + return true; + + for( auto it = decl->param_begin(); it != decl->param_end(); ++it) { + auto param = *it; + if (param->hasAttr<UnusedAttr>()) + continue; + if (!param->getName().empty()) + continue; + // ignore params which are enum types with only a single enumerator, these are marker/tag types + auto paramType = param->getType(); + if (paramType->isEnumeralType()) { + auto enumType = paramType->getAs<EnumType>(); + int cnt = std::distance(enumType->getDecl()->enumerator_begin(), enumType->getDecl()->enumerator_end()); + if (cnt == 1) + continue; + } + // ignore params which are a reference to a struct which has no fields. + // These are either + // (a) marker/tag types + // (b) selective "friend" access + if (paramType->isReferenceType()) { + auto referenceType = paramType->getAs<ReferenceType>(); + if (referenceType->getPointeeType()->isRecordType()) { + auto recordType = referenceType->getPointeeType()->getAs<RecordType>(); + if (noFieldsInRecord(recordType) == 0) + continue; + } + } + else if (paramType->isRecordType()) { + if (noFieldsInRecord(paramType->getAs<RecordType>()) == 0) + continue; + } + report( DiagnosticsEngine::Warning, + "unused param %0 in %1", param->getBeginLoc()) + << param->getSourceRange() + << param->getName() + << fqn; + if (canon != decl) + { + unsigned idx = param->getFunctionScopeIndex(); + const ParmVarDecl* pOther = canon->getParamDecl(idx); + report( DiagnosticsEngine::Note, "declaration is here", + pOther->getBeginLoc()) + << pOther->getSourceRange(); + } + } + return true; +} + +loplugin::Plugin::Registration<CheckUnusedParams> X("checkunusedparams", false); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/comparisonwithconstant.cxx b/compilerplugins/clang/store/comparisonwithconstant.cxx new file mode 100644 index 000000000000..d796b7c3a3a1 --- /dev/null +++ b/compilerplugins/clang/store/comparisonwithconstant.cxx @@ -0,0 +1,168 @@ +/* -*- 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 <cassert> +#include <string> +#include <iostream> +#include <fstream> +#include <set> + +#include "compat.hxx" +#include "plugin.hxx" + +/** + Look for comparisons where the constant is on the left, it should be on the right. + */ + +namespace { + +class ComparisonWithConstant : + public loplugin::FilteringRewritePlugin<ComparisonWithConstant> +{ +public: + explicit ComparisonWithConstant(loplugin::InstantiationData const & data): FilteringRewritePlugin(data) {} + + virtual void run() override + { + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + } + + // Deliberately drop RecursiveASTVisitor::TraverseBinaryOperator's DataRecursionQueue + // parameter; TraverseBinaryOperator must use stack instead of data recursion for any + // children's VisitBinaryOperator to see changes to occurrence_ by a parent + // VisitBinaryOperator: + bool TraverseBinaryOperator(BinaryOperator * S) + { + auto const op = S->getOpcode(); + if (op != BO_EQ && op != BO_NE) { + return RecursiveASTVisitor::TraverseBinaryOperator(S); + } + auto const saved = occurrence_; + auto const ret = RecursiveASTVisitor::TraverseBinaryOperator(S); + occurrence_ = saved; + return ret; + } + + bool VisitBinaryOperator(const BinaryOperator *); +private: + bool rewrite(const BinaryOperator *); + std::string getExprAsString(SourceRange range); + SourceRange ignoreMacroExpansions(SourceRange range); + + bool occurrence_ = false; +}; + +bool ComparisonWithConstant::VisitBinaryOperator(const BinaryOperator* binaryOp) +{ + if (ignoreLocation(binaryOp)) { + return true; + } + if (!(binaryOp->getOpcode() == BO_EQ || binaryOp->getOpcode() == BO_NE)) { + return true; + } + // protect against clang assert + if (binaryOp->getLHS()->isValueDependent() || binaryOp->getRHS()->isValueDependent()) { + return true; + } + if (!binaryOp->getLHS()->isEvaluatable(compiler.getASTContext())) { + return true; + } + if (binaryOp->getRHS()->isEvaluatable(compiler.getASTContext())) { + return true; + } + if (occurrence_ || !rewrite(binaryOp)) + { + report( + DiagnosticsEngine::Warning, "Rather put constant on right when comparing", + binaryOp->getSourceRange().getBegin()) + << binaryOp->getSourceRange(); + } + occurrence_ = true; + return true; +} + + +bool ComparisonWithConstant::rewrite(const BinaryOperator * binaryOp) { + if (rewriter == nullptr) { + return false; + } + + auto lhsRange = ignoreMacroExpansions(binaryOp->getLHS()->getSourceRange()); + if (!lhsRange.isValid()) { + return false; + } + auto rhsRange = ignoreMacroExpansions(binaryOp->getRHS()->getSourceRange()); + if (!rhsRange.isValid()) { + return false; + } + + const std::string lhsString = getExprAsString(lhsRange); + const std::string rhsString = getExprAsString(rhsRange); + + // switch LHS and RHS + if (!replaceText(lhsRange, rhsString)) { + return false; + } + if (!replaceText(rhsRange, lhsString)) { + return false; + } + + return true; +} + +// get the expression contents +std::string ComparisonWithConstant::getExprAsString(SourceRange range) +{ + SourceManager& SM = compiler.getSourceManager(); + SourceLocation startLoc = range.getBegin(); + SourceLocation endLoc = range.getEnd(); + const char *p1 = SM.getCharacterData( startLoc ); + const char *p2 = SM.getCharacterData( endLoc ); + unsigned n = Lexer::MeasureTokenLength( endLoc, SM, compiler.getLangOpts()); + return std::string( p1, p2 - p1 + n); +} + +SourceRange ComparisonWithConstant::ignoreMacroExpansions(SourceRange range) { + while (compiler.getSourceManager().isMacroArgExpansion(range.getBegin())) { + range.setBegin( + compiler.getSourceManager().getImmediateMacroCallerLoc( + range.getBegin())); + } + if (range.getBegin().isMacroID()) { + SourceLocation loc; + if (Lexer::isAtStartOfMacroExpansion( + range.getBegin(), compiler.getSourceManager(), + compiler.getLangOpts(), &loc)) + { + range.setBegin(loc); + } + } + while (compiler.getSourceManager().isMacroArgExpansion(range.getEnd())) { + range.setEnd( + compiler.getSourceManager().getImmediateMacroCallerLoc( + range.getEnd())); + } + if (range.getEnd().isMacroID()) { + SourceLocation loc; + if (Lexer::isAtEndOfMacroExpansion( + range.getEnd(), compiler.getSourceManager(), + compiler.getLangOpts(), &loc)) + { + range.setEnd(loc); + } + } + return range.getBegin().isMacroID() || range.getEnd().isMacroID() + ? SourceRange() : range; +} + +loplugin::Plugin::Registration< ComparisonWithConstant > X("comparisonwithconstant", false); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/constfields.cxx b/compilerplugins/clang/store/constfields.cxx new file mode 100644 index 000000000000..692c84daeb8c --- /dev/null +++ b/compilerplugins/clang/store/constfields.cxx @@ -0,0 +1,596 @@ +/* -*- 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/. + */ + +#if !defined _WIN32 //TODO, #include <sys/file.h> + +#include <cassert> +#include <string> +#include <iostream> +#include <fstream> +#include <unordered_set> +#include <vector> +#include <algorithm> +#include <sys/file.h> +#include <unistd.h> + +#include "config_clang.h" + +#include "plugin.hxx" +#include "check.hxx" + +#include "clang/AST/ParentMapContext.h" + +/** +Look for fields that are only assigned to in the constructor using field-init, and can therefore be const. + +The process goes something like this: + $ make check + $ make FORCE_COMPILE=all COMPILER_PLUGIN_TOOL='constfields' check + $ ./compilerplugins/clang/constfields.py + +and then + $ for dir in *; do make $dir FORCE_COMPILE=all UPDATE_FILES=$dir COMPILER_PLUGIN_TOOL='constfieldsrewrite' $dir; done +to auto-remove the method declarations + +*/ + +namespace +{ +struct MyFieldInfo +{ + const RecordDecl* parentRecord; + std::string parentClass; + std::string fieldName; + std::string fieldType; + std::string sourceLocation; + std::string access; +}; +bool operator<(const MyFieldInfo& lhs, const MyFieldInfo& rhs) +{ + return std::tie(lhs.parentClass, lhs.fieldName) < std::tie(rhs.parentClass, rhs.fieldName); +} + +// try to limit the voluminous output a little +static std::set<MyFieldInfo> cannotBeConstSet; +static std::set<MyFieldInfo> definitionSet; + +/** + * Wrap the different kinds of callable and callee objects in the clang AST so I can define methods that handle everything. + */ +class CallerWrapper +{ + const CallExpr* m_callExpr; + const CXXConstructExpr* m_cxxConstructExpr; + +public: + CallerWrapper(const CallExpr* callExpr) + : m_callExpr(callExpr) + , m_cxxConstructExpr(nullptr) + { + } + CallerWrapper(const CXXConstructExpr* cxxConstructExpr) + : m_callExpr(nullptr) + , m_cxxConstructExpr(cxxConstructExpr) + { + } + unsigned getNumArgs() const + { + return m_callExpr ? m_callExpr->getNumArgs() : m_cxxConstructExpr->getNumArgs(); + } + const Expr* getArg(unsigned i) const + { + return m_callExpr ? m_callExpr->getArg(i) : m_cxxConstructExpr->getArg(i); + } +}; +class CalleeWrapper +{ + const FunctionDecl* m_calleeFunctionDecl = nullptr; + const CXXConstructorDecl* m_cxxConstructorDecl = nullptr; + const FunctionProtoType* m_functionPrototype = nullptr; + +public: + explicit CalleeWrapper(const FunctionDecl* calleeFunctionDecl) + : m_calleeFunctionDecl(calleeFunctionDecl) + { + } + explicit CalleeWrapper(const CXXConstructExpr* cxxConstructExpr) + : m_cxxConstructorDecl(cxxConstructExpr->getConstructor()) + { + } + explicit CalleeWrapper(const FunctionProtoType* functionPrototype) + : m_functionPrototype(functionPrototype) + { + } + unsigned getNumParams() const + { + if (m_calleeFunctionDecl) + return m_calleeFunctionDecl->getNumParams(); + else if (m_cxxConstructorDecl) + return m_cxxConstructorDecl->getNumParams(); + else if (m_functionPrototype->param_type_begin() == m_functionPrototype->param_type_end()) + // FunctionProtoType will assert if we call getParamTypes() and it has no params + return 0; + else + return m_functionPrototype->getParamTypes().size(); + } + const QualType getParamType(unsigned i) const + { + if (m_calleeFunctionDecl) + return m_calleeFunctionDecl->getParamDecl(i)->getType(); + else if (m_cxxConstructorDecl) + return m_cxxConstructorDecl->getParamDecl(i)->getType(); + else + return m_functionPrototype->getParamTypes()[i]; + } + std::string getNameAsString() const + { + if (m_calleeFunctionDecl) + return m_calleeFunctionDecl->getNameAsString(); + else if (m_cxxConstructorDecl) + return m_cxxConstructorDecl->getNameAsString(); + else + return ""; + } + CXXMethodDecl const* getAsCXXMethodDecl() const + { + if (m_calleeFunctionDecl) + return dyn_cast<CXXMethodDecl>(m_calleeFunctionDecl); + return nullptr; + } +}; + +class ConstFields : public RecursiveASTVisitor<ConstFields>, public loplugin::Plugin +{ +public: + explicit ConstFields(loplugin::InstantiationData const& data) + : Plugin(data) + { + } + + virtual void run() override; + + bool shouldVisitTemplateInstantiations() const { return true; } + bool shouldVisitImplicitCode() const { return true; } + + bool VisitFieldDecl(const FieldDecl*); + bool VisitMemberExpr(const MemberExpr*); + bool TraverseCXXConstructorDecl(CXXConstructorDecl*); + bool TraverseCXXMethodDecl(CXXMethodDecl*); + bool TraverseFunctionDecl(FunctionDecl*); + bool TraverseIfStmt(IfStmt*); + +private: + MyFieldInfo niceName(const FieldDecl*); + void check(const FieldDecl* fieldDecl, const Expr* memberExpr); + bool isSomeKindOfZero(const Expr* arg); + bool IsPassedByNonConst(const FieldDecl* fieldDecl, const Stmt* child, CallerWrapper callExpr, + CalleeWrapper calleeFunctionDecl); + llvm::Optional<CalleeWrapper> getCallee(CallExpr const*); + + RecordDecl* insideMoveOrCopyDeclParent = nullptr; + // For reasons I do not understand, parentFunctionDecl() is not reliable, so + // we store the parent function on the way down the AST. + FunctionDecl* insideFunctionDecl = nullptr; + std::vector<FieldDecl const*> insideConditionalCheckOfMemberSet; +}; + +void ConstFields::run() +{ + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + + if (!isUnitTestMode()) + { + // dump all our output in one write call - this is to try and limit IO "crosstalk" between multiple processes + // writing to the same logfile + std::string output; + for (const MyFieldInfo& s : cannotBeConstSet) + output += "write-outside-constructor:\t" + s.parentClass + "\t" + s.fieldName + "\n"; + for (const MyFieldInfo& s : definitionSet) + output += "definition:\t" + s.access + "\t" + s.parentClass + "\t" + s.fieldName + "\t" + + s.fieldType + "\t" + s.sourceLocation + "\n"; + std::ofstream myfile; + myfile.open(WORKDIR "/loplugin.constfields.log", std::ios::app | std::ios::out); + myfile << output; + myfile.close(); + } + else + { + for (const MyFieldInfo& s : cannotBeConstSet) + report(DiagnosticsEngine::Warning, "notconst %0", s.parentRecord->getBeginLoc()) + << s.fieldName; + } +} + +MyFieldInfo ConstFields::niceName(const FieldDecl* fieldDecl) +{ + MyFieldInfo aInfo; + + const RecordDecl* recordDecl = fieldDecl->getParent(); + + if (const CXXRecordDecl* cxxRecordDecl = dyn_cast<CXXRecordDecl>(recordDecl)) + { + if (cxxRecordDecl->getTemplateInstantiationPattern()) + cxxRecordDecl = cxxRecordDecl->getTemplateInstantiationPattern(); + aInfo.parentRecord = cxxRecordDecl; + aInfo.parentClass = cxxRecordDecl->getQualifiedNameAsString(); + } + else + { + aInfo.parentRecord = recordDecl; + aInfo.parentClass = recordDecl->getQualifiedNameAsString(); + } + + aInfo.fieldName = fieldDecl->getNameAsString(); + // sometimes the name (if it's an anonymous thing) contains the full path of the build folder, which we don't need + size_t idx = aInfo.fieldName.find(SRCDIR); + if (idx != std::string::npos) + { + aInfo.fieldName = aInfo.fieldName.replace(idx, strlen(SRCDIR), ""); + } + aInfo.fieldType = fieldDecl->getType().getAsString(); + + SourceLocation expansionLoc + = compiler.getSourceManager().getExpansionLoc(fieldDecl->getLocation()); + StringRef name = getFilenameOfLocation(expansionLoc); + aInfo.sourceLocation + = std::string(name.substr(strlen(SRCDIR) + 1)) + ":" + + std::to_string(compiler.getSourceManager().getSpellingLineNumber(expansionLoc)); + loplugin::normalizeDotDotInFilePath(aInfo.sourceLocation); + + switch (fieldDecl->getAccess()) + { + case AS_public: + aInfo.access = "public"; + break; + case AS_private: + aInfo.access = "private"; + break; + case AS_protected: + aInfo.access = "protected"; + break; + default: + aInfo.access = "unknown"; + break; + } + + return aInfo; +} + +bool ConstFields::VisitFieldDecl(const FieldDecl* fieldDecl) +{ + fieldDecl = fieldDecl->getCanonicalDecl(); + if (ignoreLocation(fieldDecl)) + { + return true; + } + // ignore stuff that forms part of the stable URE interface + if (isInUnoIncludeFile(compiler.getSourceManager().getSpellingLoc(fieldDecl->getLocation()))) + { + return true; + } + definitionSet.insert(niceName(fieldDecl)); + return true; +} + +bool ConstFields::TraverseCXXConstructorDecl(CXXConstructorDecl* cxxConstructorDecl) +{ + auto copy = insideMoveOrCopyDeclParent; + if (!ignoreLocation(cxxConstructorDecl) && cxxConstructorDecl->isThisDeclarationADefinition()) + { + if (cxxConstructorDecl->isCopyOrMoveConstructor()) + insideMoveOrCopyDeclParent = cxxConstructorDecl->getParent(); + } + bool ret = RecursiveASTVisitor::TraverseCXXConstructorDecl(cxxConstructorDecl); + insideMoveOrCopyDeclParent = copy; + return ret; +} + +bool ConstFields::TraverseCXXMethodDecl(CXXMethodDecl* cxxMethodDecl) +{ + auto copy1 = insideMoveOrCopyDeclParent; + auto copy2 = insideFunctionDecl; + if (!ignoreLocation(cxxMethodDecl) && cxxMethodDecl->isThisDeclarationADefinition()) + { + if (cxxMethodDecl->isCopyAssignmentOperator() || cxxMethodDecl->isMoveAssignmentOperator()) + insideMoveOrCopyDeclParent = cxxMethodDecl->getParent(); + } + insideFunctionDecl = cxxMethodDecl; + bool ret = RecursiveASTVisitor::TraverseCXXMethodDecl(cxxMethodDecl); + insideMoveOrCopyDeclParent = copy1; + insideFunctionDecl = copy2; + return ret; +} + +bool ConstFields::TraverseFunctionDecl(FunctionDecl* functionDecl) +{ + auto copy2 = insideFunctionDecl; + insideFunctionDecl = functionDecl; + bool ret = RecursiveASTVisitor::TraverseFunctionDecl(functionDecl); + insideFunctionDecl = copy2; + return ret; +} + +bool ConstFields::TraverseIfStmt(IfStmt* ifStmt) +{ + FieldDecl const* memberFieldDecl = nullptr; + if (Expr const* cond = ifStmt->getCond()) + { + if (auto memberExpr = dyn_cast<MemberExpr>(cond->IgnoreParenImpCasts())) + { + if ((memberFieldDecl = dyn_cast<FieldDecl>(memberExpr->getMemberDecl()))) + insideConditionalCheckOfMemberSet.push_back(memberFieldDecl); + } + } + bool ret = RecursiveASTVisitor::TraverseIfStmt(ifStmt); + if (memberFieldDecl) + insideConditionalCheckOfMemberSet.pop_back(); + return ret; +} + +bool ConstFields::VisitMemberExpr(const MemberExpr* memberExpr) +{ + const ValueDecl* decl = memberExpr->getMemberDecl(); + const FieldDecl* fieldDecl = dyn_cast<FieldDecl>(decl); + if (!fieldDecl) + { + return true; + } + fieldDecl = fieldDecl->getCanonicalDecl(); + if (ignoreLocation(fieldDecl)) + { + return true; + } + // ignore stuff that forms part of the stable URE interface + if (isInUnoIncludeFile(compiler.getSourceManager().getSpellingLoc(fieldDecl->getLocation()))) + { + return true; + } + + check(fieldDecl, memberExpr); + + return true; +} + +void ConstFields::check(const FieldDecl* fieldDecl, const Expr* memberExpr) +{ + auto parentsRange = compiler.getASTContext().getParents(*memberExpr); + const Stmt* child = memberExpr; + const Stmt* parent + = parentsRange.begin() == parentsRange.end() ? nullptr : parentsRange.begin()->get<Stmt>(); + // walk up the tree until we find something interesting + bool bCannotBeConst = false; + bool bDump = false; + auto walkUp = [&]() { + child = parent; + auto parentsRange = compiler.getASTContext().getParents(*parent); + parent = parentsRange.begin() == parentsRange.end() ? nullptr + : parentsRange.begin()->get<Stmt>(); + }; + do + { + if (!parent) + { + // check if we have an expression like + // int& r = m_field; + auto parentsRange = compiler.getASTContext().getParents(*child); + if (parentsRange.begin() != parentsRange.end()) + { + auto varDecl = dyn_cast_or_null<VarDecl>(parentsRange.begin()->get<Decl>()); + // The isImplicit() call is to avoid triggering when we see the vardecl which is part of a for-range statement, + // which is of type 'T&&' and also an l-value-ref ? + if (varDecl && !varDecl->isImplicit() + && loplugin::TypeCheck(varDecl->getType()).LvalueReference().NonConst()) + { + bCannotBeConst = true; + } + } + break; + } + if (isa<CXXReinterpretCastExpr>(parent)) + { + // once we see one of these, there is not much useful we can know + bCannotBeConst = true; + break; + } + else if (isa<CastExpr>(parent) || isa<MemberExpr>(parent) || isa<ParenExpr>(parent) + || isa<ParenListExpr>(parent) || isa<ArrayInitLoopExpr>(parent) + || isa<ExprWithCleanups>(parent)) + { + walkUp(); + } + else if (auto unaryOperator = dyn_cast<UnaryOperator>(parent)) + { + UnaryOperator::Opcode op = unaryOperator->getOpcode(); + if (op == UO_AddrOf || op == UO_PostInc || op == UO_PostDec || op == UO_PreInc + || op == UO_PreDec) + { + bCannotBeConst = true; + } + walkUp(); + } + else if (auto operatorCallExpr = dyn_cast<CXXOperatorCallExpr>(parent)) + { + auto callee = getCallee(operatorCallExpr); + if (callee) + { + // if calling a non-const operator on the field + auto calleeMethodDecl = callee->getAsCXXMethodDecl(); + if (calleeMethodDecl && operatorCallExpr->getArg(0) == child + && !calleeMethodDecl->isConst()) + { + bCannotBeConst = true; + } + else if (IsPassedByNonConst(fieldDecl, child, operatorCallExpr, *callee)) + { + bCannotBeConst = true; + } + } + else + bCannotBeConst = true; // conservative, could improve + walkUp(); + } + else if (auto cxxMemberCallExpr = dyn_cast<CXXMemberCallExpr>(parent)) + { + const CXXMethodDecl* calleeMethodDecl = cxxMemberCallExpr->getMethodDecl(); + if (calleeMethodDecl) + { + // if calling a non-const method on the field + const Expr* tmp = dyn_cast<Expr>(child); + if (tmp->isBoundMemberFunction(compiler.getASTContext())) + { + tmp = dyn_cast<MemberExpr>(tmp)->getBase(); + } + if (cxxMemberCallExpr->getImplicitObjectArgument() == tmp + && !calleeMethodDecl->isConst()) + { + bCannotBeConst = true; + break; + } + if (IsPassedByNonConst(fieldDecl, child, cxxMemberCallExpr, + CalleeWrapper(calleeMethodDecl))) + bCannotBeConst = true; + } + else + bCannotBeConst = true; // can happen in templates + walkUp(); + } + else if (auto cxxConstructExpr = dyn_cast<CXXConstructExpr>(parent)) + { + if (IsPassedByNonConst(fieldDecl, child, cxxConstructExpr, + CalleeWrapper(cxxConstructExpr))) + bCannotBeConst = true; + walkUp(); + } + else if (auto callExpr = dyn_cast<CallExpr>(parent)) + { + auto callee = getCallee(callExpr); + if (callee) + { + if (IsPassedByNonConst(fieldDecl, child, callExpr, *callee)) + bCannotBeConst = true; + } + else + bCannotBeConst = true; // conservative, could improve + walkUp(); + } + else if (auto binaryOp = dyn_cast<BinaryOperator>(parent)) + { + BinaryOperator::Opcode op = binaryOp->getOpcode(); + const bool assignmentOp = op == BO_Assign || op == BO_MulAssign || op == BO_DivAssign + || op == BO_RemAssign || op == BO_AddAssign + || op == BO_SubAssign || op == BO_ShlAssign + || op == BO_ShrAssign || op == BO_AndAssign + || op == BO_XorAssign || op == BO_OrAssign; + if (assignmentOp) + { + if (binaryOp->getLHS() == child) + bCannotBeConst = true; + else if (loplugin::TypeCheck(binaryOp->getLHS()->getType()) + .LvalueReference() + .NonConst()) + // if the LHS is a non-const reference, we could write to the field later on + bCannotBeConst = true; + } + walkUp(); + } + else if (isa<ReturnStmt>(parent)) + { + if (insideFunctionDecl) + { + auto tc = loplugin::TypeCheck(insideFunctionDecl->getReturnType()); + if (tc.LvalueReference().NonConst()) + bCannotBeConst = true; + } + break; + } + else if (isa<SwitchStmt>(parent) || isa<WhileStmt>(parent) || isa<ForStmt>(parent) + || isa<IfStmt>(parent) || isa<DoStmt>(parent) || isa<CXXForRangeStmt>(parent) + || isa<DefaultStmt>(parent)) + { + break; + } + else + { + walkUp(); + } + } while (true); + + if (bDump) + { + report(DiagnosticsEngine::Warning, "oh dear, what can the matter be? writtenTo=%0", + memberExpr->getBeginLoc()) + << bCannotBeConst << memberExpr->getSourceRange(); + if (parent) + { + report(DiagnosticsEngine::Note, "parent over here", parent->getBeginLoc()) + << parent->getSourceRange(); + parent->dump(); + } + memberExpr->dump(); + fieldDecl->getType()->dump(); + } + + if (bCannotBeConst) + { + cannotBeConstSet.insert(niceName(fieldDecl)); + } +} + +bool ConstFields::IsPassedByNonConst(const FieldDecl* fieldDecl, const Stmt* child, + CallerWrapper callExpr, CalleeWrapper calleeFunctionDecl) +{ + unsigned len = std::min(callExpr.getNumArgs(), calleeFunctionDecl.getNumParams()); + // if it's an array, passing it by value to a method typically means the + // callee takes a pointer and can modify the array + if (fieldDecl->getType()->isConstantArrayType()) + { + for (unsigned i = 0; i < len; ++i) + if (callExpr.getArg(i) == child) + if (loplugin::TypeCheck(calleeFunctionDecl.getParamType(i)).Pointer().NonConst()) + return true; + } + else + { + for (unsigned i = 0; i < len; ++i) + if (callExpr.getArg(i) == child) + if (loplugin::TypeCheck(calleeFunctionDecl.getParamType(i)) + .LvalueReference() + .NonConst()) + return true; + } + return false; +} + +llvm::Optional<CalleeWrapper> ConstFields::getCallee(CallExpr const* callExpr) +{ + FunctionDecl const* functionDecl = callExpr->getDirectCallee(); + if (functionDecl) + return CalleeWrapper(functionDecl); + + // Extract the functionprototype from a type + clang::Type const* calleeType = callExpr->getCallee()->getType().getTypePtr(); + if (auto pointerType = calleeType->getUnqualifiedDesugaredType()->getAs<clang::PointerType>()) + { + if (auto prototype = pointerType->getPointeeType() + ->getUnqualifiedDesugaredType() + ->getAs<FunctionProtoType>()) + { + return CalleeWrapper(prototype); + } + } + + return llvm::Optional<CalleeWrapper>(); +} + +loplugin::Plugin::Registration<ConstFields> X("constfields", false); +} + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/constfields.py b/compilerplugins/clang/store/constfields.py new file mode 100755 index 000000000000..e81d3f3043f5 --- /dev/null +++ b/compilerplugins/clang/store/constfields.py @@ -0,0 +1,87 @@ +#!/usr/bin/python + +import sys +import re +import io + +definitionSet = set() +definitionToSourceLocationMap = dict() +definitionToTypeMap = dict() +writeFromOutsideConstructorSet = set() + +# clang does not always use exactly the same numbers in the type-parameter vars it generates +# so I need to substitute them to ensure we can match correctly. +normalizeTypeParamsRegex = re.compile(r"type-parameter-\d+-\d+") +def normalizeTypeParams( line ): + return normalizeTypeParamsRegex.sub("type-parameter-?-?", line) + +def parseFieldInfo( tokens ): + if len(tokens) == 3: + return (normalizeTypeParams(tokens[1]), tokens[2]) + else: + return (normalizeTypeParams(tokens[1]), "") + +with io.open("workdir/loplugin.constfields.log", "rb", buffering=1024*1024) as txt: + for line in txt: + tokens = line.strip().split("\t") + if tokens[0] == "definition:": + access = tokens[1] + fieldInfo = (normalizeTypeParams(tokens[2]), tokens[3]) + srcLoc = tokens[5] + # ignore external source code + if (srcLoc.startswith("external/")): + continue + # ignore build folder + if (srcLoc.startswith("workdir/")): + continue + definitionSet.add(fieldInfo) + definitionToTypeMap[fieldInfo] = tokens[4] + definitionToSourceLocationMap[fieldInfo] = tokens[5] + elif tokens[0] == "write-outside-constructor:": + writeFromOutsideConstructorSet.add(parseFieldInfo(tokens)) + else: + print( "unknown line: " + line) + +# Calculate can-be-const-field set +canBeConstFieldSet = set() +for d in definitionSet: + if d in writeFromOutsideConstructorSet: + continue + srcLoc = definitionToSourceLocationMap[d]; + fieldType = definitionToTypeMap[d] + if fieldType.startswith("const "): + continue + if "std::unique_ptr" in fieldType: + continue + if "std::shared_ptr" in fieldType: + continue + if "Reference<" in fieldType: + continue + if "VclPtr<" in fieldType: + continue + if "osl::Mutex" in fieldType: + continue + if "::sfx2::sidebar::ControllerItem" in fieldType: + continue + canBeConstFieldSet.add((d[0] + " " + d[1] + " " + fieldType, srcLoc)) + + +# sort the results using a "natural order" so sequences like [item1,item2,item10] sort nicely +def natural_sort_key(s, _nsre=re.compile('([0-9]+)')): + return [int(text) if text.isdigit() else text.lower() + for text in re.split(_nsre, s)] +# sort by both the source-line and the datatype, so the output file ordering is stable +# when we have multiple items on the same source line +def v_sort_key(v): + return natural_sort_key(v[1]) + [v[0]] + +# sort results by name and line number +tmp6list = sorted(canBeConstFieldSet, key=lambda v: v_sort_key(v)) + +# print out the results +with open("compilerplugins/clang/constfields.results", "wt") as f: + for t in tmp6list: + f.write( t[1] + "\n" ) + f.write( " " + t[0] + "\n" ) + + diff --git a/compilerplugins/clang/store/constfieldsrewrite.cxx b/compilerplugins/clang/store/constfieldsrewrite.cxx new file mode 100644 index 000000000000..d72bb43aad7a --- /dev/null +++ b/compilerplugins/clang/store/constfieldsrewrite.cxx @@ -0,0 +1,169 @@ +/* -*- 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/. + */ + +#if !defined _WIN32 //TODO, #include <sys/mman.h> + +#include <cassert> +#include <string> +#include <iostream> +#include "plugin.hxx" +#include "check.hxx" +#include "config_clang.h" +#include <sys/mman.h> +#include <sys/types.h> +#include <fcntl.h> +#include <unistd.h> +#include <sys/stat.h> +#include <assert.h> +#include <cstring> + +/** + This is intended to be run as the second stage of the "constfields" clang plugin. +*/ + +namespace +{ +class ConstFieldsRewrite : public RecursiveASTVisitor<ConstFieldsRewrite>, + public loplugin::RewritePlugin +{ +public: + explicit ConstFieldsRewrite(loplugin::InstantiationData const& data); + ~ConstFieldsRewrite(); + + virtual void run() override + { + if (rewriter) + { + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + } + } + + bool VisitFieldDecl(const FieldDecl* var); + +private: + // I use a brute-force approach - mmap the results file and do a linear search on it + // It works surprisingly well, because the file is small enough to fit into L2 cache on modern CPU's + size_t mmapFilesize; + int mmapFD; + char* mmappedData; +}; + +size_t getFilesize(const char* filename) +{ + struct stat st; + stat(filename, &st); + return st.st_size; +} + +ConstFieldsRewrite::ConstFieldsRewrite(loplugin::InstantiationData const& data) + : RewritePlugin(data) +{ + static const char sInputFile[] = SRCDIR "/compilerplugins/clang/constfields.results"; + mmapFilesize = getFilesize(sInputFile); + //Open file + mmapFD = open(sInputFile, O_RDONLY, 0); + assert(mmapFD != -1); + //Execute mmap + mmappedData = static_cast<char*>(mmap(NULL, mmapFilesize, PROT_READ, MAP_PRIVATE, mmapFD, 0)); + assert(mmappedData != NULL); +} + +ConstFieldsRewrite::~ConstFieldsRewrite() +{ + //Cleanup + int rc = munmap(mmappedData, mmapFilesize); + assert(rc == 0); + (void)rc; + close(mmapFD); +} + +bool ConstFieldsRewrite::VisitFieldDecl(const FieldDecl* fieldDecl) +{ + if (ignoreLocation(fieldDecl)) + return true; + // ignore stuff that forms part of the stable URE interface + if (isInUnoIncludeFile(compiler.getSourceManager().getSpellingLoc( + fieldDecl->getCanonicalDecl()->getLocation()))) + return true; + // in case we've already processed this field + if (fieldDecl->getType().isConstQualified()) + return true; + // TODO rewriting T& is a bit trickier + if (loplugin::TypeCheck(fieldDecl->getType()).LvalueReference()) + return true; + + const RecordDecl* recordDecl = fieldDecl->getParent(); + std::string parentClassName; + if (const CXXRecordDecl* cxxRecordDecl = dyn_cast<CXXRecordDecl>(recordDecl)) + { + if (cxxRecordDecl->getTemplateInstantiationPattern()) + cxxRecordDecl = cxxRecordDecl->getTemplateInstantiationPattern(); + parentClassName = cxxRecordDecl->getQualifiedNameAsString(); + } + else + { + parentClassName = recordDecl->getQualifiedNameAsString(); + } + // the extra spaces match the formatting in the results file, and help avoid false+ + std::string aNiceName = " " + parentClassName + " " + fieldDecl->getNameAsString() + " " + + fieldDecl->getType().getAsString() + "\n"; + + // search mmap'ed file for field + const char* aNiceNameStr = aNiceName.c_str(); + char* found = std::search(mmappedData, mmappedData + mmapFilesize, aNiceNameStr, + aNiceNameStr + aNiceName.size()); + if (!(found < mmappedData + mmapFilesize)) + return true; + + SourceManager& SM = compiler.getSourceManager(); + auto endLoc = fieldDecl->getTypeSourceInfo()->getTypeLoc().getEndLoc(); + endLoc = endLoc.getLocWithOffset(Lexer::MeasureTokenLength(endLoc, SM, compiler.getLangOpts())); + + // Calculate how much space is available after the type declaration that we can use to + // overwrite with the " const". This reduces the amount of formatting fixups I need to do. + char const* p1 = SM.getCharacterData(endLoc); + bool success = false; + if (*p1 != ' ') + { + // Sometimes there is no space at all e.g. in + // FastTokenHandlerBase *mpTokenHandler; + // between the "*" and the "mpTokenHandler", so add an extra space. + success = insertText(endLoc, " const "); + } + else + { + int spaceAvailable = 1; + ++p1; + for (; spaceAvailable < 6; ++spaceAvailable) + { + if (*p1 != ' ') + break; + ++p1; + } + if (spaceAvailable < 6) + success = replaceText(endLoc, spaceAvailable - 1, " const"); + else + success = replaceText(endLoc, spaceAvailable, " const"); + } + + if (!success) + { + report(DiagnosticsEngine::Warning, "Could not mark field as const", + fieldDecl->getBeginLoc()) + << fieldDecl->getSourceRange(); + } + return true; +} + +loplugin::Plugin::Registration<ConstFieldsRewrite> X("constfieldsrewrite", false); +} + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/constparams.cxx b/compilerplugins/clang/store/constparams.cxx new file mode 100644 index 000000000000..dac7322d0130 --- /dev/null +++ b/compilerplugins/clang/store/constparams.cxx @@ -0,0 +1,618 @@ +/* -*- 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 <algorithm> +#include <string> +#include <unordered_set> +#include <unordered_map> +#include <iostream> + +#include "config_clang.h" + +#include "plugin.hxx" +#include "compat.hxx" +#include "check.hxx" +#include "functionaddress.hxx" + +#include "clang/AST/ParentMapContext.h" + +/** + Find pointer and reference params that can be declared const. + + This is not a sophisticated analysis. It deliberately skips all of the hard cases for now. + It is an exercise in getting the most benefit for the least effort. +*/ +namespace +{ + +class ConstParams: + public loplugin::FunctionAddress<loplugin::FilteringPlugin<ConstParams>> +{ +public: + explicit ConstParams(loplugin::InstantiationData const & data): FunctionAddress(data) {} + + virtual void run() override { + std::string fn(handler.getMainFileName()); + loplugin::normalizeDotDotInFilePath(fn); + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sal/") + || fn == SRCDIR "/jurt/source/pipe/staticsalhack.cxx" + || loplugin::hasPathnamePrefix(fn, SRCDIR "/bridges/") + || loplugin::hasPathnamePrefix(fn, SRCDIR "/binaryurp/") + || loplugin::hasPathnamePrefix(fn, SRCDIR "/stoc/") + || loplugin::hasPathnamePrefix(fn, WORKDIR "/YaccTarget/unoidl/source/sourceprovider-parser.cxx") + // some weird calling through a function pointer + || loplugin::hasPathnamePrefix(fn, SRCDIR "/svtools/source/table/defaultinputhandler.cxx") + || loplugin::hasPathnamePrefix(fn, SRCDIR "/sdext/source/pdfimport/test/pdfunzip.cxx") + // windows only + || loplugin::hasPathnamePrefix(fn, SRCDIR "/basic/source/sbx/sbxdec.cxx") + || loplugin::hasPathnamePrefix(fn, SRCDIR "/sfx2/source/doc/syspath.cxx") + // ignore this for now + || loplugin::hasPathnamePrefix(fn, SRCDIR "/libreofficekit") + // FunctionAddress not working well enough here + || loplugin::hasPathnamePrefix(fn, SRCDIR "/pyuno/source/module/pyuno_struct.cxx") + || loplugin::hasPathnamePrefix(fn, SRCDIR "/pyuno/source/module/pyuno.cxx") + || loplugin::hasPathnamePrefix(fn, SRCDIR "/sw/source/filter/ascii/ascatr.cxx") + // TODO this plugin doesn't handle it well when we take the address of a pointer + || loplugin::hasPathnamePrefix(fn, SRCDIR "/svl/source/misc/sharedstringpool.cxx") + || loplugin::hasPathnamePrefix(fn, SRCDIR "/registry/source/regkey.cxx") + || loplugin::hasPathnamePrefix(fn, SRCDIR "/cppu/source/uno/lbenv.cxx") + || loplugin::hasPathnamePrefix(fn, SRCDIR "/cppuhelper/source/implbase_ex.cxx") + ) + return; + + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + + for (const ParmVarDecl *pParmVarDecl : interestingParamSet) { + auto functionDecl = parmToFunction[pParmVarDecl]; + auto canonicalDecl = functionDecl->getCanonicalDecl(); + if (getFunctionsWithAddressTaken().find(canonicalDecl) + != getFunctionsWithAddressTaken().end()) + { + continue; + } + std::string fname = functionDecl->getQualifiedNameAsString(); + report( + DiagnosticsEngine::Warning, + "this parameter can be const %0", + pParmVarDecl->getBeginLoc()) + << fname << pParmVarDecl->getSourceRange(); + if (canonicalDecl->getLocation() != functionDecl->getLocation()) { + unsigned idx = pParmVarDecl->getFunctionScopeIndex(); + const ParmVarDecl* pOther = canonicalDecl->getParamDecl(idx); + report( + DiagnosticsEngine::Note, + "canonical parameter declaration here", + pOther->getBeginLoc()) + << pOther->getSourceRange(); + } + //functionDecl->dump(); + } + } + + bool TraverseFunctionDecl(FunctionDecl *); + bool TraverseCXXMethodDecl(CXXMethodDecl * f); + bool TraverseCXXConstructorDecl(CXXConstructorDecl * f); + bool VisitDeclRefExpr(const DeclRefExpr *); + bool VisitLambdaExpr(const LambdaExpr*); + +private: + bool CheckTraverseFunctionDecl(FunctionDecl *); + bool checkIfCanBeConst(const Stmt*, const ParmVarDecl*); + // integral or enumeration or const * or const & + bool isOkForParameter(const QualType& qt); + bool isPointerOrReferenceToNonConst(const QualType& qt); + + std::unordered_set<const ParmVarDecl*> interestingParamSet; + std::unordered_map<const ParmVarDecl*, const FunctionDecl*> parmToFunction; + FunctionDecl* currentFunctionDecl = nullptr; +}; + +bool ConstParams::TraverseFunctionDecl(FunctionDecl * functionDecl) +{ + // We cannot short-circuit the traverse here entirely without breaking the + // loplugin::FunctionAddress stuff. + auto prev = currentFunctionDecl; + if (CheckTraverseFunctionDecl(functionDecl)) + currentFunctionDecl = functionDecl; + auto rv = FunctionAddress::TraverseFunctionDecl(functionDecl); + currentFunctionDecl = prev; + return rv; +} +bool ConstParams::TraverseCXXMethodDecl(CXXMethodDecl * f) +{ + auto prev = currentFunctionDecl; + if (CheckTraverseFunctionDecl(f)) + currentFunctionDecl = f; + auto rv = FunctionAddress::TraverseCXXMethodDecl(f); + currentFunctionDecl = prev; + return rv; +} +bool ConstParams::TraverseCXXConstructorDecl(CXXConstructorDecl * f) +{ + auto prev = currentFunctionDecl; + if (CheckTraverseFunctionDecl(f)) + currentFunctionDecl = f; + auto rv = FunctionAddress::TraverseCXXConstructorDecl(f); + currentFunctionDecl = prev; + return rv; +} + +bool ConstParams::CheckTraverseFunctionDecl(FunctionDecl * functionDecl) +{ + if (ignoreLocation(functionDecl) || !functionDecl->isThisDeclarationADefinition()) { + return false; + } + // ignore stuff that forms part of the stable URE interface + if (isInUnoIncludeFile(functionDecl)) { + return false; + } + if (functionDecl->isDeleted()) + return false; + // ignore virtual methods + if (isa<CXXMethodDecl>(functionDecl) + && dyn_cast<CXXMethodDecl>(functionDecl)->isVirtual() ) { + return false; + } + // ignore C main + if (functionDecl->isMain()) { + return false; + } + if (functionDecl->getTemplatedKind() != FunctionDecl::TK_NonTemplate) + return false; + + // ignore the macros from include/tools/link.hxx + auto canonicalDecl = functionDecl->getCanonicalDecl(); + if (compiler.getSourceManager().isMacroBodyExpansion(canonicalDecl->getBeginLoc()) + || compiler.getSourceManager().isMacroArgExpansion(canonicalDecl->getBeginLoc())) { + StringRef name { Lexer::getImmediateMacroName( + canonicalDecl->getBeginLoc(), compiler.getSourceManager(), compiler.getLangOpts()) }; + if (name.startswith("DECL_LINK") || name.startswith("DECL_STATIC_LINK")) + return false; + auto loc2 = compat::getImmediateExpansionRange(compiler.getSourceManager(), canonicalDecl->getBeginLoc()).first; + if (compiler.getSourceManager().isMacroBodyExpansion(loc2)) + { + StringRef name2 { Lexer::getImmediateMacroName( + loc2, compiler.getSourceManager(), compiler.getLangOpts()) }; + if (name2.startswith("DECL_DLLPRIVATE_LINK")) + return false; + } + } + + if (functionDecl->getIdentifier()) + { + StringRef name = functionDecl->getName(); + if ( name == "file_write" + || name == "SalMainPipeExchangeSignal_impl" + || name.startswith("SbRtl_") + || name == "GoNext" + || name == "GoPrevious" + || name.startswith("Read_F_") + // UNO component entry points + || name.endswith("component_getFactory") + || name.endswith("_get_implementation") + // callback for some external code? + || name == "ScAddInAsyncCallBack" + // used as function pointers + || name == "Read_Footnote" + || name == "Read_Field" + || name == "Read_And" + // passed as a LINK<> to another method + || name == "GlobalBasicErrorHdl_Impl" + // template + || name == "extract_throw" || name == "readProp" + // callbacks + || name == "signalDragDropReceived" || name == "signal_column_clicked" || name == "signal_key_press" + ) + return false; + + } + + std::string fqn = functionDecl->getQualifiedNameAsString(); + if ( fqn == "connectivity::jdbc::GlobalRef::set" + || fqn == "(anonymous namespace)::ReorderNotifier::operator()" + || fqn == "static_txtattr_cast") + return false; + + // calculate the ones we want to check + bool foundInterestingParam = false; + for (const ParmVarDecl *pParmVarDecl : functionDecl->parameters()) { + // ignore unused params + if (pParmVarDecl->getName().empty() + || pParmVarDecl->hasAttr<UnusedAttr>()) + continue; + auto const type = loplugin::TypeCheck(pParmVarDecl->getType()); + if (!( type.Pointer().NonConst() + || type.LvalueReference().NonConst())) + continue; + // since we normally can't change typedefs, just ignore them + if (isa<TypedefType>(pParmVarDecl->getType())) + continue; + // some typedefs turn into these + if (isa<DecayedType>(pParmVarDecl->getType())) + continue; + // TODO ignore these for now, has some effects I don't understand + if (type.Pointer().Pointer()) + continue; + // const is meaningless when applied to function pointer types + if (pParmVarDecl->getType()->isFunctionPointerType()) + continue; + interestingParamSet.insert(pParmVarDecl); + parmToFunction[pParmVarDecl] = functionDecl; + foundInterestingParam = true; + } + return foundInterestingParam; +} + +bool ConstParams::VisitDeclRefExpr( const DeclRefExpr* declRefExpr ) +{ + if (!currentFunctionDecl) + return true; + const ParmVarDecl* parmVarDecl = dyn_cast_or_null<ParmVarDecl>(declRefExpr->getDecl()); + if (!parmVarDecl) + return true; + if (interestingParamSet.find(parmVarDecl) == interestingParamSet.end()) + return true; + if (!checkIfCanBeConst(declRefExpr, parmVarDecl)) + interestingParamSet.erase(parmVarDecl); + return true; +} + +bool ConstParams::VisitLambdaExpr(const LambdaExpr* lambdaExpr) +{ + if (ignoreLocation(lambdaExpr)) + return true; + for (auto captureIt = lambdaExpr->capture_begin(); captureIt != lambdaExpr->capture_end(); + ++captureIt) + { + const LambdaCapture& capture = *captureIt; + if (capture.capturesVariable()) + { + if (auto varDecl = dyn_cast<ParmVarDecl>(capture.getCapturedVar())) + interestingParamSet.erase(varDecl); + } + } + return true; +} + +// Walk up from a statement that contains a DeclRefExpr, checking if the usage means that the +// related ParamVarDecl can be const. +bool ConstParams::checkIfCanBeConst(const Stmt* stmt, const ParmVarDecl* parmVarDecl) +{ + const Stmt* parent = getParentStmt( stmt ); + if (!parent) + { + // check if we're inside a CXXCtorInitializer + auto parentsRange = compiler.getASTContext().getParents(*stmt); + auto it = parentsRange.begin(); + if ( parentsRange.begin() != parentsRange.end()) + { + const Decl *decl = it->get<Decl>(); + if (auto cxxConstructorDecl = dyn_cast_or_null<CXXConstructorDecl>(decl)) + { + for ( auto cxxCtorInitializer : cxxConstructorDecl->inits()) + { + if ( cxxCtorInitializer->getInit() == stmt) + { + if (cxxCtorInitializer->isAnyMemberInitializer()) + { + // if the member is not pointer-to-const or ref-to-const or value, we cannot make the param const + auto fieldDecl = cxxCtorInitializer->getAnyMember(); + auto tc = loplugin::TypeCheck(fieldDecl->getType()); + if (tc.Pointer() || tc.LvalueReference()) + return tc.Pointer().Const() || tc.LvalueReference().Const(); + else + return true; + } + else + { + // probably base initialiser, but no simple way to look up the relevant constructor decl + return false; + } + } + } + } + if (auto varDecl = dyn_cast_or_null<VarDecl>(decl)) + { + return isOkForParameter(varDecl->getType()); + } + } +// parmVarDecl->dump(); +// stmt->dump(); +// report( +// DiagnosticsEngine::Warning, +// "no parent?", +// stmt->getBeginLoc()) +// << stmt->getSourceRange(); + return false; + } + + if (auto unaryOperator = dyn_cast<UnaryOperator>(parent)) { + UnaryOperator::Opcode op = unaryOperator->getOpcode(); + if (op == UO_PreInc || op == UO_PostInc + || op == UO_PreDec || op == UO_PostDec) { + return false; + } + if (op == UO_Deref || op == UO_AddrOf) { + return checkIfCanBeConst(parent, parmVarDecl); + } + return true; + } else if (auto binaryOp = dyn_cast<BinaryOperator>(parent)) { + BinaryOperator::Opcode op = binaryOp->getOpcode(); + if (binaryOp->getRHS() == stmt && op == BO_Assign) { + return isOkForParameter(binaryOp->getLHS()->getType()); + } + if (binaryOp->getRHS() == stmt) { + return true; + } + if (op == BO_Assign || op == BO_PtrMemD || op == BO_PtrMemI || op == BO_MulAssign + || op == BO_DivAssign || op == BO_RemAssign || op == BO_AddAssign + || op == BO_SubAssign || op == BO_ShlAssign || op == BO_ShrAssign + || op == BO_AndAssign || op == BO_XorAssign || op == BO_OrAssign) { + return false; + } + // for pointer arithmetic need to check parent + if (binaryOp->getType()->isPointerType()) { + return checkIfCanBeConst(parent, parmVarDecl); + } + return true; + } else if (auto constructExpr = dyn_cast<CXXConstructExpr>(parent)) { + const CXXConstructorDecl * constructorDecl = constructExpr->getConstructor(); + for (unsigned i = 0; i < constructExpr->getNumArgs(); ++i) { + if (constructExpr->getArg(i) == stmt) { + return isOkForParameter(constructorDecl->getParamDecl(i)->getType()); + } + } + } else if (auto operatorCallExpr = dyn_cast<CXXOperatorCallExpr>(parent)) { + const CXXMethodDecl* calleeMethodDecl = dyn_cast_or_null<CXXMethodDecl>(operatorCallExpr->getDirectCallee()); + if (calleeMethodDecl) { + // unary operator + if (calleeMethodDecl->getNumParams() == 0) + return calleeMethodDecl->isConst(); + // Same logic as CXXOperatorCallExpr::isAssignmentOp(), which our supported clang + // doesn't have yet. + auto Opc = operatorCallExpr->getOperator(); + if (Opc == OO_Equal || Opc == OO_StarEqual || + Opc == OO_SlashEqual || Opc == OO_PercentEqual || + Opc == OO_PlusEqual || Opc == OO_MinusEqual || + Opc == OO_LessLessEqual || Opc == OO_GreaterGreaterEqual || + Opc == OO_AmpEqual || Opc == OO_CaretEqual || + Opc == OO_PipeEqual) + { + if (operatorCallExpr->getArg(0) == stmt) // assigning to the param + return false; + // not all operator= take a const& + return isOkForParameter(calleeMethodDecl->getParamDecl(0)->getType()); + } + if (operatorCallExpr->getOperator() == OO_Subscript && operatorCallExpr->getArg(1) == stmt) + return true; + if (operatorCallExpr->getOperator() == OO_EqualEqual || operatorCallExpr->getOperator() == OO_ExclaimEqual) + return true; + // binary operator + if (operatorCallExpr->getArg(0) == stmt) + return calleeMethodDecl->isConst(); + unsigned const n = std::min( + operatorCallExpr->getNumArgs(), + calleeMethodDecl->getNumParams() + 1); + for (unsigned i = 1; i < n; ++i) + if (operatorCallExpr->getArg(i) == stmt) { + auto qt = calleeMethodDecl->getParamDecl(i - 1)->getType(); + return isOkForParameter(qt); + } + } else { + const Expr* callee = operatorCallExpr->getCallee()->IgnoreParenImpCasts(); + const DeclRefExpr* dr = dyn_cast<DeclRefExpr>(callee); + const FunctionDecl* calleeFunctionDecl = nullptr; + if (dr) { + calleeFunctionDecl = dyn_cast<FunctionDecl>(dr->getDecl()); + } + if (calleeFunctionDecl) { + for (unsigned i = 0; i < operatorCallExpr->getNumArgs(); ++i) { + if (operatorCallExpr->getArg(i) == stmt) { + return isOkForParameter(calleeFunctionDecl->getParamDecl(i)->getType()); + } + } + } + } + return false; + } else if (auto callExpr = dyn_cast<CallExpr>(parent)) { + QualType functionType = callExpr->getCallee()->getType(); + if (functionType->isFunctionPointerType()) { + functionType = functionType->getPointeeType(); + } + if (const FunctionProtoType* prototype = functionType->getAs<FunctionProtoType>()) { + // TODO could do better + if (prototype->isVariadic()) { + return false; + } + if (callExpr->getCallee() == stmt) { + return true; + } + for (unsigned i = 0; i < callExpr->getNumArgs(); ++i) { + if (callExpr->getArg(i) == stmt) { + return isOkForParameter(prototype->getParamType(i)); + } + } + } + const FunctionDecl* calleeFunctionDecl = callExpr->getDirectCallee(); + if (calleeFunctionDecl) + { + if (auto memberCallExpr = dyn_cast<CXXMemberCallExpr>(parent)) { + const MemberExpr* memberExpr = dyn_cast<MemberExpr>(stmt); + if (memberExpr && memberCallExpr->getImplicitObjectArgument() == memberExpr->getBase()) + { + const CXXMethodDecl* calleeMethodDecl = dyn_cast<CXXMethodDecl>(calleeFunctionDecl); + return calleeMethodDecl->isConst(); + } + } + // TODO could do better + if (calleeFunctionDecl->isVariadic()) { + return false; + } + if (callExpr->getCallee() == stmt) { + return true; + } + for (unsigned i = 0; i < callExpr->getNumArgs(); ++i) { + if (i >= calleeFunctionDecl->getNumParams()) // can happen in template code + return false; + if (callExpr->getArg(i) == stmt) { + return isOkForParameter(calleeFunctionDecl->getParamDecl(i)->getType()); + } + } + } + return false; + } else if (auto callExpr = dyn_cast<ObjCMessageExpr>(parent)) { + if (callExpr->getInstanceReceiver() == stmt) { + return true; + } + if (auto const method = callExpr->getMethodDecl()) { + // TODO could do better + if (method->isVariadic()) { + return false; + } + assert(method->param_size() == callExpr->getNumArgs()); + for (unsigned i = 0; i < callExpr->getNumArgs(); ++i) { + if (callExpr->getArg(i) == stmt) { + return isOkForParameter( + method->param_begin()[i]->getType()); + } + } + } + } else if (isa<CXXReinterpretCastExpr>(parent)) { + return false; + } else if (isa<CXXConstCastExpr>(parent)) { + return false; + } else if (isa<CastExpr>(parent)) { // all other cast expression subtypes + if (auto e = dyn_cast<ExplicitCastExpr>(parent)) { + if (loplugin::TypeCheck(e->getTypeAsWritten()).Void()) { + if (auto const sub = dyn_cast<DeclRefExpr>( + e->getSubExpr()->IgnoreParenImpCasts())) + { + if (sub->getDecl() == parmVarDecl) + return false; + } + } + } + return checkIfCanBeConst(parent, parmVarDecl); + } else if (isa<MemberExpr>(parent)) { + return checkIfCanBeConst(parent, parmVarDecl); + } else if (auto arraySubscriptExpr = dyn_cast<ArraySubscriptExpr>(parent)) { + if (arraySubscriptExpr->getIdx() == stmt) + return true; + return checkIfCanBeConst(parent, parmVarDecl); + } else if (isa<ParenExpr>(parent)) { + return checkIfCanBeConst(parent, parmVarDecl); + } else if (isa<DeclStmt>(parent)) { + // TODO could do better here, but would require tracking the target(s) + //return false; + } else if (isa<ReturnStmt>(parent)) { + return !isPointerOrReferenceToNonConst(currentFunctionDecl->getReturnType()); + } else if (isa<InitListExpr>(parent)) { + return false; + } else if (isa<IfStmt>(parent)) { + return true; + } else if (isa<WhileStmt>(parent)) { + return true; + } else if (isa<ForStmt>(parent)) { + return true; + } else if (isa<CompoundStmt>(parent)) { + return true; + } else if (isa<SwitchStmt>(parent)) { + return true; + } else if (isa<DoStmt>(parent)) { + return true; + } else if (isa<CXXDeleteExpr>(parent)) { + return false; + } else if (isa<VAArgExpr>(parent)) { + return false; + } else if (isa<CXXDependentScopeMemberExpr>(parent)) { + return false; + } else if (isa<MaterializeTemporaryExpr>(parent)) { + return checkIfCanBeConst(parent, parmVarDecl); + } else if (auto conditionalExpr = dyn_cast<ConditionalOperator>(parent)) { + if (conditionalExpr->getCond() == stmt) + return true; + return checkIfCanBeConst(parent, parmVarDecl); + } else if (isa<UnaryExprOrTypeTraitExpr>(parent)) { + return false; // ??? + } else if (auto cxxNewExpr = dyn_cast<CXXNewExpr>(parent)) { + for (unsigned i = 0; i < cxxNewExpr->getNumPlacementArgs(); ++i) + if (cxxNewExpr->getPlacementArg(i) == stmt) + return false; + return true; // ??? + } else if (auto lambdaExpr = dyn_cast<LambdaExpr>(parent)) { + for (auto it = lambdaExpr->capture_begin(); it != lambdaExpr->capture_end(); ++it) + { + if (it->capturesVariable() && it->getCapturedVar() == parmVarDecl) + return it->getCaptureKind() != LCK_ByRef; + } + return false; + } else if (isa<CXXTypeidExpr>(parent)) { + return true; + } else if (isa<ParenListExpr>(parent)) { + return false; // could be improved, seen in constructors when calling base class constructor + } else if (isa<CXXUnresolvedConstructExpr>(parent)) { + return false; + } else if (isa<UnresolvedMemberExpr>(parent)) { + return false; + } else if (isa<PackExpansionExpr>(parent)) { + return false; + } else if (isa<ExprWithCleanups>(parent)) { + return checkIfCanBeConst(parent, parmVarDecl); + } else if (isa<CaseStmt>(parent)) { + return true; + } else if (isa<CXXPseudoDestructorExpr>(parent)) { + return false; + } else if (isa<CXXDependentScopeMemberExpr>(parent)) { + return false; + } else if (isa<ObjCIvarRefExpr>(parent)) { + return checkIfCanBeConst(parent, parmVarDecl); + } + parent->dump(); + parmVarDecl->dump(); + report( + DiagnosticsEngine::Warning, + "oh dear, what can the matter be?", + parent->getBeginLoc()) + << parent->getSourceRange(); + return true; +} + +bool ConstParams::isOkForParameter(const QualType& qt) { + if (qt->isIntegralOrEnumerationType()) + return true; + auto const type = loplugin::TypeCheck(qt); + if (type.Pointer()) { + return bool(type.Pointer().Const()); + } else if (type.LvalueReference().Const().Pointer()) { + // If we have a method that takes (T* t) and it calls std::vector<T*>::push_back + // then the type of push_back is T * const & + // There is probably a more elegant way to check this, but it will probably require + // recalculating types while walking up the AST. + return false; + } else if (type.LvalueReference()) { + return bool(type.LvalueReference().Const()); + } + return false; +} + +bool ConstParams::isPointerOrReferenceToNonConst(const QualType& qt) { + auto const type = loplugin::TypeCheck(qt); + if (type.Pointer()) { + return !bool(type.Pointer().Const()); + } else if (type.LvalueReference()) { + return !bool(type.LvalueReference().Const()); + } + return false; +} + +loplugin::Plugin::Registration< ConstParams > X("constparams", false); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/constvars.cxx b/compilerplugins/clang/store/constvars.cxx new file mode 100644 index 000000000000..2b06f54ea343 --- /dev/null +++ b/compilerplugins/clang/store/constvars.cxx @@ -0,0 +1,558 @@ +/* -*- 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/. + */ + +#if !defined _WIN32 //TODO, #include <sys/file.h> + +#include <cassert> +#include <string> +#include <iostream> +#include <fstream> +#include <unordered_set> +#include <vector> +#include <algorithm> +#include <sys/file.h> +#include <unistd.h> + +#include "plugin.hxx" +#include "check.hxx" + +#include "clang/AST/ParentMapContext.h" + +/** +Look for static vars that are only assigned to once, and never written to, they can be const. +*/ + +namespace +{ +/** + * Wrap the different kinds of callable and callee objects in the clang AST so I can define methods that handle everything. + */ +class CallerWrapper +{ + const CallExpr* m_callExpr; + const CXXConstructExpr* m_cxxConstructExpr; + +public: + CallerWrapper(const CallExpr* callExpr) + : m_callExpr(callExpr) + , m_cxxConstructExpr(nullptr) + { + } + CallerWrapper(const CXXConstructExpr* cxxConstructExpr) + : m_callExpr(nullptr) + , m_cxxConstructExpr(cxxConstructExpr) + { + } + unsigned getNumArgs() const + { + return m_callExpr ? m_callExpr->getNumArgs() : m_cxxConstructExpr->getNumArgs(); + } + const Expr* getArg(unsigned i) const + { + return m_callExpr ? m_callExpr->getArg(i) : m_cxxConstructExpr->getArg(i); + } +}; +class CalleeWrapper +{ + const FunctionDecl* m_calleeFunctionDecl = nullptr; + const CXXConstructorDecl* m_cxxConstructorDecl = nullptr; + const FunctionProtoType* m_functionPrototype = nullptr; + +public: + explicit CalleeWrapper(const FunctionDecl* calleeFunctionDecl) + : m_calleeFunctionDecl(calleeFunctionDecl) + { + } + explicit CalleeWrapper(const CXXConstructExpr* cxxConstructExpr) + : m_cxxConstructorDecl(cxxConstructExpr->getConstructor()) + { + } + explicit CalleeWrapper(const FunctionProtoType* functionPrototype) + : m_functionPrototype(functionPrototype) + { + } + unsigned getNumParams() const + { + if (m_calleeFunctionDecl) + return m_calleeFunctionDecl->getNumParams(); + else if (m_cxxConstructorDecl) + return m_cxxConstructorDecl->getNumParams(); + else if (m_functionPrototype->param_type_begin() == m_functionPrototype->param_type_end()) + // FunctionProtoType will assert if we call getParamTypes() and it has no params + return 0; + else + return m_functionPrototype->getParamTypes().size(); + } + const QualType getParamType(unsigned i) const + { + if (m_calleeFunctionDecl) + return m_calleeFunctionDecl->getParamDecl(i)->getType(); + else if (m_cxxConstructorDecl) + return m_cxxConstructorDecl->getParamDecl(i)->getType(); + else + return m_functionPrototype->getParamTypes()[i]; + } + std::string getNameAsString() const + { + if (m_calleeFunctionDecl) + return m_calleeFunctionDecl->getNameAsString(); + else if (m_cxxConstructorDecl) + return m_cxxConstructorDecl->getNameAsString(); + else + return ""; + } + CXXMethodDecl const* getAsCXXMethodDecl() const + { + if (m_calleeFunctionDecl) + return dyn_cast<CXXMethodDecl>(m_calleeFunctionDecl); + return nullptr; + } +}; + +class ConstVars : public RecursiveASTVisitor<ConstVars>, public loplugin::Plugin +{ +public: + explicit ConstVars(loplugin::InstantiationData const& data) + : Plugin(data) + { + } + + virtual void run() override; + + bool shouldVisitTemplateInstantiations() const { return true; } + bool shouldVisitImplicitCode() const { return true; } + + bool VisitVarDecl(const VarDecl*); + bool VisitCXXForRangeStmt(const CXXForRangeStmt*); + bool VisitDeclRefExpr(const DeclRefExpr*); + bool TraverseCXXConstructorDecl(CXXConstructorDecl*); + bool TraverseCXXMethodDecl(CXXMethodDecl*); + bool TraverseFunctionDecl(FunctionDecl*); + bool TraverseIfStmt(IfStmt*); + +private: + void check(const VarDecl* varDecl, const Expr* memberExpr); + bool isSomeKindOfZero(const Expr* arg); + bool IsPassedByNonConst(const VarDecl* varDecl, const Stmt* child, CallerWrapper callExpr, + CalleeWrapper calleeFunctionDecl); + llvm::Optional<CalleeWrapper> getCallee(CallExpr const*); + + RecordDecl* insideMoveOrCopyDeclParent = nullptr; + // For reasons I do not understand, parentFunctionDecl() is not reliable, so + // we store the parent function on the way down the AST. + FunctionDecl* insideFunctionDecl = nullptr; + std::vector<VarDecl const*> insideConditionalCheckOfVarSet; + + std::set<VarDecl const*> cannotBeConstSet; + std::set<VarDecl const*> definitionSet; +}; + +void ConstVars::run() +{ + // clang::Expr::isCXX11ConstantExpr only works for C++ + if (!compiler.getLangOpts().CPlusPlus) + return; + + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + + SourceManager& SM = compiler.getSourceManager(); + for (VarDecl const* v : definitionSet) + { + if (cannotBeConstSet.find(v) != cannotBeConstSet.end()) + continue; + llvm::StringRef sourceString(SM.getCharacterData(v->getSourceRange().getEnd()), 50); + // Implement a marker that disables this plugins warning at a specific site + if (sourceString.contains("loplugin:constvars:ignore")) + continue; + report(DiagnosticsEngine::Warning, "var can be const", v->getBeginLoc()); + } +} + +bool ConstVars::VisitVarDecl(const VarDecl* varDecl) +{ + varDecl = varDecl->getCanonicalDecl(); + if (varDecl->getLocation().isValid() && ignoreLocation(varDecl)) + return true; + if (!varDecl->hasGlobalStorage()) + return true; + if (isa<ParmVarDecl>(varDecl)) + return true; + if (varDecl->getLinkageAndVisibility().getLinkage() == ExternalLinkage) + return true; + if (varDecl->getType().isConstQualified()) + return true; + if (isa<ConstantArrayType>(varDecl->getType())) + return true; + if (loplugin::TypeCheck(varDecl->getType()).Pointer().Const()) + return true; + // ignore stuff that forms part of the stable URE interface + if (isInUnoIncludeFile(compiler.getSourceManager().getSpellingLoc(varDecl->getLocation()))) + return true; + + if (!varDecl->getInit()) + return true; + if (varDecl->getInit()->isInstantiationDependent()) + return true; + if (!varDecl->getInit()->isCXX11ConstantExpr(compiler.getASTContext())) + return true; + + definitionSet.insert(varDecl); + return true; +} + +bool ConstVars::VisitCXXForRangeStmt(const CXXForRangeStmt* forStmt) +{ + if (forStmt->getBeginLoc().isValid() && ignoreLocation(forStmt)) + return true; + const VarDecl* varDecl = forStmt->getLoopVariable(); + if (!varDecl) + return true; + // we don't handle structured assignment properly + if (isa<DecompositionDecl>(varDecl)) + return true; + auto tc = loplugin::TypeCheck(varDecl->getType()); + if (!tc.LvalueReference()) + return true; + if (tc.LvalueReference().Const()) + return true; + + definitionSet.insert(varDecl); + return true; +} + +bool ConstVars::TraverseCXXConstructorDecl(CXXConstructorDecl* cxxConstructorDecl) +{ + auto copy = insideMoveOrCopyDeclParent; + if (!ignoreLocation(cxxConstructorDecl) && cxxConstructorDecl->isThisDeclarationADefinition()) + { + if (cxxConstructorDecl->isCopyOrMoveConstructor()) + insideMoveOrCopyDeclParent = cxxConstructorDecl->getParent(); + } + bool ret = RecursiveASTVisitor::TraverseCXXConstructorDecl(cxxConstructorDecl); + insideMoveOrCopyDeclParent = copy; + return ret; +} + +bool ConstVars::TraverseCXXMethodDecl(CXXMethodDecl* cxxMethodDecl) +{ + auto copy1 = insideMoveOrCopyDeclParent; + auto copy2 = insideFunctionDecl; + if (!ignoreLocation(cxxMethodDecl) && cxxMethodDecl->isThisDeclarationADefinition()) + { + if (cxxMethodDecl->isCopyAssignmentOperator() || cxxMethodDecl->isMoveAssignmentOperator()) + insideMoveOrCopyDeclParent = cxxMethodDecl->getParent(); + } + insideFunctionDecl = cxxMethodDecl; + bool ret = RecursiveASTVisitor::TraverseCXXMethodDecl(cxxMethodDecl); + insideMoveOrCopyDeclParent = copy1; + insideFunctionDecl = copy2; + return ret; +} + +bool ConstVars::TraverseFunctionDecl(FunctionDecl* functionDecl) +{ + auto copy2 = insideFunctionDecl; + insideFunctionDecl = functionDecl; + bool ret = RecursiveASTVisitor::TraverseFunctionDecl(functionDecl); + insideFunctionDecl = copy2; + return ret; +} + +bool ConstVars::TraverseIfStmt(IfStmt* ifStmt) +{ + VarDecl const* varDecl = nullptr; + if (Expr const* cond = ifStmt->getCond()) + { + if (auto declRefExpr = dyn_cast<DeclRefExpr>(cond->IgnoreParenImpCasts())) + { + if ((varDecl = dyn_cast<VarDecl>(declRefExpr->getDecl()))) + insideConditionalCheckOfVarSet.push_back(varDecl); + } + } + bool ret = RecursiveASTVisitor::TraverseIfStmt(ifStmt); + if (varDecl) + insideConditionalCheckOfVarSet.pop_back(); + return ret; +} + +bool ConstVars::VisitDeclRefExpr(const DeclRefExpr* declRefExpr) +{ + const VarDecl* varDecl = dyn_cast<VarDecl>(declRefExpr->getDecl()); + if (!varDecl) + return true; + varDecl = varDecl->getCanonicalDecl(); + if (ignoreLocation(varDecl)) + return true; + // ignore stuff that forms part of the stable URE interface + if (isInUnoIncludeFile(compiler.getSourceManager().getSpellingLoc(varDecl->getLocation()))) + return true; + + if (definitionSet.find(varDecl) != definitionSet.end()) + check(varDecl, declRefExpr); + + return true; +} + +void ConstVars::check(const VarDecl* varDecl, const Expr* memberExpr) +{ + auto parentsRange = compiler.getASTContext().getParents(*memberExpr); + const Stmt* child = memberExpr; + const Stmt* parent + = parentsRange.begin() == parentsRange.end() ? nullptr : parentsRange.begin()->get<Stmt>(); + + // walk up the tree until we find something interesting + + bool bCannotBeConst = false; + bool bDump = false; + auto walkUp = [&]() { + child = parent; + auto parentsRange = compiler.getASTContext().getParents(*parent); + parent = parentsRange.begin() == parentsRange.end() ? nullptr + : parentsRange.begin()->get<Stmt>(); + }; + do + { + if (!parent) + { + // check if we have an expression like + // int& r = var; + auto parentsRange = compiler.getASTContext().getParents(*child); + if (parentsRange.begin() != parentsRange.end()) + { + auto varDecl = dyn_cast_or_null<VarDecl>(parentsRange.begin()->get<Decl>()); + if (varDecl) + { + if (varDecl->isImplicit()) + { + // so we can walk up from inside a for-range stmt + parentsRange = compiler.getASTContext().getParents(*varDecl); + if (parentsRange.begin() != parentsRange.end()) + parent = parentsRange.begin()->get<Stmt>(); + } + else if (loplugin::TypeCheck(varDecl->getType()).LvalueReference().NonConst()) + { + bCannotBeConst = true; + break; + } + } + } + } + if (!parent) + break; + if (isa<CXXReinterpretCastExpr>(parent)) + { + // once we see one of these, there is not much useful we can know + bCannotBeConst = true; + break; + } + else if (isa<CastExpr>(parent) || isa<MemberExpr>(parent) || isa<ParenExpr>(parent) + || isa<ParenListExpr>(parent) || isa<ArrayInitLoopExpr>(parent) + || isa<ExprWithCleanups>(parent)) + { + walkUp(); + } + else if (auto unaryOperator = dyn_cast<UnaryOperator>(parent)) + { + UnaryOperator::Opcode op = unaryOperator->getOpcode(); + if (op == UO_AddrOf || op == UO_PostInc || op == UO_PostDec || op == UO_PreInc + || op == UO_PreDec) + { + bCannotBeConst = true; + } + walkUp(); + } + else if (auto operatorCallExpr = dyn_cast<CXXOperatorCallExpr>(parent)) + { + auto callee = getCallee(operatorCallExpr); + if (callee) + { + // if calling a non-const operator on the var + auto calleeMethodDecl = callee->getAsCXXMethodDecl(); + if (calleeMethodDecl && operatorCallExpr->getArg(0) == child + && !calleeMethodDecl->isConst()) + { + bCannotBeConst = true; + } + else if (IsPassedByNonConst(varDecl, child, operatorCallExpr, *callee)) + { + bCannotBeConst = true; + } + } + else + bCannotBeConst = true; // conservative, could improve + walkUp(); + } + else if (auto cxxMemberCallExpr = dyn_cast<CXXMemberCallExpr>(parent)) + { + const CXXMethodDecl* calleeMethodDecl = cxxMemberCallExpr->getMethodDecl(); + if (calleeMethodDecl) + { + // if calling a non-const method on the var + const Expr* tmp = dyn_cast<Expr>(child); + if (tmp->isBoundMemberFunction(compiler.getASTContext())) + { + tmp = dyn_cast<MemberExpr>(tmp)->getBase(); + } + if (cxxMemberCallExpr->getImplicitObjectArgument() == tmp + && !calleeMethodDecl->isConst()) + { + bCannotBeConst = true; + break; + } + if (IsPassedByNonConst(varDecl, child, cxxMemberCallExpr, + CalleeWrapper(calleeMethodDecl))) + bCannotBeConst = true; + } + else + bCannotBeConst = true; // can happen in templates + walkUp(); + } + else if (auto cxxConstructExpr = dyn_cast<CXXConstructExpr>(parent)) + { + if (IsPassedByNonConst(varDecl, child, cxxConstructExpr, + CalleeWrapper(cxxConstructExpr))) + bCannotBeConst = true; + walkUp(); + } + else if (auto callExpr = dyn_cast<CallExpr>(parent)) + { + auto callee = getCallee(callExpr); + if (callee) + { + if (IsPassedByNonConst(varDecl, child, callExpr, *callee)) + bCannotBeConst = true; + } + else + bCannotBeConst = true; // conservative, could improve + walkUp(); + } + else if (auto binaryOp = dyn_cast<BinaryOperator>(parent)) + { + BinaryOperator::Opcode op = binaryOp->getOpcode(); + const bool assignmentOp = op == BO_Assign || op == BO_MulAssign || op == BO_DivAssign + || op == BO_RemAssign || op == BO_AddAssign + || op == BO_SubAssign || op == BO_ShlAssign + || op == BO_ShrAssign || op == BO_AndAssign + || op == BO_XorAssign || op == BO_OrAssign; + if (assignmentOp) + { + if (binaryOp->getLHS() == child) + bCannotBeConst = true; + else if (loplugin::TypeCheck(binaryOp->getLHS()->getType()) + .LvalueReference() + .NonConst()) + // if the LHS is a non-const reference, we could write to the var later on + bCannotBeConst = true; + } + walkUp(); + } + else if (isa<ReturnStmt>(parent)) + { + if (insideFunctionDecl) + { + auto tc = loplugin::TypeCheck(insideFunctionDecl->getReturnType()); + if (tc.LvalueReference().NonConst()) + bCannotBeConst = true; + } + break; + } + else if (auto rangeStmt = dyn_cast<CXXForRangeStmt>(parent)) + { + if (rangeStmt->getRangeStmt() == child) + { + auto tc = loplugin::TypeCheck(rangeStmt->getLoopVariable()->getType()); + if (tc.LvalueReference().NonConst()) + bCannotBeConst = true; + } + break; + } + else if (isa<SwitchStmt>(parent) || isa<WhileStmt>(parent) || isa<ForStmt>(parent) + || isa<IfStmt>(parent) || isa<DoStmt>(parent) || isa<DefaultStmt>(parent)) + { + break; + } + else + { + walkUp(); + } + } while (true); + + if (bDump) + { + report(DiagnosticsEngine::Warning, "oh dear, what can the matter be? writtenTo=%0", + memberExpr->getBeginLoc()) + << bCannotBeConst << memberExpr->getSourceRange(); + if (parent) + { + report(DiagnosticsEngine::Note, "parent over here", parent->getBeginLoc()) + << parent->getSourceRange(); + parent->dump(); + } + memberExpr->dump(); + varDecl->getType()->dump(); + } + + if (bCannotBeConst) + cannotBeConstSet.insert(varDecl); +} + +bool ConstVars::IsPassedByNonConst(const VarDecl* varDecl, const Stmt* child, + CallerWrapper callExpr, CalleeWrapper calleeFunctionDecl) +{ + unsigned len = std::min(callExpr.getNumArgs(), calleeFunctionDecl.getNumParams()); + // if it's an array, passing it by value to a method typically means the + // callee takes a pointer and can modify the array + if (varDecl->getType()->isConstantArrayType()) + { + for (unsigned i = 0; i < len; ++i) + if (callExpr.getArg(i) == child) + if (loplugin::TypeCheck(calleeFunctionDecl.getParamType(i)).Pointer().NonConst()) + return true; + } + else + { + for (unsigned i = 0; i < len; ++i) + if (callExpr.getArg(i) == child) + { + auto tc = loplugin::TypeCheck(calleeFunctionDecl.getParamType(i)); + if (tc.LvalueReference().NonConst() || tc.Pointer().NonConst()) + return true; + } + } + return false; +} + +llvm::Optional<CalleeWrapper> ConstVars::getCallee(CallExpr const* callExpr) +{ + FunctionDecl const* functionDecl = callExpr->getDirectCallee(); + if (functionDecl) + return CalleeWrapper(functionDecl); + + // Extract the functionprototype from a type + clang::Type const* calleeType = callExpr->getCallee()->getType().getTypePtr(); + if (auto pointerType = calleeType->getUnqualifiedDesugaredType()->getAs<clang::PointerType>()) + { + if (auto prototype = pointerType->getPointeeType() + ->getUnqualifiedDesugaredType() + ->getAs<FunctionProtoType>()) + { + return CalleeWrapper(prototype); + } + } + + return llvm::Optional<CalleeWrapper>(); +} + +/** off by default because it is very expensive, it walks up the AST a lot */ +loplugin::Plugin::Registration<ConstVars> X("constvars", false); +} + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/convertlong.cxx b/compilerplugins/clang/store/convertlong.cxx new file mode 100644 index 000000000000..87b65a43f38b --- /dev/null +++ b/compilerplugins/clang/store/convertlong.cxx @@ -0,0 +1,139 @@ +/* -*- 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 <memory> +#include <cassert> +#include <string> +#include <iostream> +#include <fstream> +#include <set> +#include "config_clang.h" +#include "plugin.hxx" +#include "check.hxx" + +/** + plugin to help to when converting code from + + sal_uIntPtr/sal_uLong/sal_Long/long/unsigned long + + to something more precise. + */ +namespace +{ +class ConvertLong : public loplugin::FilteringPlugin<ConvertLong> +{ +public: + explicit ConvertLong(loplugin::InstantiationData const& data) + : FilteringPlugin(data) + { + } + + virtual void run() override + { + std::string fn(handler.getMainFileName()); + loplugin::normalizeDotDotInFilePath(fn); + // using sal_uIntPtr as in-between type when converting void* to rtl_TextEncoding + if (fn == SRCDIR "/sal/osl/unx/thread.cxx") + return; + // too much magic + if (fn == SRCDIR "/sal/rtl/alloc_arena.cxx") + return; + if (fn == SRCDIR "/sal/rtl/alloc_cache.cxx") + return; + // TODO not sure what is going on here + if (fn == SRCDIR "/tools/source/generic/bigint.cxx") + return; + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + } + + bool VisitVarDecl(VarDecl const*); + bool TraverseFunctionDecl(FunctionDecl*); + +private: + bool isInterestingType(QualType qt); +}; + +bool ConvertLong::TraverseFunctionDecl(FunctionDecl* functionDecl) +{ + // ignore template stuff + if (functionDecl->getTemplatedKind() != FunctionDecl::TK_NonTemplate) + { + return true; + } + return RecursiveASTVisitor::TraverseFunctionDecl(functionDecl); +} + +bool ConvertLong::VisitVarDecl(VarDecl const* varDecl) +{ + if (ignoreLocation(varDecl)) + return true; + StringRef fileName{ getFilenameOfLocation(varDecl->getLocation()) }; + if (loplugin::isSamePathname(fileName, SRCDIR "/include/tools/bigint.hxx")) + return true; + if (loplugin::isSamePathname(fileName, SRCDIR "/include/tools/solar.h")) + return true; + if (!varDecl->hasInit()) + return true; + if (isa<IntegerLiteral>(varDecl->getInit()->IgnoreParenImpCasts())) + return true; + // ignore int x = -1; + if (isa<UnaryOperator>(varDecl->getInit()->IgnoreParenImpCasts())) + return true; + auto lhsType = varDecl->getType(); + auto rhsType = varDecl->getInit()->IgnoreParenImpCasts()->getType(); + if (lhsType.getLocalUnqualifiedType() == rhsType) + return true; + if (!rhsType.getTypePtrOrNull()) + return true; + if (isInterestingType(rhsType)) + return true; + if (!isInterestingType(lhsType)) + return true; + if (rhsType->isFloatingType()) // TODO + return true; + report(DiagnosticsEngine::Warning, "rather replace type of decl %0 with %1", + varDecl->getLocation()) + << lhsType << rhsType << varDecl->getSourceRange(); + //lhsType->dump(); + //varDecl->dump(); + return true; +} + +bool ConvertLong::isInterestingType(QualType qt) +{ + auto tc = loplugin::TypeCheck(qt); + if (tc.Typedef()) + { + TypedefType const* typedefType = qt->getAs<TypedefType>(); + auto name = typedefType->getDecl()->getName(); + if (name == "sal_uLong") + return true; + // because this is a typedef to long on 64-bit Linux + if (name == "sal_Int64" || name == "sal_uInt64" || name.find("size_t") != StringRef::npos) + return false; + } + if (isa<AutoType>(qt.getTypePtr())) + return false; + auto unqual = qt.getUnqualifiedType(); + if (unqual->isSpecificBuiltinType(BuiltinType::Kind::Long) + || unqual->isSpecificBuiltinType(BuiltinType::Kind::ULong)) + { + return true; + } + if (!tc.Typedef()) + return false; + TypedefType const* typedefType = qt->getAs<TypedefType>(); + auto name = typedefType->getDecl()->getName(); + return name == "sal_uIntPtr" || name == "sal_IntPtr"; +} + +loplugin::Plugin::Registration<ConvertLong> X("convertlong", false); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/countusersofdefaultparams.cxx b/compilerplugins/clang/store/countusersofdefaultparams.cxx new file mode 100644 index 000000000000..c336509b3ef6 --- /dev/null +++ b/compilerplugins/clang/store/countusersofdefaultparams.cxx @@ -0,0 +1,250 @@ +/* -*- 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 <string> +#include <set> +#include <iostream> +#include <fstream> + +#include "clang/AST/Attr.h" + +#include "config_clang.h" + +#include "plugin.hxx" + +/* + Count call sites that are actually using the defaulted values on params on methods that declare such. + + The process goes something like this: + $ make check + $ make FORCE_COMPILE=all COMPILER_PLUGIN_TOOL='countusersofdefaultparams' check + $ ./compilerplugins/clang/countusersofdefaultparams.py +*/ + +namespace { + +struct MyFuncInfo +{ + std::string access; + std::string returnType; + std::string nameAndParams; + std::string sourceLocation; +}; +bool operator < (const MyFuncInfo &lhs, const MyFuncInfo &rhs) +{ + return std::tie(lhs.returnType, lhs.nameAndParams) + < std::tie(rhs.returnType, rhs.nameAndParams); +} +struct MyCallInfo : public MyFuncInfo +{ + std::string sourceLocationOfCall; +}; +bool operator < (const MyCallInfo &lhs, const MyCallInfo &rhs) +{ + return std::tie(lhs.returnType, lhs.nameAndParams, lhs.sourceLocationOfCall) + < std::tie(rhs.returnType, rhs.nameAndParams, rhs.sourceLocationOfCall); +} + + +// try to limit the voluminous output a little +static std::set<MyCallInfo> callSet; +static std::set<MyFuncInfo> definitionSet; + +class CountUsersOfDefaultParams: + public RecursiveASTVisitor<CountUsersOfDefaultParams>, public loplugin::Plugin +{ +public: + explicit CountUsersOfDefaultParams(loplugin::InstantiationData const & data): Plugin(data) {} + + virtual void run() override + { + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + + // dump all our output in one write call - this is to try and limit IO "crosstalk" between multiple processes + // writing to the same logfile + + std::string output; + for (const MyFuncInfo & s : definitionSet) + output += "defn:\t" + s.access + "\t" + s.returnType + "\t" + s.nameAndParams + "\t" + s.sourceLocation + "\n"; + for (const MyCallInfo & s : callSet) + output += "call:\t" + s.returnType + "\t" + s.nameAndParams + "\t" + s.sourceLocationOfCall + "\n"; + std::ofstream myfile; + myfile.open( WORKDIR "/loplugin.countusersofdefaultparams.log", std::ios::app | std::ios::out); + myfile << output; + myfile.close(); + } + + bool shouldVisitTemplateInstantiations () const { return true; } + + bool VisitCallExpr( const CallExpr * ); + bool VisitFunctionDecl( const FunctionDecl* ); + bool VisitCXXConstructExpr( const CXXConstructExpr * ); +private: + void niceName(const FunctionDecl* functionDecl, MyFuncInfo&); + std::string locationToString(const SourceLocation&); +}; + +void CountUsersOfDefaultParams::niceName(const FunctionDecl* functionDecl, MyFuncInfo& aInfo) +{ + if (functionDecl->getInstantiatedFromMemberFunction()) + functionDecl = functionDecl->getInstantiatedFromMemberFunction(); + else if (functionDecl->getTemplateInstantiationPattern()) + functionDecl = functionDecl->getTemplateInstantiationPattern(); + + switch (functionDecl->getAccess()) + { + case AS_public: aInfo.access = "public"; break; + case AS_private: aInfo.access = "private"; break; + case AS_protected: aInfo.access = "protected"; break; + default: aInfo.access = "unknown"; break; + } + aInfo.returnType = functionDecl->getReturnType().getCanonicalType().getAsString(); + + if (isa<CXXMethodDecl>(functionDecl)) { + const CXXRecordDecl* recordDecl = dyn_cast<CXXMethodDecl>(functionDecl)->getParent(); + aInfo.nameAndParams += recordDecl->getQualifiedNameAsString(); + aInfo.nameAndParams += "::"; + } + aInfo.nameAndParams += functionDecl->getNameAsString() + "("; + bool bFirst = true; + for (const ParmVarDecl *pParmVarDecl : functionDecl->parameters()) { + if (bFirst) + bFirst = false; + else + aInfo.nameAndParams += ","; + aInfo.nameAndParams += pParmVarDecl->getType().getCanonicalType().getAsString(); + } + aInfo.nameAndParams += ")"; + if (isa<CXXMethodDecl>(functionDecl) && dyn_cast<CXXMethodDecl>(functionDecl)->isConst()) { + aInfo.nameAndParams += " const"; + } + + aInfo.sourceLocation = locationToString(functionDecl->getLocation()); + loplugin::normalizeDotDotInFilePath(aInfo.sourceLocation); +} + +bool CountUsersOfDefaultParams::VisitCallExpr(const CallExpr * callExpr) { + if (ignoreLocation(callExpr)) { + return true; + } + const FunctionDecl* functionDecl; + if (isa<CXXMemberCallExpr>(callExpr)) { + functionDecl = dyn_cast<CXXMemberCallExpr>(callExpr)->getMethodDecl(); + } + else { + functionDecl = callExpr->getDirectCallee(); + } + if (functionDecl == nullptr) { + return true; + } + functionDecl = functionDecl->getCanonicalDecl(); + // work our way up to the root method + while (isa<CXXMethodDecl>(functionDecl)) { + const CXXMethodDecl* methodDecl = dyn_cast<CXXMethodDecl>(functionDecl); + if (methodDecl->size_overridden_methods()==0) + break; + functionDecl = *methodDecl->begin_overridden_methods(); + } + // work our way back to the root definition for template methods + if (functionDecl->getInstantiatedFromMemberFunction()) + functionDecl = functionDecl->getInstantiatedFromMemberFunction(); + else if (functionDecl->getTemplateInstantiationPattern()) + functionDecl = functionDecl->getTemplateInstantiationPattern(); + int n = functionDecl->getNumParams() - 1; + if (n < 0 || !functionDecl->getParamDecl(n)->hasDefaultArg()) { + return true; + } + while (n > 0 && functionDecl->getParamDecl(n-1)->hasDefaultArg()) { + --n; + } + // look for callsites that are actually using the defaulted values + if ( n < (int)callExpr->getNumArgs() && callExpr->getArg(n)->isDefaultArgument()) { + MyCallInfo callInfo; + niceName(functionDecl, callInfo); + callInfo.sourceLocationOfCall = locationToString(callExpr->getBeginLoc()); + callSet.insert(callInfo); + } + return true; +} + +bool CountUsersOfDefaultParams::VisitCXXConstructExpr(const CXXConstructExpr * constructExpr) { + if (ignoreLocation(constructExpr)) { + return true; + } + const CXXConstructorDecl* constructorDecl = constructExpr->getConstructor()->getCanonicalDecl(); + // work our way back to the root definition for template methods + if (constructorDecl->getInstantiatedFromMemberFunction()) + constructorDecl = dyn_cast<CXXConstructorDecl>(constructorDecl->getInstantiatedFromMemberFunction()); + else if (constructorDecl->getTemplateInstantiationPattern()) + constructorDecl = dyn_cast<CXXConstructorDecl>(constructorDecl->getTemplateInstantiationPattern()); + int n = constructorDecl->getNumParams() - 1; + if (n < 0 || !constructorDecl->getParamDecl(n)->hasDefaultArg()) { + return true; + } + while (n > 0 && constructorDecl->getParamDecl(n-1)->hasDefaultArg()) { + --n; + } + // look for callsites that are actually using the defaulted values + if ( n < (int)constructExpr->getNumArgs() && constructExpr->getArg(n)->isDefaultArgument()) { + MyCallInfo callInfo; + niceName(constructorDecl, callInfo); + callInfo.sourceLocationOfCall = locationToString(constructExpr->getBeginLoc()); + callSet.insert(callInfo); + } + return true; +} + +std::string CountUsersOfDefaultParams::locationToString(const SourceLocation& sourceLoc) +{ + SourceLocation expansionLoc = compiler.getSourceManager().getExpansionLoc( sourceLoc ); + StringRef name = getFilenameOfLocation(expansionLoc); + return std::string(name.substr(strlen(SRCDIR)+1)) + ":" + std::to_string(compiler.getSourceManager().getSpellingLineNumber(expansionLoc)); +} + +bool CountUsersOfDefaultParams::VisitFunctionDecl( const FunctionDecl* functionDecl ) +{ + functionDecl = functionDecl->getCanonicalDecl(); + if( !functionDecl || !functionDecl->getLocation().isValid() || ignoreLocation( functionDecl )) { + return true; + } + const CXXMethodDecl* methodDecl = dyn_cast_or_null<CXXMethodDecl>(functionDecl); + + // ignore method overrides, since the call will show up as being directed to the root method + if (methodDecl && (methodDecl->size_overridden_methods() != 0 || methodDecl->hasAttr<OverrideAttr>())) { + return true; + } + // ignore stuff that forms part of the stable URE interface + if (isInUnoIncludeFile(functionDecl)) { + return true; + } + if (isa<CXXDestructorDecl>(functionDecl)) { + return true; + } + if (functionDecl->isDeleted()) { + return true; + } + auto n = functionDecl->getNumParams(); + if (n == 0 || !functionDecl->getParamDecl(n - 1)->hasDefaultArg()) { + return true; + } + + if( functionDecl->getLocation().isValid() ) + { + MyFuncInfo funcInfo; + niceName(functionDecl, funcInfo); + definitionSet.insert(funcInfo); + } + return true; +} + +loplugin::Plugin::Registration< CountUsersOfDefaultParams > X("countusersofdefaultparams", false); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/countusersofdefaultparams.py b/compilerplugins/clang/store/countusersofdefaultparams.py new file mode 100755 index 000000000000..a53c17283c14 --- /dev/null +++ b/compilerplugins/clang/store/countusersofdefaultparams.py @@ -0,0 +1,81 @@ +#!/usr/bin/python + +import sys +import re +import io + +definitionToSourceLocationMap = dict() +callDict = dict() + +# clang does not always use exactly the same numbers in the type-parameter vars it generates +# so I need to substitute them to ensure we can match correctly. +normalizeTypeParamsRegex1 = re.compile(r"type-parameter-\d+-\d+") +normalizeTypeParamsRegex2 = re.compile(r"typename enable_if<.*") +def normalizeTypeParams( line ): + line = normalizeTypeParamsRegex1.sub("type-parameter-?-?", line) + return normalizeTypeParamsRegex2.sub("type-parameter-?-?", line) + +with io.open("workdir/loplugin.countusersofdefaultparams.log", "rb", buffering=1024*1024) as txt: + for line in txt: + tokens = line.strip().split("\t") + if tokens[0] == "defn:": + access = tokens[1] + returnType = tokens[2] + nameAndParams = tokens[3] + sourceLocation = tokens[4] + funcInfo = normalizeTypeParams(returnType) + " " + normalizeTypeParams(nameAndParams) + definitionToSourceLocationMap[funcInfo] = sourceLocation + if not funcInfo in callDict: + callDict[funcInfo] = set() + elif tokens[0] == "call:": + returnType = tokens[1] + nameAndParams = tokens[2] + sourceLocationOfCall = tokens[3] + funcInfo = normalizeTypeParams(returnType) + " " + normalizeTypeParams(nameAndParams) + if not funcInfo in callDict: + callDict[funcInfo] = set() + callDict[funcInfo].add(sourceLocationOfCall) + else: + print( "unknown line: " + line) + +tmp1list = list() +for k,v in callDict.iteritems(): + if len(v) >= 1: + continue + # created by macros + if k.endswith("::RegisterInterface(class SfxModule *)"): + continue + if k.endswith("::RegisterChildWindow(_Bool,class SfxModule *,enum SfxChildWindowFlags)"): + continue + if k.endswith("::RegisterControl(unsigned short,class SfxModule *)"): + continue + if k.endswith("::RegisterFactory(unsigned short)"): + continue + # windows-only stuff + if "ShutdownIcon::OpenURL" in k: + continue + # template magic + if k.startswith("void VclPtr::VclPtr(const VclPtr<type-parameter-?-?> &,typename UpCast<"): + continue + if k in definitionToSourceLocationMap: + tmp1list.append((k, definitionToSourceLocationMap[k])) + +# sort the results using a "natural order" so sequences like [item1,item2,item10] sort nicely +def natural_sort_key(s, _nsre=re.compile('([0-9]+)')): + return [int(text) if text.isdigit() else text.lower() + for text in re.split(_nsre, s)] +# sort by both the source-line and the datatype, so the output file ordering is stable +# when we have multiple items on the same source line +def v_sort_key(v): + return natural_sort_key(v[1]) + [v[0]] + +# sort results by name and line number +tmp1list.sort(key=lambda v: v_sort_key(v)) + +# print out the results +with open("loplugin.countusersofdefaultparams.report", "wt") as f: + for t in tmp1list: + f.write(t[1] + "\n") + f.write(" " + t[0] + "\n") + + diff --git a/compilerplugins/clang/store/dodgyswitch.cxx b/compilerplugins/clang/store/dodgyswitch.cxx new file mode 100644 index 000000000000..43958f1364ad --- /dev/null +++ b/compilerplugins/clang/store/dodgyswitch.cxx @@ -0,0 +1,78 @@ +/* -*- 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 <cassert> +#include <string> +#include <iostream> +#include <fstream> +#include <set> +#include "plugin.hxx" + +namespace { + +class DodgySwitch: + public loplugin::FilteringPlugin<DodgySwitch> +{ +public: + explicit DodgySwitch(loplugin::InstantiationData const & data): FilteringPlugin(data) + {} + + virtual void run() override + { + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + } + + bool VisitDefaultStmt(DefaultStmt const * ); + bool VisitCaseStmt(CaseStmt const * ); +private: + bool IsParentSwitch(Stmt const * ); +}; + +bool DodgySwitch::VisitDefaultStmt(DefaultStmt const * defaultStmt) +{ + if (ignoreLocation(defaultStmt)) + return true; + if (!IsParentSwitch(defaultStmt)) + report( + DiagnosticsEngine::Warning, "default statement not directly under switch", + defaultStmt->getBeginLoc()) + << defaultStmt->getSourceRange(); + return true; +} + +bool DodgySwitch::VisitCaseStmt(CaseStmt const * caseStmt) +{ + if (ignoreLocation(caseStmt)) + return true; + if (!IsParentSwitch(caseStmt)) + { + //parentStmt(parentStmt(caseStmt))->dump(); + report( + DiagnosticsEngine::Warning, "case statement not directly under switch", + caseStmt->getBeginLoc()) + << caseStmt->getSourceRange(); + } + return true; +} + +bool DodgySwitch::IsParentSwitch(Stmt const * stmt) +{ + auto parent = getParentStmt(stmt); + if (isa<CaseStmt>(parent) || isa<DefaultStmt>(parent)) // daisy chain + return true; + auto compoundStmt = dyn_cast<CompoundStmt>(parent); + if (!compoundStmt) + return false; + return isa<SwitchStmt>(getParentStmt(compoundStmt)); +} + +loplugin::Plugin::Registration< DodgySwitch > X("dodgyswitch", false); + +} +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/doubleconvert.cxx b/compilerplugins/clang/store/doubleconvert.cxx new file mode 100644 index 000000000000..6f9cc88df742 --- /dev/null +++ b/compilerplugins/clang/store/doubleconvert.cxx @@ -0,0 +1,83 @@ +/* -*- 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" + +/** + * Look for places where we are converting from type A through a conversion operator and back to type A, + * which is redundant. At the moment only look for Color, to aid my ColorData->Color conversion + */ +namespace +{ +class DoubleConvert final : public loplugin::FilteringPlugin<DoubleConvert> +{ +public: + explicit DoubleConvert(loplugin::InstantiationData const& data) + : FilteringPlugin(data) + { + } + void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); } + + bool VisitCXXConstructExpr(CXXConstructExpr const*); +}; + +/** + The AST looks like: + + CXXOperatorCallExpr 0x8e5b840 'class Color' lvalue +|-ImplicitCastExpr 0x8e5b828 'class Color &(*)(class Color &&) noexcept' <FunctionToPointerDecay> +| `-DeclRefExpr 0x8e5b800 'class Color &(class Color &&) noexcept' lvalue CXXMethod 0x8e59a08 'operator=' 'class Color &(class Color &&) noexcept' +|-DeclRefExpr 0x8e5b678 'class Color' lvalue Var 0x8e5b5d0 'col2' 'class Color' +`-MaterializeTemporaryExpr 0x8e5b7e8 'class Color' xvalue + `-CXXConstructExpr 0x8e5b7b0 'class Color' 'void (ColorData)' + `-ImplicitCastExpr 0x8e5b798 'ColorData':'unsigned int' <IntegralCast> + `-CXXFunctionalCastExpr 0x8e5b770 'sal_Int32':'int' functional cast to sal_Int32 <NoOp> + `-ImplicitCastExpr 0x8e5b758 'sal_Int32':'int' <UserDefinedConversion> + `-CXXMemberCallExpr 0x8e5b730 'sal_Int32':'int' + `-MemberExpr 0x8e5b6f8 '<bound member function type>' .operator int 0x8e51048 + `-ImplicitCastExpr 0x8e5b6e0 'const class Color' lvalue <NoOp> + `-DeclRefExpr 0x8e5b6b0 'class Color' lvalue Var 0x8e5b518 'col1' 'class Color' +*/ +bool DoubleConvert::VisitCXXConstructExpr(CXXConstructExpr const* cxxConstruct) +{ + if (ignoreLocation(cxxConstruct)) + return true; + if (cxxConstruct->getNumArgs() == 0) + return true; + auto cxxMemberCallExpr + = dyn_cast<CXXMemberCallExpr>(cxxConstruct->getArg(0)->IgnoreParenCasts()); + if (!cxxMemberCallExpr) + return true; + if (!isa_and_nonnull<CXXConversionDecl>(cxxMemberCallExpr->getMethodDecl())) + return true; + if (cxxConstruct->getType().getCanonicalType().getTypePtr() + != cxxMemberCallExpr->getImplicitObjectArgument() + ->getType() + .getCanonicalType() + .getTypePtr()) + return true; + if (!loplugin::TypeCheck(cxxConstruct->getType().getCanonicalType()) + .Class("Color") + .GlobalNamespace()) + return true; + + report(DiagnosticsEngine::Warning, "redundant double conversion", cxxConstruct->getExprLoc()) + << cxxConstruct->getSourceRange(); + return true; +} + +static loplugin::Plugin::Registration<DoubleConvert> doubleconvert("doubleconvert"); +} + +#endif // LO_CLANG_SHARED_PLUGINS + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/compilerplugins/clang/store/inlinefields.cxx b/compilerplugins/clang/store/inlinefields.cxx new file mode 100644 index 000000000000..1573e8d5217b --- /dev/null +++ b/compilerplugins/clang/store/inlinefields.cxx @@ -0,0 +1,251 @@ +/* -*- 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 <cassert> +#include <string> +#include <iostream> +#include <fstream> +#include <set> +#include "plugin.hxx" +#include "compat.hxx" + +/** +if a field is +- a pointer +- only assigned to in the constructor via a new expression +- unconditionally deleted in the destructor +then it can probably just be allocated inline in the parent object + +TODO check for cases where the pointer is passed by non-const reference + +Be warned that it produces around 5G of log file. + +The process goes something like this: + $ make check + $ make FORCE_COMPILE=all COMPILER_PLUGIN_TOOL='inlinefields' check + $ ./compilerplugins/clang/inlinefields.py + +and then + $ for dir in *; do make FORCE_COMPILE=all UPDATE_FILES=$dir COMPILER_PLUGIN_TOOL='inlinefieldsremove' $dir; done +to auto-remove the method declarations + +Note that the actual process may involve a fair amount of undoing, hand editing, and general messing around +to get it to work :-) + +*/ + +namespace { + +struct MyFieldInfo +{ + std::string parentClass; + std::string fieldName; + std::string sourceLocation; +}; +bool operator < (const MyFieldInfo &lhs, const MyFieldInfo &rhs) +{ + return std::tie(lhs.parentClass, lhs.fieldName) + < std::tie(rhs.parentClass, rhs.fieldName); +} + + +// try to limit the voluminous output a little +static std::set<MyFieldInfo> excludedSet; +static std::set<MyFieldInfo> definitionSet; +static std::set<MyFieldInfo> deletedInDestructorSet; +static std::set<MyFieldInfo> newedInConstructorSet; + +class InlineFields: + public RecursiveASTVisitor<InlineFields>, public loplugin::Plugin +{ +public: + explicit InlineFields(loplugin::InstantiationData const & data): + Plugin(data) {} + + virtual void run() override + { + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + + // dump all our output in one write call - this is to try and limit IO "crosstalk" between multiple processes + // writing to the same logfile + std::string output; + for (const MyFieldInfo & s : definitionSet) + output += "definition:\t" + s.parentClass + "\t" + s.fieldName + "\t" + s.sourceLocation + "\n"; + for (const MyFieldInfo & s : excludedSet) + output += "excluded:\t" + s.parentClass + "\t" + s.fieldName + "\n"; + for (const MyFieldInfo & s : deletedInDestructorSet) + output += "deletedInDestructor:\t" + s.parentClass + "\t" + s.fieldName + "\n"; + for (const MyFieldInfo & s : newedInConstructorSet) + output += "newedInConstructor:\t" + s.parentClass + "\t" + s.fieldName + "\n"; + std::ofstream myfile; + myfile.open( WORKDIR "/loplugin.inlinefields.log", std::ios::app | std::ios::out); + myfile << output; + myfile.close(); + } + + bool shouldVisitTemplateInstantiations () const { return true; } + bool shouldVisitImplicitCode() const { return true; } + + bool VisitFieldDecl( const FieldDecl* ); + bool VisitCXXConstructorDecl( const CXXConstructorDecl* ); + bool VisitCXXDeleteExpr( const CXXDeleteExpr* ); + bool VisitBinaryOperator( const BinaryOperator* ); +private: + MyFieldInfo niceName(const FieldDecl*); + void checkTouched(const FieldDecl* fieldDecl, const Expr* memberExpr); +}; + +MyFieldInfo InlineFields::niceName(const FieldDecl* fieldDecl) +{ + MyFieldInfo aInfo; + + const RecordDecl* recordDecl = fieldDecl->getParent(); + + if (const CXXRecordDecl* cxxRecordDecl = dyn_cast<CXXRecordDecl>(recordDecl)) + { + if (cxxRecordDecl->getTemplateInstantiationPattern()) + cxxRecordDecl = cxxRecordDecl->getTemplateInstantiationPattern(); + aInfo.parentClass = cxxRecordDecl->getQualifiedNameAsString(); + } + else + aInfo.parentClass = recordDecl->getQualifiedNameAsString(); + + aInfo.fieldName = fieldDecl->getNameAsString(); + + SourceLocation expansionLoc = compiler.getSourceManager().getExpansionLoc( fieldDecl->getLocation() ); + StringRef name = getFilenameOfLocation(expansionLoc); + aInfo.sourceLocation = std::string(name.substr(strlen(SRCDIR)+1)) + ":" + std::to_string(compiler.getSourceManager().getSpellingLineNumber(expansionLoc)); + loplugin::normalizeDotDotInFilePath(aInfo.sourceLocation); + + return aInfo; +} + +bool InlineFields::VisitFieldDecl( const FieldDecl* fieldDecl ) +{ + fieldDecl = fieldDecl->getCanonicalDecl(); + if (ignoreLocation( fieldDecl )) { + return true; + } + // ignore stuff that forms part of the stable URE interface + if (isInUnoIncludeFile(compiler.getSourceManager().getSpellingLoc(fieldDecl->getLocation()))) { + return true; + } + QualType type = fieldDecl->getType(); + if (!type->isPointerType()) + return true; + definitionSet.insert(niceName(fieldDecl)); + return true; +} + +bool InlineFields::VisitCXXConstructorDecl( const CXXConstructorDecl* decl ) +{ + if( ignoreLocation( decl ) ) + return true; + if (decl->isCopyOrMoveConstructor()) + return true; + for(auto it = decl->init_begin(); it != decl->init_end(); ++it) + { + const CXXCtorInitializer* init = *it; + const FieldDecl* fieldDecl = init->getMember(); + if( !fieldDecl || !fieldDecl->getType()->isPointerType() ) + continue; + auto e = init->getInit(); + if (auto parentListExpr = dyn_cast<ParenListExpr>(e)) + e = parentListExpr->getExpr(0); + e = e->IgnoreParenImpCasts(); + if( isa<CXXNewExpr>(e) ) + newedInConstructorSet.insert(niceName(fieldDecl)); + else if( isa<CXXNullPtrLiteralExpr>(e) || isa<GNUNullExpr>(e)) + ; // ignore + else + excludedSet.insert(niceName(fieldDecl)); + } + return true; +} + +static bool isSameParent(const CXXMethodDecl* cxxMethodDecl, const FieldDecl* fieldDecl) +{ + return cxxMethodDecl->getParent() == dyn_cast<CXXRecordDecl>(fieldDecl->getParent()); +} + +bool InlineFields::VisitBinaryOperator(const BinaryOperator * binaryOp) +{ + if (binaryOp->getOpcode() != BO_Assign) { + return true; + } + if( ignoreLocation( binaryOp ) ) + return true; + auto memberExpr = dyn_cast<MemberExpr>(binaryOp->getLHS()); + if (!memberExpr) + return true; + auto fieldDecl = dyn_cast<FieldDecl>(memberExpr->getMemberDecl()); + if (!fieldDecl || !fieldDecl->getType()->isPointerType()) { + return true; + } + const FunctionDecl* parentFunction = getParentFunctionDecl(binaryOp); + if (!parentFunction) { + return true; + } + // if the field is being assigned from outside its own constructor or destructor, exclude + auto constructorDecl = dyn_cast<CXXConstructorDecl>(parentFunction); + if (constructorDecl && isSameParent(constructorDecl, fieldDecl)) { + if( isa<CXXNewExpr>(binaryOp->getRHS()) ) + newedInConstructorSet.insert(niceName(fieldDecl)); + else { + excludedSet.insert(niceName(fieldDecl)); + std::cout << "assign in constructor:" << std::endl; + binaryOp->getRHS()->dump(); + } + return true; + } + auto destructorDecl = dyn_cast<CXXDestructorDecl>(parentFunction); + if (destructorDecl && isSameParent(destructorDecl, fieldDecl)) { + auto e = binaryOp->getRHS()->IgnoreParenImpCasts(); + if( !isa<CXXNullPtrLiteralExpr>(e) && !isa<GNUNullExpr>(e)) { + excludedSet.insert(niceName(fieldDecl)); + std::cout << "assign in destructor:" << std::endl; + e->dump(); + } + return true; + } + excludedSet.insert(niceName(fieldDecl)); + return true; +} + +bool InlineFields::VisitCXXDeleteExpr(const CXXDeleteExpr * deleteExpr) +{ + if( ignoreLocation( deleteExpr ) ) + return true; + auto memberExpr = dyn_cast<MemberExpr>(deleteExpr->getArgument()->IgnoreParenImpCasts()); + if (!memberExpr) + return true; + auto fieldDecl = dyn_cast<FieldDecl>(memberExpr->getMemberDecl()); + if (!fieldDecl || !fieldDecl->getType()->isPointerType()) { + return true; + } + // TODO for some reason, this part is not working properly, it doesn't find the parent + // function for delete statements properly + const FunctionDecl* parentFunction = getParentFunctionDecl(deleteExpr); + if (!parentFunction) { + return true; + } + auto destructorDecl = dyn_cast<CXXDestructorDecl>(parentFunction); + if (destructorDecl && isSameParent(destructorDecl, fieldDecl)) { + deletedInDestructorSet.insert(niceName(fieldDecl)); + return true; + } + excludedSet.insert(niceName(fieldDecl)); + return true; +} + +loplugin::Plugin::Registration< InlineFields > X("inlinefields", false); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/inlinefields.py b/compilerplugins/clang/store/inlinefields.py new file mode 100755 index 000000000000..1a0dbda34189 --- /dev/null +++ b/compilerplugins/clang/store/inlinefields.py @@ -0,0 +1,73 @@ +#!/usr/bin/python + +import sys +import re +import io + +definitionToSourceLocationMap = dict() # dict of tuple(parentClass, fieldName) to sourceLocation +definitionSet = set() +excludedSet = set() +deletedInDestructorSet = set() +newedInConstructorSet = set(); + +# clang does not always use exactly the same numbers in the type-parameter vars it generates +# so I need to substitute them to ensure we can match correctly. +normalizeTypeParamsRegex = re.compile(r"type-parameter-\d+-\d+") +def normalizeTypeParams( line ): + return normalizeTypeParamsRegex.sub("type-parameter-?-?", line) + +# reading as binary (since we known it is pure ascii) is much faster than reading as unicode +with io.open("workdir/loplugin.inlinefields.log", "rb", buffering=1024*1024) as txt: + for line in txt: + tokens = line.strip().split("\t") + if tokens[0] == "definition:": + parentClass = normalizeTypeParams(tokens[1]) + fieldName = normalizeTypeParams(tokens[2]) + sourceLocation = tokens[3] + fieldInfo = (parentClass, fieldName) + definitionSet.add(fieldInfo) + definitionToSourceLocationMap[fieldInfo] = sourceLocation + elif tokens[0] == "excluded:": + parentClass = normalizeTypeParams(tokens[1]) + fieldName = normalizeTypeParams(tokens[2]) + fieldInfo = (parentClass, fieldName) + excludedSet.add(fieldInfo) + elif tokens[0] == "deletedInDestructor:": + parentClass = normalizeTypeParams(tokens[1]) + fieldName = normalizeTypeParams(tokens[2]) + fieldInfo = (parentClass, fieldName) + deletedInDestructorSet.add(fieldInfo) + elif tokens[0] == "newedInConstructor:": + parentClass = normalizeTypeParams(tokens[1]) + fieldName = normalizeTypeParams(tokens[2]) + fieldInfo = (parentClass, fieldName) + newedInConstructorSet.add(fieldInfo) + else: + print( "unknown line: " + line) + +tmp1list = list() +for d in definitionSet: +# TODO see comment in InlineFields::VisitCXXDeleteExpr +# if d in excludedSet or d not in deletedInDestructorSet or d not in newedInConstructorSet: + if d in excludedSet or d not in newedInConstructorSet: + continue + srcLoc = definitionToSourceLocationMap[d]; + tmp1list.append((d[0] + " " + d[1], srcLoc)) + +# sort results by filename:lineno +def natural_sort_key(s, _nsre=re.compile('([0-9]+)')): + return [int(text) if text.isdigit() else text.lower() + for text in re.split(_nsre, s)] +# sort by both the source-line and the datatype, so the output file ordering is stable +# when we have multiple items on the same source line +def v_sort_key(v): + return natural_sort_key(v[1]) + [v[0]] +tmp1list.sort(key=lambda v: v_sort_key(v)) + +# print out the results +with open("loplugin.inlinefields.report", "wt") as f: + for v in tmp1list: + f.write(v[1] + "\n") + f.write(" " + v[0] + "\n") + + diff --git a/compilerplugins/clang/store/inlinesimplememberfunctions.cxx b/compilerplugins/clang/store/inlinesimplememberfunctions.cxx new file mode 100644 index 000000000000..760094b5a03a --- /dev/null +++ b/compilerplugins/clang/store/inlinesimplememberfunctions.cxx @@ -0,0 +1,294 @@ +/* -*- 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 <string> + +#include "plugin.hxx" + +// Methods that purely return a local field should be declared in the header and be declared inline. +// So that the compiler can elide the function call and turn it into a simple fixed-offset-load instruction. + +namespace { + +class InlineSimpleMemberFunctions: + public loplugin::FilteringRewritePlugin<InlineSimpleMemberFunctions> +{ +public: + explicit InlineSimpleMemberFunctions(loplugin::InstantiationData const & data): FilteringRewritePlugin(data) {} + + virtual void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); } + + bool VisitCXXMethodDecl(const CXXMethodDecl * decl); +private: + bool rewrite(const CXXMethodDecl * functionDecl); +}; + +static bool oneAndOnlyOne(clang::Stmt::const_child_range range) { + if (range.begin() == range.end()) { + return false; + } + if (++range.begin() != range.end()) { + return false; + } + return true; +} + + +bool InlineSimpleMemberFunctions::VisitCXXMethodDecl(const CXXMethodDecl * functionDecl) { + if (ignoreLocation(functionDecl)) { + return true; + } + // no point in doing virtual methods, the compiler always has to generate a vtable entry and a method + if (functionDecl->isVirtual()) { + return true; + } + if (functionDecl->getTemplatedKind() != FunctionDecl::TK_NonTemplate) { + return true; + } + if (!functionDecl->isInstance()) { + return true; + } + if (!functionDecl->isOutOfLine()) { + return true; + } + if( !functionDecl->hasBody()) { + return true; + } + if( functionDecl->isInlineSpecified()) { + return true; + } + if( functionDecl->getCanonicalDecl()->isInlineSpecified()) { + return true; + } + if( functionDecl->getNameAsString().find("Impl") != std::string::npos) { + return true; + } + // ignore stuff that forms part of the stable URE interface + if (isInUnoIncludeFile(functionDecl)) { + return true; + } + // ignore stuff like: + // template<class E> E * Sequence<E>::begin() { return getArray(); } + if( functionDecl->getParent()->getDescribedClassTemplate() != nullptr ) { + return true; + } + + /* + The chain here looks like + CompoundStmt + ReturnStmt + other stuff + CXXThisExpr + */ + + const CompoundStmt* compoundStmt = dyn_cast< CompoundStmt >( functionDecl->getBody() ); + if (compoundStmt == nullptr) { + return true; + } + if (compoundStmt->body_begin() == compoundStmt->body_end()) { + return true; + } + + + const Stmt* childStmt = *compoundStmt->child_begin(); + + if (dyn_cast<ReturnStmt>( childStmt ) == nullptr) { + return true; + } + if (!oneAndOnlyOne(childStmt->children())) { + return true; + } + + + /* Don't warn if we see a method definition like + X X::a() { + return *this; + } + which translates to: + CompoundStmt + ReturnStmt + ImplicitCastExpr + UnaryOperator + CXXThisExpr + or: + CompoundStmt + ReturnStmt + UnaryOperator + CXXThisExpr + */ + childStmt = *childStmt->child_begin(); + if (dyn_cast<ImplicitCastExpr>( childStmt ) != nullptr + && oneAndOnlyOne( childStmt->children() )) + { + const Stmt* childStmt2 = *childStmt->child_begin(); + if (dyn_cast<UnaryOperator>( childStmt2 ) != nullptr + && oneAndOnlyOne(childStmt2->children())) + { + childStmt2 = *childStmt2->child_begin(); + if (dyn_cast<CXXThisExpr>( childStmt2 ) != nullptr + && childStmt2->children().begin() == childStmt2->children().end()) + { + return true; + } + } + } + if (dyn_cast<UnaryOperator>( childStmt ) != nullptr + && oneAndOnlyOne( childStmt->children() )) + { + const Stmt* childStmt2 = *childStmt->child_begin(); + if (dyn_cast<CXXThisExpr>( childStmt2 ) != nullptr + && childStmt2->children().begin() == childStmt2->children().end()) + { + return true; + } + } + + /* look for a chains like: + CompoundStmt + ReturnStmt + stuff + CXXThisExpr + */ + childStmt = *(*compoundStmt->child_begin())->child_begin(); + while (1) { + if (dyn_cast<CallExpr>( childStmt ) != nullptr) + return true; + if (dyn_cast<CXXNewExpr>( childStmt ) != nullptr) + return true; + if (dyn_cast<CXXConstructExpr>( childStmt ) != nullptr) + return true; + if (dyn_cast<ConditionalOperator>( childStmt ) != nullptr) + return true; + if (dyn_cast<BinaryOperator>( childStmt ) != nullptr) + return true; + // exclude methods that return fields on incomplete types .e.g the pImpl pattern + const MemberExpr* memberExpr = dyn_cast<MemberExpr>( childStmt ); + if (memberExpr != nullptr && memberExpr->getMemberDecl()) { + const FieldDecl* fieldDecl = dyn_cast<FieldDecl>(memberExpr->getMemberDecl()); + if (fieldDecl != nullptr) + { + // yes, a little bit of a hack. However, it is quite hard to determine if the method + // in question is accessing a field via a pImpl pattern. + if (fieldDecl->getType()->isIncompleteType()) + return true; + if (fieldDecl->getNameAsString().find("Impl") != std::string::npos) + return true; + if (fieldDecl->getNameAsString().find("pImp") != std::string::npos) + return true; + // somewhere in VCL + if (fieldDecl->getNameAsString().find("mpGlobalSyncData") != std::string::npos) + return true; + std::string s = fieldDecl->getType().getAsString(); + if (s.find("Impl") != std::string::npos || s.find("pImp") != std::string::npos || s.find("Internal") != std::string::npos) + return true; + } + } + if (dyn_cast<CXXThisExpr>( childStmt ) != nullptr) { + if (!rewrite(functionDecl)) + { + report( + DiagnosticsEngine::Warning, + "inlinesimpleaccessmethods", + functionDecl->getSourceRange().getBegin()) + << functionDecl->getSourceRange(); + // display the location of the class member declaration so I don't have to search for it by hand + report( + DiagnosticsEngine::Note, + "inlinesimpleaccessmethods", + functionDecl->getCanonicalDecl()->getSourceRange().getBegin()) + << functionDecl->getCanonicalDecl()->getSourceRange(); + } + return true; + } + if ( childStmt->children().begin() == childStmt->children().end() ) + return true; + childStmt = *childStmt->child_begin(); + } + return true; +} + +static std::string ReplaceString(std::string subject, const std::string& search, + const std::string& replace) { + size_t pos = 0; + while ((pos = subject.find(search, pos)) != std::string::npos) { + subject.replace(pos, search.length(), replace); + pos += replace.length(); + } + return subject; +} + +bool InlineSimpleMemberFunctions::rewrite(const CXXMethodDecl * functionDecl) { + if (rewriter == nullptr) { + return false; + } + // Only rewrite declarations in include files if a + // definition is also seen, to avoid compilation of a + // definition (in a main file only processed later) to fail + // with a "mismatch" error before the rewriter had a chance + // to act upon the definition. + if (!compiler.getSourceManager().isInMainFile( + compiler.getSourceManager().getSpellingLoc( + functionDecl->getNameInfo().getLoc()))) + { + return false; + } + + const char *p1, *p2; + + // get the function body contents + p1 = compiler.getSourceManager().getCharacterData( functionDecl->getBody()->getBeginLoc() ); + p2 = compiler.getSourceManager().getCharacterData( functionDecl->getBody()->getEndLoc() ); + std::string s1( p1, p2 - p1 + 1); + + /* we can't safely move around stuff containing comments, we mess up the resulting code */ + if ( s1.find("/*") != std::string::npos || s1.find("//") != std::string::npos ) { + return false; + } + + // strip linefeeds and any double-spaces, so we have a max of one space between tokens + s1 = ReplaceString(s1, "\r", ""); + s1 = ReplaceString(s1, "\n", ""); + s1 = ReplaceString(s1, "\t", " "); + s1 = ReplaceString(s1, " ", " "); + s1 = ReplaceString(s1, " ", " "); + s1 = ReplaceString(s1, " ", " "); + s1 = " " + s1; + + // scan from the end of the function's body through the trailing whitespace, so we can do a nice clean remove +// commented out because for some reason it will sometimes chomp an extra token +// SourceLocation endOfRemoveLoc = functionDecl->getBody()->getLocEnd(); +// for (;;) { +// endOfRemoveLoc = endOfRemoveLoc.getLocWithOffset(1); +// p1 = compiler.getSourceManager().getCharacterData( endOfRemoveLoc ); +// if (*p1 != ' ' && *p1 != '\r' && *p1 != '\n' && *p1 != '\t') +// break; +// } + + // remove the function's out of line body and declaration + RewriteOptions opts; + opts.RemoveLineIfEmpty = true; + if (!removeText(SourceRange(functionDecl->getBeginLoc(), functionDecl->getBody()->getEndLoc()), opts)) { + return false; + } + + // scan forward until we find the semicolon + const FunctionDecl * canonicalDecl = functionDecl->getCanonicalDecl(); + p1 = compiler.getSourceManager().getCharacterData( canonicalDecl->getEndLoc() ); + p2 = ++p1; + while (*p2 != 0 && *p2 != ';') p2++; + + // insert the function body into the inline function definition (i.e. the one inside the class definition) + return replaceText(canonicalDecl->getEndLoc().getLocWithOffset(p2 - p1 + 1), 1, s1); +} + +loplugin::Plugin::Registration< InlineSimpleMemberFunctions > X("inlinesimplememberfunctions"); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/memoryvar.cxx b/compilerplugins/clang/store/memoryvar.cxx new file mode 100644 index 000000000000..14c328ba40dd --- /dev/null +++ b/compilerplugins/clang/store/memoryvar.cxx @@ -0,0 +1,238 @@ +/* -*- 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 <memory> +#include <string> +#include <iostream> +#include <map> +#include <set> + +#include "config_clang.h" +#include "plugin.hxx" +#include "clang/AST/CXXInheritance.h" + +// Check for local variables that we are calling delete on + +namespace +{ + +class MemoryVar: + public loplugin::FilteringPlugin<MemoryVar> +{ +public: + explicit MemoryVar(loplugin::InstantiationData const & data): FilteringPlugin(data), mbChecking(false) {} + + virtual void run() override { + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + } + + bool TraverseFunctionDecl(FunctionDecl*); + bool VisitCXXDeleteExpr(const CXXDeleteExpr*); + bool VisitCXXNewExpr(const CXXNewExpr* ); + bool VisitBinaryOperator(const BinaryOperator*); + bool VisitReturnStmt(const ReturnStmt*); + +private: + bool mbChecking; + std::set<SourceLocation> maVarUsesSet; + std::set<SourceLocation> maVarNewSet; + std::set<SourceLocation> maVarIgnoreSet; + std::map<SourceLocation,SourceRange> maVarDeclSourceRangeMap; + std::map<SourceLocation,SourceRange> maVarDeleteSourceRangeMap; + StringRef getFilename(SourceLocation loc); +}; + +StringRef MemoryVar::getFilename(SourceLocation loc) +{ + SourceLocation spellingLocation = compiler.getSourceManager().getSpellingLoc(loc); + StringRef name { getFilenameOfLocation(spellingLocation) }; + return name; +} + +bool MemoryVar::TraverseFunctionDecl(FunctionDecl * decl) +{ + if (ignoreLocation(decl)) { + return true; + } + if (!decl->hasBody() || !decl->isThisDeclarationADefinition()) { + return true; + } + + maVarUsesSet.clear(); + maVarNewSet.clear(); + maVarIgnoreSet.clear(); + maVarDeclSourceRangeMap.clear(); + maVarDeleteSourceRangeMap.clear(); + + assert(!mbChecking); + mbChecking = true; + TraverseStmt(decl->getBody()); + mbChecking = false; + + for (const auto& varLoc : maVarUsesSet) + { + // checking the location of the var instead of the function because for some reason + // I'm not getting accurate results from clang right now + StringRef aFileName = getFilename(varLoc); + // TODO these files are doing some weird stuff I don't know how to ignore yet + if (loplugin::hasPathnamePrefix(aFileName, SRCDIR "/vcl/source/filter/")) { + return true; + } + if (loplugin::isSamePathname(aFileName, SRCDIR "/sw/source/core/layout/frmtool.cxx")) { + return true; + } + + + if (maVarNewSet.find(varLoc) == maVarNewSet.end()) + continue; + if (maVarIgnoreSet.find(varLoc) != maVarIgnoreSet.end()) + continue; + + report(DiagnosticsEngine::Warning, + "calling new and delete on a local var, rather use std::unique_ptr", + varLoc) + << maVarDeclSourceRangeMap[varLoc]; + report(DiagnosticsEngine::Note, + "delete called here", + maVarDeleteSourceRangeMap[varLoc].getBegin()) + << maVarDeleteSourceRangeMap[varLoc]; + } + return true; +} + +bool MemoryVar::VisitCXXDeleteExpr(const CXXDeleteExpr *deleteExpr) +{ + if (!mbChecking) + return true; + if (ignoreLocation(deleteExpr)) { + return true; + } + const Expr* argumentExpr = deleteExpr->getArgument(); + if (isa<CastExpr>(argumentExpr)) { + argumentExpr = dyn_cast<CastExpr>(argumentExpr)->getSubExpr(); + } + const DeclRefExpr* declRefExpr = dyn_cast<DeclRefExpr>(argumentExpr); + if (!declRefExpr) + return true; + const Decl* decl = declRefExpr->getDecl(); + if (!isa<VarDecl>(decl) || isa<ParmVarDecl>(decl)) { + return true; + } + const VarDecl * varDecl = dyn_cast<VarDecl>(decl)->getCanonicalDecl(); + if (varDecl->hasGlobalStorage()) { + return true; + } + + SourceLocation loc = varDecl->getLocation(); + + if (maVarUsesSet.insert(loc).second) { + maVarDeclSourceRangeMap[loc] = varDecl->getSourceRange(); + maVarDeleteSourceRangeMap[loc] = declRefExpr->getSourceRange(); + } + return true; +} + +bool MemoryVar::VisitCXXNewExpr(const CXXNewExpr *newExpr) +{ + if (!mbChecking) + return true; + if (ignoreLocation(newExpr)) { + return true; + } + const Stmt* stmt = getParentStmt(newExpr); + + const DeclStmt* declStmt = dyn_cast<DeclStmt>(stmt); + if (declStmt) { + const VarDecl* varDecl = dyn_cast<VarDecl>(declStmt->getSingleDecl()); + if (varDecl) { + varDecl = varDecl->getCanonicalDecl(); + SourceLocation loc = varDecl->getLocation(); + maVarNewSet.insert(loc); + } + return true; + } + + const BinaryOperator* binaryOp = dyn_cast<BinaryOperator>(stmt); + if (binaryOp && binaryOp->getOpcode() == BO_Assign) { + const DeclRefExpr* declRefExpr = dyn_cast<DeclRefExpr>(binaryOp->getLHS()); + if (declRefExpr) { + const VarDecl* varDecl = dyn_cast<VarDecl>(declRefExpr->getDecl()); + if (varDecl) { + varDecl = varDecl->getCanonicalDecl(); + SourceLocation loc = varDecl->getLocation(); + maVarNewSet.insert(loc); + } + } + } + return true; +} + +// Ignore cases where the variable in question is assigned to another variable +bool MemoryVar::VisitBinaryOperator(const BinaryOperator *binaryOp) +{ + if (!mbChecking) + return true; + if (ignoreLocation(binaryOp)) { + return true; + } + if (binaryOp->getOpcode() != BO_Assign) { + return true; + } + const Expr* expr = binaryOp->getRHS(); + // unwrap casts + while (isa<CastExpr>(expr)) { + expr = dyn_cast<CastExpr>(expr)->getSubExpr(); + } + const DeclRefExpr* declRefExpr = dyn_cast<DeclRefExpr>(expr); + if (!declRefExpr) { + return true; + } + const VarDecl* varDecl = dyn_cast<VarDecl>(declRefExpr->getDecl()); + if (!varDecl) { + return true; + } + varDecl = varDecl->getCanonicalDecl(); + maVarIgnoreSet.insert(varDecl->getLocation()); + return true; +} + +// Ignore cases where the variable in question is returned from a function +bool MemoryVar::VisitReturnStmt(const ReturnStmt *returnStmt) +{ + if (!mbChecking) + return true; + if (ignoreLocation(returnStmt)) { + return true; + } + const Expr* expr = returnStmt->getRetValue(); + if (!expr) { + return true; + } + // unwrap casts + while (isa<CastExpr>(expr)) { + expr = dyn_cast<CastExpr>(expr)->getSubExpr(); + } + const DeclRefExpr* declRefExpr = dyn_cast<DeclRefExpr>(expr); + if (!declRefExpr) { + return true; + } + const VarDecl* varDecl = dyn_cast<VarDecl>(declRefExpr->getDecl()); + if (!varDecl) { + return true; + } + varDecl = varDecl->getCanonicalDecl(); + maVarIgnoreSet.insert(varDecl->getLocation()); + return true; +} + +loplugin::Plugin::Registration< MemoryVar > X("memoryvar", false); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/namespaceindentation.cxx b/compilerplugins/clang/store/namespaceindentation.cxx new file mode 100644 index 000000000000..1398efc86adf --- /dev/null +++ b/compilerplugins/clang/store/namespaceindentation.cxx @@ -0,0 +1,220 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * Based on LLVM/Clang. + * + * This file is distributed under the University of Illinois Open Source + * License. See LICENSE.TXT for details. + * + */ +#ifndef LO_CLANG_SHARED_PLUGINS + +#include <cassert> +#include <string> +#include <iostream> +#include <locale> +#include <fstream> +#include <set> +#include "plugin.hxx" + +/* +*/ + +namespace +{ +class NamespaceIndentation : public loplugin::FilteringPlugin<NamespaceIndentation> +{ +public: + explicit NamespaceIndentation(loplugin::InstantiationData const& data) + : FilteringPlugin(data) + { + } + + virtual bool preRun() override { return true; } + + virtual void run() override + { + if (preRun()) + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + } + + bool VisitNamespaceDecl(NamespaceDecl const*); + +private: + std::string GetFullNamespace(const NamespaceDecl* nsDecl); +}; + +void trim(std::string& str) +{ + // right trim + auto it1 = std::find_if(str.rbegin(), str.rend(), [](char ch) { + return !std::isspace<char>(ch, std::locale::classic()); + }); + str.erase(it1.base(), str.end()); + // left trim + auto it2 = std::find_if(str.begin(), str.end(), [](char ch) { + return !std::isspace<char>(ch, std::locale::classic()); + }); + str.erase(str.begin(), it2); +} + +bool NamespaceIndentation::VisitNamespaceDecl(NamespaceDecl const* nsDecl) +{ + if (ignoreLocation(nsDecl)) + return true; + if (nsDecl->isAnonymousNamespace()) + return true; + if (isInUnoIncludeFile(compiler.getSourceManager().getSpellingLoc(nsDecl->getLocation()))) + return true; + + // right now, just fixing up the fallout from clang-tidy-modernize-namespaces, which + // does not touch header files + if (!compiler.getSourceManager().isInMainFile(nsDecl->getLocation())) + return true; + + auto& SM = compiler.getSourceManager(); + + // if we have a combined ns (.e.g namespace aaa::bbb), this appears in the AST + // as two nested namespace sharing the same source locations, so ignore the outer decls + if (!nsDecl->decls_empty()) + { + auto child = dyn_cast_or_null<NamespaceDecl>(*nsDecl->decls_begin()); + if (child) + { + bool invalid1 = false; + bool invalid2 = false; + unsigned line1 = SM.getPresumedLineNumber(nsDecl->getBeginLoc(), &invalid1); + unsigned line2 = SM.getPresumedLineNumber(child->getBeginLoc(), &invalid2); + if (line1 == line2) + return true; + } + } + + // Truly hacky way to find the actual beginning of an xxx::yyy namespace declaration + // if we are inside the yyy NameSpaceDecl of + // namespace xxx::yyy + // the beginLoc is just between the "xxx" and the "::" + auto nsDeclBeginLoc = nsDecl->getBeginLoc(); + bool foundMultiple = false; + { + constexpr int BACKSCAN = 32; + auto beginLoc = nsDecl->getBeginLoc().getLocWithOffset(-BACKSCAN); + auto endLoc = nsDecl->getBeginLoc().getLocWithOffset(3); + const char* p1 = SM.getCharacterData(beginLoc); + const char* p2 = SM.getCharacterData(endLoc); + unsigned n = Lexer::MeasureTokenLength(endLoc, SM, compiler.getLangOpts()); + if (p2 < p1 || n > 128 || (p2 - p1 + n) > 2048) + return true; + auto s = std::string(p1, p2 - p1); + auto idx1 = s.rfind(" "); // find the space preceding the namespace token + if (idx1 != std::string::npos) + { + auto namespaceToken = s.substr(idx1); + if (namespaceToken.find("::") != std::string::npos) + { + auto idx = s.rfind("\n"); + nsDeclBeginLoc = nsDecl->getBeginLoc().getLocWithOffset(idx - BACKSCAN + 1); + foundMultiple = true; + } + } + } + + // for now, I am only interested in fixing the fallout from clang-tidy-modernize-namespace, not + // anything else + if (!foundMultiple) + return true; + + bool invalid1 = false; + bool invalid2 = false; + unsigned col1 = SM.getPresumedColumnNumber(nsDeclBeginLoc, &invalid1); + unsigned col2 = SM.getPresumedColumnNumber(nsDecl->getRBraceLoc(), &invalid2); + unsigned line1 = SM.getPresumedLineNumber(nsDeclBeginLoc, &invalid1); + unsigned line2 = SM.getPresumedLineNumber(nsDecl->getRBraceLoc(), &invalid2); + if (invalid1 || invalid2) + return true; + if (line1 == line2) // single line declaration + return true; + if (col1 != col2) + report(DiagnosticsEngine::Warning, "statement right brace mis-aligned", + nsDecl->getRBraceLoc()); + + // no easy way to get the position of the left brace + auto endLoc = nsDecl->getBeginLoc().getLocWithOffset(256); + const char* p1 = SM.getCharacterData(SM.getExpansionLoc(nsDecl->getBeginLoc())); + const char* p2 = SM.getCharacterData(SM.getExpansionLoc(endLoc)); + unsigned n = Lexer::MeasureTokenLength(endLoc, SM, compiler.getLangOpts()); + if (p2 < p1 || n > 128 || (p2 - p1 + n) > 2048) + return true; + auto s = std::string(p1, p2 - p1 + n); + auto idx1 = s.find("\n"); + auto idx2 = s.find("{"); + if (idx1 != std::string::npos && idx2 != std::string::npos) + if (idx1 < idx2) + { + auto col3 = idx2 - idx1; + if (col1 != col3) + report(DiagnosticsEngine::Warning, "statement left brace mis-aligned", + nsDecl->getBeginLoc()); + } + + // extract the comment following the end brace + auto beginLoc = nsDecl->getRBraceLoc(); + endLoc = beginLoc.getLocWithOffset(128); + p1 = SM.getCharacterData(SM.getExpansionLoc(beginLoc)); + p2 = SM.getCharacterData(SM.getExpansionLoc(endLoc)); + n = Lexer::MeasureTokenLength(endLoc, SM, compiler.getLangOpts()); + if (p2 < p1 || n > 128 || (p2 - p1 + n) > 2048) + return true; + s = std::string(p1, p2 - p1 + n); + idx1 = s.find("//"); + idx2 = s.find("\n"); + if (idx1 != std::string::npos && idx2 != std::string::npos && idx1 < idx2) + { + idx1 += 2; + s = s.substr(idx1, idx2 - idx1); + trim(s); + std::string fullNamespace = GetFullNamespace(nsDecl); + if (!(s == fullNamespace || s == (fullNamespace + " namespace") || s == "namespace" + || s == ("namespace " + fullNamespace) || s == ("namespace ::" + fullNamespace) + || s == ("end " + fullNamespace) || s == "end namespace" + || s == ("end namespace " + fullNamespace) + || s == ("end " + fullNamespace + " namespace") || s == "end of namespace" + || s == ("end of namespace " + fullNamespace) + || s == ("end of namespace ::" + fullNamespace) + || s == ("eof of namespace " + fullNamespace))) + { + report(DiagnosticsEngine::Warning, "incorrect comment at end of namespace %0", + nsDecl->getRBraceLoc()) + << fullNamespace; + } + } + + return true; +} + +std::string NamespaceIndentation::GetFullNamespace(const NamespaceDecl* nsDecl) +{ + std::vector<llvm::StringRef> names; + auto ns = nsDecl; + while (ns) + { + names.push_back(ns->getName()); + ns = dyn_cast<NamespaceDecl>(ns->getParent()); + } + std::string fullNamespace; + for (auto it = names.rbegin(); it != names.rend(); ++it) + fullNamespace += "::" + it->str(); + fullNamespace = fullNamespace.substr(2); + return fullNamespace; +} + +// leave this off by default, so as not to annoy people +loplugin::Plugin::Registration<NamespaceIndentation> namespaceindentation("namespaceindentation", + false); + +} // namespace + +#endif // LO_CLANG_SHARED_PLUGINS + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/oncevar.cxx b/compilerplugins/clang/store/oncevar.cxx new file mode 100644 index 000000000000..44fcfa950843 --- /dev/null +++ b/compilerplugins/clang/store/oncevar.cxx @@ -0,0 +1,414 @@ +/* -*- 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 <cassert> +#include <string> +#include <iostream> +#include <unordered_map> +#include <unordered_set> + +#include "plugin.hxx" +#include "check.hxx" +#include "config_clang.h" +#include "clang/AST/CXXInheritance.h" +#include "clang/AST/StmtVisitor.h" + +// Original idea from tml. +// Look for variables that are (a) initialised from zero or one constants. (b) only used in one spot. +// In which case, we might as well inline it. + +namespace +{ + +Expr const * lookThroughInitListExpr(Expr const * expr) { + if (auto const ile = dyn_cast<InitListExpr>(expr->IgnoreParenImpCasts())) { + if (ile->getNumInits() == 1) { + return ile->getInit(0); + } + } + return expr; +} + +class ConstantValueDependentExpressionVisitor: + public ConstStmtVisitor<ConstantValueDependentExpressionVisitor, bool> +{ + ASTContext const & context_; + +public: + ConstantValueDependentExpressionVisitor(ASTContext const & context): + context_(context) {} + + bool Visit(Stmt const * stmt) { + assert(isa<Expr>(stmt)); + auto const expr = cast<Expr>(stmt); + if (!expr->isValueDependent()) { + return expr->isEvaluatable(context_); + } + return ConstStmtVisitor::Visit(stmt); + } + + bool VisitParenExpr(ParenExpr const * expr) + { return Visit(expr->getSubExpr()); } + + bool VisitCastExpr(CastExpr const * expr) { + return Visit(expr->getSubExpr()); + } + + bool VisitUnaryOperator(UnaryOperator const * expr) + { return Visit(expr->getSubExpr()); } + + bool VisitBinaryOperator(BinaryOperator const * expr) { + return Visit(expr->getLHS()) && Visit(expr->getRHS()); + } + + bool VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr const *) { + return true; + } +}; + +class OnceVar: + public loplugin::FilteringPlugin<OnceVar> +{ +public: + explicit OnceVar(loplugin::InstantiationData const & data): FilteringPlugin(data) {} + + virtual void run() override { + // ignore some files with problematic macros + std::string fn(handler.getMainFileName()); + loplugin::normalizeDotDotInFilePath(fn); + // platform-specific stuff + if (fn == SRCDIR "/sal/osl/unx/thread.cxx" + || fn == SRCDIR "/sot/source/base/formats.cxx" + || fn == SRCDIR "/svl/source/config/languageoptions.cxx" + || fn == SRCDIR "/sfx2/source/appl/appdde.cxx" + || fn == SRCDIR "/configmgr/source/components.cxx" + || fn == SRCDIR "/embeddedobj/source/msole/oleembed.cxx") + return; + // some of this is necessary + if (loplugin::hasPathnamePrefix( fn, SRCDIR "/sal/qa/")) + return; + if (loplugin::hasPathnamePrefix( fn, SRCDIR "/comphelper/qa/")) + return; + // TODO need to check calls via function pointer + if (fn == SRCDIR "/i18npool/source/textconversion/textconversion_zh.cxx" + || fn == SRCDIR "/i18npool/source/localedata/localedata.cxx") + return; + // debugging stuff + if (fn == SRCDIR "/sc/source/core/data/dpcache.cxx" + || fn == SRCDIR "/sw/source/core/layout/dbg_lay.cxx" + || fn == SRCDIR "/sw/source/core/layout/ftnfrm.cxx") + return; + // TODO taking local reference to variable + if (fn == SRCDIR "/sc/source/filter/excel/xechart.cxx") + return; + // macros managing to generate to a valid warning + if (fn == SRCDIR "/solenv/bin/concat-deps.c") + return; + // TODO bug in the plugin + if (fn == SRCDIR "/vcl/unx/generic/app/saldisp.cxx") + return; + + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + + for (auto const & varDecl : maVarDeclSet) + { + if (maVarDeclToIgnoreSet.find(varDecl) != maVarDeclToIgnoreSet.end()) + continue; + int noUses = 0; + auto it = maVarUsesMap.find(varDecl); + if (it != maVarUsesMap.end()) + noUses = it->second; + if (noUses > 1) + continue; + report(DiagnosticsEngine::Warning, + "var used only once, should be inlined or declared const", + varDecl->getLocation()) + << varDecl->getSourceRange(); + if (it != maVarUsesMap.end()) + report(DiagnosticsEngine::Note, + "used here", + maVarUseSourceRangeMap[varDecl].getBegin()) + << maVarUseSourceRangeMap[varDecl]; + } + } + + bool VisitMemberExpr(MemberExpr const * expr) { + // ignore cases like: + // const OUString("xxx") xxx; + // rtl_something(xxx.pData); + // where we cannot inline the declaration. + if (isa<FieldDecl>(expr->getMemberDecl())) { + recordIgnore(expr); + } + return true; + } + + bool VisitUnaryOperator(UnaryOperator const * expr) { + // if we take the address of it, or we modify it, ignore it + UnaryOperator::Opcode op = expr->getOpcode(); + if (op == UO_AddrOf || op == UO_PreInc || op == UO_PostInc + || op == UO_PreDec || op == UO_PostDec) + { + recordIgnore(expr->getSubExpr()); + } + return true; + } + + bool VisitBinaryOperator(BinaryOperator const * expr) { + // if we assign it another value, or modify it, ignore it + BinaryOperator::Opcode op = expr->getOpcode(); + if (op == BO_Assign || op == BO_PtrMemD || op == BO_PtrMemI || op == BO_MulAssign + || op == BO_DivAssign || op == BO_RemAssign || op == BO_AddAssign + || op == BO_SubAssign || op == BO_ShlAssign || op == BO_ShrAssign + || op == BO_AndAssign || op == BO_XorAssign || op == BO_OrAssign) + { + recordIgnore(expr->getLHS()); + } + return true; + } + + bool VisitCallExpr(CallExpr const * expr) { + unsigned firstArg = 0; + if (auto const cmce = dyn_cast<CXXMemberCallExpr>(expr)) { + if (auto const e1 = cmce->getMethodDecl()) { + if (!(e1->isConst() || e1->isStatic())) { + recordIgnore(cmce->getImplicitObjectArgument()); + } + } else if (auto const e2 = dyn_cast<BinaryOperator>( + cmce->getCallee()->IgnoreParenImpCasts())) + { + switch (e2->getOpcode()) { + case BO_PtrMemD: + case BO_PtrMemI: + if (!e2->getRHS()->getType()->getAs<MemberPointerType>() + ->getPointeeType()->getAs<FunctionProtoType>() + ->isConst()) + { + recordIgnore(e2->getLHS()); + } + break; + default: + break; + } + } + } else if (auto const coce = dyn_cast<CXXOperatorCallExpr>(expr)) { + if (auto const cmd = dyn_cast_or_null<CXXMethodDecl>( + coce->getDirectCallee())) + { + if (!cmd->isStatic()) { + assert(coce->getNumArgs() != 0); + if (!cmd->isConst()) { + recordIgnore(coce->getArg(0)); + } + firstArg = 1; + } + } + } + // ignore those ones we are passing by reference + const FunctionDecl* calleeFunctionDecl = expr->getDirectCallee(); + if (calleeFunctionDecl) { + for (unsigned i = firstArg; i < expr->getNumArgs(); ++i) { + if (i < calleeFunctionDecl->getNumParams()) { + QualType qt { calleeFunctionDecl->getParamDecl(i)->getType() }; + if (loplugin::TypeCheck(qt).LvalueReference().NonConst()) { + recordIgnore(expr->getArg(i)); + } + if (loplugin::TypeCheck(qt).Pointer().NonConst()) { + recordIgnore(expr->getArg(i)); + } + } + } + } + return true; + } + + bool VisitCXXConstructExpr(CXXConstructExpr const * expr) { + // ignore those ones we are passing by reference + const CXXConstructorDecl* cxxConstructorDecl = expr->getConstructor(); + for (unsigned i = 0; i < expr->getNumArgs(); ++i) { + if (i < cxxConstructorDecl->getNumParams()) { + QualType qt { cxxConstructorDecl->getParamDecl(i)->getType() }; + if (loplugin::TypeCheck(qt).LvalueReference().NonConst()) { + recordIgnore(expr->getArg(i)); + } + if (loplugin::TypeCheck(qt).Pointer().NonConst()) { + recordIgnore(expr->getArg(i)); + } + } + } + return true; + } + + bool VisitDeclRefExpr( const DeclRefExpr* ); + bool VisitVarDecl( const VarDecl* ); + bool TraverseFunctionDecl( FunctionDecl* functionDecl ); + +private: + std::unordered_set<VarDecl const *> maVarDeclSet; + std::unordered_set<VarDecl const *> maVarDeclToIgnoreSet; + std::unordered_map<VarDecl const *, int> maVarUsesMap; + std::unordered_map<VarDecl const *, SourceRange> maVarUseSourceRangeMap; + + bool isConstantValueDependentExpression(Expr const * expr) { + return ConstantValueDependentExpressionVisitor(compiler.getASTContext()) + .Visit(expr); + } + + void recordIgnore(Expr const * expr) { + for (;;) { + expr = expr->IgnoreParenImpCasts(); + if (auto const e = dyn_cast<MemberExpr>(expr)) { + if (isa<FieldDecl>(e->getMemberDecl())) { + expr = e->getBase(); + continue; + } + } + if (auto const e = dyn_cast<ArraySubscriptExpr>(expr)) { + expr = e->getBase(); + continue; + } + if (auto const e = dyn_cast<BinaryOperator>(expr)) { + if (e->getOpcode() == BO_PtrMemD) { + expr = e->getLHS(); + continue; + } + } + break; + } + auto const dre = dyn_cast<DeclRefExpr>(expr); + if (dre == nullptr) { + return; + } + auto const var = dyn_cast<VarDecl>(dre->getDecl()); + if (var == nullptr) { + return; + } + maVarDeclToIgnoreSet.insert(var); + } +}; + +bool OnceVar::TraverseFunctionDecl( FunctionDecl* functionDecl ) +{ + // Ignore functions that contains #ifdef-ery, can be quite tricky + // to make useful changes when this plugin fires in such functions + if (containsPreprocessingConditionalInclusion( + functionDecl->getSourceRange())) + return true; + return RecursiveASTVisitor::TraverseFunctionDecl(functionDecl); +} + +bool OnceVar::VisitVarDecl( const VarDecl* varDecl ) +{ + if (ignoreLocation(varDecl)) { + return true; + } + if (auto const init = varDecl->getInit()) { + recordIgnore(lookThroughInitListExpr(init)); + } + if (varDecl->isExceptionVariable() || isa<ParmVarDecl>(varDecl)) { + return true; + } + // ignore stuff in header files (which should really not be there, but anyhow) + if (!compiler.getSourceManager().isInMainFile(varDecl->getLocation())) { + return true; + } + // Ignore macros like FD_ZERO + if (compiler.getSourceManager().isMacroBodyExpansion(varDecl->getBeginLoc())) { + return true; + } + if (varDecl->hasGlobalStorage()) { + return true; + } + auto const tc = loplugin::TypeCheck(varDecl->getType()); + if (!varDecl->getType().isCXX11PODType(compiler.getASTContext()) + && !tc.Class("OString").Namespace("rtl").GlobalNamespace() + && !tc.Class("OUString").Namespace("rtl").GlobalNamespace() + && !tc.Class("OStringBuffer").Namespace("rtl").GlobalNamespace() + && !tc.Class("OUStringBuffer").Namespace("rtl").GlobalNamespace() + && !tc.Class("Color").GlobalNamespace() + && !tc.Class("Pair").GlobalNamespace() + && !tc.Class("Point").GlobalNamespace() + && !tc.Class("Size").GlobalNamespace() + && !tc.Class("Range").GlobalNamespace() + && !tc.Class("Selection").GlobalNamespace() + && !tc.Class("Rectangle").Namespace("tools").GlobalNamespace()) + { + return true; + } + if (varDecl->getType()->isPointerType()) + return true; + // if it's declared const, ignore it, it's there to make the code easier to read + if (tc.Const()) + return true; + + if (!varDecl->hasInit()) + return true; + + // check for string or scalar literals + bool foundStringLiteral = false; + const Expr * initExpr = varDecl->getInit(); + if (auto e = dyn_cast<ExprWithCleanups>(initExpr)) { + initExpr = e->getSubExpr(); + } + if (isa<clang::StringLiteral>(initExpr)) { + foundStringLiteral = true; + } else if (auto constructExpr = dyn_cast<CXXConstructExpr>(initExpr)) { + if (constructExpr->getNumArgs() == 0) { + foundStringLiteral = true; // i.e., empty string + } else { + auto stringLit2 = dyn_cast<clang::StringLiteral>(constructExpr->getArg(0)); + foundStringLiteral = stringLit2 != nullptr; + } + } + if (!foundStringLiteral) { + auto const init = varDecl->getInit(); + if (!(init->isValueDependent() + ? isConstantValueDependentExpression(init) + : init->isConstantInitializer( + compiler.getASTContext(), false/*ForRef*/))) + { + return true; + } + } + + maVarDeclSet.insert(varDecl); + + return true; +} + +bool OnceVar::VisitDeclRefExpr( const DeclRefExpr* declRefExpr ) +{ + if (ignoreLocation(declRefExpr)) { + return true; + } + const Decl* decl = declRefExpr->getDecl(); + if (!isa<VarDecl>(decl) || isa<ParmVarDecl>(decl)) { + return true; + } + const VarDecl * varDecl = dyn_cast<VarDecl>(decl)->getCanonicalDecl(); + // ignore stuff in header files (which should really not be there, but anyhow) + if (!compiler.getSourceManager().isInMainFile(varDecl->getLocation())) { + return true; + } + + if (maVarUsesMap.find(varDecl) == maVarUsesMap.end()) { + maVarUsesMap[varDecl] = 1; + maVarUseSourceRangeMap[varDecl] = declRefExpr->getSourceRange(); + } else { + maVarUsesMap[varDecl]++; + } + + return true; +} + +loplugin::Plugin::Registration< OnceVar > X("oncevar", false); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/optvalue.cxx b/compilerplugins/clang/store/optvalue.cxx new file mode 100644 index 000000000000..2b703e194fd6 --- /dev/null +++ b/compilerplugins/clang/store/optvalue.cxx @@ -0,0 +1,66 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * Based on LLVM/Clang. + * + * This file is distributed under the University of Illinois Open Source + * License. See LICENSE.TXT for details. + * + */ + +#include <cassert> +#include <string> +#include <iostream> +#include <fstream> +#include "config_clang.h" +#include "plugin.hxx" +#include "check.hxx" + +/* +*/ + +namespace +{ +class OptValue : public loplugin::FilteringPlugin<OptValue> +{ +public: + explicit OptValue(loplugin::InstantiationData const& data) + : FilteringPlugin(data) + { + } + + virtual bool preRun() override { return true; } + + virtual void run() override + { + if (preRun()) + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + } + + bool VisitCXXMemberCallExpr(const CXXMemberCallExpr*); +}; + +bool OptValue::VisitCXXMemberCallExpr(const CXXMemberCallExpr* topExpr) +{ + if (ignoreLocation(topExpr)) + return true; + const CXXMethodDecl* methodDecl = topExpr->getMethodDecl(); + if (!methodDecl) + return true; + if (!methodDecl->getIdentifier() || methodDecl->getName() != "value") + return true; + auto expr1 = topExpr->getImplicitObjectArgument()->IgnoreImpCasts(); + if (!isa<MaterializeTemporaryExpr>(expr1)) + return true; + + report(DiagnosticsEngine::Warning, "call to OptValue::value()", topExpr->getBeginLoc()); + + return true; +} + +loplugin::Plugin::Registration<OptValue> optvalue("optvalue", false); + +} // namespace + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/sequentialassign.cxx b/compilerplugins/clang/store/sequentialassign.cxx new file mode 100644 index 000000000000..01172df17eb1 --- /dev/null +++ b/compilerplugins/clang/store/sequentialassign.cxx @@ -0,0 +1,327 @@ +/* -*- 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 <cassert> +#include <string> +#include <iostream> +#include <unordered_map> +#include <unordered_set> + +#include "plugin.hxx" +#include "check.hxx" +#include "config_clang.h" +#include "clang/AST/CXXInheritance.h" +#include "clang/AST/StmtVisitor.h" + +/** + This is a kind of simplified dead-store analysis. + + We are looking for patterns like: + Foo x = a; + x = b; + which can be simplified to + x = b + + or + Foo x = a; + x = f(x) + which can be simplified to + Foo x = f(a) + + TODO Improve this plugin to make it safer. We should really be checking the following + conditions inside the RHS of the second statement: + If the variable is having it's address taken, or a non-const method called on it, + on passed by non-const-ref. +*/ + +namespace +{ +//static bool startswith(const std::string& rStr, const char* pSubStr) { +// return rStr.compare(0, strlen(pSubStr), pSubStr) == 0; +//} +class SequentialAssign : public loplugin::FilteringPlugin<SequentialAssign> +{ +public: + explicit SequentialAssign(loplugin::InstantiationData const& data) + : FilteringPlugin(data) + { + } + + virtual void run() override + { + std::string fn(handler.getMainFileName()); + loplugin::normalizeDotDotInFilePath(fn); + // places where the existing code style just looks better + // TODO lots of these would be unnecessary if I taught the plugin + // to ignore vars which are assigned to repeatedly + if (fn == SRCDIR "/vcl/source/helper/commandinfoprovider.cxx" + || fn == SRCDIR "/basegfx/source/polygon/b2dpolygonclipper.cxx" + || fn == SRCDIR "/i18nlangtag/source/isolang/insys.cxx" + || fn == SRCDIR "/vcl/unx/generic/fontmanager/fontconfig.cxx" + || fn == SRCDIR "/svtools/source/filter/exportdialog.cxx" + || fn == SRCDIR "/svtools/source/control/ruler.cxx" + || fn == SRCDIR "/basic/qa/cppunit/test_scanner.cxx" + || fn == SRCDIR "/basic/source/uno/namecont.cxx" + || fn == SRCDIR "/test/source/sheet/xnamedrange.cxx" + || fn == SRCDIR "/i18npool/qa/cppunit/test_breakiterator.cxx" + || fn == SRCDIR "/i18npool/source/localedata/LocaleNode.cxx" + || fn == SRCDIR "/i18npool/source/transliteration/transliteration_Ignore.cxx" + || fn == SRCDIR "/i18npool/qa/cppunit/test_textsearch.cxx" + || fn == SRCDIR "/framework/source/jobs/jobdata.cxx" + || fn == SRCDIR "/framework/source/services/pathsettings.cxx" + || fn == SRCDIR "/xmloff/source/chart/SchXMLTools.cxx" + || fn == SRCDIR "/svx/source/tbxctrls/Palette.cxx" + || fn == SRCDIR "/svx/source/sdr/contact/objectcontactofpageview.cxx" + || fn == SRCDIR "/svx/source/form/fmservs.cxx" + || fn == SRCDIR "/svx/source/svdraw/svdograf.cxx" + || fn == SRCDIR "/svx/source/accessibility/AccessibleShape.cxx" + || fn == SRCDIR "/svx/source/fmcomp/fmgridcl.cxx" + || fn == SRCDIR "/chart2/source/tools/CharacterProperties.cxx" + || fn == SRCDIR "/svx/source/dialog/dialcontrol.cxx" + || fn == SRCDIR "/connectivity/source/commontools/TTableHelper.cxx" + || fn == SRCDIR "/svx/source/dialog/_bmpmask.cxx" + || fn == SRCDIR "/media/noel/disk2/libo4/cui/source/dialogs/SignSignatureLineDialog.cxx" + || fn == SRCDIR "/filter/source/msfilter/msdffimp.cxx" + || fn == SRCDIR "/cui/source/dialogs/SignSignatureLineDialog.cxx" + || fn == SRCDIR "/cui/source/dialogs/screenshotannotationdlg.cxx" + || fn == SRCDIR "/cui/source/options/optupdt.cxx" + || fn == SRCDIR "/dbaccess/source/ui/querydesign/querycontroller.cxx" + || fn == SRCDIR "/dbaccess/source/ui/dlg/DbAdminImpl.cxx" + || fn == SRCDIR "/dbaccess/source/ui/querydesign/JoinController.cxx" + || fn == SRCDIR "/dbaccess/source/ui/misc/TokenWriter.cxx" + || fn == SRCDIR "/dbaccess/source/ui/misc/TokenWriter.cxx" + || fn == SRCDIR "/dbaccess/source/ui/misc/dbsubcomponentcontroller.cxx" + || fn == SRCDIR "/dbaccess/source/core/api/querycomposer.cxx" + || fn == SRCDIR "/desktop/source/lib/init.cxx" + || fn == SRCDIR "/lotuswordpro/source/filter/lwpfribmark.cxx" + || fn == SRCDIR "/tools/qa/cppunit/test_color.cxx" + || fn == SRCDIR "/sc/qa/unit/ucalc.cxx" + || fn == SRCDIR "/sc/source/ui/view/printfun.cxx" + || fn == SRCDIR "/sc/source/ui/view/preview.cxx" + || fn == SRCDIR "/sw/source/core/doc/tblafmt.cxx" + || fn == SRCDIR "/sw/source/core/draw/dflyobj.cxx" + || fn == SRCDIR "/sw/source/core/doc/DocumentDrawModelManager.cxx" + || fn == SRCDIR "/sw/source/core/edit/edfcol.cxx" + || fn == SRCDIR "/sw/source/filter/ww8/ww8toolbar.cxx" + || fn == SRCDIR "/sw/source/ui/fldui/fldvar.cxx" + || fn == SRCDIR "/sw/source/filter/ww8/ww8atr.cxx" + || fn == SRCDIR "/sd/source/ui/accessibility/AccessiblePageShape.cxx" + || fn == SRCDIR "/xmlsecurity/source/xmlsec/nss/nssinitializer.cxx" + || fn == SRCDIR "/sd/source/ui/slidesorter/cache/SlsRequestFactory.cxx" + || fn == SRCDIR "/sd/source/ui/framework/configuration/ResourceId.cxx" + || fn == SRCDIR "/sd/source/filter/html/htmlex.cxx" + || fn == SRCDIR "/starmath/source/cfgitem.cxx" + || fn == SRCDIR "/ucb/source/ucp/ftp/ftpurl.cxx" + || fn == SRCDIR "/starmath/source/node.cxx" + || fn == SRCDIR "/starmath/source/starmathdatabase.cxx" + || fn == SRCDIR "/ucb/source/ucp/cmis/certvalidation_handler.cxx" + || fn == SRCDIR "/reportdesign/source/ui/inspection/GeometryHandler.cxx" + || fn == SRCDIR "/reportdesign/source/core/api/ReportDefinition.cxx" + || fn == SRCDIR "/test/source/table/tablerow.cxx" + || fn == SRCDIR "/basegfx/test/B2DHomMatrixTest.cxx" + || fn == SRCDIR "/comphelper/qa/unit/base64_test.cxx" + || fn == SRCDIR "/testtools/source/bridgetest/bridgetest.cxx" + || fn == SRCDIR "/comphelper/qa/string/test_string.cxx" + || fn == SRCDIR "/cppu/qa/test_unotype.cxx" + || fn == SRCDIR "/cppu/qa/cppumaker/test_cppumaker.cxx" + || fn == SRCDIR "/o3tl/qa/test-lru_map.cxx" || fn == SRCDIR "/svl/qa/unit/svl.cxx" + || fn == SRCDIR "/chart2/qa/extras/PivotChartTest.cxx" + || fn == SRCDIR "/chart2/qa/extras/chart2export.cxx" + || fn == SRCDIR "/writerfilter/qa/cppunittests/misc/misc.cxx" + || fn == SRCDIR "/sw/qa/extras/ww8export/ww8export.cxx" + || fn == SRCDIR "/sw/qa/extras/uiwriter/uiwriter.cxx") + return; + // inside ifdef + if (fn == SRCDIR "/vcl/source/filter/png/pngread.cxx" + || fn == SRCDIR "/vcl/source/window/syschild.cxx" + || fn == SRCDIR "/sal/osl/unx/security.cxx" + || fn == SRCDIR "/vcl/source/filter/png/pngwrite.cxx" + || fn == SRCDIR "/svtools/source/control/inettbc.cxx" + || fn == SRCDIR "/canvas/source/cairo/cairo_textlayout.cxx" + || fn == SRCDIR "/sal/qa/osl/file/osl_File.cxx") + return; + // taking address of variable + if (fn == SRCDIR "/vcl/unx/generic/dtrans/X11_selection.cxx") + return; + // other + if (fn == SRCDIR "/sc/source/core/tool/scmatrix.cxx" + || fn == SRCDIR "/sal/qa/rtl/oustringbuffer/test_oustringbuffer_assign.cxx") + return; + + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + } + + bool VisitCompoundStmt(CompoundStmt const*); + +private: + const VarDecl* findSimpleAssign(Stmt const*); + bool isSimpleRHS(Expr const*); + Expr const* ignore(Expr const*); + void checkForSecondAssign(Stmt const* stmt, VarDecl const* varDecl); +}; + +bool SequentialAssign::VisitCompoundStmt(CompoundStmt const* compoundStmt) +{ + if (ignoreLocation(compoundStmt)) + return true; + + auto it = compoundStmt->body_begin(); + while (true) + { + if (it == compoundStmt->body_end()) + break; + auto firstStmt = *it; + const VarDecl* foundVars = findSimpleAssign(firstStmt); + // reference types have slightly weird behaviour + if (!foundVars || foundVars->getType()->isReferenceType()) + { + ++it; + continue; + } + ++it; + if (it == compoundStmt->body_end()) + break; + checkForSecondAssign(*it, foundVars); + } + + return true; +} + +void SequentialAssign::checkForSecondAssign(Stmt const* stmt, VarDecl const* varDecl) +{ + if (auto exprCleanup = dyn_cast<ExprWithCleanups>(stmt)) + stmt = exprCleanup->getSubExpr(); + + if (auto operatorCall = dyn_cast<CXXOperatorCallExpr>(stmt)) + { + if (operatorCall->getOperator() == OO_Equal) + { + if (auto declRefExprLHS = dyn_cast<DeclRefExpr>(ignore(operatorCall->getArg(0)))) + if (declRefExprLHS->getDecl() == varDecl) + { + report(DiagnosticsEngine::Warning, + "simplify by merging with the preceding assignment", stmt->getBeginLoc()) + << stmt->getSourceRange(); + } + } + return; + } + + if (auto binaryOp = dyn_cast<BinaryOperator>(stmt)) + { + if (binaryOp->getOpcode() == BO_Assign) + { + if (auto declRefExpr = dyn_cast<DeclRefExpr>(ignore(binaryOp->getLHS()))) + if (declRefExpr->getDecl() == varDecl) + { + report(DiagnosticsEngine::Warning, + "simplify by merging with the preceding assignment", stmt->getBeginLoc()) + << stmt->getSourceRange(); + } + } + } +} + +const VarDecl* SequentialAssign::findSimpleAssign(Stmt const* stmt) +{ + if (auto declStmt = dyn_cast<DeclStmt>(stmt)) + if (declStmt->isSingleDecl()) + if (auto varDeclLHS = dyn_cast_or_null<VarDecl>(declStmt->getSingleDecl())) + { + if (varDeclLHS->getStorageDuration() == SD_Static) + return nullptr; + // if it's call-style init (e.g. OUString s("xxx")), we only treat + // it as simple if it only contains a variable in the call + // (e.g. OUString s(x)) + if (varDeclLHS->getInitStyle() == VarDecl::InitializationStyle::CallInit) + { + auto cxxConstructExpr + = dyn_cast<CXXConstructExpr>(ignore(varDeclLHS->getInit())); + if (cxxConstructExpr) + { + if (cxxConstructExpr->getNumArgs() == 0) + return varDeclLHS; + if (cxxConstructExpr->getNumArgs() > 1) + return nullptr; + if (!isa<DeclRefExpr>(ignore(cxxConstructExpr->getArg(0)))) + return nullptr; + } + } + if (auto init = varDeclLHS->getInit()) + if (isSimpleRHS(init)) + return varDeclLHS; + } + if (auto operatorCall = dyn_cast<CXXOperatorCallExpr>(stmt)) + if (operatorCall->getOperator() == OO_Equal) + if (auto declRefExprLHS = dyn_cast<DeclRefExpr>(ignore(operatorCall->getArg(0)))) + if (auto varDeclLHS = dyn_cast<VarDecl>(declRefExprLHS->getDecl())) + if (isSimpleRHS(operatorCall->getArg(1))) + return varDeclLHS; + if (auto binaryOp = dyn_cast<BinaryOperator>(stmt)) + if (binaryOp->getOpcode() == BO_Assign) + if (auto declRefExprLHS = dyn_cast<DeclRefExpr>(ignore(binaryOp->getLHS()))) + if (auto varDeclLHS = dyn_cast<VarDecl>(declRefExprLHS->getDecl())) + if (isSimpleRHS(binaryOp->getRHS())) + return varDeclLHS; + return nullptr; +} + +/** + Does the first statement have a relatively simply RHS we can inline into the second statement? +*/ +bool SequentialAssign::isSimpleRHS(Expr const* expr) +{ + expr = ignore(expr); + + // code like + // Point aCurPos = rGlyphs + // always has a CXXConstructExpr wrapping the RHS + if (auto cxxConstructExpr = dyn_cast<CXXConstructExpr>(expr)) + if (cxxConstructExpr->getNumArgs() == 1) + expr = ignore(cxxConstructExpr->getArg(0)); + + if (!expr->isValueDependent() + && expr->isConstantInitializer(compiler.getASTContext(), false /*ForRef*/)) + return true; + if (isa<CXXMemberCallExpr>(expr)) + return false; + if (isa<CXXOperatorCallExpr>(expr)) + return false; + if (isa<UserDefinedLiteral>(expr)) + return true; + if (isa<CallExpr>(expr)) + return false; + if (isa<CastExpr>(expr)) + return false; + if (isa<ArraySubscriptExpr>(expr)) + return false; + if (isa<BinaryOperator>(expr)) + return false; + if (isa<ConditionalOperator>(expr)) + return false; + if (isa<UnaryOperator>(expr)) + return false; + if (isa<CXXNewExpr>(expr)) + return false; + if (auto memberExpr = dyn_cast<MemberExpr>(expr)) + return isSimpleRHS(memberExpr->getBase()); + + return true; +} + +Expr const* SequentialAssign::ignore(Expr const* expr) +{ + return expr->IgnoreImplicit()->IgnoreParens()->IgnoreImplicit(); +} + +// Off by default because of safety concerns, see TODO at top +loplugin::Plugin::Registration<SequentialAssign> X("sequentialassign", false); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/shouldreturnbool.cxx b/compilerplugins/clang/store/shouldreturnbool.cxx new file mode 100644 index 000000000000..fa1bd4cbdfb3 --- /dev/null +++ b/compilerplugins/clang/store/shouldreturnbool.cxx @@ -0,0 +1,248 @@ +/* -*- 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 <string> +#include <set> +#include <iostream> + +#include "config_clang.h" + +#include "check.hxx" +#include "compat.hxx" +#include "plugin.hxx" +#include "functionaddress.hxx" + +/* + Look for functions that only return 1 and/or 0, which sometimes indicates that the + function should be returning bool. + + Note that is partly a question of taste and code style, which is why this plugin is off by default. +*/ + +namespace +{ +class ShouldReturnBool + : public loplugin::FunctionAddress<loplugin::FilteringPlugin<ShouldReturnBool>> +{ +public: + explicit ShouldReturnBool(loplugin::InstantiationData const& data) + : FunctionAddress(data) + { + } + + virtual void run() override + { + if (!compiler.getLangOpts().CPlusPlus) + return; + StringRef fn(handler.getMainFileName()); + // functions used as function pointers + if (loplugin::isSamePathname(fn, SRCDIR "/sal/rtl/alloc_cache.cxx")) + return; + // false +, slightly odd usage, but not wrong + if (loplugin::isSamePathname(fn, SRCDIR "/libreofficekit/qa/tilebench/tilebench.cxx")) + return; + // template magic + if (loplugin::isSamePathname(fn, SRCDIR "/vcl/source/gdi/bmpfast.cxx")) + return; + // fine + if (loplugin::isSamePathname(fn, SRCDIR "/svl/unx/source/svdde/ddedummy.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/vcl/source/opengl/OpenGLHelper.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/svtools/source/misc/imap2.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/svx/source/dialog/docrecovery.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/hwpfilter/source/lexer.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/hwpfilter/source/grammar.cxx")) + return; + if (loplugin::isSamePathname( + fn, SRCDIR "/connectivity/source/drivers/odbc/ODatabaseMetaDataResultSet.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/dbaccess/source/ui/browser/dsEntriesNoExp.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/lotuswordpro/source/filter/explode.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/filter/source/graphicfilter/ipict/ipict.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/core/data/dptabsrc.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/ui/docshell/docsh3.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/sd/source/ui/dlg/masterlayoutdlg.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/sd/source/filter/ppt/pptinanimations.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/vcl/unx/generic/app/i18n_im.cxx")) + return; + + // callback + if (loplugin::isSamePathname(fn, SRCDIR "/sax/source/expatwrap/sax_expat.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/xmlsecurity/source/xmlsec/xmlstreamio.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/sw/source/filter/ww8/ww8par.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/sw/source/filter/ww8/ww8par2.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/sw/source/filter/ww8/ww8par5.cxx")) + return; + // SaxWriterHelper::writeSequence a little weird + if (loplugin::isSamePathname(fn, SRCDIR "/sax/source/expatwrap/saxwriter.cxx")) + return; + // main function + if (loplugin::isSamePathname(fn, SRCDIR "/xmlsecurity/workben/pdfverify.cxx")) + return; + + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + + for (auto functionDecl : problemFunctions) + { + auto canonicalDecl = functionDecl->getCanonicalDecl(); + if (getFunctionsWithAddressTaken().find(canonicalDecl) + != getFunctionsWithAddressTaken().end()) + continue; + report(DiagnosticsEngine::Warning, + "only returning one or zero is an indication you want to return bool", + functionDecl->getBeginLoc()) + << functionDecl->getSourceRange(); + if (canonicalDecl->getLocation() != functionDecl->getLocation()) + { + report(DiagnosticsEngine::Note, "canonical function declaration here", + canonicalDecl->getBeginLoc()) + << canonicalDecl->getSourceRange(); + } + } + } + + bool TraverseFunctionDecl(FunctionDecl*); + bool TraverseCXXMethodDecl(CXXMethodDecl*); + bool VisitReturnStmt(ReturnStmt const*); + +private: + bool mbInsideFunction = false; + bool mbFunctionOnlyReturningOneOrZero = false; + std::unordered_set<FunctionDecl const*> problemFunctions; + + bool IsInteresting(FunctionDecl const*); + void Report(FunctionDecl const*) const; + bool isExprOneOrZero(Expr const*) const; +}; + +bool ShouldReturnBool::TraverseFunctionDecl(FunctionDecl* functionDecl) +{ + bool ret; + if (IsInteresting(functionDecl)) + { + mbInsideFunction = true; + mbFunctionOnlyReturningOneOrZero = true; + ret = FunctionAddress::TraverseFunctionDecl(functionDecl); + mbInsideFunction = false; + if (mbFunctionOnlyReturningOneOrZero) + problemFunctions.insert(functionDecl); + } + else + ret = FunctionAddress::TraverseFunctionDecl(functionDecl); + return ret; +} + +bool ShouldReturnBool::TraverseCXXMethodDecl(CXXMethodDecl* methodDecl) +{ + bool ret; + if (IsInteresting(methodDecl)) + { + mbInsideFunction = true; + mbFunctionOnlyReturningOneOrZero = true; + ret = FunctionAddress::TraverseCXXMethodDecl(methodDecl); + mbInsideFunction = false; + if (mbFunctionOnlyReturningOneOrZero) + problemFunctions.insert(methodDecl); + } + else + ret = FunctionAddress::TraverseCXXMethodDecl(methodDecl); + return ret; +} + +bool ShouldReturnBool::IsInteresting(FunctionDecl const* functionDecl) +{ + if (ignoreLocation(functionDecl)) + return false; + // ignore stuff that forms part of the stable URE interface + if (isInUnoIncludeFile(functionDecl)) + return false; + if (functionDecl->getTemplatedKind() != FunctionDecl::TK_NonTemplate) + return false; + if (!functionDecl->isThisDeclarationADefinition()) + return false; + if (functionDecl->isMain()) + return false; + if (functionDecl->isExternC() || functionDecl->isInExternCContext()) + return false; + auto methodDecl = dyn_cast<CXXMethodDecl>(functionDecl); + if (methodDecl && methodDecl->isVirtual()) + return false; + auto tc = loplugin::TypeCheck(functionDecl->getReturnType()); + if (tc.AnyBoolean() || tc.Void()) + return false; + auto returnType = functionDecl->getReturnType(); + if (returnType->isEnumeralType() || !returnType->getUnqualifiedDesugaredType()->isIntegerType()) + return false; + // Ignore functions that contains #ifdef-ery + if (containsPreprocessingConditionalInclusion(functionDecl->getSourceRange())) + return false; + + // not sure what basegfx is doing here + StringRef fileName{ getFilenameOfLocation(functionDecl->getLocation()) }; + if (loplugin::isSamePathname(fileName, SRCDIR "/include/basegfx/range/basicrange.hxx")) + return false; + // false + + if (loplugin::isSamePathname(fileName, SRCDIR "/include/svl/macitem.hxx")) + return false; + if (loplugin::isSamePathname(fileName, SRCDIR "/lotuswordpro/source/filter/lwpcharsetmgr.hxx")) + return false; + if (loplugin::isSamePathname(fileName, SRCDIR "/sc/inc/dptabsrc.hxx")) + return false; + + return true; +} + +bool ShouldReturnBool::VisitReturnStmt(const ReturnStmt* returnStmt) +{ + if (!mbInsideFunction) + return true; + if (!returnStmt->getRetValue()) + return true; + if (loplugin::TypeCheck(returnStmt->getRetValue()->getType()).AnyBoolean()) + return true; + if (!isExprOneOrZero(returnStmt->getRetValue())) + mbFunctionOnlyReturningOneOrZero = false; + return true; +} + +bool ShouldReturnBool::isExprOneOrZero(const Expr* arg) const +{ + arg = arg->IgnoreParenCasts(); + // ignore this, it seems to trigger an infinite recursion + if (isa<UnaryExprOrTypeTraitExpr>(arg)) + { + return false; + } + APSInt x1; + if (compat::EvaluateAsInt(arg, x1, compiler.getASTContext())) + { + return x1 == 1 || x1 == 0; + } + return false; +} + +loplugin::Plugin::Registration<ShouldReturnBool> X("shouldreturnbool", false); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/staticvar.cxx b/compilerplugins/clang/store/staticvar.cxx new file mode 100644 index 000000000000..21cbd0f08172 --- /dev/null +++ b/compilerplugins/clang/store/staticvar.cxx @@ -0,0 +1,213 @@ +/* -*- 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 <cassert> +#include <string> +#include <iostream> +#include <unordered_map> +#include <unordered_set> + +#include "plugin.hxx" +#include "check.hxx" +#include "config_clang.h" +#include "clang/AST/CXXInheritance.h" +#include "clang/AST/StmtVisitor.h" + +// Look for variables that either +// (a) could be statically initialised, without runtime code, and warn +// (b) variables that are statically declared, but require runtime initialisation, and warn +// +// e.g. +// static const OUString[] XXX { "xxx" }; +// requires runtime initialisation, so should rather be declared as OUStringLiteral +// and +// static int[] XXX { 1,2 }; +// can be declared const since it does not require runtime initialisation. + +namespace +{ +class StaticVar : public loplugin::FilteringPlugin<StaticVar> +{ +public: + explicit StaticVar(loplugin::InstantiationData const& data) + : FilteringPlugin(data) + { + } + + virtual void run() override + { + std::string fn(handler.getMainFileName()); + loplugin::normalizeDotDotInFilePath(fn); + + if ( + // uses icu::UnicodeString + fn == SRCDIR "/l10ntools/source/xmlparse.cxx" + // contains mutable state + || fn == SRCDIR "/sal/osl/unx/signal.cxx" + || fn == SRCDIR "/sal/qa/rtl/digest/rtl_digest.cxx" + || fn == SRCDIR "/sal/qa/rtl/strings/test_oustring_endswith.cxx" + || fn == SRCDIR "/sal/qa/rtl/strings/test_oustring_convert.cxx" + || fn == SRCDIR "/svl/qa/unit/items/test_itempool.cxx" + // contains mutable state + || fn == SRCDIR "/vcl/unx/generic/dtrans/X11_selection.cxx" + || fn == SRCDIR "/sax/qa/cppunit/xmlimport.cxx" + || fn == SRCDIR "/pyuno/source/module/pyuno.cxx" + || fn == SRCDIR "/pyuno/source/module/pyuno_module.cxx" + || fn == SRCDIR "/pyuno/source/module/pyuno_struct.cxx" + // TODO for this one we need a static OUString + || fn == SRCDIR "/xmloff/source/core/xmltoken.cxx" + // mutable + || fn == SRCDIR "/basic/source/runtime/stdobj.cxx" + // TODO this needs more extensive cleanup + || fn == SRCDIR "/connectivity/source/drivers/postgresql/pq_statics.cxx" + // mutable + || fn == SRCDIR "/hwpfilter/source/hwpreader.cxx" + // mutable + || fn == SRCDIR "/sw/source/filter/basflt/fltini.cxx" + // mutable + || fn == SRCDIR "/sw/source/uibase/docvw/srcedtw.cxx" + // mutable + || fn == SRCDIR "/forms/source/misc/limitedformats.cxx" + // aHTMLOptionTab is ordered by useful grouping, so let it sort at runtime + || fn == SRCDIR "/svtools/source/svhtml/htmlkywd.cxx" + // TODO sorting some of these tables will be a lot of work... + || fn == SRCDIR "/sw/source/filter/ww8/ww8par6.cxx" + // this only triggers on older versions of clang, not sure why + // in any case, it is actually about the array in vcl/inc/units.hrc, which we can't change + || fn == SRCDIR "/vcl/source/app/svdata.cxx" + // I tried doing this, but got very weird unit test failures, apparently sorting this table + // disturbs some code elsewhere + || fn == SRCDIR "/svx/source/unodraw/unoprov.cxx" + // aRTFTokenTab is ordered by useful grouping, so let it sort at runtime + || fn == SRCDIR "/svtools/source/svrtf/rtfkeywd.cxx") + return; + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + } + + bool VisitVarDecl(VarDecl const*); +}; + +static bool containsNonLiteral(Expr const* expr) +{ + expr = expr->IgnoreImplicit(); + if (auto initList = dyn_cast<InitListExpr>(expr)) + { + for (unsigned i = 0; i < initList->getNumInits(); ++i) + if (containsNonLiteral(initList->getInit(i))) + return true; + } + else if (auto constructExpr = dyn_cast<CXXConstructExpr>(expr)) + { + for (Expr const* arg : constructExpr->arguments()) + if (containsNonLiteral(arg)) + return true; + } + else if (isa<MemberExpr>(expr)) + return true; + else if (auto declRefExpr = dyn_cast<DeclRefExpr>(expr)) + { + auto varDecl = dyn_cast_or_null<VarDecl>(declRefExpr->getDecl()); + return varDecl && varDecl->isLocalVarDeclOrParm(); + } + else if (isa<CXXMemberCallExpr>(expr)) + return true; + else if (auto castExpr = dyn_cast<CXXFunctionalCastExpr>(expr)) + return containsNonLiteral(castExpr->getSubExpr()); + else if (auto unaryOp = dyn_cast<UnaryOperator>(expr)) + return containsNonLiteral(unaryOp->getSubExpr()); + + return false; +} + +bool StaticVar::VisitVarDecl(VarDecl const* varDecl) +{ + if (ignoreLocation(varDecl)) + return true; + if (!varDecl->hasInit()) + return true; + auto initList = dyn_cast_or_null<InitListExpr>(varDecl->getInit()); + if (!initList) + return true; + if (varDecl->isExceptionVariable() || isa<ParmVarDecl>(varDecl)) + return true; + if (!varDecl->getType()->isArrayType()) + return true; + auto elementType = varDecl->getType()->getBaseElementTypeUnsafe(); + if (!elementType->isRecordType()) + return true; + auto elementRecordDecl + = dyn_cast_or_null<CXXRecordDecl>(elementType->getAs<RecordType>()->getDecl()); + if (!elementRecordDecl) + return true; + if (containsNonLiteral(initList)) + return true; + + if (elementRecordDecl->hasTrivialDestructor()) + { + if (varDecl->isLocalVarDecl()) + { + if (varDecl->getStorageDuration() == SD_Static && varDecl->getType().isConstQualified()) + return true; + } + else + { + if (varDecl->getType().isConstQualified()) + return true; + } + + // TODO cannot figure out how to make the loplugin::TypeCheck stuff match this + // std::string typeName = varDecl->getType().getAsString(); + // if (typeName == "std::va_list" || typeName == "va_list") + // return true; + + auto const tcElement = loplugin::TypeCheck(elementRecordDecl); + if (tcElement.Struct("ContextID_Index_Pair").GlobalNamespace()) + return true; + if (tcElement.Class("SfxSlot").GlobalNamespace()) + return true; + + if (varDecl->isLocalVarDecl()) + report(DiagnosticsEngine::Warning, "var should be static const, or allowlisted", + varDecl->getLocation()) + << varDecl->getSourceRange(); + else + report(DiagnosticsEngine::Warning, "var should be const, or allowlisted", + varDecl->getLocation()) + << varDecl->getSourceRange(); + } + else + { + if (varDecl->isLocalVarDecl()) + { + if (varDecl->getStorageDuration() != SD_Static + || !varDecl->getType().isConstQualified()) + return true; + } + else + { + if (!varDecl->getType().isConstQualified()) + return true; + } + + if (varDecl->isLocalVarDecl()) + report(DiagnosticsEngine::Warning, "static const var requires runtime initialization?", + varDecl->getLocation()) + << varDecl->getSourceRange(); + else + report(DiagnosticsEngine::Warning, "static var requires runtime initialization?", + varDecl->getLocation()) + << varDecl->getSourceRange(); + } + return true; +} + +loplugin::Plugin::Registration<StaticVar> X("staticvar", false); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/stringliteraldefine.cxx b/compilerplugins/clang/store/stringliteraldefine.cxx new file mode 100644 index 000000000000..8d7e778051e1 --- /dev/null +++ b/compilerplugins/clang/store/stringliteraldefine.cxx @@ -0,0 +1,171 @@ +/* -*- 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/. + */ + +// Find constant character array variables that are either +// (a) passed into O[U]String constructors +// (b) assigned to O[U]String +// and are declared using macro names +// and should thus be turned into O[U]StringLiteral variables. +// + +#include <cassert> + +#include "config_clang.h" + +#include "check.hxx" +#include "plugin.hxx" + +namespace +{ +class StringLiteralDefine final : public loplugin::FilteringPlugin<StringLiteralDefine> +{ +public: + explicit StringLiteralDefine(loplugin::InstantiationData const& data) + : FilteringPlugin(data) + { + } + + bool TraverseInitListExpr(InitListExpr* expr, DataRecursionQueue* queue = nullptr) + { + return WalkUpFromInitListExpr(expr) + && TraverseSynOrSemInitListExpr( + expr->isSemanticForm() ? expr : expr->getSemanticForm(), queue); + } + + bool VisitCXXConstructExpr(CXXConstructExpr const* expr) + { + if (ignoreLocation(expr)) + return true; + loplugin::TypeCheck const tc(expr->getType()); + if (!(tc.Class("OString").Namespace("rtl").GlobalNamespace() + || tc.Class("OUString").Namespace("rtl").GlobalNamespace())) + { + return true; + } + auto const ctor = expr->getConstructor(); + if (ctor->getNumParams() != 2) + return true; + + const Expr* arg0 = expr->getArg(0)->IgnoreParenImpCasts(); + auto const e1 = dyn_cast<clang::StringLiteral>(arg0); + if (!e1) + return true; + auto argLoc = arg0->getBeginLoc(); + // check if the arg is a macro + auto macroLoc = compiler.getSourceManager().getSpellingLoc(argLoc); + if (argLoc == macroLoc) + return true; + // check if it is the right kind of macro (not particularly reliable checks) + if (!macroLoc.isValid() || !compiler.getSourceManager().isInMainFile(macroLoc) + || compiler.getSourceManager().isInSystemHeader(macroLoc) + || compiler.getSourceManager().isWrittenInBuiltinFile(macroLoc) + || compiler.getSourceManager().isWrittenInScratchSpace(macroLoc) + || compiler.getSourceManager().isWrittenInCommandLineFile(macroLoc) + || isInUnoIncludeFile(macroLoc)) + return true; + StringRef fileName = getFilenameOfLocation(macroLoc); + StringRef name{ Lexer::getImmediateMacroName( + arg0->getBeginLoc(), compiler.getSourceManager(), compiler.getLangOpts()) }; + if (loplugin::hasPathnamePrefix(fileName, SRCDIR "/config_host/")) + return true; + // used in both OUString and OString context + if (name == "FM_COL_LISTBOX" || name == "HID_RELATIONDIALOG_LEFTFIELDCELL" + || name == "OOO_HELP_INDEX" || name == "IMP_PNG" || name.startswith("MNI_ACTION_")) + return true; + if (loplugin::hasPathnamePrefix(fileName, SRCDIR "/svx/source/stbctrls/pszctrl.cxx")) + return true; + // used as a prefix and/or concatenated with other strings + if (name.startswith("UNO_JAVA_JFW") || name == "SETNODE_BINDINGS" || name == "PATHDELIMITER" + || name == "SETNODE_ALLFILEFORMATS" || name == "SETNODE_DISABLED" + || name == "XMLNS_DIALOGS_PREFIX" || name == "XMLNS_LIBRARY_PREFIX" + || name == "XMLNS_SCRIPT_PREFIX" || name == "XMLNS_TOOLBAR" || name == "XMLNS_XLINK" + || name == "XMLNS_XLINK_PREFIX") + return true; + if (loplugin::hasPathnamePrefix(fileName, + SRCDIR "/stoc/source/security/access_controller.cxx") + && (name == "SERVICE_NAME" || name == "USER_CREDS")) + return true; + if (loplugin::hasPathnamePrefix(fileName, SRCDIR "/stoc/source/security/file_policy.cxx") + && name == "IMPL_NAME") + return true; + if (loplugin::hasPathnamePrefix(fileName, + SRCDIR "/desktop/source/migration/services/jvmfwk.cxx") + && name == "IMPL_NAME") + return true; + if (loplugin::hasPathnamePrefix( + fileName, SRCDIR "/xmlsecurity/source/xmlsec/xmldocumentwrapper_xmlsecimpl.cxx") + && name == "STRXMLNS") + return true; + if (loplugin::hasPathnamePrefix(fileName, SRCDIR "/sw/source/ui/fldui/fldvar.cxx") + && name == "USER_DATA_VERSION_1") + return true; + // not sure how to exclude the case where the whole block is in a macro + // (vs. what I am looking for - regular code with a macro name as the argument) + if (name == "assert" || name == "SAL_INFO" || name == "DECLIMPL_SERVICEINFO_DERIVED" + || name == "OSL_VERIFY" || name == "OSL_ENSURE" || name == "DECL_PROP_2" + || name == "DECL_PROP_3" || name == "DECL_PROP_1" || name == "DECL_DEP_PROP_2" + || name == "DECL_DEP_PROP_3" || name == "CALL_ELEMENT_HANDLER_AND_CARE_FOR_EXCEPTIONS" + || name == "IMPLEMENT_SERVICE_INFO" || name == "SQL_GET_REFERENCES" + || name == "SFX_IMPL_OBJECTFACTORY" || name == "IMPLEMENT_SERVICE_INFO1" + || name == "IMPLEMENT_SERVICE_INFO2" || name == "IMPLEMENT_SERVICE_INFO3" + || name == "IMPLEMENT_SERVICE_INFO_IMPLNAME" || name == "SC_SIMPLE_SERVICE_INFO" + || name == "SC_SIMPLE_SERVICE_INFO_COMPAT" || name == "OUT_COMMENT" + || name == "LOCALE_EN" || name == "LOCALE" || name == "VBAFONTBASE_PROPNAME" + || name == "VBAHELPER_IMPL_XHELPERINTERFACE" || name == "IMPRESS_MAP_ENTRIES" + || name == "DRAW_MAP_ENTRIES" || name == "DRAW_PAGE_NOTES_PROPERTIES" + || name == "COMMON_FLDTYP_PROPERTIES" || name == "GRAPHIC_PAGE_PROPERTIES" + || name == "makeDelay" || name == "makeEvent" || name == "OOO_IMPORTER" + || name == "DBG_ASSERT" || name.startswith("CPPUNIT_ASSERT")) + return true; + if (loplugin::hasPathnamePrefix(fileName, SRCDIR + "/dbaccess/source/ui/querydesign/SelectionBrowseBox.cxx") + && name == "DEFAULT_SIZE") + return true; + if (loplugin::hasPathnamePrefix(fileName, SRCDIR "/filter/source/t602/t602filter.cxx")) + return true; + if (loplugin::hasPathnamePrefix(fileName, SRCDIR "/hwpfilter/source/formula.cxx")) + return true; + if (loplugin::hasPathnamePrefix(fileName, SRCDIR "/hwpfilter/source/hwpreader.cxx")) + return true; + if (loplugin::hasPathnamePrefix(fileName, SRCDIR "/filter/source/svg/svgexport.cxx") + && name == "NSPREFIX") + return true; + + if (!reported_.insert(macroLoc).second) + return true; + + report(DiagnosticsEngine::Warning, + "change macro '%0' to 'constexpr " + "%select{OStringLiteral|OUStringLiteral}1'", + macroLoc) + << name << (tc.Class("OString").Namespace("rtl").GlobalNamespace() ? 0 : 1); + report(DiagnosticsEngine::Note, "macro used here", arg0->getBeginLoc()) + << arg0->getSourceRange(); + return true; + } + + bool preRun() override { return compiler.getLangOpts().CPlusPlus; } + +private: + void run() override + { + if (preRun()) + { + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + } + } + + std::set<SourceLocation> reported_; +}; + +// Off by default because it needs some hand-holding +static loplugin::Plugin::Registration<StringLiteralDefine> reg("stringliteraldefine", false); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/compilerplugins/clang/store/stringloop.cxx b/compilerplugins/clang/store/stringloop.cxx new file mode 100644 index 000000000000..3bae1a225b1e --- /dev/null +++ b/compilerplugins/clang/store/stringloop.cxx @@ -0,0 +1,292 @@ +/* -*- 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 "check.hxx" +#include "plugin.hxx" +#include "config_clang.h" +#include <vector> + +/** Look for OUString/OString being appended to inside a loop, where OUStringBuffer/OStringBuffer would be a better idea + */ +namespace +{ +class StringLoop : public clang::RecursiveASTVisitor<StringLoop>, public loplugin::Plugin +{ +public: + explicit StringLoop(loplugin::InstantiationData const& rData) + : Plugin(rData) + { + } + + void run() override; + bool TraverseForStmt(ForStmt*); + bool TraverseCXXForRangeStmt(CXXForRangeStmt*); + bool TraverseDoStmt(DoStmt*); + bool TraverseWhileStmt(WhileStmt*); + bool VisitVarDecl(VarDecl const*); + bool VisitCallExpr(CallExpr const*); + +private: + int m_insideLoop = 0; + using VarDeclList = std::vector<VarDecl const*>; + std::vector<VarDeclList> m_varsPerLoopLevel; +}; + +void StringLoop::run() +{ + // Various places are not worth changing, the code becomes too awkward + // Just exclude stuff as I go + StringRef fn(handler.getMainFileName()); + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/bridges/")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/cppuhelper/source/shlib.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/registry/source/regimpl.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/l10ntools/source/lngmerge.cxx")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/tools/qa/")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/jvmfwk/")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/svl/source/passwordcontainer/passwordcontainer.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/svl/source/numbers/zformat.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/svl/source/numbers/zforscan.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/vcl/source/control/combobox.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/vcl/source/gdi/pdfwriter_impl.cxx")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/svtools/")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/idl/")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/framework/")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/basic/")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sfx2/")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/avmedia/")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/connectivity/")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/editeng/")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/svx/")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/basctl/")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/filter/")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/chart2/")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/cui/")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/dbaccess/")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/oox/")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/writerfilter/")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/desktop/")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/extensions/")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/dtrans/")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/i18npool/")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/embeddedobj/")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sd/")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/xmloff/")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/xmlhelp/")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/forms/")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sc/source/core/tool/address.cxx")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sc/source/core/tool/compiler.cxx")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sc/source/ui/docshell/impex.cxx")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sc/source/ui/miscdlgs/acredlin.cxx")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sc/source/ui/pagedlg/areasdlg.cxx")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sc/source/ui/view/gridwin2.cxx")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sc/source/filter/html/htmlpars.cxx")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sw/source/core/doc/doctxm.cxx")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sw/source/core/edit/edattr.cxx")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sw/source/core/layout/dbg_lay.cxx")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sw/source/filter/ascii/ascatr.cxx")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sw/source/filter/html/htmlforw.cxx")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sw/source/core/unocore/unosect.cxx")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sw/source/core/unocore/unochart.cxx")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sw/source/core/unocore/unoobj.cxx")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sw/source/filter/html/parcss1.cxx")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sw/source/filter/html/svxcss1.cxx")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sw/source/filter/html/swhtml.cxx")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sw/source/uibase/utlui/gloslst.cxx")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sw/source/uibase/utlui/content.cxx")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sw/source/uibase/docvw/edtwin.cxx")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sw/source/filter/ww8/ww8atr.cxx")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sw/source/filter/ww8/ww8scan.cxx")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sw/source/filter/ww8/ww8par5.cxx")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sw/source/ui/fldui/fldfunc.cxx")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sw/source/ui/misc/bookmark.cxx")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sw/source/ui/dbui/mmlayoutpage.cxx")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sw/source/ui/dbui/dbinsdlg.cxx")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sw/source/ui/dbui/mmresultdialogs.cxx")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sw/source/ui/index/cnttab.cxx")) + return; + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/ucb/source/ucp/file/bc.cxx")) + return; + + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); +} + +bool StringLoop::TraverseForStmt(ForStmt* stmt) +{ + ++m_insideLoop; + m_varsPerLoopLevel.push_back({}); + auto const ret = RecursiveASTVisitor::TraverseForStmt(stmt); + m_varsPerLoopLevel.pop_back(); + --m_insideLoop; + return ret; +} + +bool StringLoop::TraverseCXXForRangeStmt(CXXForRangeStmt* stmt) +{ + ++m_insideLoop; + m_varsPerLoopLevel.push_back({}); + auto const ret = RecursiveASTVisitor::TraverseCXXForRangeStmt(stmt); + m_varsPerLoopLevel.pop_back(); + --m_insideLoop; + return ret; +} + +bool StringLoop::TraverseDoStmt(DoStmt* stmt) +{ + ++m_insideLoop; + m_varsPerLoopLevel.push_back({}); + auto const ret = RecursiveASTVisitor::TraverseDoStmt(stmt); + m_varsPerLoopLevel.pop_back(); + --m_insideLoop; + return ret; +} + +bool StringLoop::TraverseWhileStmt(WhileStmt* stmt) +{ + ++m_insideLoop; + m_varsPerLoopLevel.push_back({}); + auto const ret = RecursiveASTVisitor::TraverseWhileStmt(stmt); + m_varsPerLoopLevel.pop_back(); + --m_insideLoop; + return ret; +} + +bool StringLoop::VisitVarDecl(VarDecl const* varDecl) +{ + if (ignoreLocation(varDecl)) + return true; + if (!m_insideLoop) + return true; + m_varsPerLoopLevel.back().push_back(varDecl); + return true; +} + +bool StringLoop::VisitCallExpr(CallExpr const* callExpr) +{ + if (ignoreLocation(callExpr)) + return true; + if (!m_insideLoop) + return true; + auto operatorCallExpr = dyn_cast<CXXOperatorCallExpr>(callExpr); + if (!operatorCallExpr) + return true; + if (operatorCallExpr->getOperator() != OO_PlusEqual) + return true; + + if (auto memberExpr = dyn_cast<MemberExpr>(callExpr->getArg(0))) + { + auto tc = loplugin::TypeCheck(memberExpr->getType()); + if (!tc.Class("OUString").Namespace("rtl").GlobalNamespace() + && !tc.Class("OString").Namespace("rtl").GlobalNamespace()) + return true; + auto fieldDecl = dyn_cast<FieldDecl>(memberExpr->getMemberDecl()); + if (isInUnoIncludeFile( + compiler.getSourceManager().getSpellingLoc(fieldDecl->getLocation()))) + return true; + if (ignoreLocation(compiler.getSourceManager().getSpellingLoc(fieldDecl->getLocation()))) + return true; + report(DiagnosticsEngine::Warning, + "appending to OUString in loop, rather use OUStringBuffer", + operatorCallExpr->getBeginLoc()) + << operatorCallExpr->getSourceRange(); + report(DiagnosticsEngine::Note, "field here", fieldDecl->getBeginLoc()) + << fieldDecl->getSourceRange(); + } + else if (auto declRefExpr = dyn_cast<DeclRefExpr>(callExpr->getArg(0))) + { + if (auto varDecl = dyn_cast<VarDecl>(declRefExpr->getDecl())) + { + auto tc = loplugin::TypeCheck(varDecl->getType()); + if (!tc.Class("OUString").Namespace("rtl").GlobalNamespace() + && !tc.Class("OString").Namespace("rtl").GlobalNamespace()) + return true; + // if the var is at the same block scope as the +=, not interesting + auto vars = m_varsPerLoopLevel.back(); + if (std::find(vars.begin(), vars.end(), varDecl) != vars.end()) + return true; + report(DiagnosticsEngine::Warning, + "appending to OUString in loop, rather use OUStringBuffer", + operatorCallExpr->getBeginLoc()) + << operatorCallExpr->getSourceRange(); + report(DiagnosticsEngine::Note, "var here", varDecl->getBeginLoc()) + << varDecl->getSourceRange(); + } + } + return true; +} + +loplugin::Plugin::Registration<StringLoop> X("stringloop", false); + +} // namespace + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/unusedfieldsremove.cxx b/compilerplugins/clang/store/unusedfieldsremove.cxx new file mode 100644 index 000000000000..61df036f2c62 --- /dev/null +++ b/compilerplugins/clang/store/unusedfieldsremove.cxx @@ -0,0 +1,137 @@ +/* -*- 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/. + */ + +#if !defined _WIN32 //TODO, #include <sys/mman.h> + +#include <cassert> +#include <string> +#include <iostream> +#include "config_clang.h" +#include "plugin.hxx" +#include <sys/mman.h> +#include <sys/types.h> +#include <fcntl.h> +#include <unistd.h> +#include <sys/stat.h> +#include <assert.h> +#include <cstring> + +/** + This is intended to be run as the second stage of the "unusedfields" clang plugin. +*/ + +namespace { + +class UnusedFieldsRemove: + public loplugin::FilteringRewritePlugin<UnusedFieldsRemove> +{ +public: + explicit UnusedFieldsRemove(loplugin::InstantiationData const & data); + ~UnusedFieldsRemove(); + + virtual void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); } + + bool VisitFieldDecl( const FieldDecl* var ); +private: + // I use a brute-force approach - mmap the results file and do a linear search on it + // It works surprisingly well, because the file is small enough to fit into L2 cache on modern CPU's + size_t mmapFilesize; + int mmapFD; + char* mmappedData; +}; + +size_t getFilesize(const char* filename) +{ + struct stat st; + stat(filename, &st); + return st.st_size; +} + +UnusedFieldsRemove::UnusedFieldsRemove(loplugin::InstantiationData const & data): FilteringRewritePlugin(data) +{ + static const char sInputFile[] = SRCDIR "/result.txt"; + mmapFilesize = getFilesize(sInputFile); + //Open file + mmapFD = open(sInputFile, O_RDONLY, 0); + assert(mmapFD != -1); + //Execute mmap + mmappedData = static_cast<char*>(mmap(NULL, mmapFilesize, PROT_READ, MAP_PRIVATE, mmapFD, 0)); + assert(mmappedData != NULL); +} + +UnusedFieldsRemove::~UnusedFieldsRemove() +{ + //Cleanup + int rc = munmap(mmappedData, mmapFilesize); + assert(rc == 0); + (void)rc; + close(mmapFD); +} + +std::string niceName(const FieldDecl* fieldDecl) +{ + return fieldDecl->getParent()->getQualifiedNameAsString() + " " + + fieldDecl->getNameAsString(); +} + +bool UnusedFieldsRemove::VisitFieldDecl( const FieldDecl* fieldDecl ) +{ + if (rewriter == nullptr) { + return true; + } + if (ignoreLocation(fieldDecl)) { + return true; + } + // ignore stuff that forms part of the stable URE interface + if (isInUnoIncludeFile(compiler.getSourceManager().getSpellingLoc( + fieldDecl->getCanonicalDecl()->getLocation()))) { + return true; + } + + // don't mess with templates +/* if (isa<CXXRecordDecl>(fieldDecl->getParent())) { + if (dyn_cast<CXXRecordDecl>(fieldDecl->getParent())->getDescribedClassTemplate() != nullptr) { + return true; + } + } +*/ + std::string aNiceName = " " + niceName(fieldDecl) + "\n"; + const char *aNiceNameStr = aNiceName.c_str(); + char* found = std::search(mmappedData, mmappedData + mmapFilesize, aNiceNameStr, aNiceNameStr + strlen(aNiceNameStr)); + if(!(found < mmappedData + mmapFilesize)) { + return true; + } + SourceRange replaceRange(fieldDecl->getSourceRange()); + // sometimes the declaration has a semicolon just after it, and it's much neater to remove that too. + if (rewriter->getRewrittenText(SourceRange(replaceRange.getEnd(), replaceRange.getEnd().getLocWithOffset(1))) == ";") { + replaceRange.setEnd(replaceRange.getEnd().getLocWithOffset(1)); + } + // remove leading spaces + while (rewriter->getRewrittenText(SourceRange(replaceRange.getBegin().getLocWithOffset(-1), replaceRange.getBegin())) == " ") + { + replaceRange.setBegin(replaceRange.getBegin().getLocWithOffset(-1)); + } + if (!replaceText(replaceRange, "")) { + report( + DiagnosticsEngine::Warning, + "Could not remove unused field (" + niceName(fieldDecl) + ")", + fieldDecl->getBeginLoc()) + << fieldDecl->getSourceRange(); + } + return true; +} + + +loplugin::Plugin::Registration< UnusedFieldsRemove > X("unusedfieldsremove", false); + +} + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/unusedindex.cxx b/compilerplugins/clang/store/unusedindex.cxx new file mode 100644 index 000000000000..63b9d4dcaeaf --- /dev/null +++ b/compilerplugins/clang/store/unusedindex.cxx @@ -0,0 +1,87 @@ + +/* -*- 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 <string> +#include <iostream> +#include <unordered_set> + +#include "plugin.hxx" +#include "check.hxx" +#include "clang/AST/CXXInheritance.h" +#include "clang/AST/StmtVisitor.h" + +/* + Mike Kaganski found a bug where the code was looping over a block and + not using the index var, and the loop was unnecessary. + So he wanted to have a look for other places like that. +*/ +namespace +{ +class UnusedIndex : public loplugin::FilteringPlugin<UnusedIndex> +{ +public: + explicit UnusedIndex(loplugin::InstantiationData const& data) + : FilteringPlugin(data) + { + } + + virtual void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); } + + bool TraverseForStmt(ForStmt* stmt); + bool VisitDeclRefExpr(DeclRefExpr const* stmt); + +private: + std::vector<VarDecl const*> mLoopVarDecls; + std::unordered_set<VarDecl const*> mFoundSet; +}; + +bool UnusedIndex::TraverseForStmt(ForStmt* stmt) +{ + if (ignoreLocation(stmt)) + return true; + + VarDecl const* loopVarDecl = nullptr; + if (stmt->getInit()) + { + auto declStmt = dyn_cast<DeclStmt>(stmt->getInit()); + if (declStmt && declStmt->isSingleDecl()) + { + loopVarDecl = dyn_cast<VarDecl>(declStmt->getSingleDecl()); + } + } + if (loopVarDecl) + mLoopVarDecls.push_back(loopVarDecl); + + // deliberately ignore the other parts of the for stmt, except for the body + auto ret = RecursiveASTVisitor::TraverseStmt(stmt->getBody()); + + if (loopVarDecl && mFoundSet.erase(loopVarDecl) == 0) + report(DiagnosticsEngine::Warning, "loop variable not used", loopVarDecl->getBeginLoc()) + << loopVarDecl->getSourceRange(); + + if (loopVarDecl) + mLoopVarDecls.pop_back(); + return ret; +} + +bool UnusedIndex::VisitDeclRefExpr(DeclRefExpr const* stmt) +{ + auto varDecl = dyn_cast<VarDecl>(stmt->getDecl()); + if (!varDecl) + return true; + if (std::find(mLoopVarDecls.begin(), mLoopVarDecls.end(), varDecl) != mLoopVarDecls.end()) + mFoundSet.insert(varDecl); + return true; +} + +loplugin::Plugin::Registration<UnusedIndex> X("unusedindex", false); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/unusedmethodsremove.cxx b/compilerplugins/clang/store/unusedmethodsremove.cxx new file mode 100644 index 000000000000..ff87c6b7771a --- /dev/null +++ b/compilerplugins/clang/store/unusedmethodsremove.cxx @@ -0,0 +1,153 @@ +/* -*- 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/. + */ + +#if !defined _WIN32 //TODO, #include <sys/mman.h> + +#include <cassert> +#include <string> +#include <iostream> +#include "config_clang.h" +#include "plugin.hxx" +#include <sys/mman.h> +#include <sys/types.h> +#include <fcntl.h> +#include <unistd.h> +#include <sys/stat.h> +#include <assert.h> +#include <cstring> + +/** + This is intended to be run as the second stage of the "unusedmethods" clang plugin. +*/ + +namespace { + +class UnusedMethodsRemove: + public loplugin::FilteringRewritePlugin<UnusedMethodsRemove> +{ +public: + explicit UnusedMethodsRemove(loplugin::InstantiationData const & data); + ~UnusedMethodsRemove(); + + virtual void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); } + + bool VisitCXXMethodDecl( const CXXMethodDecl* var ); +private: + // I use a brute-force approach - mmap the results file and do a linear search on it + // It works surprisingly well, because the file is small enough to fit into L2 cache on modern CPU's + size_t mmapFilesize; + int mmapFD; + char* mmappedData; +}; + +size_t getFilesize(const char* filename) +{ + struct stat st; + stat(filename, &st); + return st.st_size; +} + +UnusedMethodsRemove::UnusedMethodsRemove(loplugin::InstantiationData const & data): FilteringRewritePlugin(data) +{ + static const char sInputFile[] = SRCDIR "/result.txt"; + mmapFilesize = getFilesize(sInputFile); + //Open file + mmapFD = open(sInputFile, O_RDONLY, 0); + assert(mmapFD != -1); + //Execute mmap + mmappedData = static_cast<char*>(mmap(NULL, mmapFilesize, PROT_READ, MAP_PRIVATE, mmapFD, 0)); + assert(mmappedData != NULL); +} + +UnusedMethodsRemove::~UnusedMethodsRemove() +{ + //Cleanup + int rc = munmap(mmappedData, mmapFilesize); + assert(rc == 0); + (void)rc; + close(mmapFD); +} + +std::string niceName(const CXXMethodDecl* functionDecl) +{ + std::string s = + functionDecl->getReturnType().getCanonicalType().getAsString() + + " " + functionDecl->getParent()->getQualifiedNameAsString() + + "::" + functionDecl->getNameAsString() + + "("; + bool bFirst = true; + for (const ParmVarDecl *pParmVarDecl : functionDecl->parameters()) { + if (bFirst) + bFirst = false; + else + s += ","; + s += pParmVarDecl->getType().getCanonicalType().getAsString(); + } + s += ")"; + if (functionDecl->isConst()) { + s += " const"; + } + return s; +} + +bool UnusedMethodsRemove::VisitCXXMethodDecl( const CXXMethodDecl* functionDecl ) +{ + if (rewriter == nullptr) { + return true; + } + if (ignoreLocation(functionDecl)) { + return true; + } + // ignore stuff that forms part of the stable URE interface + if (isInUnoIncludeFile(functionDecl)) { + return true; + } + + // don't mess with templates + if (functionDecl->getParent()->getDescribedClassTemplate() != nullptr) { + return true; + } + if (functionDecl->getTemplatedKind() != FunctionDecl::TK_NonTemplate) { + return true; + } + + std::string aNiceName = "\n" + niceName(functionDecl) + "\n"; + const char *aNiceNameStr = aNiceName.c_str(); + char* found = std::search(mmappedData, mmappedData + mmapFilesize, aNiceNameStr, aNiceNameStr + strlen(aNiceNameStr)); + if(!(found < mmappedData + mmapFilesize)) { + return true; + } + SourceRange replaceRange(functionDecl->getSourceRange()); + // sometimes the declaration has a semicolon just after it, and it's much neater to remove that too. + if (rewriter->getRewrittenText(SourceRange(replaceRange.getEnd(), replaceRange.getEnd().getLocWithOffset(1))) == ";") { + replaceRange.setEnd(replaceRange.getEnd().getLocWithOffset(1)); + } + // remove leading spaces + while (rewriter->getRewrittenText(SourceRange(replaceRange.getBegin().getLocWithOffset(-1), replaceRange.getBegin())) == " ") + { + replaceRange.setBegin(replaceRange.getBegin().getLocWithOffset(-1)); + } + if (!replaceText(replaceRange, "")) { + report( + DiagnosticsEngine::Warning, + "Could not remove unused method (" + niceName(functionDecl) + ")", + functionDecl->getBeginLoc()) + << functionDecl->getSourceRange(); + } + return true; +} + + +loplugin::Plugin::Registration< UnusedMethodsRemove > X("unusedmethodsremove", false); + +} + +#endif + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |