/* -*- 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/.
*/
/* Look for member functions that merely return a compile-time constant, or they are empty, and can thus be either removed, or converted into a constant.
This mostly tends to happen as a side-effect of other cleanups.
*/ namespace
{ class ReturnConstant : public loplugin::FunctionAddress<loplugin::FilteringPlugin<ReturnConstant>>
{ public: explicit ReturnConstant(loplugin::InstantiationData const& data)
: FunctionAddress(data)
{
}
for (auto& pair : problemFunctions)
{ auto functionDecl = pair.first; auto canonicalDecl = functionDecl->getCanonicalDecl(); if (getFunctionsWithAddressTaken().find(canonicalDecl)
!= getFunctionsWithAddressTaken().end()) continue;
report(DiagnosticsEngine::Warning, "Method only returns a single constant value %0, does it make sense?",
functionDecl->getBeginLoc())
<< pair.second << functionDecl->getSourceRange(); if (functionDecl != functionDecl->getCanonicalDecl())
report(DiagnosticsEngine::Note, "decl here",
functionDecl->getCanonicalDecl()->getBeginLoc())
<< functionDecl->getCanonicalDecl()->getSourceRange();
}
}
bool ReturnConstant::TraverseCXXMethodDecl(CXXMethodDecl* functionDecl)
{ if (ignoreLocation(functionDecl)) returntrue; if (isInUnoIncludeFile(functionDecl)) returntrue;
if (!functionDecl->hasBody()) returntrue; if (!functionDecl->isThisDeclarationADefinition()) returntrue; if (functionDecl->isConstexpr()) returntrue; if (functionDecl->getReturnType()->isVoidType()) returntrue; if (functionDecl->isVirtual()) returntrue; // static with inline body will be optimised at compile-time to a constant anyway if (functionDecl->isStatic()
&& (functionDecl->hasInlineBody() || functionDecl->isInlineSpecified())) returntrue; // this catches some stuff in templates if (functionDecl->hasAttr<OverrideAttr>()) returntrue;
// include/unotools/localedatawrapper.hxx if (functionDecl->getIdentifier() && functionDecl->getName() == "getCurrZeroChar") returntrue; // sc/inc/stlalgorithm.hxx if (loplugin::DeclCheck(functionDecl->getParent())
.Class("AlignedAllocator")
.Namespace("sc")
.GlobalNamespace()) returntrue;
switch (functionDecl->getOverloadedOperator())
{ case OO_Delete: case OO_EqualEqual: case OO_Call: returntrue; default: break;
}
// gtk signals and slots stuff if (loplugin::TypeCheck(functionDecl->getReturnType()).Typedef("gboolean")) returntrue;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.