Skip to content

Commit

Permalink
Excape code more carefully
Browse files Browse the repository at this point in the history
  • Loading branch information
jw3126 committed Oct 26, 2017
1 parent 9b4288b commit bea36b8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
24 changes: 11 additions & 13 deletions src/ArgCheck.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ end
```
"""
macro argcheck(code,args...)
esc(argcheck(code, args...))
argcheck(code, args...)
end

function argcheck(ex, args...)
Expand All @@ -35,17 +35,17 @@ end

function argcheck_fallback(ex, args...)
quote
if !($ex)
err = ArgCheck.build_error($(QuoteNode(ex)), $(args...))
if !$(esc(ex))
err = ArgCheck.build_error($(QuoteNode(ex)), $(esc.(args)...))
throw(err)
end
end
end

function argcheck_call(ex, args...)
variables = map(i -> gensym("v$i"), 1:length(ex.args))
variables = [gensym() for _ in 1:length(ex.args)]
assignments = map(variables, ex.args) do vi, exi
Expr(:(=), vi, exi)
Expr(:(=), vi, esc(exi))
end
condition = Expr(:call, variables...)
values = :([$(variables...)])
Expand All @@ -54,7 +54,7 @@ function argcheck_call(ex, args...)
QuoteNode(ex),
QuoteNode(ex.args),
values,
args...
esc.(args)...
)
quote
$(assignments...)
Expand All @@ -64,28 +64,27 @@ function argcheck_call(ex, args...)
end
end


function argcheck_comparison(ex, args...)
exprs = ex.args[1:2:end]
ops = ex.args[2:2:end]
variables = map(gensym, [string("v$i") for i in 1:length(exprs)])
variables = [gensym() for _ in 1:length(exprs)]
ret = quote end
rhs = exprs[1]
vrhs = variables[1]
assignment = Expr(:(=), vrhs, rhs)
assignment = Expr(:(=), vrhs, esc(rhs))
push!(ret.args, assignment)
for i in eachindex(ops)
op = ops[i]
lhs = rhs
vlhs = vrhs
rhs = exprs[i+1]
vrhs = variables[i+1]
assignment = Expr(:(=), vrhs, rhs)
condition = Expr(:call, op, vlhs, vrhs)
assignment = Expr(:(=), vrhs, esc(rhs))
condition = Expr(:call, esc(op), vlhs, vrhs)
code = Expr(:call, op, lhs, rhs)
err = Expr(:call, :(ArgCheck.build_error_comparison),
QuoteNode(code), QuoteNode(lhs), QuoteNode(rhs),
vlhs, vrhs, args...)
vlhs, vrhs, esc.(args)...)
reti = quote
$assignment
if !($condition)
Expand All @@ -97,7 +96,6 @@ function argcheck_comparison(ex, args...)
ret
end


function build_error(code, T::Type{<:Exception}, args...)
ret = T(args...)
warn("`@argcheck condition $T $(join(args, ' ')...)` is deprecated. Use `@argcheck condition $ret` instead")
Expand Down
10 changes: 10 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import ArgCheck: is_comparison_call, canonicalize
@test is_comparison_call(:(1==2))
@test is_comparison_call(:(f(2x) + 1 f(x)))
@test is_comparison_call(:(<(2,3)))
@test is_comparison_call(:(1 2))
@test !is_comparison_call(:(f(1,1)))

ex = :(x1 < x2)
Expand Down Expand Up @@ -114,6 +115,15 @@ end
@test contains(msg, "z")
@test contains(msg, "<")
@test !contains(msg, string(x))

(a,b) = false
err = @catch_exception_object @argcheck x y z
msg = err.msg
@test contains(msg, "x")
@test contains(msg, "y")
@test contains(msg, string(x))
@test contains(msg, string(y))
@test contains(msg, "")
end

@testset "error message call" begin
Expand Down

0 comments on commit bea36b8

Please sign in to comment.