Skip to content

Commit 7f22ef4

Browse files
Set ValueType for auto with ternary (#5304)
1 parent 827e87a commit 7f22ef4

3 files changed

Lines changed: 29 additions & 8 deletions

File tree

addons/test/misra/misra-test.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -645,11 +645,11 @@ static void misra_10_1_ternary(void)
645645

646646
a = ui16 << ui16; // 10.6
647647
a = ui16 << (get_bool(42) ? ui16 : ui16);
648-
a = ui16 << (get_bool(42) ? ui16 : (get_bool(34) ? ui16 : ui16)); // 10.4
649-
a = ui16 << (get_bool(42) ? (get_bool(34) ? ui16 : ui16) : ui16); // 10.4
650-
a = ui16 << (get_bool(42) ? i16 : (get_bool(34) ? ui16 : ui16)); // 10.1
648+
a = ui16 << (get_bool(42) ? ui16 : (get_bool(34) ? ui16 : ui16));
649+
a = ui16 << (get_bool(42) ? (get_bool(34) ? ui16 : ui16) : ui16);
650+
a = ui16 << (get_bool(42) ? i16 : (get_bool(34) ? ui16 : ui16)); // 10.1 10.4
651651
a = ui16 << (get_bool(42) ? (get_bool(34) ? ui16 : i16) : ui16); // 10.1 10.4
652-
a = ui16 << (get_bool(42) ? (get_bool(34) ? ui16 : ui16) : i16); // 10.1
652+
a = ui16 << (get_bool(42) ? (get_bool(34) ? ui16 : ui16) : i16); // 10.1 10.4
653653
a = ui16 << (get_bool(42) ? (get_bool(34) ? ui16 : ui8) : ui8); // 10.4
654654
a = ui16 << (get_bool(42) ? (get_bool(34) ? i16 : ui8) : ui8); // 10.1 10.4
655655
a = (get_bool(42) ? (get_bool(34) ? ui16 : ui8) : ui8) << ui16; // 10.4
@@ -1171,7 +1171,7 @@ static void misra_14_2_fn1(bool b) {
11711171
g += 2;
11721172
i2 ^= 2; // 14.2
11731173
if (i2 == 2) {
1174-
g += g_arr[i2];
1174+
g += g_arr[i2]; // cppcheck-suppress legacyUninitvar
11751175
}
11761176
misra_14_2_init_value(&i2); // TODO: Fix false negative in function call
11771177
}

lib/symboldatabase.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6552,6 +6552,11 @@ void SymbolDatabase::setValueType(Token* tok, const ValueType& valuetype, Source
65526552
setValueType(parent, *vt1);
65536553
return;
65546554
}
6555+
6556+
if (vt1->isTypeEqual(vt2)) {
6557+
setValueType(parent, *vt1);
6558+
return;
6559+
}
65556560
}
65566561

65576562
if (vt1->pointer != 0U) {

test/testsymboldatabase.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ class TestSymbolDatabase : public TestFixture {
510510
TEST_CASE(auto18);
511511
TEST_CASE(auto19);
512512
TEST_CASE(auto20);
513+
TEST_CASE(auto21);
513514

514515
TEST_CASE(unionWithConstructor);
515516

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

57235723
void sizeOfType() {
@@ -9399,7 +9399,6 @@ class TestSymbolDatabase : public TestFixture {
93999399
ASSERT(autoTok && autoTok->valueType());
94009400
ASSERT_EQUALS(autoTok->valueType()->constness, 3);
94019401
ASSERT_EQUALS(autoTok->valueType()->pointer, 1);
9402-
TODO_ASSERT(autoTok->valueType()->reference == Reference::LValue);
94039402
}
94049403

94059404
void auto19() { // #11517
@@ -9454,6 +9453,23 @@ class TestSymbolDatabase : public TestFixture {
94549453
ASSERT_EQUALS(g->function()->tokenDef->linenr(), 4);
94559454
}
94569455

9456+
void auto21() {
9457+
GET_SYMBOL_DB("int f(bool b) {\n"
9458+
" std::vector<int> v1(1), v2(2);\n"
9459+
" auto& v = b ? v1 : v2;\n"
9460+
" v.push_back(1);\n"
9461+
" return v.size();\n"
9462+
"}\n");
9463+
ASSERT_EQUALS("", errout.str());
9464+
const Token* a = Token::findsimplematch(tokenizer.tokens(), "auto");
9465+
ASSERT(a && a->valueType());
9466+
ASSERT_EQUALS(a->valueType()->type, ValueType::CONTAINER);
9467+
const Token* v = Token::findsimplematch(a, "v . size");
9468+
ASSERT(v && v->valueType());
9469+
ASSERT_EQUALS(v->valueType()->type, ValueType::CONTAINER);
9470+
ASSERT(v->variable() && v->variable()->isReference());
9471+
}
9472+
94579473
void unionWithConstructor() {
94589474
GET_SYMBOL_DB("union Fred {\n"
94599475
" Fred(int x) : i(x) { }\n"

0 commit comments

Comments
 (0)