Skip to content

Commit 41d1974

Browse files
committed
Printer: Standardise printing of Subprog nodes
Print them in a tree format, as is done for other nodes, instead of approximating the BpfScript syntax. This makes displaying the types of the subprog and its arguments consistent with other nodes. Before: Program foo: uint64($a : uint8) return int: 123 After: Program subprog: foo :: [uint64] args $a :: [uint8] return int: 123 Signed-off-by: Alastair Robertson <ajor@meta.com>
1 parent 0d64219 commit 41d1974

File tree

3 files changed

+49
-25
lines changed

3 files changed

+49
-25
lines changed

src/ast/passes/printer.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -477,22 +477,32 @@ void Printer::visit(Probe &probe)
477477
--depth_;
478478
}
479479

480+
void Printer::visit(SubprogArg &arg)
481+
{
482+
std::string indent(depth_, ' ');
483+
484+
++depth_;
485+
out_ << indent << arg.name << type(arg.type) << std::endl;
486+
--depth_;
487+
}
488+
480489
void Printer::visit(Subprog &subprog)
481490
{
482491
std::string indent(depth_, ' ');
483-
out_ << indent << subprog.name << ": " << subprog.return_type;
492+
out_ << indent << "subprog: " << subprog.name << type(subprog.return_type)
493+
<< std::endl;
494+
495+
++depth_;
484496

485-
out_ << "(";
486-
for (size_t i = 0; i < subprog.args.size(); i++) {
487-
auto &arg = subprog.args.at(i);
488-
out_ << arg->name << " : " << arg->type;
489-
if (i < subprog.args.size() - 1)
490-
out_ << ", ";
497+
if (!subprog.args.empty()) {
498+
++depth_;
499+
out_ << indent << " args" << std::endl;
500+
visit(subprog.args);
501+
--depth_;
491502
}
492-
out_ << ")" << std::endl;
493503

494-
++depth_;
495504
visit(subprog.stmts);
505+
496506
--depth_;
497507
}
498508

src/ast/passes/printer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class Printer : public Visitor<Printer> {
5151
void visit(Predicate &pred);
5252
void visit(AttachPoint &ap);
5353
void visit(Probe &probe);
54+
void visit(SubprogArg &arg);
5455
void visit(Subprog &subprog);
5556
void visit(Import &imp);
5657
void visit(Program &program);

tests/parser.cpp

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2774,8 +2774,8 @@ TEST(Parser, subprog_probe_mixed)
27742774
{
27752775
test("i:s:1 {} fn f1(): void {} i:s:1 {} fn f2(): void {}",
27762776
"Program\n"
2777-
" f1: void()\n"
2778-
" f2: void()\n"
2777+
" subprog: f1 :: [void]\n"
2778+
" subprog: f2 :: [void]\n"
27792779
" interval:s:1\n"
27802780
" interval:s:1\n");
27812781
}
@@ -2784,7 +2784,7 @@ TEST(Parser, subprog_void_no_args)
27842784
{
27852785
test("fn f(): void {}",
27862786
"Program\n"
2787-
" f: void()\n");
2787+
" subprog: f :: [void]\n");
27882788
}
27892789

27902790
TEST(Parser, subprog_invalid_return_type)
@@ -2801,42 +2801,55 @@ TEST(Parser, subprog_one_arg)
28012801
{
28022802
test("fn f($a : uint8): void {}",
28032803
"Program\n"
2804-
" f: void($a : uint8)\n");
2804+
" subprog: f :: [void]\n"
2805+
" args\n"
2806+
" $a :: [uint8]\n");
28052807
}
28062808

28072809
TEST(Parser, subprog_two_args)
28082810
{
28092811
test("fn f($a : uint8, $b : uint8): void {}",
28102812
"Program\n"
2811-
" f: void($a : uint8, $b : uint8)\n");
2813+
" subprog: f :: [void]\n"
2814+
" args\n"
2815+
" $a :: [uint8]\n"
2816+
" $b :: [uint8]\n");
28122817
}
28132818

28142819
TEST(Parser, subprog_string_arg)
28152820
{
2816-
test("fn f($a : string[16]): void {}",
2821+
test("fn f($a : string): void {}",
28172822
"Program\n"
2818-
" f: void($a : string[16])\n");
2823+
" subprog: f :: [void]\n"
2824+
" args\n"
2825+
" $a :: [string[0]]\n");
28192826
}
28202827

28212828
TEST(Parser, subprog_struct_arg)
28222829
{
28232830
test("fn f($a: struct x): void {}",
28242831
"Program\n"
2825-
" f: void($a : struct x)\n");
2832+
" subprog: f :: [void]\n"
2833+
" args\n"
2834+
" $a :: [struct x]\n");
28262835
}
28272836

28282837
TEST(Parser, subprog_union_arg)
28292838
{
28302839
test("fn f($a : union x): void {}",
28312840
"Program\n"
2832-
" f: void($a : union x)\n");
2841+
" subprog: f :: [void]\n"
2842+
" args\n"
2843+
" $a :: [union x]\n");
28332844
}
28342845

28352846
TEST(Parser, subprog_enum_arg)
28362847
{
28372848
test("fn f($a : enum x): void {}",
28382849
"Program\n"
2839-
" f: void($a : enum x)\n");
2850+
" subprog: f :: [void]\n"
2851+
" args\n"
2852+
" $a :: [enum x]\n");
28402853
}
28412854

28422855
TEST(Parser, subprog_invalid_arg)
@@ -2853,7 +2866,7 @@ TEST(Parser, subprog_return)
28532866
{
28542867
test("fn f(): void { return 1 + 1; }",
28552868
"Program\n"
2856-
" f: void()\n"
2869+
" subprog: f :: [void]\n"
28572870
" return\n"
28582871
" +\n"
28592872
" int: 1\n"
@@ -2862,30 +2875,30 @@ TEST(Parser, subprog_return)
28622875

28632876
TEST(Parser, subprog_string)
28642877
{
2865-
test("fn f(): string[16] {}",
2878+
test("fn f(): string {}",
28662879
"Program\n"
2867-
" f: string[16]()\n");
2880+
" subprog: f :: [string[0]]\n");
28682881
}
28692882

28702883
TEST(Parser, subprog_struct)
28712884
{
28722885
test("fn f(): struct x {}",
28732886
"Program\n"
2874-
" f: struct x()\n");
2887+
" subprog: f :: [struct x]\n");
28752888
}
28762889

28772890
TEST(Parser, subprog_union)
28782891
{
28792892
test("fn f(): union x {}",
28802893
"Program\n"
2881-
" f: union x()\n");
2894+
" subprog: f :: [union x]\n");
28822895
}
28832896

28842897
TEST(Parser, subprog_enum)
28852898
{
28862899
test("fn f(): enum x {}",
28872900
"Program\n"
2888-
" f: enum x()\n");
2901+
" subprog: f :: [enum x]\n");
28892902
}
28902903

28912904
TEST(Parser, for_loop)

0 commit comments

Comments
 (0)