#6700 const vs non-const inside assert() statement. Function matching in symboldatabase did not honor const'ness of a class instance variable

This commit is contained in:
Alexander Mai 2015-05-23 11:56:11 +02:00
parent f0bc300198
commit e8d84bc6b4
3 changed files with 28 additions and 3 deletions

View File

@ -3101,7 +3101,7 @@ void Scope::findFunctionInBase(const std::string & name, size_t args, std::vecto
This can be difficult because of promotion and conversion operators and casts This can be difficult because of promotion and conversion operators and casts
and because the argument can also be a function call. and because the argument can also be a function call.
*/ */
const Function* Scope::findFunction(const Token *tok) const const Function* Scope::findFunction(const Token *tok, bool requireConst) const
{ {
// make sure this is a function call // make sure this is a function call
const Token *end = tok->linkAt(1); const Token *end = tok->linkAt(1);
@ -3232,6 +3232,9 @@ const Function* Scope::findFunction(const Token *tok) const
// check if all arguments matched // check if all arguments matched
if (same == args) { if (same == args) {
if (requireConst && func->isConst())
return func;
// get the function this call is in // get the function this call is in
const Scope * scope = tok->scope(); const Scope * scope = tok->scope();
@ -3320,7 +3323,7 @@ const Function* SymbolDatabase::findFunction(const Token *tok) const
if (Token::Match(tok1, "%var% .")) { if (Token::Match(tok1, "%var% .")) {
const Variable *var = getVariableFromVarId(tok1->varId()); const Variable *var = getVariableFromVarId(tok1->varId());
if (var && var->typeScope()) if (var && var->typeScope())
return var->typeScope()->findFunction(tok); return var->typeScope()->findFunction(tok, var->isConst());
} }
} }

View File

@ -842,9 +842,10 @@ public:
/** /**
* @brief find a function * @brief find a function
* @param tok token of function call * @param tok token of function call
* @param requireConst if const refers to a const variable only const methods should be matched
* @return pointer to function if found or NULL if not found * @return pointer to function if found or NULL if not found
*/ */
const Function *findFunction(const Token *tok) const; const Function *findFunction(const Token *tok, bool requireConst=false) const;
/** /**
* @brief find if name is in nested list * @brief find if name is in nested list

View File

@ -240,6 +240,7 @@ private:
TEST_CASE(findFunction4); TEST_CASE(findFunction4);
TEST_CASE(findFunction5); // #6230 TEST_CASE(findFunction5); // #6230
TEST_CASE(findFunction6); TEST_CASE(findFunction6);
TEST_CASE(findFunction7); // #6700
TEST_CASE(noexceptFunction1); TEST_CASE(noexceptFunction1);
TEST_CASE(noexceptFunction2); TEST_CASE(noexceptFunction2);
@ -2517,6 +2518,26 @@ private:
ASSERT_EQUALS(true, db && f && !f->function()); // regression value only ASSERT_EQUALS(true, db && f && !f->function()); // regression value only
} }
void findFunction7() {
GET_SYMBOL_DB("class ResultEnsemble {\n"
"public:\n"
" std::vector<int> &nodeResults() const;\n"
" std::vector<int> &nodeResults();\n"
"};\n"
"class Simulator {\n"
" int generatePinchResultEnsemble(const ResultEnsemble &power, const ResultEnsemble &ground) {\n"
" power.nodeResults().size();\n"
" assert(power.nodeResults().size()==ground.nodeResults().size());\n"
" }\n"
"};")
const Token *callfunc = Token::findsimplematch(tokenizer.tokens(), "power . nodeResults ( ) . size ( ) ;");
ASSERT_EQUALS("", errout.str());
ASSERT_EQUALS(true, db != nullptr); // not null
ASSERT_EQUALS(true, callfunc != nullptr); // not null
ASSERT_EQUALS(true, callfunc && callfunc->tokAt(2)->function() && callfunc->tokAt(2)->function()->tokenDef->linenr() == 3);
}
#define FUNC(x) const Function *x = findFunctionByName(#x, &db->scopeList.front()); \ #define FUNC(x) const Function *x = findFunctionByName(#x, &db->scopeList.front()); \
ASSERT_EQUALS(true, x != nullptr); \ ASSERT_EQUALS(true, x != nullptr); \