Skip to content

Commit 79942df

Browse files
committed
Fixed cppcheck-opensource#5602 (false positive on std::vector - after unknown macro around the function header)
1 parent 547803f commit 79942df

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

‎lib/symboldatabase.cpp‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -640,11 +640,12 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti
640640
tok = funcStart;
641641

642642
// class function
643-
if (tok->previous()->str() == "::")
643+
if (tok->previous() && tok->previous()->str() == "::")
644644
addClassFunction(&scope, &tok, argStart);
645645

646646
// class destructor
647-
else if (tok->previous()->str() == "~" &&
647+
else if (tok->previous() &&
648+
tok->previous()->str() == "~" &&
648649
tok->strAt(-2) == "::")
649650
addClassFunction(&scope, &tok, argStart);
650651

@@ -1100,6 +1101,17 @@ bool SymbolDatabase::isFunction(const Token *tok, const Scope* outerScope, const
11001101
}
11011102
}
11021103

1104+
// UNKNOWN_MACRO(a,b) { ... }
1105+
else if (outerScope->type == Scope::eGlobal &&
1106+
Token::Match(tok, "%var% (") &&
1107+
tok->isUpperCaseName() &&
1108+
Token::Match(tok->linkAt(1), ") {") &&
1109+
(!tok->previous() || Token::Match(tok->previous(), "[;{}]"))) {
1110+
*funcStart = tok;
1111+
*argStart = tok->next();
1112+
return true;
1113+
}
1114+
11031115
// template constructor?
11041116
else if (Token::Match(tok, "%var% <") && Token::simpleMatch(tok->next()->link(), "> (")) {
11051117
const Token* tok2 = tok->next()->link()->next()->link();

‎test/testsymboldatabase.cpp‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ class TestSymbolDatabase: public TestFixture {
225225

226226
TEST_CASE(garbage);
227227

228+
TEST_CASE(isFunction); // UNKNOWN_MACRO(a,b) { .. }
229+
228230
TEST_CASE(findFunction1);
229231
TEST_CASE(findFunction2); // mismatch: parameter passed by address => reference argument
230232

@@ -1986,6 +1988,19 @@ class TestSymbolDatabase: public TestFixture {
19861988
}
19871989
}
19881990

1991+
void isFunction() { // #5602 - UNKNOWN_MACRO(a,b) { .. }
1992+
GET_SYMBOL_DB("TEST(a,b) {\n"
1993+
" std::vector<int> messages;\n"
1994+
" foo(messages[2].size());\n"
1995+
"}");
1996+
const Variable * const var = db ? db->getVariableFromVarId(1U) : nullptr;
1997+
ASSERT(db &&
1998+
db->findScopeByName("TEST") &&
1999+
var &&
2000+
var->typeStartToken() &&
2001+
var->typeStartToken()->str() == "std");
2002+
}
2003+
19892004
void findFunction1() {
19902005
GET_SYMBOL_DB("int foo(int x);\n" /* 1 */
19912006
"void foo();\n" /* 2 */

0 commit comments

Comments
 (0)