From ed9153ee800329f9b211b084416bb760a64870cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 19 Feb 2014 06:35:51 +0100 Subject: [PATCH] Symbol database: added simple mismatch check in Scope::findFunction when passing address to function that expects a reference --- lib/symboldatabase.cpp | 11 +++++++++++ test/testsymboldatabase.cpp | 15 +++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 5fa9481d7..bb08ac289 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -2506,6 +2506,17 @@ const Function* Scope::findFunction(const Token *tok) const const Token *arg = tok->tokAt(2); while (arg && arg->str() != ")") { /** @todo check argument type for match */ + + // mismatch parameter: passing parameter by address to function, argument is reference + if (arg->str() == "&") { + // check that function argument type is not mismatching + const Variable *funcarg = func->getArgumentVar(args); + if (funcarg && funcarg->isReference()) { + args = ~0U; + break; + } + } + args++; arg = arg->nextArgument(); } diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index c75e1e731..37420412a 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -212,6 +212,7 @@ private: TEST_CASE(garbage); TEST_CASE(findFunction1); + TEST_CASE(findFunction2); // mismatch: parameter passed by address => reference argument } void array() const { @@ -1857,6 +1858,20 @@ private: } } } + + void findFunction2() { + // The function does not match the function call. + GET_SYMBOL_DB("void func(const int x, const Fred &fred);\n" + "void otherfunc() {\n" + " float t;\n" + " func(x, &t);\n" + "}"); + const Token *callfunc = Token::findmatch(tokenizer.tokens(), "func ( x , & t ) ;"); + ASSERT_EQUALS("", errout.str()); + ASSERT_EQUALS(true, db != nullptr); // not null + ASSERT_EQUALS(true, callfunc != nullptr); // not null + ASSERT_EQUALS(false, (callfunc && callfunc->function())); // callfunc->function() should be null + } }; REGISTER_TEST(TestSymbolDatabase)