Skip to content

Commit

Permalink
Implement __LINE__ and __FILE__ magic macros
Browse files Browse the repository at this point in the history
Fixes #8066
  • Loading branch information
ihnorton committed Aug 22, 2015
1 parent e0b4510 commit fc647fe
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 13 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ New language features
Default continues to be of length 1 ([#12385]).
See http:https://docs.julialang.org/en/latest/manual/parallel-computing/#remoterefs-and-abstractchannels for details.

* `@__LINE__` special macro now available to reflect invocation source line number ([#12727]).

Language changes
----------------
Expand Down
16 changes: 13 additions & 3 deletions base/docs/helpdb.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

# Base.LinAlg.BLAS

import .Docs: keywords

doc"""
ger!(alpha, x, y, A)
Expand Down Expand Up @@ -9531,12 +9533,20 @@ Returns an iterator over substrings of `s` that correspond to the extended graph
"""
graphemes

doc"""
keywords[symbol("@__FILE__")] = doc"""
@__FILE__() -> AbstractString
`@__FILE__` expands to a string with the absolute path and file name of the script being run. Returns `nothing` if run from a REPL or an empty string if evaluated by `julia -e <expr>`.
`@__FILE__` expands to a string with the absolute path and file name of the script being run.
Returns `"none"` if run from a REPL or command-line context.
"""

keywords[symbol("@__LINE__")] = doc"""
@__LINE__() -> Int
`@__LINE__` expands to the line number of the call-site.
"""
:@__FILE__

doc"""
charwidth(c)
Expand Down
1 change: 0 additions & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1343,7 +1343,6 @@ export

# Macros
# parser internal
@__FILE__,
@int128_str,
@uint128_str,
@big_str,
Expand Down
2 changes: 0 additions & 2 deletions base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,6 @@ function source_dir()
p === nothing ? p : dirname(p)
end

macro __FILE__() source_path() end

function include_from_node1(_path::AbstractString)
path, prev = _include_dependency(_path)
tls = task_local_storage()
Expand Down
14 changes: 9 additions & 5 deletions src/julia-parser.scm
Original file line number Diff line number Diff line change
Expand Up @@ -2039,14 +2039,18 @@
(with-space-sensitive
(let* ((head (parse-unary-prefix s))
(t (peek-token s)))
(if (ts:space? s)
(cond
((eqv? head '__LINE__) (input-port-line (ts:port s)))
((eqv? head '__FILE__) (string current-filename))
((ts:space? s)
`(macrocall ,(macroify-name head)
,@(parse-space-separated-exprs s))
(let ((call (parse-call-chain s head #t)))
(if (and (pair? call) (eq? (car call) 'call))
,@(parse-space-separated-exprs s)))
(else
(let ((call (parse-call-chain s head #t)))
(if (and (pair? call) (eq? (car call) 'call))
`(macrocall ,(macroify-name (cadr call)) ,@(cddr call))
`(macrocall ,(macroify-name call)
,@(parse-space-separated-exprs s))))))))
,@(parse-space-separated-exprs s)))))))))

;; command syntax
((eqv? t #\`)
Expand Down
6 changes: 4 additions & 2 deletions test/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
using Base.Test

include("test_sourcepath.jl")
thefname = "the fname!//\\&\0\1*"
@test include_string("include_string_test() = @__FILE__", thefname)() == Base.source_path()
thefname = "the fname!//\\&\1*"
# passing a second argument to include_string should be reflected by @__FILE__
@test include_string("test_atsign_file() = @__FILE__", thefname)() == thefname
@test include_string("test_atsign_file() = @__FILE__")() == "string"
@test include_string("Base.source_path()", thefname) == Base.source_path()
@test basename(@__FILE__) == "loading.jl"
@test isabspath(@__FILE__)

0 comments on commit fc647fe

Please sign in to comment.