libreoffice/compilerplugins/clang/unusedvariablecheck.cxx
Stephan Bergmann e62b087fce Drop HAVE_GCC_ATTRIBUTE_WARN_UNUSED_STL
For one, loplugin:unusedvariablecheck does not merely check for unused variables
with types from the standard library since
fe2164949b38a7f73883dbdcb3271b94e5c81744 "teach unusedvariablecheck plugin about
SfxPoolItem subclasses", so disabling loplugin:unusedvariablecheck based on
HAVE_GCC_ATTRIBUTE_WARN_UNUSED_STL is wrong.

For another, I have seen no standard library implementation that decorates its
types with such "warn-if-unused" attributes, and
<http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0600r0.pdf>
"[[nodiscard]] in the Library" (which proposes to add the corresponding C++17
attribute to just a few select functions and no types at all) makes it appear
unlikely that will happen.

Change-Id: I0a7759e1caf3e3137057c9689080948a4d6747e0
2017-11-08 15:15:54 +01:00

80 lines
2.6 KiB
C++

/* -*- 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 <config_global.h>
#include "compat.hxx"
#include "check.hxx"
#include "unusedvariablecheck.hxx"
namespace loplugin
{
/*
This is a compile check.
Check for unused classes where the compiler cannot decide (e.g. because of
non-trivial or extern ctors) if a variable is unused if only its ctor/dtor
are called and nothing else. For example std::vector is a class where
the ctor may call further functions, but an unused std::string variable
does nothing. On the other hand, std::lock_guard instances are used
for their dtors and so are not unused even if not otherwise accessed.
Classes which are safe to be warned about need to be marked using
SAL_WARN_UNUSED (see e.g. OUString). For external classes such as std::vector
that cannot be edited there is a manual list below.
*/
UnusedVariableCheck::UnusedVariableCheck( const InstantiationData& data )
: Plugin( data )
{
}
void UnusedVariableCheck::run()
{
TraverseDecl( compiler.getASTContext().getTranslationUnitDecl());
}
bool UnusedVariableCheck::VisitVarDecl( const VarDecl* var )
{
if( ignoreLocation( var ))
return true;
if( var->isReferenced() || var->isUsed())
return true;
if( var->isDefinedOutsideFunctionOrMethod())
return true;
if( loplugin::isExtraWarnUnusedType(var->getType()))
{
if( const ParmVarDecl* param = dyn_cast< ParmVarDecl >( var ))
{
if( !param->getDeclName())
return true; // unnamed parameter -> unused
// If this declaration does not have a body, then the parameter is indeed not used,
// so ignore.
if( const FunctionDecl* func = dyn_cast_or_null< FunctionDecl >( param->getParentFunctionOrMethod()))
if( !func->doesThisDeclarationHaveABody())
return true;
report( DiagnosticsEngine::Warning, "unused parameter %0",
var->getLocation()) << var->getDeclName();
}
else
report( DiagnosticsEngine::Warning, "unused variable %0",
var->getLocation()) << var->getDeclName();
}
return true;
}
static Plugin::Registration< UnusedVariableCheck > X( "unusedvariablecheck" );
} // namespace
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */