Skip to content

Commit

Permalink
[DSLX:fmt] Add support for zero! macro construct.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 572722446
  • Loading branch information
cdleary authored and copybara-github committed Oct 11, 2023
1 parent 14129a9 commit 2888d60
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions xls/dslx/fmt/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ cc_test(
deps = [
":ast_fmt",
":pretty_print",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/status:statusor",
"//xls/common:xls_gunit",
"//xls/common:xls_gunit_main",
Expand Down
9 changes: 8 additions & 1 deletion xls/dslx/fmt/ast_fmt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,14 @@ DocRef Fmt(const UnrollFor& n, const Comments& comments, DocArena& arena) {
}

DocRef Fmt(const ZeroMacro& n, const Comments& comments, DocArena& arena) {
XLS_LOG(FATAL) << "handle zero macro: " << n.ToString();
return ConcatNGroup(arena, {
arena.MakeText("zero!"),
arena.oangle(),
FmtExprOrType(n.type(), comments, arena),
arena.cangle(),
arena.oparen(),
arena.cparen(),
});
}

DocRef Fmt(const Unop& n, const Comments& comments, DocArena& arena) {
Expand Down
22 changes: 21 additions & 1 deletion xls/dslx/fmt/ast_fmt_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <vector>

#include "gtest/gtest.h"
#include "absl/container/flat_hash_set.h"
#include "absl/status/statusor.h"
#include "xls/common/logging/logging.h"
#include "xls/common/status/matchers.h"
Expand Down Expand Up @@ -123,10 +124,20 @@ let x: u32 = u32:42)");
// Use `ModuleFmtTest` for formatting of entire modules.
class FunctionFmtTest : public testing::Test {
public:
absl::StatusOr<std::string> DoFmt(std::string_view original) {
// Args:
// original: The text to parse-then-auto-format.
// builtin_name_defs: Names to initialize in the bindings as built-in.
absl::StatusOr<std::string> DoFmt(
std::string_view original,
const absl::flat_hash_set<std::string>& builtin_name_defs = {}) {
XLS_CHECK(!scanner_.has_value());
scanner_.emplace("fake.x", std::string{original});
parser_.emplace("fake", &scanner_.value());

for (const std::string& name : builtin_name_defs) {
bindings_.Add(name, parser_->module().GetOrCreateBuiltinNameDef(name));
}

XLS_ASSIGN_OR_RETURN(
f_, parser_->ParseFunction(/*is_public=*/false, bindings_));
Comments comments = Comments::Create(scanner_->comments());
Expand All @@ -135,6 +146,8 @@ class FunctionFmtTest : public testing::Test {
return PrettyPrint(arena_, doc, /*text_width=*/100);
}

Bindings& bindings() { return bindings_; }

private:
DocArena arena_;
std::optional<Scanner> scanner_;
Expand Down Expand Up @@ -377,6 +390,13 @@ TEST_F(FunctionFmtTest, MatchMultiPattern) {
EXPECT_EQ(got, want);
}

TEST_F(FunctionFmtTest, ZeroMacro) {
const std::string_view original = "fn f()->u32{zero!<u32>()}";
XLS_ASSERT_OK_AND_ASSIGN(std::string got, DoFmt(original, {"zero!"}));
const std::string_view want = R"(fn f() -> u32 { zero!<u32>() })";
EXPECT_EQ(got, want);
}

// -- ModuleFmtTest cases, formatting entire modules

TEST(ModuleFmtTest, TwoSimpleFunctions) {
Expand Down

0 comments on commit 2888d60

Please sign in to comment.