Skip to content

Commit a3c0c6a

Browse files
Fix #12168 ValueType: wrong constness for expression 'array[0].port' (#7240)
1 parent 841baa3 commit a3c0c6a

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

lib/symboldatabase.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6580,10 +6580,18 @@ void SymbolDatabase::setValueType(Token* tok, const Variable& var, const SourceL
65806580
if (parsedecl(var.typeStartToken(), &valuetype, mDefaultSignedness, mSettings)) {
65816581
if (tok->str() == "." && tok->astOperand1()) {
65826582
const ValueType * const vt = tok->astOperand1()->valueType();
6583-
if (vt && (vt->constness & 1) != 0)
6584-
valuetype.constness |= 1;
6585-
if (vt && (vt->volatileness & 1) != 0)
6586-
valuetype.volatileness |= 1;
6583+
if (vt && (vt->constness & 1) != 0) {
6584+
if (var.isArray()) // constness propagates to arrays, but not to regular pointers
6585+
valuetype.constness |= 1;
6586+
else
6587+
valuetype.constness |= (1 << valuetype.pointer);
6588+
}
6589+
if (vt && (vt->volatileness & 1) != 0) {
6590+
if (var.isArray())
6591+
valuetype.volatileness |= 1;
6592+
else
6593+
valuetype.volatileness |= (1 << valuetype.pointer);
6594+
}
65876595
}
65886596
setValueType(tok, valuetype);
65896597
}

test/testsymboldatabase.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,7 @@ class TestSymbolDatabase : public TestFixture {
601601
TEST_CASE(auto20);
602602
TEST_CASE(auto21);
603603
TEST_CASE(auto22);
604+
TEST_CASE(auto23);
604605

605606
TEST_CASE(unionWithConstructor);
606607

@@ -10757,6 +10758,24 @@ class TestSymbolDatabase : public TestFixture {
1075710758
ASSERT(s->variable() && s->variable()->isReference());
1075810759
}
1075910760

10761+
void auto23() {
10762+
GET_SYMBOL_DB("struct S { int* p; };\n" // #12168
10763+
"void f(const S& s) {\n"
10764+
" auto q = s.p;\n"
10765+
"}\n");
10766+
ASSERT_EQUALS("", errout_str());
10767+
const Token* a = Token::findsimplematch(tokenizer.tokens(), "auto");
10768+
ASSERT(a && a->valueType());
10769+
ASSERT_EQUALS(a->valueType()->type, ValueType::INT);
10770+
ASSERT_EQUALS(a->valueType()->pointer, 1);
10771+
ASSERT_EQUALS(a->valueType()->constness, 0);
10772+
const Token* dot = Token::findsimplematch(a, ".");
10773+
ASSERT(dot && dot->valueType());
10774+
ASSERT_EQUALS(dot->valueType()->type, ValueType::INT);
10775+
ASSERT_EQUALS(dot->valueType()->pointer, 1);
10776+
ASSERT_EQUALS(dot->valueType()->constness, 2);
10777+
}
10778+
1076010779
void unionWithConstructor() {
1076110780
GET_SYMBOL_DB("union Fred {\n"
1076210781
" Fred(int x) : i(x) { }\n"

0 commit comments

Comments
 (0)