SymbolDatabase: Set correct ValueType when there is array-to-pointer decay

This commit is contained in:
Daniel Marjamäki 2022-11-05 17:38:31 +01:00
parent d7a8f7f297
commit ecb2938e7e
2 changed files with 14 additions and 1 deletions

View File

@ -6207,6 +6207,18 @@ void SymbolDatabase::setValueType(Token* tok, const ValueType& valuetype, Source
if (parent->str() == "&" && !parent->astOperand2()) { if (parent->str() == "&" && !parent->astOperand2()) {
ValueType vt(valuetype); ValueType vt(valuetype);
vt.reference = Reference::None; //Given int& x; the type of &x is int* not int&* vt.reference = Reference::None; //Given int& x; the type of &x is int* not int&*
bool isArrayToPointerDecay = false;
for (const Token* child = parent->astOperand1(); child;) {
if (Token::Match(child, ".|::"))
child = child->astOperand2();
else if (Token::simpleMatch(child, "["))
child = child->astOperand1();
else {
isArrayToPointerDecay = child->variable() && child->variable()->isArray();
break;
}
}
if (!isArrayToPointerDecay)
vt.pointer += 1U; vt.pointer += 1U;
setValueType(parent, vt); setValueType(parent, vt);
return; return;

View File

@ -7601,6 +7601,7 @@ private:
ASSERT_EQUALS("signed int", typeOf("int x[10]; a = x[0] + 1;", "+")); ASSERT_EQUALS("signed int", typeOf("int x[10]; a = x[0] + 1;", "+"));
ASSERT_EQUALS("", typeOf("a = x[\"hello\"];", "[", "test.cpp")); ASSERT_EQUALS("", typeOf("a = x[\"hello\"];", "[", "test.cpp"));
ASSERT_EQUALS("const char", typeOf("a = x[\"hello\"];", "[", "test.c")); ASSERT_EQUALS("const char", typeOf("a = x[\"hello\"];", "[", "test.c"));
ASSERT_EQUALS("signed int *", typeOf("int x[10]; a = &x;", "&"));
// cast.. // cast..
ASSERT_EQUALS("void *", typeOf("a = (void *)0;", "(")); ASSERT_EQUALS("void *", typeOf("a = (void *)0;", "("));