Skip to content

Commit

Permalink
Add missing debug locations to constant / class variable read calls (#…
Browse files Browse the repository at this point in the history
…11417)

Fixes #11416.

Although the constant read functions have a debug location, for some reason the calls themselves to the read functions do not. This PR adds them.
  • Loading branch information
HertzDevil authored Nov 9, 2021
1 parent c2429e2 commit e5bf6b9
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 2 deletions.
19 changes: 19 additions & 0 deletions spec/compiler/codegen/debug_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,23 @@ describe "Code gen: debug" do
LibFoo.foo = ->{ }
), debug: Crystal::Debug::All)
end

it "doesn't fail on constant read calls (#11416)" do
codegen(%(
require "prelude"
class Foo
def foo
end
end
def a_foo
Foo.new
end
THE_FOO.foo
THE_FOO = a_foo
), debug: Crystal::Debug::All)
end
end
19 changes: 19 additions & 0 deletions spec/llvm-ir/class-var-read-debug-loc.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "prelude"

class Foo
def foo
@@x
# CHECK: call %String** @"~Bar::x:read"
# CHECK-SAME: !dbg [[LOC:![0-9]+]]
# CHECK: [[LOC]] = !DILocation
# CHECK-SAME: line: [[# @LINE - 4]]
# CHECK-SAME: column: 5
end

@@x = ""
end

class Bar < Foo
end

Bar.new.foo
19 changes: 19 additions & 0 deletions spec/llvm-ir/const-read-debug-loc.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require "prelude"

class Foo
def foo
end
end

def a_foo
Foo.new
end

THE_FOO.foo
# CHECK: call %Foo** @"~THE_FOO:read"()
# CHECK-SAME: !dbg [[LOC:![0-9]+]]
# CHECK: [[LOC]] = !DILocation
# CHECK-SAME: line: 12
# CHECK-SAME: column: 1

THE_FOO = a_foo
6 changes: 6 additions & 0 deletions spec/llvm-ir/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ pushd $BUILD_DIR >/dev/null

test argless-initialize-debug-loc.cr "--cross-compile --target x86_64-unknown-linux-gnu --prelude=empty"
test proc-pointer-debug-loc.cr "--cross-compile --target x86_64-unknown-linux-gnu --prelude=empty"
test const-read-debug-loc.cr "--cross-compile --target x86_64-unknown-linux-gnu --prelude=empty"

# #11416
test class-var-read-debug-loc.cr "--cross-compile --target x86_64-unknown-linux-gnu --prelude=empty"
test virtual-class-var-read-debug-loc.cr "--cross-compile --target x86_64-unknown-linux-gnu --prelude=empty"
test virtual-metaclass-var-read-debug-loc.cr "--cross-compile --target x86_64-unknown-linux-gnu --prelude=empty"

test memset.cr "--cross-compile --target i386-apple-darwin --prelude=empty --no-debug" X32
test memset.cr "--cross-compile --target i386-unknown-linux-gnu --prelude=empty --no-debug" X32
Expand Down
17 changes: 17 additions & 0 deletions spec/llvm-ir/virtual-class-var-read-debug-loc.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Foo
def foo
@@x
# CHECK: call i32* @"~Foo+::x:read"
# CHECK-SAME: !dbg [[LOC:![0-9]+]]
# CHECK: [[LOC]] = !DILocation
# CHECK-SAME: line: [[# @LINE - 4]]
# CHECK-SAME: column: 5
end

@@x = 1
end

class Bar < Foo
end

(Foo.new || Bar.new).foo
17 changes: 17 additions & 0 deletions spec/llvm-ir/virtual-metaclass-var-read-debug-loc.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Foo
def self.foo
@@x
# CHECK: call i32* @"~Foo+.class::x:read"
# CHECK-SAME: !dbg [[LOC3:![0-9]+]]
# CHECK: [[LOC3]] = !DILocation
# CHECK-SAME: line: [[# @LINE - 4]]
# CHECK-SAME: column: 5
end

@@x = 1
end

class Bar < Foo
end

(Foo || Bar).foo
1 change: 1 addition & 0 deletions src/compiler/crystal/codegen/class_var.cr
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ class Crystal::CodeGenVisitor
end

def read_class_var(node : ClassVar)
set_current_debug_location node if @debug.line_numbers?
read_class_var(node.var)
end

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/codegen/codegen.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1470,7 +1470,7 @@ module Crystal

def visit(node : Path)
if const = node.target_const
read_const(const)
read_const(const, node)
elsif replacement = node.syntax_replacement
accept replacement
else
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/crystal/codegen/const.cr
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ class Crystal::CodeGenVisitor
end
end

def read_const(const)
def read_const(const, node)
# We inline constants. Otherwise we use an LLVM const global.
@last =
case value = const.compile_time_value
Expand All @@ -205,6 +205,7 @@ class Crystal::CodeGenVisitor
when UInt32 then int32(value)
when UInt64 then int64(value)
else
set_current_debug_location node if @debug.line_numbers?
last = read_const_pointer(const)
to_lhs last, const.value.type
end
Expand Down

0 comments on commit e5bf6b9

Please sign in to comment.