Skip to content

Commit

Permalink
Fix offsetof ast print (#3013)
Browse files Browse the repository at this point in the history
Print the field that we want the offsetof.

Co-authored-by: Jordan Rome <jordalgo@fedoraproject.org>
  • Loading branch information
jordalgo and Jordan Rome authored Feb 21, 2024
1 parent bb35157 commit 7e04b1c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
10 changes: 9 additions & 1 deletion src/ast/passes/printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,16 @@ void Printer::visit(Offsetof &ofof)
out_ << indent << "offsetof: " << type(ofof.type) << std::endl;

++depth_;
if (ofof.expr)
std::string indentParam(depth_, ' ');

// Print the args
if (ofof.expr) {
ofof.expr->accept(*this);
} else {
out_ << indentParam << ofof.record << std::endl;
}

out_ << indentParam << ofof.field << std::endl;
--depth_;
}

Expand Down
10 changes: 7 additions & 3 deletions tests/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1745,7 +1745,9 @@ TEST(Parser, offsetof_type)
"\n"
"Program\n"
" BEGIN\n"
" offsetof: \n");
" offsetof: \n"
" struct Foo\n"
" x\n");
}

TEST(Parser, offsetof_expression)
Expand All @@ -1762,7 +1764,8 @@ TEST(Parser, offsetof_expression)
" int: 0\n"
" offsetof: \n"
" dereference\n"
" variable: $foo\n");
" variable: $foo\n"
" x\n");
}

TEST(Parser, dereference_precedence)
Expand Down Expand Up @@ -2159,7 +2162,8 @@ TEST(Parser, keywords_as_identifiers)
keyword + "\n");
test("BEGIN { $x = offsetof(*curtask, " + keyword + "); }",
"Program\n BEGIN\n =\n variable: $x\n offsetof: \n "
"dereference\n builtin: curtask\n");
"dereference\n builtin: curtask\n " +
keyword + "\n");
}
}

Expand Down

0 comments on commit 7e04b1c

Please sign in to comment.