Skip to content

Commit fcff834

Browse files
dplassgitcopybara-github
authored andcommitted
Replace deprecated public Fmt methods with calls to Formatter::Format.
PiperOrigin-RevId: 703132251
1 parent f4450eb commit fcff834

File tree

3 files changed

+28
-46
lines changed

3 files changed

+28
-46
lines changed

xls/dslx/fmt/ast_fmt.cc

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959
namespace xls::dslx {
6060
namespace {
6161

62+
// Forward decls.
63+
DocRef Fmt(const Expr& n, const Comments& comments, DocArena& arena);
6264
DocRef FmtBlockedExprLeader(const Expr& e, const Comments& comments,
6365
DocArena& arena);
6466

@@ -1730,8 +1732,16 @@ DocRef FmtBlockedExprLeader(const Expr& e, const Comments& comments,
17301732
}
17311733
}
17321734

1735+
DocRef Fmt(const Expr& n, const Comments& comments, DocArena& arena) {
1736+
return FmtExpr(n, comments, arena, /*suppress_parens=*/false);
1737+
}
1738+
17331739
} // namespace
17341740

1741+
DocRef Formatter::Format(const Expr& n) {
1742+
return FmtExpr(n, comments_, arena_, /*suppress_parens=*/false);
1743+
}
1744+
17351745
DocRef Formatter::Format(const ConstantDef& n) {
17361746
std::vector<DocRef> leader_pieces;
17371747
if (n.is_public()) {
@@ -2650,10 +2660,6 @@ DocRef Formatter::Format(const ModuleMember& n) {
26502660
n);
26512661
}
26522662

2653-
DocRef Fmt(const Expr& n, const Comments& comments, DocArena& arena) {
2654-
return FmtExpr(n, comments, arena, /*suppress_parens=*/false);
2655-
}
2656-
26572663
// Returns whether the given members are of the given "MemberT" and "grouped" --
26582664
// that is, one is placed directly on the line above the other. We use this as
26592665
// an indicator they should also be grouped in the formatted output for certain
@@ -2840,16 +2846,8 @@ DocRef Formatter::Format(const Module& n) {
28402846
return ConcatN(arena_, pieces);
28412847
}
28422848

2843-
DocRef Fmt(const Function& n, const Comments& comments, DocArena& arena) {
2844-
return Formatter(comments, arena).Format(n);
2845-
}
2846-
2847-
DocRef Fmt(const Module& n, const Comments& comments, DocArena& arena) {
2848-
return Formatter(comments, arena).Format(n);
2849-
}
2850-
2851-
std::string LegacyAutoFmt(const Module& m, const Comments& comments,
2852-
int64_t text_width) {
2849+
static std::string AutoFmt(const Module& m, const Comments& comments,
2850+
int64_t text_width) {
28532851
DocArena arena(*m.file_table());
28542852
Formatter formatter(comments, arena);
28552853
DocRef ref = formatter.Format(m);
@@ -2864,7 +2862,7 @@ absl::StatusOr<std::string> AutoFmt(VirtualizableFilesystem& vfs,
28642862
XLS_ASSIGN_OR_RETURN(
28652863
std::unique_ptr<Module> clone,
28662864
CloneModule(m, std::bind_front(&FormatDisabler::operator(), &disabler)));
2867-
return LegacyAutoFmt(*clone, comments, text_width);
2865+
return AutoFmt(*clone, comments, text_width);
28682866
}
28692867

28702868
absl::StatusOr<std::string> AutoFmt(VirtualizableFilesystem& vfs,
@@ -2874,7 +2872,7 @@ absl::StatusOr<std::string> AutoFmt(VirtualizableFilesystem& vfs,
28742872
XLS_ASSIGN_OR_RETURN(
28752873
std::unique_ptr<Module> clone,
28762874
CloneModule(m, std::bind_front(&FormatDisabler::operator(), &disabler)));
2877-
return LegacyAutoFmt(*clone, comments, text_width);
2875+
return AutoFmt(*clone, comments, text_width);
28782876
}
28792877

28802878
// AutoFmt output should be the same as input after whitespace is eliminated

xls/dslx/fmt/ast_fmt.h

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class Formatter {
4242
// Each `Format` method creates a pretty-printable document from the given AST
4343
// node `n`.
4444
DocRef Format(const Function& n);
45+
DocRef Format(const Expr& n);
4546
DocRef Format(const Let& n, bool trailing_semi);
4647
DocRef Format(const Module& n);
4748
// If `trailing_semi` is true, then a trailing semicolon will also be emitted.
@@ -79,28 +80,10 @@ class Formatter {
7980
DocArena& arena_;
8081
};
8182

82-
// Functions with this signature create a pretty printable document from the AST
83-
// node "n".
84-
85-
// Deprecated; use Formatter::Format instead.
86-
DocRef Fmt(const Expr& n, const Comments& comments, DocArena& arena);
87-
88-
// Deprecated; use Formatter::Format instead.
89-
DocRef Fmt(const Function& n, const Comments& comments, DocArena& arena);
90-
91-
// Deprecated; use Formatter::Format instead.
92-
DocRef Fmt(const Module& n, const Comments& comments, DocArena& arena);
93-
9483
inline constexpr int64_t kDslxDefaultTextWidth = 100;
9584

9685
// Auto-formatting entry points.
9786
//
98-
// Performs a reflow-capable formatting of module `m` with standard line width.
99-
// This method is deprecated and will be removed soon. Use the AutoFmt method
100-
// below instead.
101-
std::string LegacyAutoFmt(const Module& m, const Comments& comments,
102-
int64_t text_width = kDslxDefaultTextWidth);
103-
10487
// Performs a reflow-capable formatting of module `m` with standard line width,
10588
// but with the ability to disable formatting for specific ranges of text.
10689
absl::StatusOr<std::string> AutoFmt(VirtualizableFilesystem& vfs,

xls/dslx/fmt/ast_fmt_test.cc

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ TEST(BuiltAstFmtTest, FormatCastThatNeedsParens) {
5454
const Comments empty_comments = Comments::Create({});
5555

5656
DocArena arena(file_table);
57-
DocRef doc = Fmt(*lt, empty_comments, arena);
57+
DocRef doc = Formatter(empty_comments, arena).Format(*lt);
5858
EXPECT_EQ(PrettyPrint(arena, doc, /*text_width=*/100), "(x as t) < x");
5959
}
6060

@@ -63,7 +63,7 @@ TEST(BuiltAstFmtTest, FormatIndexThatNeedsParens) {
6363
const Comments empty_comments = Comments::Create({});
6464

6565
DocArena arena(file_table);
66-
DocRef doc = Fmt(*index, empty_comments, arena);
66+
DocRef doc = Formatter(empty_comments, arena).Format(*index);
6767
EXPECT_EQ(PrettyPrint(arena, doc, /*text_width=*/100), "(x as u32[42])[i]");
6868
}
6969

@@ -73,7 +73,7 @@ TEST(BuiltAstFmtTest, FormatTupleIndexThatNeedsParens) {
7373
const Comments empty_comments = Comments::Create({});
7474

7575
DocArena arena(file_table);
76-
DocRef doc = Fmt(*tuple_index, empty_comments, arena);
76+
DocRef doc = Formatter(empty_comments, arena).Format(*tuple_index);
7777
EXPECT_EQ(PrettyPrint(arena, doc, /*text_width=*/100), "(x[i]).2");
7878
}
7979

@@ -83,7 +83,7 @@ TEST(BuiltAstFmtTest, FormatSingleElementTuple) {
8383
const Comments empty_comments = Comments::Create({});
8484

8585
DocArena arena(file_table);
86-
DocRef doc = Fmt(*tuple, empty_comments, arena);
86+
DocRef doc = Formatter(empty_comments, arena).Format(*tuple);
8787
EXPECT_EQ(PrettyPrint(arena, doc, /*text_width=*/100), "(x0,)");
8888
}
8989

@@ -93,7 +93,7 @@ TEST(BuiltAstFmtTest, FormatShortTupleWithoutTrailingComma) {
9393
const Comments empty_comments = Comments::Create({});
9494

9595
DocArena arena(file_table);
96-
DocRef doc = Fmt(*tuple, empty_comments, arena);
96+
DocRef doc = Formatter(empty_comments, arena).Format(*tuple);
9797
EXPECT_EQ(PrettyPrint(arena, doc, /*text_width=*/100), "(x0, x1)");
9898
}
9999

@@ -103,7 +103,7 @@ TEST(BuiltAstFmtTest, FormatShortTupleWithTrailingComma) {
103103
const Comments empty_comments = Comments::Create({});
104104

105105
DocArena arena(file_table);
106-
DocRef doc = Fmt(*tuple, empty_comments, arena);
106+
DocRef doc = Formatter(empty_comments, arena).Format(*tuple);
107107
EXPECT_EQ(PrettyPrint(arena, doc, /*text_width=*/100), "(x0, x1)");
108108
}
109109

@@ -114,7 +114,7 @@ TEST(BuiltAstFmtTest, FormatLongTupleShouldAddTrailingComma) {
114114
MakeNElementTupleExpression(40, /*has_trailing_comma=*/true);
115115

116116
DocArena arena(file_table);
117-
DocRef doc = Fmt(*tuple, empty_comments, arena);
117+
DocRef doc = Formatter(empty_comments, arena).Format(*tuple);
118118
EXPECT_EQ(PrettyPrint(arena, doc, /*text_width=*/100),
119119
R"((
120120
x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20,
@@ -127,7 +127,8 @@ TEST(BuiltAstFmtTest, FormatLongTupleShouldAddTrailingComma) {
127127
MakeNElementTupleExpression(40, /*has_trailing_comma=*/false);
128128

129129
DocArena arena(file_table);
130-
DocRef doc = Fmt(*tuple_without_trailing_comma, empty_comments, arena);
130+
DocRef doc =
131+
Formatter(empty_comments, arena).Format(*tuple_without_trailing_comma);
131132
EXPECT_EQ(PrettyPrint(arena, doc, /*text_width=*/100),
132133
R"((
133134
x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20,
@@ -141,7 +142,7 @@ TEST(BuiltAstFmtTest, FormatUnopThatNeedsParensOnOperand) {
141142
const Comments empty_comments = Comments::Create({});
142143

143144
DocArena arena(file_table);
144-
DocRef doc = Fmt(*unop, empty_comments, arena);
145+
DocRef doc = Formatter(empty_comments, arena).Format(*unop);
145146
EXPECT_EQ(PrettyPrint(arena, doc, /*text_width=*/100), "-(x as u32)");
146147
}
147148

@@ -150,7 +151,7 @@ TEST(BuiltAstFmtTest, FormatAttrThatNeedsParensOnOperand) {
150151
const Comments empty_comments = Comments::Create({});
151152

152153
DocArena arena(file_table);
153-
DocRef doc = Fmt(*attr, empty_comments, arena);
154+
DocRef doc = Formatter(empty_comments, arena).Format(*attr);
154155
EXPECT_EQ(PrettyPrint(arena, doc, /*text_width=*/100), "(x * y).my_attr");
155156
}
156157

@@ -181,7 +182,7 @@ TEST(AstFmtTest, FormatVerbatimNodeTop) {
181182
const Comments empty_comments = Comments::Create({});
182183

183184
DocArena arena(file_table);
184-
DocRef doc = Fmt(m, empty_comments, arena);
185+
DocRef doc = Formatter(empty_comments, arena).Format(m);
185186

186187
// Intentionally small text width, should still be formatted verbatim.
187188
EXPECT_EQ(PrettyPrint(arena, doc, /*text_width=*/10), verbatim_text);
@@ -233,7 +234,7 @@ class FunctionFmtTest : public testing::Test {
233234
f_, parser_->ParseFunction(Pos(), /*is_public=*/false, bindings_));
234235
Comments comments = Comments::Create(scanner_->comments());
235236

236-
DocRef doc = Fmt(*f_, comments, arena_);
237+
DocRef doc = Formatter(comments, arena_).Format(*f_);
237238
std::string formatted = PrettyPrint(arena_, doc, kDslxDefaultTextWidth);
238239

239240
std::optional<AutoFmtPostconditionViolation> maybe_violation =

0 commit comments

Comments
 (0)