Skip to content

Commit ecb2938

Browse files
committed
SymbolDatabase: Set correct ValueType when there is array-to-pointer decay
1 parent d7a8f7f commit ecb2938

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

lib/symboldatabase.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6207,7 +6207,19 @@ void SymbolDatabase::setValueType(Token* tok, const ValueType& valuetype, Source
62076207
if (parent->str() == "&" && !parent->astOperand2()) {
62086208
ValueType vt(valuetype);
62096209
vt.reference = Reference::None; //Given int& x; the type of &x is int* not int&*
6210-
vt.pointer += 1U;
6210+
bool isArrayToPointerDecay = false;
6211+
for (const Token* child = parent->astOperand1(); child;) {
6212+
if (Token::Match(child, ".|::"))
6213+
child = child->astOperand2();
6214+
else if (Token::simpleMatch(child, "["))
6215+
child = child->astOperand1();
6216+
else {
6217+
isArrayToPointerDecay = child->variable() && child->variable()->isArray();
6218+
break;
6219+
}
6220+
}
6221+
if (!isArrayToPointerDecay)
6222+
vt.pointer += 1U;
62116223
setValueType(parent, vt);
62126224
return;
62136225
}

test/testsymboldatabase.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7601,6 +7601,7 @@ class TestSymbolDatabase : public TestFixture {
76017601
ASSERT_EQUALS("signed int", typeOf("int x[10]; a = x[0] + 1;", "+"));
76027602
ASSERT_EQUALS("", typeOf("a = x[\"hello\"];", "[", "test.cpp"));
76037603
ASSERT_EQUALS("const char", typeOf("a = x[\"hello\"];", "[", "test.c"));
7604+
ASSERT_EQUALS("signed int *", typeOf("int x[10]; a = &x;", "&"));
76047605

76057606
// cast..
76067607
ASSERT_EQUALS("void *", typeOf("a = (void *)0;", "("));

0 commit comments

Comments
 (0)