From b52fa9451f58045ee3250b3abe2b9c781707ce4c Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Fri, 12 Feb 2010 22:24:06 +0200 Subject: [PATCH] Fix #1385 (False positive: unsigned division) http://sourceforge.net/apps/trac/cppcheck/ticket/1385 This also fixes a bug in setVarId(). "unsigned int a" didn't get varid, untill later when unsigned was simplified away. --- lib/checkother.cpp | 5 ++++- lib/tokenize.cpp | 5 ++++- test/testdivision.cpp | 11 +++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index d3243e3e2..132467294 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -323,7 +323,10 @@ void CheckOther::checkUnsignedDivision() else if (Token::Match(tok, "[{};(,] unsigned %type% %var% [;=,)]")) varsign[tok->tokAt(3)->varId()] = 'u'; - else if (!Token::Match(tok, "[).]") && Token::Match(tok->next(), "%var% / %var%")) + else if (!Token::Match(tok, "[).]") && + Token::Match(tok->next(), "%var% / %var%") && + tok->tokAt(1)->varId() != 0 && + tok->tokAt(3)->varId() != 0) { if (ErrorLogger::udivWarning(*_settings)) { diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 407703096..3f9a0c796 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -1830,9 +1830,12 @@ void Tokenizer::setVarId() if (Token::Match(tok, "[,;{}(] %type%")) tok = tok->next(); - if (tok->str() == "new" || tok->str() == "unsigned") + if (tok->str() == "new") continue; + if (tok->str() == "unsigned") + tok = tok->next(); + if (Token::Match(tok, "class|struct %type% :|{|;")) continue; diff --git a/test/testdivision.cpp b/test/testdivision.cpp index 6486a4e5b..4e8e0f1f9 100644 --- a/test/testdivision.cpp +++ b/test/testdivision.cpp @@ -115,6 +115,17 @@ private: " result = i2 / i1;}\n" ); ASSERT_EQUALS("", errout.str()); + + check("void f1()\n" + "{\n" + " unsigned int num = 0;\n" + "}\n" + "\n" + "void f2(int X)\n" + "{\n" + " X = X / z;}\n" + ); + ASSERT_EQUALS("", errout.str()); } void division5()