From 5ee85ee88aa37b8f70595a05acf05fb36baebd57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 14 Apr 2014 06:45:39 +0200 Subject: [PATCH] ValueFlow: Improved handling of bitand against a single-bit integer literal --- lib/valueflow.cpp | 29 +++++++++++++++++++++++++++++ test/testvalueflow.cpp | 13 +++++++++++++ 2 files changed, 42 insertions(+) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 9044b2225..9942d1281 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -238,6 +238,34 @@ static void valueFlowNumber(TokenList *tokenlist) } } +static void valueFlowBitAnd(TokenList *tokenlist) +{ + for (Token *tok = tokenlist->front(); tok; tok = tok->next()) { + if (tok->str() != "&") + continue; + + if (!tok->astOperand1() || !tok->astOperand2()) + continue; + + MathLib::bigint number; + if (MathLib::isInt(tok->astOperand1()->str())) + number = MathLib::toLongNumber(tok->astOperand1()->str()); + else if (MathLib::isInt(tok->astOperand2()->str())) + number = MathLib::toLongNumber(tok->astOperand2()->str()); + else + continue; + + int bit = 0; + while (bit <= 60 && ((1LL<front(); tok; tok = tok->next()) { @@ -971,6 +999,7 @@ void ValueFlow::setValues(TokenList *tokenlist, ErrorLogger *errorLogger, const tok->values.clear(); valueFlowNumber(tokenlist); + valueFlowBitAnd(tokenlist); valueFlowForLoop(tokenlist, errorLogger, settings); valueFlowBeforeCondition(tokenlist, errorLogger, settings); valueFlowAfterAssign(tokenlist, errorLogger, settings); diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 501b1da75..ba9776fc7 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -36,6 +36,8 @@ private: void run() { TEST_CASE(valueFlowNumber); + TEST_CASE(valueFlowBitAnd); + TEST_CASE(valueFlowCalculations); TEST_CASE(valueFlowBeforeCondition); @@ -629,6 +631,17 @@ private: ASSERT_EQUALS(false, testValueOfX(code, 8U, 2)); // x is not 2 at line 8 } + void valueFlowBitAnd() { + const char *code; + + code = "int f(int a) {\n" + " int x = a & 0x80;\n" + " return x;\n" + "}"; + ASSERT_EQUALS(true, testValueOfX(code,3U,0)); + ASSERT_EQUALS(true, testValueOfX(code,3U,0x80)); + } + void valueFlowForLoop() { const char *code;