From 0cf551351b2d108c331cd7cacaac2448b54c3181 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 19 Sep 2017 08:50:19 +0200 Subject: [PATCH] tooBigBitwiseShift: check all calculations, not only in function bodies --- lib/checktype.cpp | 55 +++++++++++++++++++++-------------------------- 1 file changed, 25 insertions(+), 30 deletions(-) diff --git a/lib/checktype.cpp b/lib/checktype.cpp index 37c0ac146..5b70b9ddd 100644 --- a/lib/checktype.cpp +++ b/lib/checktype.cpp @@ -57,40 +57,35 @@ void CheckType::checkTooBigBitwiseShift() if (_settings->platformType == Settings::Unspecified) return; - const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase(); - const std::size_t functions = symbolDatabase->functionScopes.size(); - for (std::size_t i = 0; i < functions; ++i) { - const Scope * scope = symbolDatabase->functionScopes[i]; - for (const Token* tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) { - // C++ and macro: OUT(x<isCPP() && Token::Match(tok, "[;{}] %name% (") && Token::simpleMatch(tok->linkAt(2), ") ;") && tok->next()->isUpperCaseName() && !tok->next()->function()) - tok = tok->linkAt(2); + for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) { + // C++ and macro: OUT(x<isCPP() && Token::Match(tok, "[;{}] %name% (") && Token::simpleMatch(tok->linkAt(2), ") ;") && tok->next()->isUpperCaseName() && !tok->next()->function()) + tok = tok->linkAt(2); - if (!tok->astOperand1() || !tok->astOperand2()) - continue; + if (!tok->astOperand1() || !tok->astOperand2()) + continue; - if (!Token::Match(tok, "<<|>>|<<=|>>=")) - continue; + if (!Token::Match(tok, "<<|>>|<<=|>>=")) + continue; - // get number of bits of lhs - const ValueType *lhstype = tok->astOperand1()->valueType(); - if (!lhstype || !lhstype->isIntegral() || lhstype->pointer >= 1U) - continue; - int lhsbits = 0; - if (lhstype->type <= ValueType::Type::INT) - lhsbits = _settings->int_bit; - else if (lhstype->type == ValueType::Type::LONG) - lhsbits = _settings->long_bit; - else if (lhstype->type == ValueType::Type::LONGLONG) - lhsbits = _settings->long_long_bit; - else - continue; + // get number of bits of lhs + const ValueType *lhstype = tok->astOperand1()->valueType(); + if (!lhstype || !lhstype->isIntegral() || lhstype->pointer >= 1U) + continue; + int lhsbits = 0; + if (lhstype->type <= ValueType::Type::INT) + lhsbits = _settings->int_bit; + else if (lhstype->type == ValueType::Type::LONG) + lhsbits = _settings->long_bit; + else if (lhstype->type == ValueType::Type::LONGLONG) + lhsbits = _settings->long_long_bit; + else + continue; - // Get biggest rhs value. preferably a value which doesn't have 'condition'. - const ValueFlow::Value *value = tok->astOperand2()->getValueGE(lhsbits, _settings); - if (value && _settings->isEnabled(value, false)) - tooBigBitwiseShiftError(tok, lhsbits, *value); - } + // Get biggest rhs value. preferably a value which doesn't have 'condition'. + const ValueFlow::Value *value = tok->astOperand2()->getValueGE(lhsbits, _settings); + if (value && _settings->isEnabled(value, false)) + tooBigBitwiseShiftError(tok, lhsbits, *value); } }