Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / compilerplugins / clang / unusedvariablecheck.cxx
blobf155219942b6ec823511ebd3c64c4b7416552dad
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"
19 #include "plugin.hxx"
21 namespace loplugin
25 This is a compile check.
27 Check for unused classes where the compiler cannot decide (e.g. because of
28 non-trivial or extern ctors) if a variable is unused if only its ctor/dtor
29 are called and nothing else. For example std::vector is a class where
30 the ctor may call further functions, but an unused std::string variable
31 does nothing. On the other hand, std::scoped_lock instances are used
32 for their dtors and so are not unused even if not otherwise accessed.
34 Classes which are safe to be warned about need to be marked using
35 SAL_WARN_UNUSED (see e.g. OUString). For external classes such as std::vector
36 that cannot be edited there is a manual list below.
39 UnusedVariableCheck::UnusedVariableCheck( const InstantiationData& data )
40 : FilteringPlugin( data )
44 void UnusedVariableCheck::run()
46 TraverseDecl( compiler.getASTContext().getTranslationUnitDecl());
49 bool UnusedVariableCheck::VisitVarDecl( const VarDecl* var )
51 if( ignoreLocation( var ))
52 return true;
53 if( var->isReferenced() || var->isUsed())
54 return true;
55 if( var->isDefinedOutsideFunctionOrMethod())
56 return true;
58 auto type = var->getType();
59 bool check = loplugin::isExtraWarnUnusedType(type);
61 if (!check)
62 check = isUnusedSmartPointer(var);
65 // this chunk of logic generates false+, which is why we don't leave it on
67 if (!check && type->isRecordType())
69 auto recordDecl
70 = dyn_cast_or_null<CXXRecordDecl>(type->getAs<RecordType>()->getDecl());
71 if (recordDecl && recordDecl->hasDefinition() && recordDecl->hasTrivialDestructor())
72 check = true;
75 if(check)
77 if( const ParmVarDecl* param = dyn_cast< ParmVarDecl >( var ))
79 if( !param->getDeclName())
80 return true; // unnamed parameter -> unused
81 // If this declaration does not have a body, then the parameter is indeed not used,
82 // so ignore.
83 auto const parent = param->getParentFunctionOrMethod();
84 if( const FunctionDecl* func = dyn_cast_or_null< FunctionDecl >( parent))
85 if( !func->doesThisDeclarationHaveABody() || func->getBody() == nullptr)
86 return true;
87 if (auto const d = dyn_cast_or_null<ObjCMethodDecl>(parent)) {
88 if (!d->hasBody()) {
89 return true;
92 report( DiagnosticsEngine::Warning, "unused parameter %0",
93 var->getLocation()) << var->getDeclName();
95 else
96 report( DiagnosticsEngine::Warning, "unused variable %0",
97 var->getLocation()) << var->getDeclName();
99 return true;
102 bool UnusedVariableCheck::isUnusedSmartPointer( const VarDecl* var )
104 // if we have a var of smart-pointer type, and that var is both uninitialised and
105 // not referenced, then we can remove it
106 if (!isSmartPointerType(var->getType()))
107 return false;
109 if (!var->hasInit())
110 return true;
112 auto cxxConstructExpr = dyn_cast<CXXConstructExpr>(var->getInit());
113 if (!cxxConstructExpr)
114 return false;
115 return
116 cxxConstructExpr->getNumArgs() == 0 || cxxConstructExpr->getArg(0)->isDefaultArgument();
119 static Plugin::Registration< UnusedVariableCheck > unusedvariablecheck( "unusedvariablecheck" );
121 } // namespace
123 #endif // LO_CLANG_SHARED_PLUGINS
125 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */