From 6601de768143befc2e3bcb6318a4639c04d56b62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 24 Oct 2010 18:26:59 +0200 Subject: [PATCH] uninitialized variables: refactoring handling of loop bodies --- lib/checkother.cpp | 67 +++++----------------------------------------- test/testother.cpp | 3 +-- test/testsuite.cpp | 4 ++- 3 files changed, 10 insertions(+), 64 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index a9fecb2a6..34962ea92 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -3647,70 +3647,15 @@ private: return ExecutionPath::parseCondition(tok, checks); } - void parseLoopBody(const Token *tok_, std::list &checks) const + void parseLoopBody(const Token *tok, std::list &checks) const { - for (; tok_; tok_ = tok_->next()) + while (tok) { - if (tok_->str() == "{") + if (tok->str() == "{") return; - - const Token &tok = *tok_; - - if (Token::Match(tok.previous(), "[;{}] %var% [=[.]")) - { - if (tok.next()->str() == ".") - { - if (use_dead_pointer(checks, &tok)) - { - return; - } - } - else - { - // check variable usages in rhs/index - for (const Token *tok2 = tok.tokAt(2); tok2; tok2 = tok2->next()) - { - if (Token::Match(tok2, "[;)=?]")) - break; - if (Token::Match(tok2, "%var% (")) - break; - if (tok2->varId() && - !Token::Match(tok2->previous(), "&|::") && - !Token::simpleMatch(tok2->next(), "=")) - { - // Multiple assignments.. - if (Token::simpleMatch(tok2->next(), "[")) - { - const Token * tok3 = tok2; - while (Token::simpleMatch(tok3->next(), "[")) - tok3 = tok3->next()->link(); - if (Token::simpleMatch(tok3, "] =")) - continue; - } - bool foundError; - if (tok2->previous()->str() == "*" || tok2->next()->str() == "[") - foundError = use_array_or_pointer_data(checks, tok2); - else - foundError = use(checks, tok2); - - // prevent duplicate error messages - if (foundError) - { - bailOutVar(checks, tok2->varId()); - } - } - } - } - - // pointer aliasing? - if (Token::Match(tok.tokAt(2), "%var% ;")) - { - pointer_assignment(checks, &tok, tok.tokAt(2)); - } - } - - - + const Token *next = parse(*tok, checks); + if (next) + tok = tok->next(); } } diff --git a/test/testother.cpp b/test/testother.cpp index 833cadec2..0d45a49ab 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -1452,8 +1452,7 @@ private: " *p = 0;\n" " }\n" "}\n"); - ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: p\n" // <-- duplicate - "[test.cpp:5]: (error) Uninitialized variable: p\n", errout.str()); + ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: p\n", errout.str()); // += checkUninitVar("void f()\n" diff --git a/test/testsuite.cpp b/test/testsuite.cpp index f7b59ac1f..2c1177b39 100644 --- a/test/testsuite.cpp +++ b/test/testsuite.cpp @@ -271,5 +271,7 @@ void TestFixture::reportOut(const std::string & outmsg) void TestFixture::reportErr(const ErrorLogger::ErrorMessage &msg) { - errout << msg.toString() << std::endl; + const std::string errormessage(msg.toString()); + if (errout.str().find(errormessage) == std::string::npos) + errout << errormessage << std::endl; }