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

Method sizeofVoid

lib/checksizeof.cpp:442–478  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

440}
441
442void CheckSizeofImpl::sizeofVoid()
443{
444 if (!mSettings.severity.isEnabled(Severity::portability))
445 return;
446
447 logChecker("CheckSizeof::sizeofVoid"); // portability
448
449 for (const Token *tok = mTokenizer->tokens(); tok; tok = tok->next()) {
450 if (Token::simpleMatch(tok, "sizeof ( void )")) {
451 sizeofVoidError(tok);
452 } else if (Token::simpleMatch(tok, "sizeof (") && tok->next()->astOperand2()) {
453 const ValueType *vt = tok->next()->astOperand2()->valueType();
454 if (vt && vt->type == ValueType::Type::VOID && vt->pointer == 0U)
455 sizeofDereferencedVoidPointerError(tok, tok->strAt(3));
456 } else if (tok->str() == "-") {
457 // only warn for: 'void *' - 'integral'
458 const ValueType *vt1 = tok->astOperand1() ? tok->astOperand1()->valueType() : nullptr;
459 const ValueType *vt2 = tok->astOperand2() ? tok->astOperand2()->valueType() : nullptr;
460 const bool op1IsvoidPointer = (vt1 && vt1->type == ValueType::Type::VOID && vt1->pointer == 1U);
461 const bool op2IsIntegral = (vt2 && vt2->isIntegral() && vt2->pointer == 0U);
462 if (op1IsvoidPointer && op2IsIntegral)
463 arithOperationsOnVoidPointerError(tok, tok->astOperand1()->expressionString(), vt1->str());
464 } else if (Token::Match(tok, "+|++|--|+=|-=")) { // Arithmetic operations on variable of type "void*"
465 const ValueType *vt1 = tok->astOperand1() ? tok->astOperand1()->valueType() : nullptr;
466 const ValueType *vt2 = tok->astOperand2() ? tok->astOperand2()->valueType() : nullptr;
467
468 const bool voidpointer1 = (vt1 && vt1->type == ValueType::Type::VOID && vt1->pointer == 1U);
469 const bool voidpointer2 = (vt2 && vt2->type == ValueType::Type::VOID && vt2->pointer == 1U);
470
471 if (voidpointer1)
472 arithOperationsOnVoidPointerError(tok, tok->astOperand1()->expressionString(), vt1->str());
473
474 if (!tok->isAssignmentOp() && voidpointer2)
475 arithOperationsOnVoidPointerError(tok, tok->astOperand2()->expressionString(), vt2->str());
476 }
477 }
478}
479
480void CheckSizeofImpl::sizeofVoidError(const Token *tok)
481{

Callers 1

runChecksMethod · 0.45

Calls 9

nextMethod · 0.80
astOperand2Method · 0.80
astOperand1Method · 0.80
isIntegralMethod · 0.80
isAssignmentOpMethod · 0.80
simpleMatchFunction · 0.70
isEnabledMethod · 0.45
strMethod · 0.45
expressionStringMethod · 0.45

Tested by

no test coverage detected