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 #include <config_global.h>
14 // If there is support for warn_unused attribute even in STL classes, then there's
15 // no point in having this check enabled, otherwise keep it at least for STL
16 // (LO classes won't get duplicated warnings, as the attribute is different).
17 #if !HAVE_GCC_ATTRIBUTE_WARN_UNUSED_STL
19 #include "unusedvariablecheck.hxx"
21 #include <clang/AST/Attr.h>
27 This is a compile check.
29 Check for unused classes where the compiler cannot decide (e.g. because of
30 non-trivial or extern ctors) if a variable is unused if only its ctor/dtor
31 are called and nothing else. For example std::vector is a class where
32 the ctor may call further functions, but an unused std::string variable
33 does nothing. On the other hand, std::lock_guard instances are used
34 for their dtors and so are not unused even if not otherwise accessed.
36 Classes which are safe to be warned about need to be marked using
37 SAL_WARN_UNUSED (see e.g. OUString). For external classes such as std::vector
38 that cannot be edited there is a manual list below.
41 UnusedVariableCheck::UnusedVariableCheck( const InstantiationData
& data
)
46 void UnusedVariableCheck::run()
48 TraverseDecl( compiler
.getASTContext().getTranslationUnitDecl());
51 bool BaseCheckNotDialogSubclass(const CXXRecordDecl
*BaseDefinition
, void *) {
52 if (BaseDefinition
&& BaseDefinition
->getQualifiedNameAsString().compare("Dialog") == 0) {
58 bool isDerivedFromDialog(const CXXRecordDecl
*decl
) {
61 if (decl
->getQualifiedNameAsString() == "Dialog")
63 if (!decl
->hasDefinition()) {
66 if (// not sure what hasAnyDependentBases() does,
67 // but it avoids classes we don't want, e.g. WeakAggComponentImplHelper1
68 !decl
->hasAnyDependentBases() &&
69 !decl
->forallBases(BaseCheckNotDialogSubclass
, nullptr, true)) {
75 bool UnusedVariableCheck::VisitVarDecl( const VarDecl
* var
)
77 if( ignoreLocation( var
))
79 if( var
->isReferenced() || var
->isUsed())
81 if( var
->isDefinedOutsideFunctionOrMethod())
83 if( CXXRecordDecl
* type
= var
->getType()->getAsCXXRecordDecl())
85 bool warn_unused
= false;
88 // Clang currently has no support for custom attributes, but
89 // the annotate attribute comes close, so check for __attribute__((annotate("lo_warn_unused")))
90 for( specific_attr_iterator
<AnnotateAttr
> i
= type
->specific_attr_begin
<AnnotateAttr
>(),
91 e
= type
->specific_attr_end
<AnnotateAttr
>();
95 if( (*i
)->getAnnotation() == "lo_warn_unused" )
104 string n
= type
->getQualifiedNameAsString();
105 // Check some common non-LO types.
106 if( n
== "std::string" || n
== "std::basic_string"
107 || n
== "std::list" || n
== "std::__debug::list"
108 || n
== "std::vector" || n
== "std::__debug::vector" )
110 // check if this field is derived from Dialog
111 if (!warn_unused
&& isDerivedFromDialog(type
))
116 if( const ParmVarDecl
* param
= dyn_cast
< ParmVarDecl
>( var
))
118 if( !param
->getDeclName())
119 return true; // unnamed parameter -> unused
120 // If this declaration does not have a body, then the parameter is indeed not used,
122 if( const FunctionDecl
* func
= dyn_cast_or_null
< FunctionDecl
>( param
->getParentFunctionOrMethod()))
123 if( !func
->doesThisDeclarationHaveABody())
125 report( DiagnosticsEngine::Warning
, "unused parameter %0",
126 var
->getLocation()) << var
->getDeclName();
129 report( DiagnosticsEngine::Warning
, "unused variable %0",
130 var
->getLocation()) << var
->getDeclName();
136 static Plugin::Registration
< UnusedVariableCheck
> X( "unusedvariablecheck" );
142 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */