Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix format for asm with comments #14278

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 additions & 0 deletions spec/compiler/formatter/formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1695,6 +1695,43 @@ describe Crystal::Formatter do
assert_format %(asm("a" : "b"(c) : "d"(e)\n : "f",\n "g"))
assert_format %(asm("a" ::: "a"\n : "volatile",\n "intel"))

assert_format <<-CRYSTAL, <<-CRYSTAL
asm(
# foo
"nop"
# bar
)
CRYSTAL
asm(
# foo
"nop"
# bar
)
CRYSTAL
assert_format <<-CRYSTAL, <<-CRYSTAL
straight-shoota marked this conversation as resolved.
Show resolved Hide resolved
asm(
# the assembly template string, following the
# syntax for LLVM's integrated assembler
"nop" : # output operands
"=r"(foo), "=r"(bar) : # input operands
"r"(1), "r"(baz) : # names of clobbered registers
"eax", "memory" : # optional flags, corresponding to the LLVM IR
# sideeffect / alignstack / inteldialect / unwind attributes
"volatile", "alignstack", "intel", "unwind"
)
CRYSTAL
asm(
# the assembly template string, following the
# syntax for LLVM's integrated assembler
"nop" : # output operands
"=r"(foo), "=r"(bar) : # input operands
"r"(1), "r"(baz) : # names of clobbered registers
"eax", "memory" : # optional flags, corresponding to the LLVM IR
# sideeffect / alignstack / inteldialect / unwind attributes
"volatile", "alignstack", "intel", "unwind"
)
CRYSTAL

assert_format "1 # foo\n1234 # bar", "1 # foo\n1234 # bar"
assert_format "1234 # foo\n1 # bar", "1234 # foo\n1 # bar"
assert_format "1#foo", "1 # foo"
Expand Down
19 changes: 14 additions & 5 deletions src/compiler/crystal/tools/formatter.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4370,6 +4370,7 @@ module Crystal
skip_space

if @token.type.newline?
@indent += 2
consume_newlines
has_newlines = true
end
Expand All @@ -4378,16 +4379,16 @@ module Crystal
string = StringLiteral.new(node.text)

if has_newlines
write_indent(@indent + 2, string)
write_indent(@indent, string)
else
indent(@column, string)
end

skip_space

if @token.type.newline?
consume_newlines
if node.outputs || node.inputs
consume_newlines
column += 4
write_indent(column)
end
Expand Down Expand Up @@ -4419,7 +4420,8 @@ module Crystal
write_token :OP_COLON
part_index += 1
end
skip_space_or_newline
skip_space
consume_newlines

case part_index
when 1
Expand Down Expand Up @@ -4457,7 +4459,9 @@ module Crystal
skip_space_or_newline

if has_newlines
write_line
@indent -= 2

write_line unless @wrote_newline
write_indent
end

Expand All @@ -4480,7 +4484,12 @@ module Crystal
end

def visit_asm_parts(parts, colon_column, &) : Nil
write " "
if @wrote_newline
write_indent
else
write " "
end

column = @column

parts.each_with_index do |part, i|
Expand Down