2013-09-21 14:42:35 +01:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-10-09 14:50:19 +02:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2019-03-07 14:31:17 +01:00
|
|
|
#ifndef LO_CLANG_SHARED_PLUGINS
|
|
|
|
|
2013-07-23 09:49:57 +02:00
|
|
|
#include <config_global.h>
|
|
|
|
|
2015-08-04 09:36:32 +02:00
|
|
|
#include "compat.hxx"
|
2017-01-26 15:36:47 +02:00
|
|
|
#include "check.hxx"
|
2012-10-09 14:50:19 +02:00
|
|
|
#include "unusedvariablecheck.hxx"
|
|
|
|
|
|
|
|
namespace loplugin
|
|
|
|
{
|
|
|
|
|
|
|
|
/*
|
2012-10-15 15:36:25 +02:00
|
|
|
This is a compile check.
|
|
|
|
|
2012-10-09 14:50:19 +02:00
|
|
|
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
|
2014-09-30 08:24:10 +02:00
|
|
|
does nothing. On the other hand, std::lock_guard instances are used
|
2012-10-09 14:50:19 +02:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2014-01-27 13:09:20 +01:00
|
|
|
UnusedVariableCheck::UnusedVariableCheck( const InstantiationData& data )
|
2018-08-13 17:24:26 +02:00
|
|
|
: FilteringPlugin( data )
|
2012-10-09 14:50:19 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnusedVariableCheck::run()
|
|
|
|
{
|
2013-03-21 16:42:10 +01:00
|
|
|
TraverseDecl( compiler.getASTContext().getTranslationUnitDecl());
|
2012-10-09 14:50:19 +02:00
|
|
|
}
|
|
|
|
|
2013-05-02 18:17:32 +02:00
|
|
|
bool UnusedVariableCheck::VisitVarDecl( const VarDecl* var )
|
2012-10-09 14:50:19 +02:00
|
|
|
{
|
2012-10-13 17:38:58 +02:00
|
|
|
if( ignoreLocation( var ))
|
2012-10-09 14:50:19 +02:00
|
|
|
return true;
|
|
|
|
if( var->isReferenced() || var->isUsed())
|
|
|
|
return true;
|
2012-10-12 15:52:35 +02:00
|
|
|
if( var->isDefinedOutsideFunctionOrMethod())
|
|
|
|
return true;
|
2019-11-06 09:04:58 +02:00
|
|
|
|
|
|
|
auto type = var->getType();
|
|
|
|
bool check = loplugin::isExtraWarnUnusedType(type);
|
|
|
|
|
|
|
|
// this chunk of logic generates false+, which is why we don't leave it on
|
|
|
|
/*
|
|
|
|
if (!check && type->isRecordType())
|
|
|
|
{
|
|
|
|
auto recordDecl
|
|
|
|
= dyn_cast_or_null<CXXRecordDecl>(type->getAs<RecordType>()->getDecl());
|
|
|
|
if (recordDecl && recordDecl->hasDefinition() && recordDecl->hasTrivialDestructor())
|
|
|
|
check = true;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
if(check)
|
2012-10-09 14:50:19 +02:00
|
|
|
{
|
|
|
|
if( const ParmVarDecl* param = dyn_cast< ParmVarDecl >( var ))
|
|
|
|
{
|
2012-10-12 12:59:31 +02:00
|
|
|
if( !param->getDeclName())
|
|
|
|
return true; // unnamed parameter -> unused
|
2012-10-09 14:50:19 +02:00
|
|
|
// If this declaration does not have a body, then the parameter is indeed not used,
|
|
|
|
// so ignore.
|
2012-10-12 13:09:08 +02:00
|
|
|
if( const FunctionDecl* func = dyn_cast_or_null< FunctionDecl >( param->getParentFunctionOrMethod()))
|
2020-12-07 08:53:15 +01:00
|
|
|
if( !func->doesThisDeclarationHaveABody() || func->getBody() == nullptr)
|
2012-10-09 14:50:19 +02:00
|
|
|
return true;
|
2013-02-02 19:38:56 +01:00
|
|
|
report( DiagnosticsEngine::Warning, "unused parameter %0",
|
2012-10-12 15:34:14 +02:00
|
|
|
var->getLocation()) << var->getDeclName();
|
2012-10-09 14:50:19 +02:00
|
|
|
}
|
|
|
|
else
|
2013-02-02 19:38:56 +01:00
|
|
|
report( DiagnosticsEngine::Warning, "unused variable %0",
|
2012-10-12 15:34:14 +02:00
|
|
|
var->getLocation()) << var->getDeclName();
|
2012-10-09 14:50:19 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-07 14:31:17 +01:00
|
|
|
static Plugin::Registration< UnusedVariableCheck > unusedvariablecheck( "unusedvariablecheck" );
|
2013-02-02 17:45:18 +01:00
|
|
|
|
2012-10-09 14:50:19 +02:00
|
|
|
} // namespace
|
2013-07-23 09:49:57 +02:00
|
|
|
|
2019-03-07 14:31:17 +01:00
|
|
|
#endif // LO_CLANG_SHARED_PLUGINS
|
|
|
|
|
2013-09-21 14:42:35 +01:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|