Fixed #3919 (False positive: uninitialized variable (extern))

This commit is contained in:
Daniel Marjamäki 2012-06-26 18:28:41 +02:00
parent 1ad56a25c6
commit 9da3373876
2 changed files with 15 additions and 2 deletions

View File

@ -390,7 +390,7 @@ private:
// Variable declaration.. // Variable declaration..
if (tok.varId() && Token::Match(&tok, "%var% [[;]")) { if (tok.varId() && Token::Match(&tok, "%var% [[;]")) {
const Variable* var2 = symbolDatabase->getVariableFromVarId(tok.varId()); const Variable* var2 = symbolDatabase->getVariableFromVarId(tok.varId());
if (var2 && var2->nameToken() == &tok && !var2->isStatic() && !var2->isConst()) { if (var2 && var2->nameToken() == &tok && !var2->isStatic() && !var2->isExtern() && !var2->isConst()) {
if (tok.linkAt(1)) { // array if (tok.linkAt(1)) { // array
const Token* endtok = tok.next(); const Token* endtok = tok.next();
while (endtok->link()) while (endtok->link())
@ -1044,7 +1044,7 @@ void CheckUninitVar::check()
void CheckUninitVar::checkScope(const Scope* scope) void CheckUninitVar::checkScope(const Scope* scope)
{ {
for (std::list<Variable>::const_iterator i = scope->varlist.begin(); i != scope->varlist.end(); ++i) { for (std::list<Variable>::const_iterator i = scope->varlist.begin(); i != scope->varlist.end(); ++i) {
if ((i->type() && !i->isPointer()) || i->isStatic() || i->isConst() || i->isArray() || i->isReference()) if ((i->type() && !i->isPointer()) || i->isStatic() || i->isExtern() || i->isConst() || i->isArray() || i->isReference())
continue; continue;
if (i->nameToken()->strAt(1) == "(") if (i->nameToken()->strAt(1) == "(")
continue; continue;

View File

@ -207,6 +207,13 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: a\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized variable: a\n", errout.str());
checkUninitVar("void f()\n"
"{\n"
" extern int a;\n"
" a++;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
checkUninitVar("void f()\n" checkUninitVar("void f()\n"
"{\n" "{\n"
" int a;\n" " int a;\n"
@ -1914,6 +1921,12 @@ private:
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkUninitVar2("int f() {\n"
" extern int x;\n"
" return ++x;\n"
"}");
ASSERT_EQUALS("", errout.str());
// using uninit var in condition // using uninit var in condition
checkUninitVar2("void f() {\n" checkUninitVar2("void f() {\n"
" int x;\n" " int x;\n"