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
20 #include "unusedvariablecheck.hxx"
22 #include <clang/AST/Attr.h>
28 This is a compile check.
30 Check for unused classes where the compiler cannot decide (e.g. because of
31 non-trivial or extern ctors) if a variable is unused if only its ctor/dtor
32 are called and nothing else. For example std::vector is a class where
33 the ctor may call further functions, but an unused std::string variable
34 does nothing. On the other hand, std::lock_guard instances are used
35 for their dtors and so are not unused even if not otherwise accessed.
37 Classes which are safe to be warned about need to be marked using
38 SAL_WARN_UNUSED (see e.g. OUString). For external classes such as std::vector
39 that cannot be edited there is a manual list below.
42 UnusedVariableCheck::UnusedVariableCheck( const InstantiationData
& data
)
47 void UnusedVariableCheck::run()
49 TraverseDecl( compiler
.getASTContext().getTranslationUnitDecl());
52 bool BaseCheckNotDialogSubclass(
53 const CXXRecordDecl
*BaseDefinition
54 #if CLANG_VERSION < 30800
59 if (BaseDefinition
&& BaseDefinition
->getQualifiedNameAsString().compare("Dialog") == 0) {
65 bool isDerivedFromDialog(const CXXRecordDecl
*decl
) {
68 if (decl
->getQualifiedNameAsString() == "Dialog")
70 if (!decl
->hasDefinition()) {
73 if (// not sure what hasAnyDependentBases() does,
74 // but it avoids classes we don't want, e.g. WeakAggComponentImplHelper1
75 !decl
->hasAnyDependentBases() &&
76 !compat::forallBases(*decl
, BaseCheckNotDialogSubclass
, nullptr, true)) {
82 bool UnusedVariableCheck::VisitVarDecl( const VarDecl
* var
)
84 if( ignoreLocation( var
))
86 if( var
->isReferenced() || var
->isUsed())
88 if( var
->isDefinedOutsideFunctionOrMethod())
90 if( CXXRecordDecl
* type
= var
->getType()->getAsCXXRecordDecl())
92 bool warn_unused
= false;
95 // Clang currently has no support for custom attributes, but
96 // the annotate attribute comes close, so check for __attribute__((annotate("lo_warn_unused")))
97 for( specific_attr_iterator
<AnnotateAttr
> i
= type
->specific_attr_begin
<AnnotateAttr
>(),
98 e
= type
->specific_attr_end
<AnnotateAttr
>();
102 if( (*i
)->getAnnotation() == "lo_warn_unused" )
111 string n
= type
->getQualifiedNameAsString();
112 // Check some common non-LO types.
113 if( n
== "std::string" || n
== "std::basic_string"
114 || n
== "std::list" || n
== "std::__debug::list"
115 || n
== "std::vector" || n
== "std::__debug::vector" )
117 // check if this field is derived from Dialog
118 if (!warn_unused
&& isDerivedFromDialog(type
))
123 if( const ParmVarDecl
* param
= dyn_cast
< ParmVarDecl
>( var
))
125 if( !param
->getDeclName())
126 return true; // unnamed parameter -> unused
127 // If this declaration does not have a body, then the parameter is indeed not used,
129 if( const FunctionDecl
* func
= dyn_cast_or_null
< FunctionDecl
>( param
->getParentFunctionOrMethod()))
130 if( !func
->doesThisDeclarationHaveABody())
132 report( DiagnosticsEngine::Warning
, "unused parameter %0",
133 var
->getLocation()) << var
->getDeclName();
136 report( DiagnosticsEngine::Warning
, "unused variable %0",
137 var
->getLocation()) << var
->getDeclName();
143 static Plugin::Registration
< UnusedVariableCheck
> X( "unusedvariablecheck" );
149 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */