Skip to content

Commit 2b843b2

Browse files
authored
Merge pull request #200 from FluxML/qq
Define @qq
2 parents 6b3034a + 7547d12 commit 2b843b2

5 files changed

Lines changed: 42 additions & 4 deletions

File tree

.github/workflows/CI.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
fail-fast: false
2020
matrix:
2121
version:
22-
- '1.0'
22+
- '1.6'
2323
- '1'
2424
- 'nightly'
2525
os: [ubuntu-latest, macOS-latest, windows-latest]

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
name = "MacroTools"
22
uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
3-
version = "0.5.12"
3+
version = "0.5.13"
44

55
[deps]
66
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
77
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
88

99
[compat]
10-
julia = "1"
10+
julia = "1.6"
1111

1212
[extras]
1313
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

docs/src/utilities.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ MacroTools.combinearg
4343

4444
```@docs
4545
MacroTools.@q
46+
MacroTools.@qq
4647
MacroTools.isexpr
4748
MacroTools.rmlines
4849
MacroTools.unblock

src/utils.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,29 @@ macro q(ex)
3636
esc(Expr(:quote, striplines(ex)))
3737
end
3838

39+
struct Flag end
40+
41+
to_flag(ex) = MacroTools.prewalk(x->x isa LineNumberNode ? Flag() : x, ex)
42+
to_line(l::LineNumberNode, ex) = MacroTools.prewalk(x->x isa Flag ? l : x, ex)
43+
44+
"""
45+
@qq [expression]
46+
47+
Like the `quote` keyword but replace construction site line numbers with __source__.
48+
Line numbers of interpolated expressions are preserved. The result is that line numbers will be
49+
attributed to the macro usage site, instead of the macro source code.
50+
51+
Only works inside of a macro definition.
52+
53+
See also: [`@q`](@ref)
54+
"""
55+
macro qq(ex)
56+
# This was a difficult macro to write; the round-trip to Flag appears to be necessary.
57+
# Crucially, we want interpolated expressions to preserve their line numbers.
58+
esc(:($MacroTools.to_line(__source__, $(Expr(:quote, to_flag(ex))))))
59+
end
60+
61+
3962
"""
4063
isexpr(x, ts...)
4164

test/utils.jl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using MacroTools: isdef, flatten, striplines
1+
using MacroTools: isdef, flatten, striplines, @qq
22

33
@testset "utils" begin
44
ex1 = :(function foo(a) return a; end)
@@ -45,3 +45,17 @@ end
4545
@test flatten(ex) |> striplines == ex |> striplines
4646
end
4747
end
48+
49+
## Test for @qq
50+
51+
macro my_fff_def(a)
52+
@qq function fff() $a end
53+
end
54+
55+
@my_fff_def begin # line where fff() is defined
56+
function g() # line where fff()() is defined
57+
22
58+
end
59+
end
60+
61+
@test which(fff,()).line == which(fff(),()).line - 1

0 commit comments

Comments
 (0)