Skip to content

Commit

Permalink
Fix ValueType for auto&
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Aug 21, 2023
1 parent 869af84 commit 5b7b8ad
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6789,11 +6789,15 @@ static const Token* parsedecl(const Token* type,
valuetype->constness = vt->constness;
valuetype->originalTypeName = vt->originalTypeName;
const bool hasConst = Token::simpleMatch(type->previous(), "const");
while (Token::Match(type, "%name%|*|&|::") && !type->variable()) {
while (Token::Match(type, "%name%|*|&|&&|::") && !type->variable()) {
if (type->str() == "*") {
valuetype->pointer = 1;
if (hasConst)
valuetype->constness = 1;
} else if (type->str() == "&") {
valuetype->reference = Reference::LValue;
} else if (type->str() == "&&") {
valuetype->reference = Reference::RValue;
}
if (type->str() == "const")
valuetype->constness |= (1 << valuetype->pointer);
Expand Down
19 changes: 18 additions & 1 deletion test/testsymboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ class TestSymbolDatabase : public TestFixture {
TEST_CASE(auto19);
TEST_CASE(auto20);
TEST_CASE(auto21);
TEST_CASE(auto22);

TEST_CASE(unionWithConstructor);

Expand Down Expand Up @@ -8412,7 +8413,7 @@ class TestSymbolDatabase : public TestFixture {
const Token* tok = tokenizer.tokens();
tok = Token::findsimplematch(tok, "s .");
ASSERT(tok && tok->valueType());
ASSERT_EQUALS("container(std :: set|unordered_set <)", tok->valueType()->str());
ASSERT_EQUALS("container(std :: set|unordered_set <) &", tok->valueType()->str());
}
{
GET_SYMBOL_DB("void f(std::vector<int> v) {\n"
Expand Down Expand Up @@ -9487,6 +9488,22 @@ class TestSymbolDatabase : public TestFixture {
ASSERT(v->variable() && v->variable()->isReference());
}

void auto22() {
GET_SYMBOL_DB("void f(std::vector<std::string>& v, bool b) {\n"
" auto& s = b ? v[0] : v[1];\n"
" s += \"abc\";\n"
"}\n");
ASSERT_EQUALS("", errout.str());
const Token* a = Token::findsimplematch(tokenizer.tokens(), "auto");
ASSERT(a && a->valueType());
ASSERT_EQUALS(a->valueType()->type, ValueType::CONTAINER);
const Token* s = Token::findsimplematch(a, "s +=");
ASSERT(s && s->valueType());
ASSERT_EQUALS(s->valueType()->type, ValueType::CONTAINER);
ASSERT(s->valueType()->reference == Reference::LValue);
ASSERT(s->variable() && s->variable()->isReference());
}

void unionWithConstructor() {
GET_SYMBOL_DB("union Fred {\n"
" Fred(int x) : i(x) { }\n"
Expand Down

0 comments on commit 5b7b8ad

Please sign in to comment.