@@ -3499,4 +3499,54 @@ fn top(x: u32, y: MyE) -> u32 { x }
34993499 xls_dslx_import_data_free (import_data);
35003500}
35013501
3502+ TEST (XlsCApiTest, DslxStringLiteralAttribute) {
3503+ const char * kDslx = R"DSLX(
3504+ #[dslx_format_disable("fmt-off")]
3505+ fn fmt_fn(x: u32) -> u32 { x }
3506+ )DSLX" ;
3507+ const char * additional_search_paths[] = {};
3508+ std::string dslx_stdlib_path = std::string (xls::kDefaultDslxStdlibPath );
3509+ xls_dslx_import_data* import_data = xls_dslx_import_data_create (
3510+ dslx_stdlib_path.c_str (), additional_search_paths, 0 );
3511+ ASSERT_NE (import_data, nullptr );
3512+ absl::Cleanup free_import_data (
3513+ [&]() { xls_dslx_import_data_free (import_data); });
3514+ char * error = nullptr ;
3515+ xls_dslx_typechecked_module* tm = nullptr ;
3516+ ASSERT_TRUE (xls_dslx_parse_and_typecheck (kDslx , " attr_test.x" , " attr_test" ,
3517+ import_data, &error, &tm))
3518+ << (error ? error : " " );
3519+ absl::Cleanup free_tm ([&]() { xls_dslx_typechecked_module_free (tm); });
3520+ xls_c_str_free (error);
3521+
3522+ xls_dslx_module* module = xls_dslx_typechecked_module_get_module (tm);
3523+ auto find_function = [&](std::string_view target) -> xls_dslx_function* {
3524+ int64_t member_count = xls_dslx_module_get_member_count (module );
3525+ for (int64_t i = 0 ; i < member_count; ++i) {
3526+ xls_dslx_module_member* member = xls_dslx_module_get_member (module , i);
3527+ xls_dslx_function* fn = xls_dslx_module_member_get_function (member);
3528+ if (fn) {
3529+ char * id = xls_dslx_function_get_identifier (fn);
3530+ absl::Cleanup free_id ([&]() { xls_c_str_free (id); });
3531+ if (std::string_view (id) == target) return fn;
3532+ }
3533+ }
3534+ return nullptr ;
3535+ };
3536+
3537+ xls_dslx_function* fmt_fn = find_function (" fmt_fn" );
3538+ ASSERT_NE (fmt_fn, nullptr );
3539+
3540+ ASSERT_EQ (xls_dslx_function_get_attribute_count (fmt_fn), 1 );
3541+ xls_dslx_attribute* fmt_attr = xls_dslx_function_get_attribute (fmt_fn, 0 );
3542+ EXPECT_EQ (xls_dslx_attribute_get_kind (fmt_attr),
3543+ xls_dslx_attribute_kind_dslx_format_disable);
3544+ ASSERT_EQ (xls_dslx_attribute_get_argument_count (fmt_attr), 1 );
3545+ EXPECT_EQ (xls_dslx_attribute_get_argument_kind (fmt_attr, 0 ),
3546+ xls_dslx_attribute_argument_kind_string_literal);
3547+ char * fmt_arg = xls_dslx_attribute_get_string_literal_argument (fmt_attr, 0 );
3548+ absl::Cleanup free_fmt_arg ([&]() { xls_c_str_free (fmt_arg); });
3549+ EXPECT_EQ (std::string (fmt_arg), " fmt-off" );
3550+ }
3551+
35023552} // namespace
0 commit comments