Fixed #9596 (False positive: Returning an integer in a function with a pointer return type auto handling)

This commit is contained in:
Daniel Marjamäki 2020-04-04 20:03:48 +02:00
parent 048b08f5be
commit f01783238e
2 changed files with 37 additions and 15 deletions

View File

@ -5685,8 +5685,13 @@ static const Token * parsedecl(const Token *type, ValueType * const valuetype, V
valuetype->sign = vt->sign; valuetype->sign = vt->sign;
valuetype->constness = vt->constness; valuetype->constness = vt->constness;
valuetype->originalTypeName = vt->originalTypeName; valuetype->originalTypeName = vt->originalTypeName;
while (Token::Match(type, "%name%|*|&|::") && !type->variable()) while (Token::Match(type, "%name%|*|&|::") && !type->variable()) {
if (type->str() == "*")
valuetype->pointer++;
if (type->str() == "const")
valuetype->constness |= (1 << valuetype->pointer);
type = type->next(); type = type->next();
}
break; break;
} else if (!valuetype->typeScope && (type->str() == "struct" || type->str() == "enum")) } else if (!valuetype->typeScope && (type->str() == "struct" || type->str() == "enum"))
valuetype->type = type->str() == "struct" ? ValueType::Type::RECORD : ValueType::Type::NONSTD; valuetype->type = type->str() == "struct" ? ValueType::Type::RECORD : ValueType::Type::NONSTD;

View File

@ -418,6 +418,7 @@ private:
TEST_CASE(auto10); // #8020 TEST_CASE(auto10); // #8020
TEST_CASE(auto11); // #8964 - const auto startX = x; TEST_CASE(auto11); // #8964 - const auto startX = x;
TEST_CASE(auto12); // #8993 - const std::string &x; auto y = x; if (y.empty()) .. TEST_CASE(auto12); // #8993 - const std::string &x; auto y = x; if (y.empty()) ..
TEST_CASE(auto13);
TEST_CASE(unionWithConstructor); TEST_CASE(unionWithConstructor);
@ -7464,23 +7465,20 @@ private:
// auto = int, v11 = int // auto = int, v11 = int
autotok = Token::findsimplematch(autotok, "auto v11"); autotok = Token::findsimplematch(autotok, "auto v11");
ASSERT(autotok); ASSERT(autotok);
TODO_ASSERT(autotok->valueType()); ASSERT(autotok->valueType());
if (autotok->valueType()) {
ASSERT_EQUALS(0, autotok->valueType()->constness); ASSERT_EQUALS(0, autotok->valueType()->constness);
ASSERT_EQUALS(0, autotok->valueType()->pointer); ASSERT_EQUALS(0, autotok->valueType()->pointer);
ASSERT_EQUALS(ValueType::SIGNED, autotok->valueType()->sign); ASSERT_EQUALS(ValueType::SIGNED, autotok->valueType()->sign);
ASSERT_EQUALS(ValueType::INT, autotok->valueType()->type); ASSERT_EQUALS(ValueType::INT, autotok->valueType()->type);
}
vartok = autotok->next(); vartok = autotok->next();
ASSERT(autotok); ASSERT(autotok);
TODO_ASSERT(autotok->valueType()); ASSERT(autotok->valueType());
if (vartok->valueType()) {
ASSERT_EQUALS(0, vartok->valueType()->constness); ASSERT_EQUALS(0, vartok->valueType()->constness);
ASSERT_EQUALS(0, vartok->valueType()->pointer); ASSERT_EQUALS(0, vartok->valueType()->pointer);
ASSERT_EQUALS(ValueType::SIGNED, vartok->valueType()->sign); ASSERT_EQUALS(ValueType::SIGNED, vartok->valueType()->sign);
ASSERT_EQUALS(ValueType::INT, vartok->valueType()->type); ASSERT_EQUALS(ValueType::INT, vartok->valueType()->type);
} }
}
void auto8() { void auto8() {
GET_SYMBOL_DB("std::vector<int> vec;\n" GET_SYMBOL_DB("std::vector<int> vec;\n"
@ -7572,6 +7570,25 @@ private:
ASSERT(tok && tok->valueType() && tok->valueType()->container); ASSERT(tok && tok->valueType() && tok->valueType()->container);
} }
void auto13() {
GET_SYMBOL_DB("uint8_t *get();\n"
"\n"
"uint8_t *test()\n"
"{\n"
" auto *next = get();\n"
" return next;\n"
"}");
const Token *tok;
tok = Token::findsimplematch(tokenizer.tokens(), "return")->next();
ASSERT(tok);
ASSERT(tok->valueType());
ASSERT(tok->valueType()->pointer);
ASSERT(tok->variable()->valueType());
ASSERT(tok->variable()->valueType()->pointer);
}
void unionWithConstructor() { void unionWithConstructor() {
GET_SYMBOL_DB("union Fred {\n" GET_SYMBOL_DB("union Fred {\n"
" Fred(int x) : i(x) { }\n" " Fred(int x) : i(x) { }\n"