Skip to content

Commit bf5b2d5

Browse files
authored
Strip macros in definition(String, m) (#32)
This will run into problems for macros that create methods, but that's problematic anyway.
1 parent 3a27606 commit bf5b2d5

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

src/CodeTracking.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module CodeTracking
22

33
using Base: PkgId
44
using Core: LineInfoNode
5+
using Base.Meta: isexpr
56
using UUIDs
67
using InteractiveUtils
78

@@ -203,9 +204,16 @@ function definition(::Type{String}, method::Method)
203204
end
204205
# The function declaration was presumably on a previous line
205206
lineindex = lastindex(linestarts)
207+
local istart_noerr
206208
while !isfuncexpr(ex) && lineindex > 0
207209
istart = linestarts[lineindex]
208-
ex, iend = Meta.parse(src, istart)
210+
try
211+
ex, iend = Meta.parse(src, istart)
212+
catch
213+
istart = istart_noerr
214+
break
215+
end
216+
istart_noerr = istart
209217
lineindex -= 1
210218
line -= 1
211219
end

src/utils.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
function isfuncexpr(ex)
2+
# Strip any macros that wrap the method definition
3+
while isexpr(ex, :macrocall)
4+
ex = ex.args[3]
5+
end
6+
isa(ex, Expr) || return false
27
ex.head == :function && return true
38
if ex.head == :(=)
49
a = ex.args[1]

test/runtests.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ isdefined(Main, :Revise) ? includet("script.jl") : include("script.jl")
1717
catch
1818
stacktrace(catch_backtrace())
1919
end
20-
@test whereis(trace[2]) == (scriptpath, 11)
20+
@test whereis(trace[2]) == (scriptpath, 9)
2121
@test whereis(trace[3]) === nothing
2222

2323
src, line = definition(String, m)
@@ -32,6 +32,11 @@ isdefined(Main, :Revise) ? includet("script.jl") : include("script.jl")
3232
m = first(methods(f2))
3333
src, line = definition(String, m)
3434
@test src == "f2(x, y) = x + y"
35+
@test line == 14
36+
37+
m = first(methods(throws))
38+
src, line = definition(String, m)
39+
@test startswith(src, "@noinline")
3540
@test line == 7
3641

3742
info = CodeTracking.PkgFiles(Base.PkgId(CodeTracking))

test/script.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ function f1(x, y)
44
return x + y
55
end
66

7-
f2(x, y) = x + y
8-
97
@noinline function throws()
108
x = nothing
119
error("oops")
1210
end
1311
@inline inlined() = throws()
1412
call_throws() = inlined()
13+
14+
f2(x, y) = x + y

0 commit comments

Comments
 (0)