cURL: follow redirects
[LibreOffice.git] / compilerplugins / clang / unusedvariablecheck.cxx
blob8642032df12a624b72951390281e68cd7215474c
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 "compat.hxx"
20 #include "unusedvariablecheck.hxx"
22 #include <clang/AST/Attr.h>
24 namespace loplugin
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 )
43 : Plugin( data )
47 void UnusedVariableCheck::run()
49 TraverseDecl( compiler.getASTContext().getTranslationUnitDecl());
52 bool BaseCheckNotDialogSubclass(
53 const CXXRecordDecl *BaseDefinition
54 #if CLANG_VERSION < 30800
55 , void *
56 #endif
59 if (BaseDefinition && BaseDefinition->getQualifiedNameAsString().compare("Dialog") == 0) {
60 return false;
62 return true;
65 bool isDerivedFromDialog(const CXXRecordDecl *decl) {
66 if (!decl)
67 return false;
68 if (decl->getQualifiedNameAsString() == "Dialog")
69 return true;
70 if (!decl->hasDefinition()) {
71 return false;
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)) {
77 return true;
79 return false;
82 bool UnusedVariableCheck::VisitVarDecl( const VarDecl* var )
84 if( ignoreLocation( var ))
85 return true;
86 if( var->isReferenced() || var->isUsed())
87 return true;
88 if( var->isDefinedOutsideFunctionOrMethod())
89 return true;
90 if( CXXRecordDecl* type = var->getType()->getAsCXXRecordDecl())
92 bool warn_unused = false;
93 if( type->hasAttrs())
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>();
99 i != e;
100 ++i )
102 if( (*i)->getAnnotation() == "lo_warn_unused" )
104 warn_unused = true;
105 break;
109 if( !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" )
116 warn_unused = true;
117 // check if this field is derived from Dialog
118 if (!warn_unused && isDerivedFromDialog(type))
119 warn_unused = true;
121 if( warn_unused )
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,
128 // so ignore.
129 if( const FunctionDecl* func = dyn_cast_or_null< FunctionDecl >( param->getParentFunctionOrMethod()))
130 if( !func->doesThisDeclarationHaveABody())
131 return true;
132 report( DiagnosticsEngine::Warning, "unused parameter %0",
133 var->getLocation()) << var->getDeclName();
135 else
136 report( DiagnosticsEngine::Warning, "unused variable %0",
137 var->getLocation()) << var->getDeclName();
140 return true;
143 static Plugin::Registration< UnusedVariableCheck > X( "unusedvariablecheck" );
145 } // namespace
147 #endif
149 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */