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

Method sizeofFunction

lib/checksizeof.cpp:355–387  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

353//-----------------------------------------------------------------------------
354
355void CheckSizeofImpl::sizeofFunction()
356{
357 if (!mSettings.severity.isEnabled(Severity::warning) && !mSettings.isPremiumEnabled("sizeofFunctionCall"))
358 return;
359
360 logChecker("CheckSizeof::sizeofFunction"); // warning
361
362 for (const Token *tok = mTokenizer->tokens(); tok; tok = tok->next()) {
363 if (Token::simpleMatch(tok, "sizeof (")) {
364
365 // ignore if the `sizeof` result is cast to void inside a macro, i.e. the calculation is
366 // expected to be parsed but skipped, such as in a disabled custom ASSERT() macro
367 if (tok->isExpandedMacro() && tok->previous()) {
368 const Token *cast_end = (tok->strAt(-1) == "(") ? tok->previous() : tok;
369 if (Token::simpleMatch(cast_end->tokAt(-3), "( void )") ||
370 Token::simpleMatch(cast_end->tokAt(-4), "static_cast < void >")) {
371 continue;
372 }
373 }
374
375 if (const Token *argument = tok->next()->astOperand2()) {
376 const Token *checkToken = argument->previous();
377 if (checkToken->tokType() == Token::eName)
378 continue;
379 const Function * fun = checkToken->function();
380 // Don't report error if the function is overloaded
381 if (fun && fun->nestedIn->functionMap.count(checkToken->str()) == 1) {
382 sizeofFunctionError(tok);
383 }
384 }
385 }
386 }
387}
388
389void CheckSizeofImpl::sizeofFunctionError(const Token *tok)
390{

Callers 1

runChecksMethod · 0.45

Calls 8

isPremiumEnabledMethod · 0.80
nextMethod · 0.80
astOperand2Method · 0.80
simpleMatchFunction · 0.70
isEnabledMethod · 0.45
tokAtMethod · 0.45
functionMethod · 0.45
strMethod · 0.45

Tested by

no test coverage detected