diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 6a73ce92e83..f851b2bd794 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -1139,7 +1139,7 @@ static size_t bitCeil(size_t x) static size_t getAlignOf(const ValueType& vt, const Settings& settings) { - if (vt.pointer || vt.isPrimitive()) { + if (vt.pointer || vt.reference != Reference::None || vt.isPrimitive()) { auto align = ValueFlow::getSizeOf(vt, settings); return align == 0 ? 0 : bitCeil(align); } @@ -1161,7 +1161,7 @@ static nonneg int getSizeOfType(const Token *typeTok, const Settings &settings) size_t ValueFlow::getSizeOf(const ValueType &vt, const Settings &settings) { - if (vt.pointer) + if (vt.pointer || vt.reference != Reference::None) return settings.platform.sizeof_pointer; if (vt.type == ValueType::Type::BOOL || vt.type == ValueType::Type::CHAR) return 1; diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index e160a7fb66a..2c4b5f8eff7 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -1448,6 +1448,16 @@ class TestValueFlow : public TestFixture { values = tokenValues(code, "( X )"); ASSERT_EQUALS(1U, values.size()); ASSERT_EQUALS(12, values.back().intvalue); + + code = "struct T;\n" + "struct S { T& r; };\n" + "struct T { S s{ *this }; };\n" + "void f() {\n" + " sizeof(T) == sizeof(void*);\n" + "}\n"; + values = tokenValues(code, "=="); + ASSERT_EQUALS(1U, values.size()); + ASSERT_EQUALS(1LL, values.back().intvalue); } void valueFlowComma()