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

Method checkNegativeBitwiseShift

lib/checkother.cpp:3444–3483  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3442}
3443
3444void CheckOtherImpl::checkNegativeBitwiseShift()
3445{
3446 const bool portability = mSettings.severity.isEnabled(Severity::portability);
3447
3448 logChecker("CheckOther::checkNegativeBitwiseShift");
3449
3450 for (const Token* tok = mTokenizer->tokens(); tok; tok = tok->next()) {
3451 tok = skipUnreachableBranch(tok);
3452
3453 if (!tok->astOperand1() || !tok->astOperand2())
3454 continue;
3455
3456 if (!Token::Match(tok, "<<|>>|<<=|>>="))
3457 continue;
3458
3459 // don't warn if lhs is a class. this is an overloaded operator then
3460 if (tok->isCpp()) {
3461 const ValueType * lhsType = tok->astOperand1()->valueType();
3462 if (!lhsType || !lhsType->isIntegral() || lhsType->pointer)
3463 continue;
3464 }
3465
3466 // bailout if operation is protected by ?:
3467 bool ternary = false;
3468 for (const Token *parent = tok; parent; parent = parent->astParent()) {
3469 if (Token::Match(parent, "?|:")) {
3470 ternary = true;
3471 break;
3472 }
3473 }
3474 if (ternary)
3475 continue;
3476
3477 // Get negative rhs value. preferably a value which doesn't have 'condition'.
3478 if (portability && isNegative(tok->astOperand1(), mSettings))
3479 negativeBitwiseShiftError(tok, 1);
3480 else if (isNegative(tok->astOperand2(), mSettings))
3481 negativeBitwiseShiftError(tok, 2);
3482 }
3483}
3484
3485
3486void CheckOtherImpl::negativeBitwiseShiftError(const Token *tok, int op)

Callers 1

runChecksMethod · 0.80

Calls 8

skipUnreachableBranchFunction · 0.85
isNegativeFunction · 0.85
nextMethod · 0.80
astOperand1Method · 0.80
astOperand2Method · 0.80
isIntegralMethod · 0.80
astParentMethod · 0.80
isEnabledMethod · 0.45

Tested by

no test coverage detected