Skip to content

Commit

Permalink
Fix valueFlowBailoutIncompleteVar with ptr to ptr (refs #10045) (#5488)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Sep 28, 2023
1 parent 73d305e commit 63b76d2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 22 deletions.
44 changes: 22 additions & 22 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,7 @@ void SymbolDatabase::createSymbolDatabaseSetFunctionPointers(bool firstPass)
}

// Set function call pointers
for (const Token* tok = mTokenizer.list.front(); tok != mTokenizer.list.back(); tok = tok->next()) {
for (Token* tok = mTokenizer.list.front(); tok != mTokenizer.list.back(); tok = tok->next()) {
if (tok->isName() && !tok->function() && tok->varId() == 0 && ((tok->astParent() && tok->astParent()->isComparisonOp()) || Token::Match(tok, "%name% [{(,)>;]")) && !isReservedName(tok->str())) {
if (tok->next()->str() == ">" && !tok->next()->link())
continue;
Expand All @@ -1121,7 +1121,7 @@ void SymbolDatabase::createSymbolDatabaseSetFunctionPointers(bool firstPass)
if (!function || (isTemplateArg && function->isConstructor()))
continue;

const_cast<Token *>(tok)->function(function);
tok->function(function);

if (tok->next()->str() != "(")
const_cast<Function *>(function)->functionPointerUsage = tok;
Expand Down Expand Up @@ -1170,7 +1170,7 @@ void SymbolDatabase::createSymbolDatabaseSetTypePointers()
}

// Set type pointers
for (const Token* tok = mTokenizer.list.front(); tok != mTokenizer.list.back(); tok = tok->next()) {
for (Token* tok = mTokenizer.list.front(); tok != mTokenizer.list.back(); tok = tok->next()) {
if (!tok->isName() || tok->varId() || tok->function() || tok->type() || tok->enumerator())
continue;

Expand All @@ -1179,7 +1179,7 @@ void SymbolDatabase::createSymbolDatabaseSetTypePointers()

const Type *type = findVariableType(tok->scope(), tok);
if (type)
const_cast<Token *>(tok)->type(type); // TODO: avoid const_cast
tok->type(type);
}
}

Expand Down Expand Up @@ -1241,7 +1241,7 @@ void SymbolDatabase::createSymbolDatabaseSetVariablePointers()
if (!tok->isName() || tok->isKeyword() || tok->isStandardType())
continue;
if (tok->varId())
const_cast<Token*>(tok)->variable(getVariableFromVarId(tok->varId()));
tok->variable(getVariableFromVarId(tok->varId()));

// Set Token::variable pointer for array member variable
// Since it doesn't point at a fixed location it doesn't have varid
Expand All @@ -1253,24 +1253,24 @@ void SymbolDatabase::createSymbolDatabaseSetVariablePointers()
if (isVar && (isArrayAccess || isDirectAccess || isDerefAccess)) {
Token* membertok{};
if (isArrayAccess) {
membertok = const_cast<Token*>(tok->astParent());
membertok = tok->astParent();
while (Token::simpleMatch(membertok, "["))
membertok = membertok->astParent();
if (membertok)
membertok = membertok->astOperand2();
}
else if (isDirectAccess) {
membertok = const_cast<Token*>(tok->astParent()->astOperand2());
membertok = tok->astParent()->astOperand2();
if (membertok == tok) {
Token* gptok = const_cast<Token*>(tok->astParent()->astParent());
Token* gptok = tok->astParent()->astParent();
if (Token::simpleMatch(gptok, ".")) // chained access
membertok = gptok->astOperand2();
else if (Token::simpleMatch(gptok, "[") && Token::simpleMatch(gptok->astParent(), "."))
membertok = gptok->astParent()->astOperand2();
}
}
else { // isDerefAccess
membertok = const_cast<Token*>(tok->astParent());
membertok = tok->astParent();
while (Token::simpleMatch(membertok, "*"))
membertok = membertok->astParent();
if (membertok)
Expand Down Expand Up @@ -1394,15 +1394,15 @@ void SymbolDatabase::createSymbolDatabaseEnums()
}

// find enumerators
for (const Token* tok = mTokenizer.list.front(); tok != mTokenizer.list.back(); tok = tok->next()) {
for (Token* tok = mTokenizer.list.front(); tok != mTokenizer.list.back(); tok = tok->next()) {
const bool isVariable = (tok->tokType() == Token::eVariable && !tok->variable());
if (tok->tokType() != Token::eName && !isVariable)
continue;
const Enumerator * enumerator = findEnumerator(tok, tokensThatAreNotEnumeratorValues);
if (enumerator) {
if (isVariable)
const_cast<Token*>(tok)->varId(0);
const_cast<Token*>(tok)->enumerator(enumerator);
tok->varId(0);
tok->enumerator(enumerator);
}
}
}
Expand Down Expand Up @@ -1470,7 +1470,7 @@ void SymbolDatabase::createSymbolDatabaseIncompleteVars()
"volatile",
"NULL",
};
for (const Token* tok = mTokenizer.list.front(); tok != mTokenizer.list.back(); tok = tok->next()) {
for (Token* tok = mTokenizer.list.front(); tok != mTokenizer.list.back(); tok = tok->next()) {
const Scope * scope = tok->scope();
if (!scope)
continue;
Expand All @@ -1492,7 +1492,7 @@ void SymbolDatabase::createSymbolDatabaseIncompleteVars()
continue;
if (Token::Match(tok->next(), "::|.|(|{|:|%var%"))
continue;
if (Token::Match(tok->next(), "&|&&|* )|,|%var%|const"))
if (Token::Match(tok->next(), "&|&&|* *| *| )|,|%var%|const"))
continue;
// Very likely a typelist
if (Token::Match(tok->tokAt(-2), "%type% ,") || Token::Match(tok->next(), ", %type%"))
Expand Down Expand Up @@ -1529,7 +1529,7 @@ void SymbolDatabase::createSymbolDatabaseIncompleteVars()
if (Token::simpleMatch(parent, "new"))
continue;
}
const_cast<Token *>(tok)->isIncompleteVar(true); // TODO: avoid const_cast
tok->isIncompleteVar(true);
}
}

Expand Down Expand Up @@ -1806,13 +1806,13 @@ void SymbolDatabase::setArrayDimensionsUsingValueFlow()
SymbolDatabase::~SymbolDatabase()
{
// Clear scope, type, function and variable pointers
for (const Token* tok = mTokenizer.list.front(); tok; tok = tok->next()) {
const_cast<Token *>(tok)->scope(nullptr);
const_cast<Token *>(tok)->type(nullptr);
const_cast<Token *>(tok)->function(nullptr);
const_cast<Token *>(tok)->variable(nullptr);
const_cast<Token *>(tok)->enumerator(nullptr);
const_cast<Token *>(tok)->setValueType(nullptr);
for (Token* tok = mTokenizer.list.front(); tok; tok = tok->next()) {
tok->scope(nullptr);
tok->type(nullptr);
tok->function(nullptr);
tok->variable(nullptr);
tok->enumerator(nullptr);
tok->setValueType(nullptr);
}
}

Expand Down
14 changes: 14 additions & 0 deletions test/testsymboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5511,6 +5511,20 @@ class TestSymbolDatabase : public TestFixture {
const Token* s4 = Token::findsimplematch(s3->next(), "string [");
ASSERT(s4 && !s4->isIncompleteVar());
}
{
GET_SYMBOL_DB("void f() {\n"
" T** p;\n"
" T*** q;\n"
" T** const * r;\n"
"}\n");
ASSERT(db && errout.str().empty());
const Token* p = Token::findsimplematch(tokenizer.tokens(), "p");
ASSERT(p && !p->isIncompleteVar());
const Token* q = Token::findsimplematch(p, "q");
ASSERT(q && !q->isIncompleteVar());
const Token* r = Token::findsimplematch(q, "r");
ASSERT(r && !r->isIncompleteVar());
}
}

void enum1() {
Expand Down

0 comments on commit 63b76d2

Please sign in to comment.