MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / checkUnionZeroInit

Method checkUnionZeroInit

lib/checkother.cpp:4575–4611  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4573
4574
4575void CheckOtherImpl::checkUnionZeroInit()
4576{
4577 if (!mSettings.severity.isEnabled(Severity::portability))
4578 return;
4579
4580 logChecker("CheckOther::checkUnionZeroInit"); // portability
4581
4582 const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
4583
4584 std::unordered_map<const Scope *, Union> unionsByScopeId;
4585 const std::vector<Union> unions = parseUnions(*symbolDatabase, mSettings);
4586 for (const Union &u : unions) {
4587 unionsByScopeId.emplace(u.scope, u);
4588 }
4589
4590 for (const Token *tok = mTokenizer->tokens(); tok; tok = tok->next()) {
4591 if (!tok->isName() || !isZeroInitializer(tok->next()))
4592 continue;
4593
4594 const ValueType *vt = tok->valueType();
4595 if (vt == nullptr || vt->typeScope == nullptr)
4596 continue;
4597 auto it = unionsByScopeId.find(vt->typeScope);
4598 if (it == unionsByScopeId.end())
4599 continue;
4600 const Union &u = it->second;
4601 if (!u.isLargestMemberFirst()) {
4602 const UnionMember *largestMember = u.largestMember();
4603 if (largestMember == nullptr) {
4604 throw InternalError(tok, "Largest union member not found",
4605 InternalError::INTERNAL);
4606 }
4607 assert(largestMember != nullptr);
4608 unionZeroInitError(tok, *largestMember);
4609 }
4610 }
4611}
4612
4613void CheckOtherImpl::unionZeroInitError(const Token *tok,
4614 const UnionMember& largestMember)

Callers 1

runChecksMethod · 0.80

Calls 8

parseUnionsFunction · 0.85
isZeroInitializerFunction · 0.85
nextMethod · 0.80
isLargestMemberFirstMethod · 0.80
largestMemberMethod · 0.80
isEnabledMethod · 0.45
findMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected