| 440 | } |
| 441 | |
| 442 | void 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 | |
| 480 | void CheckSizeofImpl::sizeofVoidError(const Token *tok) |
| 481 | { |
no test coverage detected