Update git submodules
[LibreOffice.git] / compilerplugins / clang / unusedmember.cxx
blob610c94e162b3c6f099df1958c2d70f61bf86bf9c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 // A more aggressive check for unused C struct/C++ class members than what plain Clang offers. On
11 // the one hand, unlike -Wunused-private-field, it warns about all members regardless of access
12 // specifiers, if all code that can use a class has been seen. On the other hand, it warns about
13 // all kinds of members. But it uses some heuristics (the type showing up in sizeof, alignof,
14 // offsetof, certain casts) to determine that seemingly unused data members are probably used after
15 // all; the used heuristics were enough to not require any explicit [[maybe_unused]] decorations
16 // across the existing code base.
18 #include <cassert>
19 #include <set>
21 #include "check.hxx"
22 #include "compat.hxx"
23 #include "plugin.hxx"
25 namespace
27 // Whether the CXXRecordDecl itself or one of its enclosing classes is a template:
28 bool isTemplated(CXXRecordDecl const* decl)
30 if (decl->getDescribedClassTemplate() != nullptr)
32 return true;
34 if (auto const d = dyn_cast<CXXRecordDecl>(decl->getParent()))
36 return isTemplated(d);
38 return false;
41 bool isWarnUnusedType(QualType type)
43 if (auto const t = type->getAs<RecordType>())
45 if (t->getDecl()->hasAttr<WarnUnusedAttr>())
47 return true;
50 return loplugin::isExtraWarnUnusedType(type);
53 class UnusedMember final : public loplugin::FilteringPlugin<UnusedMember>
55 public:
56 explicit UnusedMember(loplugin::InstantiationData const& data)
57 : FilteringPlugin(data)
61 bool VisitDeclaratorDecl(DeclaratorDecl const* decl)
63 // For declarations like
65 // enum E { ... } e;
67 // it may be that the declaration of E is not marked as referenced even though the
68 // declaration of e clearly references it:
69 if (auto const t = decl->getType()->getAs<EnumType>())
71 deferred_.erase(t->getDecl());
73 return true;
76 bool VisitCXXRecordDecl(CXXRecordDecl const* decl) //TODO: non-CXX RecordDecl?
78 if (ignoreLocation(decl))
80 return true;
82 if (!decl->isThisDeclarationADefinition())
84 return true;
86 if (!handler.isAllRelevantCodeDefined(decl))
88 return true;
90 if (!compiler.getSourceManager().isInMainFile(decl->getLocation()))
92 // include/rtl/instance.hxx declares entities in an unnamed namespace
93 return true;
95 if (isTemplated(decl) || isa<ClassTemplatePartialSpecializationDecl>(decl))
97 return true;
99 if (decl->isUnion() && decl->getIdentifier() == nullptr)
101 return true; //TODO
103 for (auto i = decl->decls_begin(); i != decl->decls_end(); ++i)
105 auto const d = *i;
106 if (d->isImplicit() || isa<AccessSpecDecl>(d) || isa<UsingDecl>(d))
108 //TODO: only filter out UsingDecls that are actually used (if only to silence
109 // -Woverloaded-virtual)
110 continue;
112 if (isa<ClassTemplateDecl>(d) || isa<FunctionTemplateDecl>(d))
114 //TODO: only filter out ones that are not instantiated at all
115 continue;
117 if (auto const d1 = dyn_cast<FriendDecl>(d))
119 //TODO: determine whether the friendship is actually required
120 auto const d2 = d1->getFriendDecl();
121 if (d2 == nullptr)
122 { // happens for "friend class C;"
123 continue;
125 if (auto const d3 = dyn_cast<FunctionDecl>(d2))
127 #if 0 //TODO: friend function definitions are not marked as referenced even if used?
128 if (!d3->isThisDeclarationADefinition()) //TODO: do this check for all kinds?
129 #else
130 (void)d3;
131 #endif
133 continue;
137 if (d->isReferenced())
139 continue;
141 if (d->hasAttr<UnusedAttr>())
143 continue;
145 // Check individual members instead of the whole CXXRecordDecl for coming from a macro,
146 // as CppUnit's CPPUNIT_TEST_SUITE_END (cppunit/extensions/HelperMacros.h) contains a
147 // partial member list ending in
149 // private: /* dummy typedef so that the macro can still end with ';'*/
150 // typedef int CppUnitDummyTypedefForSemiColonEnding__
152 if (compiler.getSourceManager().isMacroBodyExpansion(d->getLocation()))
154 return true;
156 if (auto const d1 = dyn_cast<FieldDecl>(d))
158 if (compat::isUnnamedBitField(d1))
160 continue;
162 if (!isWarnWhenUnusedType(d1->getType()))
164 continue;
166 deferred_.insert(d1);
167 continue;
169 if (auto const d1 = dyn_cast<FunctionDecl>(d))
171 if (d1->isDeletedAsWritten()) // TODO: just isDeleted?
173 continue;
175 if (d1->isExplicitlyDefaulted())
177 continue;
180 else if (auto const d2 = dyn_cast<TagDecl>(d))
182 if (d2->getIdentifier() == nullptr)
184 continue;
186 if (isa<EnumDecl>(d2))
188 deferred_.insert(d2);
189 continue;
192 else if (auto const d3 = dyn_cast<TypedefNameDecl>(d))
194 // Some types, like (specializations of) std::iterator_traits, have specific
195 // requirements on their members; only covers std::iterator_traits for now (TODO:
196 // check that at least some member is actually used)
197 // (isa<ClassTemplatePartialSpecializationDecl>(decl) is already filtered out
198 // above):
199 if (isa<ClassTemplateSpecializationDecl>(decl)
200 && loplugin::DeclCheck(decl).Struct("iterator_traits").StdNamespace())
202 auto const id = d3->getIdentifier();
203 assert(id != nullptr);
204 auto const n = id->getName();
205 if (n == "difference_type" || n == "iterator_category" || n == "pointer"
206 || n == "reference" || n == "value_type")
208 continue;
212 report(DiagnosticsEngine::Warning, "unused class member", d->getLocation())
213 << d->getSourceRange();
215 return true;
218 bool VisitOffsetOfExpr(OffsetOfExpr const* expr)
220 if (ignoreLocation(expr))
222 return true;
224 auto const t1 = expr->getTypeSourceInfo()->getType();
225 if (t1->isTemplateTypeParmType())
227 return true;
229 RecordDecl const* d;
230 if (auto const t2 = t1->getAs<InjectedClassNameType>())
232 d = t2->getDecl();
234 else
236 d = t1->castAs<RecordType>()->getDecl();
238 recordRecordDeclAndBases(d);
239 return true;
242 bool VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr const* expr)
244 if (ignoreLocation(expr))
246 return true;
248 switch (expr->getKind())
250 case UETT_SizeOf:
251 case UETT_AlignOf:
252 case UETT_PreferredAlignOf:
253 break;
254 default:
255 return true;
257 if (!expr->isArgumentType())
259 return true;
261 auto t = expr->getArgumentType();
262 if (auto const t1 = t->getAs<ReferenceType>())
264 t = t1->getPointeeType();
266 if (auto const t1 = t->getAsArrayTypeUnsafe())
268 t = compiler.getASTContext().getBaseElementType(t1);
270 if (auto const t1 = t->getAs<RecordType>())
272 recordRecordDeclAndBases(t1->getDecl());
274 return true;
277 // Handling implicit, C-style, static and reinterpret casts between void* and record types
278 // (though reinterpret_cast would be ruled out by loplugin:redundantcast):
279 bool VisitCastExpr(CastExpr const* expr)
281 if (ignoreLocation(expr))
283 return true;
285 auto const t1 = expr->getType();
286 auto const t2 = compat::getSubExprAsWritten(expr)->getType();
287 if (loplugin::TypeCheck(t1).Pointer().Void())
289 recordCastedRecordDecl(t2);
291 else if (loplugin::TypeCheck(t2).Pointer().Void())
293 recordCastedRecordDecl(t1);
295 return true;
298 bool VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr const* expr)
300 if (ignoreLocation(expr))
302 return true;
304 recordCastedRecordDecl(expr->getTypeAsWritten());
305 recordCastedRecordDecl(expr->getSubExprAsWritten()->getType());
306 return true;
309 bool VisitElaboratedTypeLoc(ElaboratedTypeLoc tloc)
311 if (ignoreLocation(tloc))
313 return true;
315 auto const tl = tloc.getNamedTypeLoc().getAs<TagTypeLoc>();
316 if (tl.isNull())
318 return true;
320 if (tl.isDefinition())
322 return true;
324 if (auto const d = dyn_cast<EnumDecl>(tl.getDecl()))
326 // For some reason, using an elaborated type specifier in (at least) a FieldDecl, as in
328 // enum E { ... };
329 // enum E e;
331 // doesn't cause the EnumDecl to be marked as referenced. (This should fix it, but note
332 // the warning at <https://github.com/llvm/llvm-project/commit/
333 // b96ec568715450106b4f1dd4a20c1c14e9bca6c4#diff-019094457f96a6ed0ee072731d447073R396>:
334 // "[...] a type written 'struct foo' should be represented as an ElaboratedTypeLoc. We
335 // currently only do that when C++ is enabled [...]"
336 deferred_.erase(d->getCanonicalDecl());
338 return true;
341 void postRun() override
343 for (auto const d : deferred_)
345 if (auto const d1 = dyn_cast<FieldDecl>(d))
347 bool layout = false;
348 for (auto d2 = d1->getParent();;)
350 if (layout_.find(d2->getCanonicalDecl()) != layout_.end())
352 layout = true;
353 break;
355 // Heuristic to recursively check parent RecordDecl if given RecordDecl is
356 // unnamed and either an anonymous struct (or union, but which are already
357 // filtered out anyway), or defined in a non-static data member declaration
358 // (TODO: which is erroneously approximated here with getTypedefNameForAnonDecl
359 // for now, which fails to filter out RecordDecls in static data member
360 // declarations):
361 if (!(d2->getDeclName().isEmpty()
362 && (d2->isAnonymousStructOrUnion()
363 || d2->getTypedefNameForAnonDecl() == nullptr)))
365 break;
367 d2 = dyn_cast<RecordDecl>(d2->getParent());
368 if (d2 == nullptr)
370 break;
373 if (layout)
375 continue;
378 report(DiagnosticsEngine::Warning, "unused class member", d->getLocation())
379 << d->getSourceRange();
383 private:
384 void run() override
386 if (TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()))
388 postRun();
392 bool isWarnWhenUnusedType(QualType type)
394 auto t = type;
395 if (auto const t1 = t->getAs<ReferenceType>())
397 t = t1->getPointeeType();
399 return t.isTrivialType(compiler.getASTContext()) || isWarnUnusedType(t);
402 void recordRecordDeclAndBases(RecordDecl const* decl)
404 if (!layout_.insert(decl->getCanonicalDecl()).second)
406 return;
408 if (auto const d2 = dyn_cast_or_null<CXXRecordDecl>(decl->getDefinition()))
410 for (auto i = d2->bases_begin(); i != d2->bases_end(); ++i)
412 recordRecordDeclAndBases(i->getType()->castAs<RecordType>()->getDecl());
414 //TODO: doesn't iterate vbases, but presence of such would run counter to the layout
415 // heuristic anyway
419 void recordCastedRecordDecl(QualType type)
421 for (auto t = type;;)
423 if (auto const t1 = t->getAs<clang::PointerType>())
425 t = t1->getPointeeType();
426 continue;
428 if (auto const t1 = t->getAs<RecordType>())
430 recordRecordDeclAndBases(t1->getDecl());
432 break;
436 // RecordDecls whose layout (i.e., contained FieldDecls) must presumably not be changed:
437 std::set<TagDecl const*> layout_;
439 std::set<Decl const*> deferred_;
442 loplugin::Plugin::Registration<UnusedMember> unusedmember("unusedmember");
445 // Cannot be shared, uses TraverseStmt().
447 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */