1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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/.
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.
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)
34 if (auto const d
= dyn_cast
<CXXRecordDecl
>(decl
->getParent()))
36 return isTemplated(d
);
41 bool isWarnUnusedType(QualType type
)
43 if (auto const t
= type
->getAs
<RecordType
>())
45 if (t
->getDecl()->hasAttr
<WarnUnusedAttr
>())
50 return loplugin::isExtraWarnUnusedType(type
);
53 class UnusedMember final
: public loplugin::FilteringPlugin
<UnusedMember
>
56 explicit UnusedMember(loplugin::InstantiationData
const& data
)
57 : FilteringPlugin(data
)
61 bool VisitDeclaratorDecl(DeclaratorDecl
const* decl
)
63 // For declarations like
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());
76 bool VisitCXXRecordDecl(CXXRecordDecl
const* decl
) //TODO: non-CXX RecordDecl?
78 if (ignoreLocation(decl
))
82 if (!decl
->isThisDeclarationADefinition())
86 if (!handler
.isAllRelevantCodeDefined(decl
))
90 if (!compiler
.getSourceManager().isInMainFile(decl
->getLocation()))
92 // include/rtl/instance.hxx declares entities in an unnamed namespace
95 if (isTemplated(decl
) || isa
<ClassTemplatePartialSpecializationDecl
>(decl
))
99 if (decl
->isUnion() && decl
->getIdentifier() == nullptr)
103 for (auto i
= decl
->decls_begin(); i
!= decl
->decls_end(); ++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)
112 if (isa
<ClassTemplateDecl
>(d
) || isa
<FunctionTemplateDecl
>(d
))
114 //TODO: only filter out ones that are not instantiated at all
117 if (auto const d1
= dyn_cast
<FriendDecl
>(d
))
119 //TODO: determine whether the friendship is actually required
120 auto const d2
= d1
->getFriendDecl();
122 { // happens for "friend class C;"
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?
137 if (d
->isReferenced())
141 if (d
->hasAttr
<UnusedAttr
>())
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()))
156 if (auto const d1
= dyn_cast
<FieldDecl
>(d
))
158 if (compat::isUnnamedBitField(d1
))
162 if (!isWarnWhenUnusedType(d1
->getType()))
166 deferred_
.insert(d1
);
169 if (auto const d1
= dyn_cast
<FunctionDecl
>(d
))
171 if (d1
->isDeletedAsWritten()) // TODO: just isDeleted?
175 if (d1
->isExplicitlyDefaulted())
180 else if (auto const d2
= dyn_cast
<TagDecl
>(d
))
182 if (d2
->getIdentifier() == nullptr)
186 if (isa
<EnumDecl
>(d2
))
188 deferred_
.insert(d2
);
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
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")
212 report(DiagnosticsEngine::Warning
, "unused class member", d
->getLocation())
213 << d
->getSourceRange();
218 bool VisitOffsetOfExpr(OffsetOfExpr
const* expr
)
220 if (ignoreLocation(expr
))
224 auto const t1
= expr
->getTypeSourceInfo()->getType();
225 if (t1
->isTemplateTypeParmType())
230 if (auto const t2
= t1
->getAs
<InjectedClassNameType
>())
236 d
= t1
->castAs
<RecordType
>()->getDecl();
238 recordRecordDeclAndBases(d
);
242 bool VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr
const* expr
)
244 if (ignoreLocation(expr
))
248 switch (expr
->getKind())
252 case UETT_PreferredAlignOf
:
257 if (!expr
->isArgumentType())
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());
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
))
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
);
298 bool VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr
const* expr
)
300 if (ignoreLocation(expr
))
304 recordCastedRecordDecl(expr
->getTypeAsWritten());
305 recordCastedRecordDecl(expr
->getSubExprAsWritten()->getType());
309 bool VisitElaboratedTypeLoc(ElaboratedTypeLoc tloc
)
311 if (ignoreLocation(tloc
))
315 auto const tl
= tloc
.getNamedTypeLoc().getAs
<TagTypeLoc
>();
320 if (tl
.isDefinition())
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
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());
341 void postRun() override
343 for (auto const d
: deferred_
)
345 if (auto const d1
= dyn_cast
<FieldDecl
>(d
))
348 for (auto d2
= d1
->getParent();;)
350 if (layout_
.find(d2
->getCanonicalDecl()) != layout_
.end())
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
361 if (!(d2
->getDeclName().isEmpty()
362 && (d2
->isAnonymousStructOrUnion()
363 || d2
->getTypedefNameForAnonDecl() == nullptr)))
367 d2
= dyn_cast
<RecordDecl
>(d2
->getParent());
378 report(DiagnosticsEngine::Warning
, "unused class member", d
->getLocation())
379 << d
->getSourceRange();
386 if (TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl()))
392 bool isWarnWhenUnusedType(QualType 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
)
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
419 void recordCastedRecordDecl(QualType type
)
421 for (auto t
= type
;;)
423 if (auto const t1
= t
->getAs
<clang::PointerType
>())
425 t
= t1
->getPointeeType();
428 if (auto const t1
= t
->getAs
<RecordType
>())
430 recordRecordDeclAndBases(t1
->getDecl());
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: */