Skip to content

Commit

Permalink
Set ValueType for auto with ternary
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Aug 8, 2023
1 parent f1749ab commit 6f7aa0d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
7 changes: 7 additions & 0 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6552,6 +6552,13 @@ void SymbolDatabase::setValueType(Token* tok, const ValueType& valuetype, Source
setValueType(parent, *vt1);
return;
}

if (vt1->pointer == 0U && vt2 && vt2->pointer == 0U) {
if (vt1->isTypeEqual(vt2)) {
setValueType(parent, *vt1);
return;
}
}
}

if (vt1->pointer != 0U) {
Expand Down
23 changes: 21 additions & 2 deletions test/testsymboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ class TestSymbolDatabase : public TestFixture {
TEST_CASE(auto18);
TEST_CASE(auto19);
TEST_CASE(auto20);
TEST_CASE(auto21);

TEST_CASE(unionWithConstructor);

Expand Down Expand Up @@ -5716,8 +5717,7 @@ class TestSymbolDatabase : public TestFixture {
ASSERT_EQUALS(E1->value, 1);
const Token* const a = Token::findsimplematch(tokenizer.tokens(), "auto");
ASSERT(a && a->valueType());
TODO_ASSERT(E1->scope == a->valueType()->typeScope);
ASSERT_EQUALS(a->valueType()->type, ValueType::INT);
ASSERT(E1->scope == a->valueType()->typeScope);
}

void sizeOfType() {
Expand Down Expand Up @@ -9454,6 +9454,25 @@ class TestSymbolDatabase : public TestFixture {
ASSERT_EQUALS(g->function()->tokenDef->linenr(), 4);
}

void auto21() {
GET_SYMBOL_DB("int f(bool b) {\n"
" std::vector<int> v1(1), v2(2);\n"
" auto& v = b ? v1 : v2;\n"
" v.push_back(1);\n"
" int& r = v[0];"
" return v.size();\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* v = Token::findsimplematch(a, "v . size");
ASSERT(v && v->valueType());
ASSERT_EQUALS(v->valueType()->type, ValueType::CONTAINER);
TODO_ASSERT(v->valueType()->reference == Reference::LValue);
ASSERT(v->variable() && v->variable()->isReference());
}

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

0 comments on commit 6f7aa0d

Please sign in to comment.