aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAdam Goertz <adambgoertz@gmail.com>2023-11-04 23:00:28 +0000
committerAdam Goertz <adambgoertz@gmail.com>2023-11-06 04:31:55 +0000
commit91570cc42d4fe973d15ac05cdecff42051f154ad (patch)
tree933200ee149c2557d3739ed7332e59b4e7ca341e /lib
parent1b0b46a8a9f5ed3ebaf35e3018fd5402957552ae (diff)
downloadzig-91570cc42d4fe973d15ac05cdecff42051f154ad.tar.gz
zig-91570cc42d4fe973d15ac05cdecff42051f154ad.zip
zig-reduce: Reduce `if` expressions
Perform these transformations in this priority order: 1. If the `else` expression is missing or an empty block, replace the condition with `if (true)` if it is not already. 2. If the `then` block is empty, replace the condition with `if (false)` if it is not already. 3. If the condition is `if (true)`, replace the `if` expression with the contents of the `then` expression. 4. If the condition is `if (false)`, replace the `if` expression with the contents of the `else` expression.
Diffstat (limited to 'lib')
-rw-r--r--lib/std/zig/render.zig17
1 files changed, 12 insertions, 5 deletions
diff --git a/lib/std/zig/render.zig b/lib/std/zig/render.zig
index c2a8ac9926..d6e862a922 100644
--- a/lib/std/zig/render.zig
+++ b/lib/std/zig/render.zig
@@ -25,7 +25,9 @@ pub const Fixups = struct {
/// These global declarations will be omitted.
omit_nodes: std.AutoHashMapUnmanaged(Ast.Node.Index, void) = .{},
/// These expressions will be replaced with the string value.
- replace_nodes: std.AutoHashMapUnmanaged(Ast.Node.Index, []const u8) = .{},
+ replace_nodes_with_string: std.AutoHashMapUnmanaged(Ast.Node.Index, []const u8) = .{},
+ /// These nodes will be replaced with a different node.
+ replace_nodes_with_node: std.AutoHashMapUnmanaged(Ast.Node.Index, Ast.Node.Index) = .{},
/// Change all identifier names matching the key to be value instead.
rename_identifiers: std.StringArrayHashMapUnmanaged([]const u8) = .{},
@@ -37,7 +39,8 @@ pub const Fixups = struct {
return f.unused_var_decls.count() +
f.gut_functions.count() +
f.omit_nodes.count() +
- f.replace_nodes.count() +
+ f.replace_nodes_with_string.count() +
+ f.replace_nodes_with_node.count() +
f.rename_identifiers.count() +
@intFromBool(f.rebase_imported_paths != null);
}
@@ -46,7 +49,8 @@ pub const Fixups = struct {
f.unused_var_decls.clearRetainingCapacity();
f.gut_functions.clearRetainingCapacity();
f.omit_nodes.clearRetainingCapacity();
- f.replace_nodes.clearRetainingCapacity();
+ f.replace_nodes_with_string.clearRetainingCapacity();
+ f.replace_nodes_with_node.clearRetainingCapacity();
f.rename_identifiers.clearRetainingCapacity();
f.rebase_imported_paths = null;
@@ -56,7 +60,8 @@ pub const Fixups = struct {
f.unused_var_decls.deinit(gpa);
f.gut_functions.deinit(gpa);
f.omit_nodes.deinit(gpa);
- f.replace_nodes.deinit(gpa);
+ f.replace_nodes_with_string.deinit(gpa);
+ f.replace_nodes_with_node.deinit(gpa);
f.rename_identifiers.deinit(gpa);
f.* = undefined;
}
@@ -329,10 +334,12 @@ fn renderExpression(r: *Render, node: Ast.Node.Index, space: Space) Error!void {
const main_tokens = tree.nodes.items(.main_token);
const node_tags = tree.nodes.items(.tag);
const datas = tree.nodes.items(.data);
- if (r.fixups.replace_nodes.get(node)) |replacement| {
+ if (r.fixups.replace_nodes_with_string.get(node)) |replacement| {
try ais.writer().writeAll(replacement);
try renderOnlySpace(r, space);
return;
+ } else if (r.fixups.replace_nodes_with_node.get(node)) |replacement| {
+ return renderExpression(r, replacement, space);
}
switch (node_tags[node]) {
.identifier => {