Symbol database: Improved function lookup (a.b.f()). Ticket: #4494

This commit is contained in:
Robert Reif 2013-01-25 06:49:04 +01:00 committed by Daniel Marjamäki
parent 163cce726c
commit 3b08b410a2
3 changed files with 53 additions and 1 deletions

View File

@ -852,6 +852,34 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti
}
}
// fill in missing variables if possible
for (std::size_t i = 1; i <= _tokenizer->varIdCount(); i++) {
// check each missing variable
if (_variableList[i] == 0) {
// find the token with this varid
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
if (tok->varId() == i) {
// check if it is a member variable
const Token *tok1 = tok->tokAt(-2);
if (tok1 && Token::Match(tok1, "%var% .") && tok1->varId()) {
// check if this varid has a variable
const Variable *var = getVariableFromVarId(tok1->varId());
if (var && var->type()) {
// find the member variable of this variable
const Variable *var1 = var->type()->getVariable(tok->str());
if (var1) {
// add this variable to the look up table
_variableList[i] = var1;
}
}
}
// found varid so stop searching
break;
}
}
}
}
/* set all unknown array dimensions that are set by a variable to the maximum size of that variable type */
for (std::size_t i = 1; i <= _tokenizer->varIdCount(); i++) {
// check each array variable

View File

@ -1711,7 +1711,8 @@ private:
" TEST test;\n"
" test.a[-1] = 3;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Array 'test.a[10]' accessed at index -1, which is out of bounds.\n", errout.str());
ASSERT_EQUALS("[test.cpp:4]: (error) Array 'test.a[10]' accessed at index -1, which is out of bounds.\n"
"[test.cpp:4]: (error) Array index -1 is out of bounds.\n", errout.str());
}
void array_index_for_decr() {

View File

@ -2745,6 +2745,29 @@ private:
" fred.f1(p);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:8]: (error) Uninitialized variable: p\n", errout.str());
checkUninitVar2("class Fred {\n"
"public:\n"
" class Wilma {\n"
" public:\n"
" class Barney {\n"
" public:\n"
" class Betty {\n"
" public:\n"
" void f1(char *p) { *p = 0; }\n"
" };\n"
" Betty betty;\n"
" };\n"
" Barney barney;\n"
" };\n"
" Wilma wilma;\n"
"};\n"
"Fred fred;\n"
"void f(void) {\n"
" char *p;\n"
" fred.wilma.barney.betty.f1(p);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:20]: (error) Uninitialized variable: p\n", errout.str());
}
};