bump product version to 6.4.0.3
[LibreOffice.git] / compilerplugins / clang / unusedvariablecheck.cxx
blob07e5fe0b2acef70be8d27370baa3064c22274c4c
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;
57 auto type = var->getType();
58 bool check = loplugin::isExtraWarnUnusedType(type);
60 // this chunk of logic generates false+, which is why we don't leave it on
62 if (!check && type->isRecordType())
64 auto recordDecl
65 = dyn_cast_or_null<CXXRecordDecl>(type->getAs<RecordType>()->getDecl());
66 if (recordDecl && recordDecl->hasDefinition() && recordDecl->hasTrivialDestructor())
67 check = true;
70 if(check)
72 if( const ParmVarDecl* param = dyn_cast< ParmVarDecl >( var ))
74 if( !param->getDeclName())
75 return true; // unnamed parameter -> unused
76 // If this declaration does not have a body, then the parameter is indeed not used,
77 // so ignore.
78 if( const FunctionDecl* func = dyn_cast_or_null< FunctionDecl >( param->getParentFunctionOrMethod()))
79 if( !func->doesThisDeclarationHaveABody())
80 return true;
81 report( DiagnosticsEngine::Warning, "unused parameter %0",
82 var->getLocation()) << var->getDeclName();
84 else
85 report( DiagnosticsEngine::Warning, "unused variable %0",
86 var->getLocation()) << var->getDeclName();
88 return true;
91 static Plugin::Registration< UnusedVariableCheck > unusedvariablecheck( "unusedvariablecheck" );
93 } // namespace
95 #endif // LO_CLANG_SHARED_PLUGINS
97 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */