bump product version to 6.3.0.0.beta1
[LibreOffice.git] / compilerplugins / clang / unusedvariablecheck.cxx
blob25af5438b465157bd300d48905e3789d208be781
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 #ifndef LO_CLANG_SHARED_PLUGINS
14 #include <config_global.h>
16 #include "compat.hxx"
17 #include "check.hxx"
18 #include "unusedvariablecheck.hxx"
20 namespace loplugin
24 This is a compile check.
26 Check for unused classes where the compiler cannot decide (e.g. because of
27 non-trivial or extern ctors) if a variable is unused if only its ctor/dtor
28 are called and nothing else. For example std::vector is a class where
29 the ctor may call further functions, but an unused std::string variable
30 does nothing. On the other hand, std::lock_guard instances are used
31 for their dtors and so are not unused even if not otherwise accessed.
33 Classes which are safe to be warned about need to be marked using
34 SAL_WARN_UNUSED (see e.g. OUString). For external classes such as std::vector
35 that cannot be edited there is a manual list below.
38 UnusedVariableCheck::UnusedVariableCheck( const InstantiationData& data )
39 : FilteringPlugin( data )
43 void UnusedVariableCheck::run()
45 TraverseDecl( compiler.getASTContext().getTranslationUnitDecl());
48 bool UnusedVariableCheck::VisitVarDecl( const VarDecl* var )
50 if( ignoreLocation( var ))
51 return true;
52 if( var->isReferenced() || var->isUsed())
53 return true;
54 if( var->isDefinedOutsideFunctionOrMethod())
55 return true;
56 if( loplugin::isExtraWarnUnusedType(var->getType()))
58 if( const ParmVarDecl* param = dyn_cast< ParmVarDecl >( var ))
60 if( !param->getDeclName())
61 return true; // unnamed parameter -> unused
62 // If this declaration does not have a body, then the parameter is indeed not used,
63 // so ignore.
64 if( const FunctionDecl* func = dyn_cast_or_null< FunctionDecl >( param->getParentFunctionOrMethod()))
65 if( !func->doesThisDeclarationHaveABody())
66 return true;
67 report( DiagnosticsEngine::Warning, "unused parameter %0",
68 var->getLocation()) << var->getDeclName();
70 else
71 report( DiagnosticsEngine::Warning, "unused variable %0",
72 var->getLocation()) << var->getDeclName();
74 return true;
77 static Plugin::Registration< UnusedVariableCheck > unusedvariablecheck( "unusedvariablecheck" );
79 } // namespace
81 #endif // LO_CLANG_SHARED_PLUGINS
83 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */