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"
25 This is a compile check.
27 Check for unused classes where the compiler cannot decide (e.g. because of
28 non-trivial or extern ctors) if a variable is unused if only its ctor/dtor
29 are called and nothing else. For example std::vector is a class where
30 the ctor may call further functions, but an unused std::string variable
31 does nothing. On the other hand, std::scoped_lock instances are used
32 for their dtors and so are not unused even if not otherwise accessed.
34 Classes which are safe to be warned about need to be marked using
35 SAL_WARN_UNUSED (see e.g. OUString). For external classes such as std::vector
36 that cannot be edited there is a manual list below.
39 UnusedVariableCheck::UnusedVariableCheck( const InstantiationData
& data
)
40 : FilteringPlugin( data
)
44 void UnusedVariableCheck::run()
46 TraverseDecl( compiler
.getASTContext().getTranslationUnitDecl());
49 bool UnusedVariableCheck::VisitVarDecl( const VarDecl
* var
)
51 if( ignoreLocation( var
))
53 if( var
->isReferenced() || var
->isUsed())
55 if( var
->isDefinedOutsideFunctionOrMethod())
58 auto type
= var
->getType();
59 bool check
= loplugin::isExtraWarnUnusedType(type
);
62 check
= isUnusedSmartPointer(var
);
65 // this chunk of logic generates false+, which is why we don't leave it on
67 if (!check && type->isRecordType())
70 = dyn_cast_or_null<CXXRecordDecl>(type->getAs<RecordType>()->getDecl());
71 if (recordDecl && recordDecl->hasDefinition() && recordDecl->hasTrivialDestructor())
77 if( const ParmVarDecl
* param
= dyn_cast
< ParmVarDecl
>( var
))
79 if( !param
->getDeclName())
80 return true; // unnamed parameter -> unused
81 // If this declaration does not have a body, then the parameter is indeed not used,
83 auto const parent
= param
->getParentFunctionOrMethod();
84 if( const FunctionDecl
* func
= dyn_cast_or_null
< FunctionDecl
>( parent
))
85 if( !func
->doesThisDeclarationHaveABody() || func
->getBody() == nullptr)
87 if (auto const d
= dyn_cast_or_null
<ObjCMethodDecl
>(parent
)) {
92 report( DiagnosticsEngine::Warning
, "unused parameter %0",
93 var
->getLocation()) << var
->getDeclName();
96 report( DiagnosticsEngine::Warning
, "unused variable %0",
97 var
->getLocation()) << var
->getDeclName();
102 bool UnusedVariableCheck::isUnusedSmartPointer( const VarDecl
* var
)
104 // if we have a var of smart-pointer type, and that var is both uninitialised and
105 // not referenced, then we can remove it
106 if (!isSmartPointerType(var
->getType()))
112 auto cxxConstructExpr
= dyn_cast
<CXXConstructExpr
>(var
->getInit());
113 if (!cxxConstructExpr
)
116 cxxConstructExpr
->getNumArgs() == 0 || cxxConstructExpr
->getArg(0)->isDefaultArgument();
119 static Plugin::Registration
< UnusedVariableCheck
> unusedvariablecheck( "unusedvariablecheck" );
123 #endif // LO_CLANG_SHARED_PLUGINS
125 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */