Skip to content

Commit

Permalink
Union Assignment fix (danmar#6501)
Browse files Browse the repository at this point in the history
  • Loading branch information
olabetskyi committed Jun 10, 2024
1 parent 2924622 commit d33d29c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/fwdanalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ FwdAnalysis::Result FwdAnalysis::checkRecursive(const Token *expr, const Token *
if (parent->variable() && parent->variable()->type() && parent->variable()->type()->isUnionType() && parent->varId() == expr->varId()) {
while (parent && Token::simpleMatch(parent->astParent(), "."))
parent = parent->astParent();
if (parent && parent->valueType() && Token::Match(parent->astParent(), "%assign%") && !Token::Match(parent->astParent()->astParent(), "%assign%") && parent->astParent()->astOperand1() == parent) {
if (parent && parent->valueType() && Token::simpleMatch(parent->astParent(), "=") && !Token::Match(parent->astParent()->astParent(), "%assign%") && parent->astParent()->astOperand1() == parent) {
const Token * assignment = parent->astParent()->astOperand2();
while (Token::simpleMatch(assignment, ".") && assignment->varId() != expr->varId())
assignment = assignment->astOperand1();
Expand Down
14 changes: 12 additions & 2 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4535,8 +4535,7 @@ class TestOther : public TestFixture {
ASSERT_EQUALS("", errout_str());

// Ticket #10093 "redundantAssignment when using a union"
check("#include <stdio.h>\n"
"typedef union{\n"
check("typedef union{\n"
" char as_char[4];\n"
" int as_int;\n"
"} union_t;\n"
Expand All @@ -4552,6 +4551,17 @@ class TestOther : public TestFixture {
" u.as_int = 0;\n"
"}", true, false, false);
ASSERT_EQUALS("", errout_str());

// Ticket #5115 "redundantAssignment when using a union"
check("void foo(char *ptr) {\n"
" union {\n"
" char * s8;\n"
" unsigned long long u64;\n"
" } addr;\n"
" addr.s8 = ptr;\n"
" addr.u64 += 8;\n"
"}", true, false, false);
ASSERT_EQUALS("", errout_str());
}

void switchRedundantOperationTest() {
Expand Down

0 comments on commit d33d29c

Please sign in to comment.