How to determine where a macro is defined?

I figured out how to find where a method is defined: methods(generic_function).ms returns a handy array of objects which have file and line properties.

I’m now trying to do the same thing for macros. For example, Printf.@sprintf — where is it defined? methods and code_lowered don’t work, since they only understand generic functions.

Edit: Clarification: I’m looking for a general way to find the definition of something known in the Julia environment given its name. Ideally, I’d like to use a Symbol to find it, so something like methods(:+) or methods("+") (which does not work) would be preferred to methods(+).

1 Like

You can use

julia> methods(var"@info")
# 1 method for macro "@info":
[1] @info(__source__::LineNumberNode, __module__::Module, exs...) in Base.CoreLogging at logging.jl:214

Or in Julia < 1.3

julia> methods(getproperty(Main, Symbol("@info")))
# 1 method for macro "@info":
[1] @info(__source__::LineNumberNode, __module__::Module, exs...) in Base.CoreLogging at logging.jl:214

FYI, you can also do @search @info with https://github.com/tkf/InteractiveCodeSearch.jl

3 Likes

See also the macros @which,@edit

3 Likes

That works. Thank you!