Fix #12138 FP passedByValue with anonymous union (#5611)

This commit is contained in:
chrchr-github 2023-11-04 13:34:08 +01:00 committed by GitHub
parent df860a937d
commit 789c032e42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 2 deletions

View File

@ -1200,16 +1200,22 @@ static int estimateSize(const Type* type, const Settings* settings, const Symbol
else
cumulatedSize += size;
};
for (const Variable&var : type->classScope->varlist) {
std::set<const Scope*> anonScopes;
for (const Variable& var : type->classScope->varlist) {
int size = 0;
if (var.isStatic())
continue;
if (var.isPointer() || var.isReference())
size = settings->platform.sizeof_pointer;
else if (var.type() && var.type()->classScope)
size = estimateSize(var.type(), settings, symbolDatabase, recursionDepth+1);
size = estimateSize(var.type(), settings, symbolDatabase, recursionDepth + 1);
else if (var.valueType() && var.valueType()->type == ValueType::Type::CONTAINER)
size = 3 * settings->platform.sizeof_pointer; // Just guess
else if (var.nameToken()->scope() != type->classScope && var.nameToken()->scope()->definedType) { // anonymous union
const auto ret = anonScopes.insert(var.nameToken()->scope());
if (ret.second)
size = estimateSize(var.nameToken()->scope()->definedType, settings, symbolDatabase, recursionDepth + 1);
}
else
size = symbolDatabase->sizeOfType(var.typeStartToken());

View File

@ -2172,6 +2172,25 @@ private:
"}\n");
ASSERT_EQUALS("[test.cpp:1]: (performance) Function parameter 't' should be passed by const reference.\n", errout.str());
check("struct S {\n" // #12138
" union {\n"
" int a = 0;\n"
" int x;\n"
" };\n"
" union {\n"
" int b = 0;\n"
" int y;\n"
" };\n"
" union {\n"
" int c = 0;\n"
" int z;\n"
" };\n"
"};\n"
"void f(S s) {\n"
" if (s.x > s.y) {}\n"
"}\n");
ASSERT_EQUALS("", errout.str());
Settings settings1 = settingsBuilder().platform(Platform::Type::Win64).build();
check("using ui64 = unsigned __int64;\n"
"ui64 Test(ui64 one, ui64 two) { return one + two; }\n",