From 7e04b1cecb5eafaf425b7bf088b61ed77fba0ec7 Mon Sep 17 00:00:00 2001 From: Jordan Rome Date: Wed, 21 Feb 2024 09:29:38 -0500 Subject: [PATCH] Fix offsetof ast print (#3013) Print the field that we want the offsetof. Co-authored-by: Jordan Rome --- src/ast/passes/printer.cpp | 10 +++++++++- tests/parser.cpp | 10 +++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/ast/passes/printer.cpp b/src/ast/passes/printer.cpp index e894a749..b7e3f536 100644 --- a/src/ast/passes/printer.cpp +++ b/src/ast/passes/printer.cpp @@ -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_; } diff --git a/tests/parser.cpp b/tests/parser.cpp index 62481e65..8ab5ca15 100644 --- a/tests/parser.cpp +++ b/tests/parser.cpp @@ -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) @@ -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) @@ -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"); } }