From 5aaec7adc5501ca18cad29e81d71302a8c67b1cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 26 Dec 2011 18:56:40 +0100 Subject: [PATCH] Uninitialized variables: Better handling of variable usage in condition --- lib/checkuninitvar.cpp | 8 ++++---- lib/checkuninitvar.h | 2 +- test/testuninitvar.cpp | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 8ef80a58e..92135d2b4 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1114,7 +1114,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const unsigned int // Inner scope.. if (Token::Match(tok, "if (")) { // initialization / usage in condition.. - if (checkIfForWhileHead(tok->next(), varid, ispointer, suppressErrors)) + if (checkIfForWhileHead(tok->next(), varid, ispointer, suppressErrors, bool(number_of_if == 0))) return true; // goto the { @@ -1185,7 +1185,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const unsigned int return true; // is variable used / initialized in for-head - if (checkIfForWhileHead(tok->next(), varid, ispointer, suppressErrors)) + if (checkIfForWhileHead(tok->next(), varid, ispointer, suppressErrors, bool(number_of_if == 0))) return true; } @@ -1219,7 +1219,7 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const unsigned int return ret; } -bool CheckUninitVar::checkIfForWhileHead(const Token *startparanthesis, unsigned int varid, bool ispointer, bool suppressErrors) +bool CheckUninitVar::checkIfForWhileHead(const Token *startparanthesis, unsigned int varid, bool ispointer, bool suppressErrors, bool isuninit) { const Token * const endpar = startparanthesis->link(); for (const Token *tok = startparanthesis->next(); tok && tok != endpar; tok = tok->next()) { @@ -1230,7 +1230,7 @@ bool CheckUninitVar::checkIfForWhileHead(const Token *startparanthesis, unsigned } if (Token::Match(tok, "sizeof|decltype|offsetof (")) tok = tok->next()->link(); - if (tok->str() == "&&") + if (!isuninit && tok->str() == "&&") suppressErrors = true; } return false; diff --git a/lib/checkuninitvar.h b/lib/checkuninitvar.h index d6896ed4c..f06a3a9fb 100644 --- a/lib/checkuninitvar.h +++ b/lib/checkuninitvar.h @@ -61,7 +61,7 @@ public: /** Check for uninitialized variables */ void check(); bool checkScopeForVariable(const Token *tok, const unsigned int varid, bool ispointer, bool * const possibleInit); - bool checkIfForWhileHead(const Token *startparanthesis, unsigned int varid, bool ispointer, bool suppressErrors); + bool checkIfForWhileHead(const Token *startparanthesis, unsigned int varid, bool ispointer, bool suppressErrors, bool isuninit); bool isVariableUsage(const Token *vartok, bool ispointer) const; diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 5ce31e8c4..1f15ea6d4 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -1928,7 +1928,7 @@ private: " int a;\n" " if (x == 0 && (a == 1)) { }\n" "}\n"); - TODO_ASSERT_EQUALS("error", "", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str()); // asm checkUninitVar2("void f() {\n"