bump product version to 5.0.4.1
[LibreOffice.git] / compilerplugins / clang / unusedvariablecheck.cxx
blob0308d9b6150ccc4be647b66b9129d9698f6dc21f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * Based on LLVM/Clang.
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>
23 namespace loplugin
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 )
42 : Plugin( 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) {
53 return false;
55 return true;
58 bool isDerivedFromDialog(const CXXRecordDecl *decl) {
59 if (!decl)
60 return false;
61 if (decl->getQualifiedNameAsString() == "Dialog")
62 return true;
63 if (!decl->hasDefinition()) {
64 return false;
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)) {
70 return true;
72 return false;
75 bool UnusedVariableCheck::VisitVarDecl( const VarDecl* var )
77 if( ignoreLocation( var ))
78 return true;
79 if( var->isReferenced() || var->isUsed())
80 return true;
81 if( var->isDefinedOutsideFunctionOrMethod())
82 return true;
83 if( CXXRecordDecl* type = var->getType()->getAsCXXRecordDecl())
85 bool warn_unused = false;
86 if( type->hasAttrs())
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>();
92 i != e;
93 ++i )
95 if( (*i)->getAnnotation() == "lo_warn_unused" )
97 warn_unused = true;
98 break;
102 if( !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" )
109 warn_unused = true;
110 // check if this field is derived from Dialog
111 if (!warn_unused && isDerivedFromDialog(type))
112 warn_unused = true;
114 if( warn_unused )
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,
121 // so ignore.
122 if( const FunctionDecl* func = dyn_cast_or_null< FunctionDecl >( param->getParentFunctionOrMethod()))
123 if( !func->doesThisDeclarationHaveABody())
124 return true;
125 report( DiagnosticsEngine::Warning, "unused parameter %0",
126 var->getLocation()) << var->getDeclName();
128 else
129 report( DiagnosticsEngine::Warning, "unused variable %0",
130 var->getLocation()) << var->getDeclName();
133 return true;
136 static Plugin::Registration< UnusedVariableCheck > X( "unusedvariablecheck" );
138 } // namespace
140 #endif
142 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */