Skip to content

Commit d278866

Browse files
authored
Merge pull request #17 from timholy/teh/macros
File location info for macro expansions.
2 parents f3963c3 + 8cef563 commit d278866

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

src/CodeTracking.jl

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,11 @@ function whereis(method::Method)
4242
end
4343
end
4444
if lin === nothing
45-
file, line = maybe_fixup_stdlib_path(String(method.file)), method.line
45+
file, line = String(method.file), method.line
4646
else
4747
file, line = fileline(lin[1])
4848
end
49-
if !isabspath(file)
50-
# This may be a Base or Core method
51-
newfile = Base.find_source_file(file)
52-
if isa(newfile, AbstractString)
53-
file = normpath(newfile)
54-
end
55-
end
49+
file = maybe_fix_path(file)
5650
return file, line
5751
end
5852

@@ -77,7 +71,21 @@ was compiled. The current location is returned.
7771
"""
7872
function whereis(lineinfo, method::Method)
7973
file, line1 = whereis(method)
80-
return file, lineinfo.line-method.line+line1
74+
# We could be in an expanded macro. Apply the correction only if the filename checks out.
75+
# (We're not super-fastidious here because of symlinks and other path ambiguities)
76+
samefile = basename(file) == basename(String(lineinfo.file))
77+
if !samefile
78+
return maybe_fix_path(String(lineinfo.file)), lineinfo.line
79+
end
80+
return file, lineinfo.line - method.line + line1
81+
end
82+
function whereis(lineinfo::Core.LineInfoNode, method::Method)
83+
# With LineInfoNode we have certainty about whether we're in a macro expansion
84+
if lineinfo.method == Symbol("macro expansion")
85+
return maybe_fix_path(String(lineinfo.file)), lineinfo.line
86+
end
87+
file, line1 = whereis(method)
88+
return file, lineinfo.line - method.line + line1
8189
end
8290

8391
"""

src/utils.jl

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@ function basepath(id::PkgId)
4646
return dirname(dirname(loc))
4747
end
4848

49+
"""
50+
path = maybe_fix_path(path)
51+
52+
Return a normalized, absolute path for a source file `path`.
53+
"""
54+
function maybe_fix_path(file)
55+
if !isabspath(file)
56+
# This may be a Base or Core method
57+
newfile = Base.find_source_file(file)
58+
if isa(newfile, AbstractString)
59+
file = normpath(newfile)
60+
end
61+
end
62+
return maybe_fixup_stdlib_path(file)
63+
end
64+
4965
const BUILDBOT_STDLIB_PATH = dirname(abspath(joinpath(String((@which uuid1()).file), "..", "..", "..")))
5066
replace_buildbot_stdlibpath(str::String) = replace(str, BUILDBOT_STDLIB_PATH => Sys.STDLIB)
5167
"""
@@ -54,7 +70,7 @@ replace_buildbot_stdlibpath(str::String) = replace(str, BUILDBOT_STDLIB_PATH =>
5470
Return `path` corrected for julia issue [#26314](https://github.com/JuliaLang/julia/issues/26314) if applicable.
5571
Otherwise, return the input `path` unchanged.
5672
57-
Due to the issue mentioned above, location info for methods defined one of Julia's standard libraries
73+
Due to the issue mentioned above, location info for methods defined one of Julia's standard libraries
5874
are, for non source Julia builds, given as absolute paths on the worker that built the `julia` executable.
5975
This function corrects such a path to instead refer to the local path on the users drive.
6076
"""
@@ -64,4 +80,4 @@ function maybe_fixup_stdlib_path(path)
6480
isfile(maybe_stdlib_path) && return maybe_stdlib_path
6581
end
6682
return path
67-
end
83+
end

test/runtests.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,16 @@ include("script.jl")
5656

5757
m = first(methods(Test.eval))
5858
@test occursin(Sys.STDLIB, whereis(m)[1])
59+
60+
# https://github.com/JuliaDebug/JuliaInterpreter.jl/issues/150
61+
function f150()
62+
x = 1 + 1
63+
@info "hello"
64+
end
65+
m = first(methods(f150))
66+
src = Base.uncompressed_ast(m)
67+
idx = findfirst(lin -> String(lin.file) != @__FILE__, src.linetable)
68+
lin = src.linetable[idx]
69+
file, line = whereis(lin, m)
70+
@test endswith(file, String(lin.file))
5971
end

0 commit comments

Comments
 (0)