Set ValueType for auto with ternary (#5304)

This commit is contained in:
chrchr-github 2023-08-18 10:33:26 +02:00 committed by GitHub
parent 827e87afe7
commit 7f22ef4e14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 8 deletions

View File

@ -645,11 +645,11 @@ static void misra_10_1_ternary(void)
a = ui16 << ui16; // 10.6
a = ui16 << (get_bool(42) ? ui16 : ui16);
a = ui16 << (get_bool(42) ? ui16 : (get_bool(34) ? ui16 : ui16)); // 10.4
a = ui16 << (get_bool(42) ? (get_bool(34) ? ui16 : ui16) : ui16); // 10.4
a = ui16 << (get_bool(42) ? i16 : (get_bool(34) ? ui16 : ui16)); // 10.1
a = ui16 << (get_bool(42) ? ui16 : (get_bool(34) ? ui16 : ui16));
a = ui16 << (get_bool(42) ? (get_bool(34) ? ui16 : ui16) : ui16);
a = ui16 << (get_bool(42) ? i16 : (get_bool(34) ? ui16 : ui16)); // 10.1 10.4
a = ui16 << (get_bool(42) ? (get_bool(34) ? ui16 : i16) : ui16); // 10.1 10.4
a = ui16 << (get_bool(42) ? (get_bool(34) ? ui16 : ui16) : i16); // 10.1
a = ui16 << (get_bool(42) ? (get_bool(34) ? ui16 : ui16) : i16); // 10.1 10.4
a = ui16 << (get_bool(42) ? (get_bool(34) ? ui16 : ui8) : ui8); // 10.4
a = ui16 << (get_bool(42) ? (get_bool(34) ? i16 : ui8) : ui8); // 10.1 10.4
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) {
g += 2;
i2 ^= 2; // 14.2
if (i2 == 2) {
g += g_arr[i2];
g += g_arr[i2]; // cppcheck-suppress legacyUninitvar
}
misra_14_2_init_value(&i2); // TODO: Fix false negative in function call
}

View File

@ -6552,6 +6552,11 @@ void SymbolDatabase::setValueType(Token* tok, const ValueType& valuetype, Source
setValueType(parent, *vt1);
return;
}
if (vt1->isTypeEqual(vt2)) {
setValueType(parent, *vt1);
return;
}
}
if (vt1->pointer != 0U) {

View File

@ -510,6 +510,7 @@ private:
TEST_CASE(auto18);
TEST_CASE(auto19);
TEST_CASE(auto20);
TEST_CASE(auto21);
TEST_CASE(unionWithConstructor);
@ -5716,8 +5717,7 @@ private:
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() {
@ -9399,7 +9399,6 @@ private:
ASSERT(autoTok && autoTok->valueType());
ASSERT_EQUALS(autoTok->valueType()->constness, 3);
ASSERT_EQUALS(autoTok->valueType()->pointer, 1);
TODO_ASSERT(autoTok->valueType()->reference == Reference::LValue);
}
void auto19() { // #11517
@ -9454,6 +9453,23 @@ private:
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"
" 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);
ASSERT(v->variable() && v->variable()->isReference());
}
void unionWithConstructor() {
GET_SYMBOL_DB("union Fred {\n"
" Fred(int x) : i(x) { }\n"