1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
7 * This file is distributed under the University of Illinois Open Source
8 * License. See LICENSE.TXT for details.
12 #ifndef LO_CLANG_SHARED_PLUGINS
14 #include <config_global.h>
18 #include "unusedvariablecheck.hxx"
24 This is a compile check.
26 Check for unused classes where the compiler cannot decide (e.g. because of
27 non-trivial or extern ctors) if a variable is unused if only its ctor/dtor
28 are called and nothing else. For example std::vector is a class where
29 the ctor may call further functions, but an unused std::string variable
30 does nothing. On the other hand, std::lock_guard instances are used
31 for their dtors and so are not unused even if not otherwise accessed.
33 Classes which are safe to be warned about need to be marked using
34 SAL_WARN_UNUSED (see e.g. OUString). For external classes such as std::vector
35 that cannot be edited there is a manual list below.
38 UnusedVariableCheck::UnusedVariableCheck( const InstantiationData
& data
)
39 : FilteringPlugin( data
)
43 void UnusedVariableCheck::run()
45 TraverseDecl( compiler
.getASTContext().getTranslationUnitDecl());
48 bool UnusedVariableCheck::VisitVarDecl( const VarDecl
* var
)
50 if( ignoreLocation( var
))
52 if( var
->isReferenced() || var
->isUsed())
54 if( var
->isDefinedOutsideFunctionOrMethod())
57 auto type
= var
->getType();
58 bool check
= loplugin::isExtraWarnUnusedType(type
);
60 // this chunk of logic generates false+, which is why we don't leave it on
62 if (!check && type->isRecordType())
65 = dyn_cast_or_null<CXXRecordDecl>(type->getAs<RecordType>()->getDecl());
66 if (recordDecl && recordDecl->hasDefinition() && recordDecl->hasTrivialDestructor())
72 if( const ParmVarDecl
* param
= dyn_cast
< ParmVarDecl
>( var
))
74 if( !param
->getDeclName())
75 return true; // unnamed parameter -> unused
76 // If this declaration does not have a body, then the parameter is indeed not used,
78 if( const FunctionDecl
* func
= dyn_cast_or_null
< FunctionDecl
>( param
->getParentFunctionOrMethod()))
79 if( !func
->doesThisDeclarationHaveABody())
81 report( DiagnosticsEngine::Warning
, "unused parameter %0",
82 var
->getLocation()) << var
->getDeclName();
85 report( DiagnosticsEngine::Warning
, "unused variable %0",
86 var
->getLocation()) << var
->getDeclName();
91 static Plugin::Registration
< UnusedVariableCheck
> unusedvariablecheck( "unusedvariablecheck" );
95 #endif // LO_CLANG_SHARED_PLUGINS
97 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */