Fixed #8668 (SymbolDatabase: Wrong findFunction match for const pointer argument)

This commit is contained in:
Daniel Marjamäki 2019-07-29 18:14:06 +02:00
parent cce061fe95
commit 3066c0653c
3 changed files with 41 additions and 0 deletions

View File

@ -4041,6 +4041,12 @@ void Scope::findFunctionInBase(const std::string & name, nonneg int args, std::v
static void checkVariableCallMatch(const Variable* callarg, const Variable* funcarg, size_t& same, size_t& fallback1, size_t& fallback2)
{
if (callarg) {
ValueType::MatchResult res = ValueType::matchParameter(callarg->valueType(), funcarg->valueType());
if (res == ValueType::MatchResult::SAME) {
same++;
return;
}
bool ptrequals = callarg->isArrayOrPointer() == funcarg->isArrayOrPointer();
bool constEquals = !callarg->isArrayOrPointer() || ((callarg->typeStartToken()->strAt(-1) == "const") == (funcarg->typeStartToken()->strAt(-1) == "const"));
if (ptrequals && constEquals &&
@ -5892,3 +5898,21 @@ std::string ValueType::str() const
}
return ret.empty() ? ret : ret.substr(1);
}
ValueType::MatchResult ValueType::matchParameter(const ValueType *call, const ValueType *func)
{
if (!call || !func)
return ValueType::MatchResult::UNKNOWN;
if (call->pointer == 0 || call->pointer != func->pointer)
return ValueType::MatchResult::UNKNOWN; // TODO
if ((call->constness | func->constness) != func->constness)
return ValueType::MatchResult::UNKNOWN;
if (func->sign != func->sign)
return ValueType::MatchResult::UNKNOWN; // TODO
if (call->type != func->type)
return ValueType::MatchResult::UNKNOWN; // TODO
if (func->type < ValueType::Type::VOID)
return ValueType::MatchResult::UNKNOWN;
return ValueType::MatchResult::SAME;
}

View File

@ -1137,6 +1137,9 @@ public:
static Type typeFromString(const std::string &typestr, bool longType);
enum class MatchResult { UNKNOWN, SAME, FALLBACK };
static MatchResult matchParameter(const ValueType *call, const ValueType *func);
bool isIntegral() const {
return (type >= ValueType::Type::BOOL && type <= ValueType::Type::UNKNOWN_INT);
}

View File

@ -342,6 +342,7 @@ private:
TEST_CASE(findFunction23);
TEST_CASE(findFunction24); // smart pointer
TEST_CASE(findFunction25); // std::vector<std::shared_ptr<Fred>>
TEST_CASE(findFunction26); // #8668 - pointer parameter in function call, const pointer function argument
TEST_CASE(noexceptFunction1);
TEST_CASE(noexceptFunction2);
@ -5515,6 +5516,19 @@ private:
ASSERT(tok1->function());
}
void findFunction26() {
GET_SYMBOL_DB("void dostuff(const int *p) {}\n"
"void dostuff(float) {}\n"
"void f(int *p) {\n"
" dostuff(p);\n"
"}");
ASSERT(db != nullptr);
const Token *dostuff1 = Token::findsimplematch(tokenizer.tokens(), "dostuff ( p ) ;");
ASSERT(dostuff1->function());
ASSERT(dostuff1->function() && dostuff1->function()->token);
ASSERT(dostuff1->function() && dostuff1->function()->token && dostuff1->function()->token->linenr() == 1);
}
#define FUNC(x) const Function *x = findFunctionByName(#x, &db->scopeList.front()); \
ASSERT_EQUALS(true, x != nullptr); \
if (x) ASSERT_EQUALS(true, x->isNoExcept());