Skip to content

Commit f92dd68

Browse files
Merge pull request #3438 from xlsynth:meheff/2025-11-27-attribute
PiperOrigin-RevId: 838951951
2 parents a4e31e6 + 87c5520 commit f92dd68

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

xls/public/c_api_dslx.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,10 @@ xls_dslx_attribute_argument_kind xls_dslx_attribute_get_argument_kind(
681681
argument)) {
682682
return xls_dslx_attribute_argument_kind_int_key_value;
683683
}
684+
if (std::holds_alternative<xls::dslx::Attribute::StringLiteralArgument>(
685+
argument)) {
686+
return xls_dslx_attribute_argument_kind_string_literal;
687+
}
684688
CHECK(false) << "Unexpected attribute argument kind";
685689
return xls_dslx_attribute_argument_kind_string;
686690
}
@@ -695,6 +699,17 @@ char* xls_dslx_attribute_get_string_argument(
695699
return xls::ToOwnedCString(*value);
696700
}
697701

702+
char* xls_dslx_attribute_get_string_literal_argument(
703+
struct xls_dslx_attribute* attribute, int64_t index) {
704+
auto* cpp_attribute = reinterpret_cast<xls::dslx::Attribute*>(attribute);
705+
const xls::dslx::Attribute::Argument& argument =
706+
cpp_attribute->args().at(index);
707+
const xls::dslx::Attribute::StringLiteralArgument* value =
708+
std::get_if<xls::dslx::Attribute::StringLiteralArgument>(&argument);
709+
CHECK(value != nullptr) << "Attribute argument is not a string literal";
710+
return xls::ToOwnedCString(value->text);
711+
}
712+
698713
char* xls_dslx_attribute_get_key_value_argument_key(
699714
struct xls_dslx_attribute* attribute, int64_t index) {
700715
auto* cpp_attribute = reinterpret_cast<xls::dslx::Attribute*>(attribute);

xls/public/c_api_dslx.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ enum {
7878
xls_dslx_attribute_argument_kind_string,
7979
xls_dslx_attribute_argument_kind_string_key_value,
8080
xls_dslx_attribute_argument_kind_int_key_value,
81+
xls_dslx_attribute_argument_kind_string_literal,
8182
};
8283

8384
// Opaque structs.
@@ -326,6 +327,12 @@ xls_dslx_attribute_argument_kind xls_dslx_attribute_get_argument_kind(
326327
char* xls_dslx_attribute_get_string_argument(
327328
struct xls_dslx_attribute* attribute, int64_t index);
328329

330+
// Returns the argument value when the argument kind is
331+
// `xls_dslx_attribute_argument_kind_string_literal`.
332+
// Note: return value is owned by the caller, free via `xls_c_str_free`.
333+
char* xls_dslx_attribute_get_string_literal_argument(
334+
struct xls_dslx_attribute* attribute, int64_t index);
335+
329336
// Returns the argument key when the argument kind is a key/value variant.
330337
// Note: return value is owned by the caller, free via `xls_c_str_free`.
331338
char* xls_dslx_attribute_get_key_value_argument_key(

xls/public/c_api_symbols.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ xls_dslx_attribute_get_key_value_int_argument_value
112112
xls_dslx_attribute_get_key_value_string_argument_value
113113
xls_dslx_attribute_get_kind
114114
xls_dslx_attribute_get_string_argument
115+
xls_dslx_attribute_get_string_literal_argument
115116
xls_dslx_attribute_to_string
116117
xls_dslx_call_graph_free
117118
xls_dslx_call_graph_get_callee_count

xls/public/c_api_test.cc

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)