android: Reuse launcher icon in activities
[LibreOffice.git] / compilerplugins / clang / unusedvariablemore.cxx
blob908edf33a465ab1fce5421398ceb58d31fcc301e
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_clang.h"
13 #include <config_global.h>
14 #include "plugin.hxx"
15 #include "compat.hxx"
16 #include "check.hxx"
17 #include <unordered_set>
18 #include <unordered_map>
20 #include "clang/AST/ParentMapContext.h"
22 namespace loplugin
25 This is a compile check. The results of this plugin need to be checked by hand, since it is a collection of heuristics.
27 Check for unused variable where
28 (*) we never call methods that return information from the variable.
29 (*) we never pass the variable to anything else
31 Classes which are safe to be warned about need to be marked using
32 SAL_WARN_UNUSED (see e.g. OUString). For external classes such as std::vector
33 that cannot be edited there is a manual list.
35 This is an expensive plugin, since it walks up the parent tree,
36 so it is off by default.
39 class UnusedVariableMore : public loplugin::FilteringPlugin<UnusedVariableMore>
41 public:
42 explicit UnusedVariableMore(const InstantiationData& data);
43 virtual void run() override;
44 bool VisitVarDecl(VarDecl const*);
45 bool VisitDeclRefExpr(DeclRefExpr const*);
46 bool VisitFunctionDecl(FunctionDecl const*);
48 private:
49 bool checkifUnused(Stmt const*, VarDecl const*);
50 bool isOkForParameter(const QualType&);
52 std::unordered_set<VarDecl const*> interestingSet;
53 // used to dump the last place that said the usage was unused, for debug purposes
54 std::unordered_map<VarDecl const*, Stmt const*> interestingDebugMap;
57 UnusedVariableMore::UnusedVariableMore(const InstantiationData& data)
58 : FilteringPlugin(data)
62 void UnusedVariableMore::run()
64 std::string fn(handler.getMainFileName());
65 loplugin::normalizeDotDotInFilePath(fn);
67 // ignore QA folders
68 if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sal/qa/"))
69 return;
70 if (loplugin::hasPathnamePrefix(fn, SRCDIR "/i18npool/qa/"))
71 return;
72 if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sc/qa/"))
73 return;
75 // vector of shared_ptr used to delay destruction
76 if (fn == SRCDIR "/cppuhelper/source/servicemanager.cxx")
77 return;
78 if (fn == SRCDIR "/i18nlangtag/source/languagetag/languagetag.cxx")
79 return;
80 // unordered_set of Reference to delay destruction
81 if (fn == SRCDIR "/stoc/source/servicemanager/servicemanager.cxx")
82 return;
83 // TODO "operator >>" fooling me here
84 if (fn == SRCDIR "/editeng/source/accessibility/AccessibleEditableTextPara.cxx")
85 return;
86 // some weird stuff
87 if (fn == SRCDIR "/sfx2/source/dialog/dinfdlg.cxx")
88 return;
89 // std::vector< Reference< XInterface > > keep alive
90 if (fn == SRCDIR "/dbaccess/source/core/dataaccess/databasedocument.cxx")
91 return;
92 // template magic
93 if (fn == SRCDIR "/sc/source/core/tool/scmatrix.cxx")
94 return;
95 // storing local copy of Link<>
96 if (fn == SRCDIR "/sc/source/ui/miscdlgs/simpref.cxx")
97 return;
98 // Using an SwPaM to do stuff
99 if (fn == SRCDIR "/sw/source/core/crsr/bookmark.cxx")
100 return;
101 // index variable in for loop?
102 if (fn == SRCDIR "/sw/source/uibase/docvw/edtwin.cxx")
103 return;
104 // TODO "operator >>" fooling me here
105 if (fn == SRCDIR "/sw/source/filter/ww8/ww8par.cxx")
106 return;
107 // TODO "operator >>" fooling me here
108 if (fn == SRCDIR "/sc/source/filter/excel/xistream.cxx")
109 return;
111 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
113 for (VarDecl const* varDecl : interestingSet)
115 report(DiagnosticsEngine::Warning, "unused variable %0", varDecl->getLocation())
116 << varDecl->getDeclName();
117 //auto it = interestingDebugMap.find(varDecl);
118 //if (it != interestingDebugMap.end())
119 // it->second->dump();
123 bool isWarnUnusedType(QualType type)
125 if (auto const t = type->getAs<TypedefType>())
127 if (t->getDecl()->hasAttr<WarnUnusedAttr>())
129 return true;
132 if (auto const t = type->getAs<RecordType>())
134 if (t->getDecl()->hasAttr<WarnUnusedAttr>())
136 return true;
139 return loplugin::isExtraWarnUnusedType(type);
142 bool UnusedVariableMore::VisitVarDecl(VarDecl const* var)
144 if (ignoreLocation(var))
145 return true;
146 if (var->isDefinedOutsideFunctionOrMethod())
147 return true;
148 if (isa<ParmVarDecl>(var))
149 return true;
150 if (!isWarnUnusedType(var->getType()))
151 return true;
153 // some false +
154 auto dc = loplugin::TypeCheck(var->getType());
155 if (dc.Class("ZCodec").GlobalNamespace())
156 return true;
157 if (dc.Class("ScopedVclPtrInstance").GlobalNamespace())
158 return true;
159 if (dc.Class("VclPtrInstance").GlobalNamespace())
160 return true;
161 if (dc.Class("Config").GlobalNamespace())
162 return true;
163 // I think these classes modify global state somehow
164 if (dc.Class("SvtHistoryOptions").GlobalNamespace())
165 return true;
166 if (dc.Class("SvtSecurityOptions").GlobalNamespace())
167 return true;
168 if (dc.Class("SvtViewOptions").GlobalNamespace())
169 return true;
170 if (dc.Class("SvtUserOptions").GlobalNamespace())
171 return true;
172 if (dc.Class("SvtMenuOptions").GlobalNamespace())
173 return true;
174 if (dc.Class("SvtPathOptions").GlobalNamespace())
175 return true;
176 if (dc.Class("SvtSysLocaleOptions").GlobalNamespace())
177 return true;
179 interestingSet.insert(var);
180 return true;
183 bool UnusedVariableMore::VisitDeclRefExpr(DeclRefExpr const* declRefExpr)
185 if (ignoreLocation(declRefExpr))
186 return true;
187 auto varDecl = dyn_cast<VarDecl>(declRefExpr->getDecl());
188 if (!varDecl)
189 return true;
190 if (interestingSet.find(varDecl) == interestingSet.end())
191 return true;
192 if (!checkifUnused(declRefExpr, varDecl))
193 interestingSet.erase(varDecl);
194 return true;
197 // Walk up from a statement that contains a DeclRefExpr, checking if the usage means that the
198 // related VarDecl is unused.
199 bool UnusedVariableMore::checkifUnused(Stmt const* stmt, VarDecl const* varDecl)
201 const Stmt* parent = getParentStmt(stmt);
202 if (!parent)
204 // check if we're inside a CXXCtorInitializer
205 auto parentsRange = compiler.getASTContext().getParents(*stmt);
206 if (parentsRange.begin() != parentsRange.end())
208 auto parentDecl = parentsRange.begin()->get<Decl>();
209 if (parentDecl && (isa<CXXConstructorDecl>(parentDecl) || isa<VarDecl>(parentDecl)))
210 return false;
212 interestingDebugMap[varDecl] = stmt;
213 return true;
216 if (isa<ReturnStmt>(parent))
217 return false;
218 if (isa<IfStmt>(parent))
219 return false;
220 if (isa<SwitchStmt>(parent))
221 return false;
222 if (isa<InitListExpr>(parent))
223 return false;
224 if (isa<CXXConstructExpr>(parent))
225 return false;
226 if (isa<BinaryOperator>(parent))
227 return false;
228 if (isa<UnaryOperator>(parent))
229 return false;
230 if (isa<ConditionalOperator>(parent))
231 return false;
232 if (isa<ArraySubscriptExpr>(parent))
233 return false;
234 if (isa<CXXBindTemporaryExpr>(parent))
235 return checkifUnused(parent, varDecl);
236 if (isa<MaterializeTemporaryExpr>(parent))
237 return checkifUnused(parent, varDecl);
239 if (isa<CompoundStmt>(parent))
241 interestingDebugMap[varDecl] = parent;
242 return true;
245 // check for cast to void
246 if (auto explicitCastExpr = dyn_cast<ExplicitCastExpr>(parent))
248 if (loplugin::TypeCheck(explicitCastExpr->getTypeAsWritten()).Void())
249 return false;
252 if (isa<MemberExpr>(parent))
253 return checkifUnused(parent, varDecl);
254 if (isa<ExprWithCleanups>(parent))
255 return checkifUnused(parent, varDecl);
256 if (isa<CastExpr>(parent))
257 return checkifUnused(parent, varDecl);
258 if (isa<ParenExpr>(parent))
259 return checkifUnused(parent, varDecl);
261 if (auto operatorCallExpr = dyn_cast<CXXOperatorCallExpr>(parent))
263 const CXXMethodDecl* calleeMethodDecl
264 = dyn_cast_or_null<CXXMethodDecl>(operatorCallExpr->getDirectCallee());
265 if (calleeMethodDecl)
267 if (calleeMethodDecl->getNumParams() == 0)
268 return checkifUnused(parent, varDecl);
269 if (operatorCallExpr->getArg(0) == stmt)
270 return checkifUnused(parent, varDecl);
273 if (auto memberCallExpr = dyn_cast<CXXMemberCallExpr>(parent))
275 const MemberExpr* memberExpr = dyn_cast<MemberExpr>(stmt);
276 if (memberExpr && memberCallExpr->getImplicitObjectArgument() == memberExpr->getBase())
278 // if we are calling a method on the varDecl, walk up
279 if (!checkifUnused(parent, varDecl))
280 return false;
281 // check if we are passing something to the var by non-const ref, in which case it is updating something else for us
282 const FunctionDecl* calleeFunctionDecl = memberCallExpr->getDirectCallee();
283 if (calleeFunctionDecl)
285 if (calleeFunctionDecl->isVariadic())
286 return false;
287 for (unsigned i = 0; i < memberCallExpr->getNumArgs(); ++i)
289 if (i >= calleeFunctionDecl->getNumParams()) // can happen in template code
291 interestingDebugMap[varDecl] = parent;
292 return true;
294 if (!isOkForParameter(calleeFunctionDecl->getParamDecl(i)->getType()))
295 return false;
298 interestingDebugMap[varDecl] = parent;
299 return true;
302 if (isa<CallExpr>(parent))
303 return false;
305 interestingDebugMap[varDecl] = parent;
306 return true;
309 bool UnusedVariableMore::isOkForParameter(const QualType& qt)
311 if (qt->isIntegralOrEnumerationType())
312 return true;
313 auto const type = loplugin::TypeCheck(qt);
314 if (type.Pointer())
316 return bool(type.Pointer().Const());
318 else if (type.LvalueReference())
320 return bool(type.LvalueReference().Const());
322 return false;
325 bool UnusedVariableMore::VisitFunctionDecl(FunctionDecl const* functionDecl)
327 if (ignoreLocation(functionDecl))
328 return true;
329 //functionDecl->dump();
330 return true;
333 static Plugin::Registration<UnusedVariableMore> X("unusedvariablemore", false);
335 } // namespace
337 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */