From ff5dda3683c02f22e7ccd7e71bdc517f181942aa Mon Sep 17 00:00:00 2001 From: Scott Graham <scottmg@google.com> Date: Tue, 16 Apr 2019 15:49:18 -0700 Subject: [PATCH] [abigen][banjo] Tests for attribute markup Confirm that the custom abigen attributes are handled as expected when outputting .abigen from banjo. (Also fixes unnecessary space in array spec that became apparent from looking at test output.) ZX-3882 #comment [abigen][banjo] Tests for attribute markup Test: new codegen test Change-Id: Id51fbaf0a4c8286af3ef8079dba2518a900430e9 --- tools/banjo/banjo/src/backends/abigen.rs | 22 +++++++------ .../abigen/abigen-protocol-markup.abigen.out | 30 ++++++++++++++++++ .../banjo/abigen-protocol-markup.test.banjo | 31 +++++++++++++++++++ tools/banjo/banjo/test/codegen_tests.rs | 8 +++++ 4 files changed, 82 insertions(+), 9 deletions(-) create mode 100644 tools/banjo/banjo/test/abigen/abigen-protocol-markup.abigen.out create mode 100644 tools/banjo/banjo/test/banjo/abigen-protocol-markup.test.banjo diff --git a/tools/banjo/banjo/src/backends/abigen.rs b/tools/banjo/banjo/src/backends/abigen.rs index e9577f4f14b..80a0fe0569f 100644 --- a/tools/banjo/banjo/src/backends/abigen.rs +++ b/tools/banjo/banjo/src/backends/abigen.rs @@ -52,16 +52,20 @@ impl ValuedAttributes { ValuedAttributes(result) } - // Returns the value for a given name, prepended with a leading space. Note - // that this name is the LHS of the string value, not the outer attribute - // name. In the example above, the name would be "p0" or "p1", rather than - // "zippy". - fn get_arg_spaced(&self, name: &str) -> String { + // Returns the value for a given name, prepended with prefix. + // Note that this name is the LHS of the string value, not the outer + // attribute name. In the example above, the name would be "p0" or "p1", + // rather than "zippy". + fn get_arg_with_prefix(&self, name: &str, prefix: &str) -> String { match self.0.get(name) { - Some(annot) => " ".to_owned() + &annot, + Some(annot) => prefix.to_owned() + &annot, _ => String::default(), } } + + fn get_arg(&self, name: &str) -> String { + self.get_arg_with_prefix(name, "") + } } pub fn to_c_name(name: &str) -> String { @@ -186,8 +190,8 @@ fn get_in_params( m.in_params .iter() .map(|(name, ty)| { - let extra = arg_types.get_arg_spaced(name); - let arrsize = array_sizes.get_arg_spaced(name); + let extra = arg_types.get_arg_with_prefix(name, " "); + let arrsize = array_sizes.get_arg(name); match ty { ast::Ty::Identifier { id, .. } => { if id.is_base_type() { @@ -256,7 +260,7 @@ fn get_out_params( if index == 0 { return ty_name; } - let extra = arg_types.get_arg_spaced(name); + let extra = arg_types.get_arg_with_prefix(name, " "); match ty { ast::Ty::Handle { .. } => format!("{}: {}{}", to_c_name(name), ty_name, extra), _ => format!("{}: {}{}", to_c_name(name), ty_name, extra), diff --git a/tools/banjo/banjo/test/abigen/abigen-protocol-markup.abigen.out b/tools/banjo/banjo/test/abigen/abigen-protocol-markup.abigen.out new file mode 100644 index 00000000000..cade8036d98 --- /dev/null +++ b/tools/banjo/banjo/test/abigen/abigen-protocol-markup.abigen.out @@ -0,0 +1,30 @@ +# Copyright 2019 The Fuchsia Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +# WARNING: THIS FILE IS MACHINE GENERATED. DO NOT EDIT. +# Generated from the banjo.examples.abigen.protocol.markup banjo file + +#^ This is a short description. +syscall an_excellent_thing + (clock_id: zx_clock_t) + returns (zx_time_t); + +#^ Very much like an object_wait_one. +#! handle must have ZX_RIGHT_WAIT. +syscall do_another_thing blocking + (handle: zx_handle_t, signals: zx_signals_t, deadline: zx_time_t) + returns (zx_status_t, observed: zx_signals_t optional); + +#^ Fancy hacks for array sizes. +#! handle must be of type ZX_OBJ_TYPE_FIFO and have ZX_RIGHT_READ. +syscall a_third_thing + (handle: zx_handle_t, elem_size: size_t, data: any[count*elem_size] OUT, count: size_t) + returns (zx_status_t, actual_count: size_t optional); + +syscall the_attributes blocking const internal test_category1 test_category2 vdsocall + () + returns (); + +syscall noreturn_handling noreturn + (); diff --git a/tools/banjo/banjo/test/banjo/abigen-protocol-markup.test.banjo b/tools/banjo/banjo/test/banjo/abigen-protocol-markup.test.banjo new file mode 100644 index 00000000000..1f558076843 --- /dev/null +++ b/tools/banjo/banjo/test/banjo/abigen-protocol-markup.test.banjo @@ -0,0 +1,31 @@ +// Copyright 2019 The Fuchsia Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +library banjo.examples.abigen.protocol.markup; + +protocol Api { + [shortdesc="This is a short description."] + an_excellent_thing(zx.clock clock_id) -> (zx.time time); + + [shortdesc="Very much like an object_wait_one.", + rights="handle must have ZX_RIGHT_WAIT.", + blocking, + argtype="observed optional"] + do_another_thing(handle handle, zx.signals signals, zx.time deadline) -> + (zx.status status, zx.signals observed); + + [shortdesc="Fancy hacks for array sizes.", + rights="handle must be of type ZX_OBJ_TYPE_FIFO and have ZX_RIGHT_READ.", + argtype="data OUT", + argtype="actual_count optional", + arraysize="data count*elem_size"] + a_third_thing(handle<fifo> handle, usize elem_size, array<voidptr>:N data, usize count) -> + (zx.status status, usize actual_count); + + [blocking, const, internal, test_category1, test_category2, vdsocall] + the_attributes() -> (); + + [noreturn] + noreturn_handling(); +}; diff --git a/tools/banjo/banjo/test/codegen_tests.rs b/tools/banjo/banjo/test/codegen_tests.rs index b3413677266..49284e4d9c6 100644 --- a/tools/banjo/banjo/test/codegen_tests.rs +++ b/tools/banjo/banjo/test/codegen_tests.rs @@ -233,10 +233,18 @@ mod abigen { // Review" bit on Gerrit. codegen_test!(empty, AbigenBackend, ["banjo/empty.test.banjo"], "abigen/empty.abigen.out"); + codegen_test!( abigen_protocol_basic, AbigenBackend, ["banjo/abigen-protocol-basic.test.banjo"], "abigen/abigen-protocol-basic.abigen.out" ); + + codegen_test!( + abigen_protocol_markup, + AbigenBackend, + ["banjo/abigen-protocol-markup.test.banjo"], + "abigen/abigen-protocol-markup.abigen.out" + ); } -- GitLab