• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JuliaLang / julia / #37728

26 Mar 2024 03:46AM UTC coverage: 80.612% (-0.8%) from 81.423%
#37728

push

local

web-flow
Update zlib to 1.3.1 (#53841)

Released January 22, 2024

69920 of 86737 relevant lines covered (80.61%)

14456248.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.93
/base/docs/Docs.jl
1
# This file is a part of Julia. License is MIT: https://julialang.org/license
2

3
"""
4
    Docs
5

6
The `Docs` module provides the [`@doc`](@ref) macro which can be used to set and retrieve
7
documentation metadata for Julia objects.
8

9
Please see the manual section on [documentation](@ref man-documentation) for more
10
information.
11
"""
12
module Docs
13

14
@nospecialize # don't specialize on any arguments of the methods declared herein
15

16
"""
17
# Documentation
18

19
Functions, methods and types can be documented by placing a string before the definition:
20

21
    \"\"\"
22
    # The Foo Function
23
    `foo(x)`: Foo the living hell out of `x`.
24
    \"\"\"
25
    foo(x) = ...
26

27
The `@doc` macro can be used directly to both set and retrieve documentation / metadata.
28
The macro has special parsing so that the documented object may occur on the next line:
29

30
    @doc "blah"
31
    function foo() ...
32

33
By default, documentation is written as Markdown, but any object can be used as
34
the first argument.
35

36
## Documenting objects separately from their definitions
37
You can document an object before or after its definition with
38

39
    @doc "foo" function_to_doc
40
    @doc "bar" TypeToDoc
41

42
For macros, the syntax is `@doc "macro doc" :(Module.@macro)` or `@doc "macro doc"
43
:(string_macro"")` for string macros. Without the quote `:()` the expansion of the macro
44
will be documented.
45

46
## Retrieving Documentation
47
You can retrieve docs for functions, macros and other objects as follows:
48

49
    @doc foo
50
    @doc @time
51
    @doc md""
52

53
## Functions & Methods
54
Placing documentation before a method definition (e.g. `function foo() ...` or `foo() = ...`)
55
will cause that specific method to be documented, as opposed to the whole function. Method
56
docs are concatenated together in the order they were defined to provide docs for the
57
function.
58
"""
59
:(Core.@doc)
60

61
include("bindings.jl")
62

63
import .Base.Meta: quot, isexpr, unblock, unescape, uncurly
64
import .Base: Callable, with_output_color
65
using .Base: RefValue, mapany
66
import ..CoreDocs: lazy_iterpolate
67

68
export doc, hasdoc, undocumented_names
69

70
# Basic API / Storage
71

72
const modules = Module[]
73
const META    = gensym(:meta)
74
const METAType = IdDict{Any,Any}
75

76
function meta(m::Module; autoinit::Bool=true)
621,314✔
77
    if !isdefined(m, META) || getfield(m, META) === nothing
621,339✔
78
        autoinit ? initmeta(m) : return nothing
1✔
79
    end
80
    return getfield(m, META)::METAType
310,670✔
81
end
82

83
function initmeta(m::Module)
135✔
84
    if !isdefined(m, META) || getfield(m, META) === nothing
226✔
85
        Core.eval(m, :($META = $(METAType())))
44✔
86
        push!(modules, m)
44✔
87
    end
88
    nothing
×
89
end
90

91
function signature!(tv::Vector{Any}, expr::Expr)
197✔
92
    is_macrocall = isexpr(expr, :macrocall)
197✔
93
    if is_macrocall || isexpr(expr, :call)
372✔
94
        sig = :(Union{Tuple{}})
116✔
95
        first_arg = is_macrocall ? 3 : 2 # skip function arguments
116✔
96
        for arg in expr.args[first_arg:end]
116✔
97
            isexpr(arg, :parameters) && continue
107✔
98
            if isexpr(arg, :kw) # optional arg
103✔
99
                push!(sig.args, :(Tuple{$((sig.args[end]::Expr).args[2:end]...)}))
8✔
100
            end
101
            push!((sig.args[end]::Expr).args, argtype(arg))
103✔
102
        end
107✔
103
        if isexpr(expr.args[1], :curly) && isempty(tv)
116✔
104
            append!(tv, mapany(tvar, (expr.args[1]::Expr).args[2:end]))
×
105
        end
106
        for i = length(tv):-1:1
221✔
107
            push!(sig.args, :(Tuple{$((tv[i]::Expr).args[1])}))
16✔
108
        end
19✔
109
        for i = length(tv):-1:1
221✔
110
            sig = Expr(:where, sig, tv[i])
16✔
111
        end
19✔
112
        return sig
116✔
113
    elseif isexpr(expr, :where)
81✔
114
        append!(tv, mapany(tvar, expr.args[2:end]))
30✔
115
        return signature!(tv, expr.args[1])
15✔
116
    else
117
        return signature!(tv, expr.args[1])
66✔
118
    end
119
end
120
signature!(tv::Vector{Any}, @nospecialize(other)) = :(Union{})
2✔
121
signature(expr::Expr) = signature!([], expr)
117✔
122
signature(@nospecialize other) = signature!([], other)
2✔
123

124
function argtype(expr::Expr)
65✔
125
    isexpr(expr, :(::))  && return expr.args[end]
65✔
126
    isexpr(expr, :(...)) && return :(Vararg{$(argtype(expr.args[1]))})
12✔
127
    if isexpr(expr, :meta) && length(expr.args) == 2
10✔
128
        a1 = expr.args[1]
4✔
129
        if a1 === :nospecialize || a1 === :specialize
4✔
130
            return argtype(expr.args[2])
4✔
131
        end
132
    end
133
    return argtype(expr.args[1])
6✔
134
end
135
argtype(@nospecialize other) = :Any
×
136

137
tvar(x::Expr)   = x
×
138
tvar(s::Symbol) = :($s <: Any)
6✔
139

140
# Docsystem types.
141
# ================
142

143
"""
144
    Docs.DocStr
145

146
Stores the contents of a single docstring as well as related metadata.
147

148
Both the raw text, `.text`, and the parsed markdown, `.object`, are tracked by this type.
149
Parsing of the raw text is done lazily when a request is made to render the docstring,
150
which helps to reduce total precompiled image size.
151

152
The `.data` fields stores several values related to the docstring, such as: path,
153
linenumber, source code, and fielddocs.
154
"""
155
mutable struct DocStr
156
    text   :: Core.SimpleVector
129✔
157
    object :: Any
158
    data   :: Dict{Symbol, Any}
159
end
160

161
function docstr(binding::Binding, typesig = Union{})
2✔
162
    @nospecialize typesig
2✔
163
    for m in modules
2✔
164
        dict = meta(m; autoinit=false)
324✔
165
        isnothing(dict) && continue
324✔
166
        if haskey(dict, binding)
162✔
167
            docs = dict[binding].docs
2✔
168
            if haskey(docs, typesig)
2✔
169
                return docs[typesig]
2✔
170
            end
171
        end
172
    end
160✔
173
    error("could not find matching docstring for '$binding :: $typesig'.")
×
174
end
175
docstr(object, data = Dict{Symbol,Any}()) = _docstr(object, data)
136✔
176

177
_docstr(vec::Core.SimpleVector, data::Dict{Symbol,Any}) = DocStr(vec,            nothing, data)
124✔
178
_docstr(str::AbstractString,    data::Dict{Symbol,Any}) = DocStr(Core.svec(str), nothing, data)
×
179
_docstr(object,                 data::Dict{Symbol,Any}) = DocStr(Core.svec(),     object, data)
5✔
180

181
_docstr(doc::DocStr, data::Dict{Symbol,Any}) = (doc.data = merge(data, doc.data); doc)
7✔
182

183
macro ref(x)
2✔
184
    binding = bindingexpr(namify(x))
2✔
185
    typesig = signature(x)
2✔
186
    return esc(docexpr(__source__, __module__, binding, typesig))
2✔
187
end
188

189
docexpr(__source__, __module__, args...) = Expr(:call, docstr, args...)
145✔
190

191
"""
192
    MultiDoc
193

194
Stores a collection of docstrings for related objects, ie. a `Function`/`DataType` and
195
associated `Method` objects.
196

197
Each documented object in a `MultiDoc` is referred to by its signature which is represented
198
by a `Union` of `Tuple` types. For example, the following `Method` definition
199

200
    f(x, y) = ...
201

202
is stored as `Tuple{Any, Any}` in the `MultiDoc` while
203

204
    f(x::T, y = ?) where {T} = ...
205

206
is stored as `Union{Tuple{T, Any}, Tuple{T}} where T`.
207

208
Note: The `Function`/`DataType` object's signature is always `Union{}`.
209
"""
210
mutable struct MultiDoc
211
    "Ordered (via definition order) vector of object signatures."
212
    order::Vector{Type}
213
    "Documentation for each object. Keys are signatures."
214
    docs::METAType
215

216
    MultiDoc() = new(Type[], METAType())
134✔
217
end
218

219
# Docstring registration.
220
# =======================
221

222
"""
223
    Docs.doc!(__module__, binding, str, sig)
224

225
Adds a new docstring `str` to the docsystem of `__module__` for `binding` and signature `sig`.
226
"""
227
function doc!(__module__::Module, b::Binding, str::DocStr, @nospecialize sig = Union{})
140✔
228
    # Module docstrings are in the module itself
229
    if defined(b)
140✔
230
        obj = resolve(b)
132✔
231
        if isa(obj, Module)
132✔
232
            __module__ = obj
8✔
233
        end
234
    end
235
    initmeta(__module__)
134✔
236
    m = get!(meta(__module__), b, MultiDoc())
268✔
237
    if haskey(m.docs, sig)
134✔
238
        # We allow for docstrings to be updated, but print a warning since it is possible
239
        # that over-writing a docstring *may* have been accidental.  The warning
240
        # is suppressed for symbols in Main (or current active module),
241
        # for interactive use (#23011).
242
        __module__ === Base.active_module() ||
3✔
243
            @warn "Replacing docs for `$b :: $sig` in module `$(__module__)`"
244
    else
245
        # The ordering of docstrings for each Binding is defined by the order in which they
246
        # are initially added. Replacing a specific docstring does not change its ordering.
247
        push!(m.order, sig)
132✔
248
    end
249
    m.docs[sig] = str
134✔
250
    str.data[:binding] = b
134✔
251
    str.data[:typesig] = sig
134✔
252
    return b
134✔
253
end
254

255
# Docstring lookup.
256
# =================
257

258
"""
259
    getdoc(obj)
260
    getdoc(obj, sig)
261

262
Return a custom docstring object associated with the object `obj` and, optionally, the tuple
263
type signature `sig`. See `MultiDoc` docs for an explanation of the possible values of `sig`.
264

265
The returned object can either be a markdown object generated by `Markdown.parse` or some
266
other custom type used to display non-markdown formatted documentation.
267

268
A return value of `nothing` can be used to signify to the docsystem that no documentation
269
was found for `obj`, in which case the docsystem will fall back to searching for the
270
`Binding` associated with `obj` instead.
271
"""
272
function getdoc end
273

274
getdoc(@nospecialize(x), @nospecialize(sig)) = getdoc(x)
3,539✔
275
getdoc(@nospecialize(x)) = nothing
3,539✔
276

277
# Utilities.
278
# ==========
279

280
"""
281
`catdoc(xs...)`: Combine the documentation metadata `xs` into a single meta object.
282
"""
283
catdoc() = nothing
×
284
catdoc(xs...) = vcat(xs...)
×
285

286
const keywords = Dict{Symbol, DocStr}()
287

288
namify(@nospecialize x) = astname(x, isexpr(x, :macro))::Union{Symbol,Expr,GlobalRef}
294✔
289

290
function astname(x::Expr, ismacro::Bool)
404✔
291
    head = x.head
404✔
292
    if head === :.
404✔
293
        ismacro ? macroname(x) : x
82✔
294
    elseif head === :call && isexpr(x.args[1], :(::))
322✔
295
        return astname((x.args[1]::Expr).args[end], ismacro)
6✔
296
    else
297
        n = isexpr(x, (:module, :struct)) ? 2 : 1
316✔
298
        astname(x.args[n], ismacro)
316✔
299
    end
300
end
301
astname(q::QuoteNode, ismacro::Bool) = astname(q.value, ismacro)
5✔
302
astname(s::Symbol, ismacro::Bool)    = ismacro ? macroname(s) : s
208✔
303
astname(@nospecialize(other), ismacro::Bool) = other
4✔
304

305
macroname(s::Symbol) = Symbol('@', s)
10✔
306
macroname(x::Expr)   = Expr(x.head, x.args[1], macroname(x.args[end].value))
×
307

308
isfield(@nospecialize x) = isexpr(x, :.) &&
54✔
309
    (isa(x.args[1], Symbol) || isfield(x.args[1])) &&
310
    (isa(x.args[2], QuoteNode) || isexpr(x.args[2], :quote))
311

312
# @doc expression builders.
313
# =========================
314

315
"""
316
    Docs.metadata(source, module, expr, ismodule)
317

318
Build a `Dict` expression containing metadata captured from the expression `expr`.
319

320
Fields that may be included in the returned `Dict`:
321

322
- `:path`:       Symbol representing the file where `expr` is defined.
323
- `:linenumber`: Linenumber where `expr` is defined.
324
- `:module`:     Module where the docstring is defined.
325
- `:fields`:     `Dict` of all field docs found in `expr`. Only for concrete types.
326
"""
327
function metadata(__source__, __module__, expr, ismodule)
143✔
328
    args = []
143✔
329
    # Filename and linenumber of the docstring.
330
    __file__ = isa(__source__.file, Symbol) ? String(__source__.file) : ""
143✔
331
    push!(args, Pair(:path, __file__))
143✔
332
    push!(args, Pair(:linenumber, __source__.line))
143✔
333
    # Module in which the docstring is defined.
334
    if ismodule # Module docs go inside the module with name `expr`
143✔
335
        push!(args, :($Pair(:module, $expr)))
6✔
336
    else
337
        push!(args, Pair(:module, __module__))
137✔
338
    end
339
    if isexpr(expr, :struct)
143✔
340
        # Field docs for concrete types.
341
        P = Pair{Symbol,Any}
26✔
342
        fields = P[]
26✔
343
        last_docstr = nothing
×
344
        for each in (expr.args[3]::Expr).args
26✔
345
            eachex = unescape(each)
80✔
346
            if isa(eachex, Symbol) || isexpr(eachex, :(::))
145✔
347
                # a field declaration
348
                if last_docstr !== nothing
23✔
349
                    push!(fields, P(namify(eachex::Union{Symbol,Expr}), last_docstr))
20✔
350
                    last_docstr = nothing
×
351
                end
352
            elseif isexpr(eachex, :function) || isexpr(eachex, :(=))
114✔
353
                break
×
354
            elseif isa(eachex, String) || isexpr(eachex, :string) || isexpr(eachex, :call) ||
57✔
355
                (isexpr(eachex, :macrocall) && eachex.args[1] === Symbol("@doc_str"))
356
                # forms that might be doc strings
357
                last_docstr = each
×
358
            end
359
        end
80✔
360
        dict = :($(Dict{Symbol,Any})($([(:($(P)($(quot(f)), $d)))::Expr for (f, d) in fields]...)))
26✔
361
        push!(args, :($(Pair)(:fields, $dict)))
26✔
362
    end
363
    return :($(Dict{Symbol,Any})($(args...)))
143✔
364
end
365

366
function keyworddoc(__source__, __module__, str, def::Base.BaseDocs.Keyword)
×
367
    @nospecialize str
×
368
    docstr = esc(docexpr(__source__, __module__, lazy_iterpolate(str), metadata(__source__, __module__, def, false)))
×
369
    return :($setindex!($(keywords), $docstr, $(esc(quot(def.name)))); nothing)
×
370
end
371

372
function objectdoc(__source__, __module__, str, def, expr, sig = :(Union{}))
195✔
373
    @nospecialize str def expr sig
×
374
    binding = esc(bindingexpr(namify(expr)))
328✔
375
    docstr  = esc(docexpr(__source__, __module__, lazy_iterpolate(str), metadata(__source__, __module__, expr, false)))
151✔
376
    # Note: we want to avoid introducing line number nodes here (issue #24468)
377
    return Expr(:block, esc(def), :($(doc!)($__module__, $binding, $docstr, $(esc(sig)))))
135✔
378
end
379

380
function calldoc(__source__, __module__, str, def::Expr)
16✔
381
    @nospecialize str
×
382
    args = callargs(def)
16✔
383
    if isempty(args) || all(validcall, args)
31✔
384
        objectdoc(__source__, __module__, str, nothing, def, signature(def))
13✔
385
    else
386
        docerror(def)
3✔
387
    end
388
end
389
callargs(ex::Expr) = isexpr(ex, :where) ? callargs(ex.args[1]) :
22✔
390
    isexpr(ex, :call) ? ex.args[2:end] : error("Invalid expression to callargs: $ex")
391
validcall(x) = isa(x, Symbol) || isexpr(x, (:(::), :..., :kw, :parameters))
36✔
392

393
function moduledoc(__source__, __module__, meta, def, def′::Expr)
6✔
394
    @nospecialize meta def
×
395
    name  = namify(def′)
6✔
396
    docex = Expr(:call, doc!, name, bindingexpr(name),
12✔
397
        docexpr(__source__, name, lazy_iterpolate(meta), metadata(__source__, __module__, name, true)))
398
    if def === nothing
6✔
399
        esc(:(Core.eval($name, $(quot(docex)))))
×
400
    else
401
        def = unblock(def)
6✔
402
        block = def.args[3].args
6✔
403
        if !def.args[1]
6✔
404
            pushfirst!(block, :(import Base: @doc))
2✔
405
        end
406
        push!(block, docex)
6✔
407
        esc(Expr(:toplevel, def))
6✔
408
    end
409
end
410

411
# Shares a single doc, `meta`, between several expressions from the tuple expression `ex`.
412
function multidoc(__source__, __module__, meta, ex::Expr, define::Bool)
2✔
413
    @nospecialize meta
×
414
    out = Expr(:block)
2✔
415
    str = docexpr(__source__, __module__, lazy_iterpolate(meta), metadata(__source__, __module__, ex, false))
2✔
416
    ref = RefValue{DocStr}()
2✔
417
    first = true
×
418
    for arg in ex.args
2✔
419
        # The first `arg` to be documented needs to also create the docstring for the group
420
        # (after doing the action defined by the argument).
421
        # Subsequent `arg`s just need `ref` to be able to find the docstring without having
422
        # to create an entirely new one each.
423
        if first
5✔
424
            first = false
×
425
            docstr = :($getindex($setindex!($(ref), $str)))
2✔
426
        else
427
            docstr = :($getindex($(ref)))
3✔
428
        end
429
        push!(out.args, docm(__source__, __module__, docstr, arg, define))
5✔
430
    end
5✔
431
    return out
2✔
432
end
433

434
"""
435
    @__doc__(ex)
436

437
Low-level macro used to mark expressions returned by a macro that should be documented. If
438
more than one expression is marked then the same docstring is applied to each expression.
439

440
    macro example(f)
441
        quote
442
            \$(f)() = 0
443
            @__doc__ \$(f)(x) = 1
444
            \$(f)(x, y) = 2
445
        end |> esc
446
    end
447

448
`@__doc__` has no effect when a macro that uses it is not documented.
449

450
!!! compat "Julia 1.12"
451

452
    This section documents a very subtle corner case that is only relevant to
453
    macros which themselves both define other macros and then attempt to use them
454
    within the same expansion. Such macros were impossible to write prior to
455
    Julia 1.12 and are still quite rare. If you are not writing such a macro,
456
    you may ignore this note.
457

458
    In versions prior to Julia 1.12, macroexpansion would recursively expand through
459
    `Expr(:toplevel)` blocks. This behavior was changed in Julia 1.12 to allow
460
    macros to recursively define other macros and use them in the same returned
461
    expression. However, to preserve backwards compatibility with existing uses of
462
    `@__doc__`, the doc system will still expand through `Expr(:toplevel)` blocks
463
    when looking for `@__doc__` markers. As a result, macro-defining-macros will
464
    have an observable behavior difference when annotated with a docstring:
465

466
    ```julia
467
    julia> macro macroception()
468
        Expr(:toplevel, :(macro foo() 1 end), :(@foo))
469
    end
470

471
    julia> @macroception
472
    1
473

474
    julia> "Docstring" @macroception
475
    ERROR: LoadError: UndefVarError: `@foo` not defined in `Main`
476
    ```
477

478
    The supported workaround is to manually expand the `@__doc__` macro in the
479
    defining macro, which the docsystem will recognize and suppress the recursive
480
    expansion:
481

482
    ```julia
483
    julia> macro macroception()
484
        Expr(:toplevel,
485
            macroexpand(__module__, :(@__doc__ macro foo() 1 end); recursive=false),
486
            :(@foo))
487
    end
488

489
    julia> @macroception
490
    1
491

492
    julia> "Docstring" @macroception
493
    1
494
    ```
495
"""
496
:(Core.@__doc__)
497

498
function __doc__!(source, mod, meta, def, define::Bool)
499
    @nospecialize source mod meta def
×
500
    # Two cases must be handled here to avoid redefining all definitions contained in `def`:
501
    if define
14✔
502
        function replace_meta_doc(each)
22✔
503
            each.head = :macrocall
8✔
504
            each.args = Any[Symbol("@doc"), source, mod, nothing, meta, each.args[end], define]
8✔
505
        end
506

507
        # `def` has not been defined yet (this is the common case, i.e. when not generating
508
        # the Base image). We just need to convert each `@__doc__` marker to an `@doc`.
509
        found = finddoc(replace_meta_doc, mod, def; expand_toplevel = false)
20✔
510

511
        if !found
14✔
512
            found = finddoc(replace_meta_doc, mod, def; expand_toplevel = true)
14✔
513
        end
514
    else
515
        # `def` has already been defined during Base image gen so we just need to find and
516
        # document any subexpressions marked with `@__doc__`.
517
        docs  = []
×
518
        found = finddoc(mod, def; expand_toplevel = true) do each
×
519
            push!(docs, :(@doc($source, $mod, $meta, $(each.args[end]), $define)))
520
        end
521
        # If any subexpressions have been documented then replace the entire expression with
522
        # just those documented subexpressions to avoid redefining any definitions.
523
        if found
×
524
            def.head = :toplevel
×
525
            def.args = docs
×
526
        end
527
    end
528
    return found
14✔
529
end
530
# Walk expression tree `def` and call `λ` when any `@__doc__` markers are found. Returns
531
# `true` to signify that at least one `@__doc__` has been found, and `false` otherwise.
532
function finddoc(λ, mod::Module, def::Expr; expand_toplevel::Bool=false)
738✔
533
    if isexpr(def, :block, 2) && isexpr(def.args[1], :meta, 1) && (def.args[1]::Expr).args[1] === :doc
369✔
534
        # Found the macroexpansion of an `@__doc__` expression.
535
        λ(def)
8✔
536
        true
537
    else
538
        if expand_toplevel && isexpr(def, :toplevel)
361✔
539
            for i = 1:length(def.args)
1✔
540
                def.args[i] = macroexpand(mod, def.args[i])
18✔
541
            end
35✔
542
        end
543
        found = false
×
544
        for each in def.args
361✔
545
            found |= finddoc(λ, mod, each; expand_toplevel)
1,353✔
546
        end
856✔
547
        found
361✔
548
    end
549
end
550
finddoc(λ, mod::Module, @nospecialize def; expand_toplevel::Bool=false) = false
×
551

552
# Predicates and helpers for `docm` expression selection:
553

554
const FUNC_HEADS    = [:function, :macro, :(=)]
555
const BINDING_HEADS = [:const, :global, :(=)]
556
# For the special `:@mac` / `:(Base.@mac)` syntax for documenting a macro after definition.
557
isquotedmacrocall(@nospecialize x) =
27✔
558
    isexpr(x, :copyast, 1) &&
559
    isa(x.args[1], QuoteNode) &&
560
    isexpr(x.args[1].value, :macrocall, 2)
561
# Simple expressions / atoms the may be documented.
562
isbasicdoc(@nospecialize x) = isexpr(x, :.) || isa(x, Union{QuoteNode, Symbol})
14✔
563
is_signature(@nospecialize x) = isexpr(x, :call) || (isexpr(x, :(::), 2) && isexpr(x.args[1], :call)) || isexpr(x, :where)
105✔
564

565
function docm(source::LineNumberNode, mod::Module, ex)
151✔
566
    @nospecialize ex
×
567
    if isexpr(ex, :->) && length(ex.args) > 1
151✔
568
        return docm(source, mod, ex.args...)
×
569
    elseif isassigned(Base.REPL_MODULE_REF)
151✔
570
        # TODO: this is a shim to continue to allow `@doc` for looking up docstrings
571
        REPL = Base.REPL_MODULE_REF[]
151✔
572
        return invokelatest(REPL.lookup_doc, ex)
151✔
573
    end
574
    return nothing
×
575
end
576
# Drop incorrect line numbers produced by nested macro calls.
577
docm(source::LineNumberNode, mod::Module, _, _, x...) = docm(source, mod, x...)
8✔
578

579
# iscallexpr checks if an expression is a :call expression. The call expression may be
580
# also part of a :where expression, so it unwraps the :where layers until it reaches the
581
# "actual" expression
582
iscallexpr(ex::Expr) = isexpr(ex, :where) ? iscallexpr(ex.args[1]) : isexpr(ex, :call)
84✔
583
iscallexpr(ex) = false
×
584

585
function docm(source::LineNumberNode, mod::Module, meta, ex, define::Bool = true)
159✔
586
    @nospecialize meta ex
66✔
587
    # Some documented expressions may be decorated with macro calls which obscure the actual
588
    # expression. Expand the macro calls.
589
    x = macroexpand(mod, ex)
305✔
590
    return _docm(source, mod, meta, x, define)
157✔
591
end
592

593
function _docm(source::LineNumberNode, mod::Module, meta, x, define::Bool = true)
158✔
594
    if isexpr(x, :var"hygienic-scope")
373✔
595
        x.args[1] = _docm(source, mod, meta, x.args[1])
408✔
596
        return x
×
597
    elseif isexpr(x, :escape)
158✔
598
        x.args[1] = _docm(source, mod, meta, x.args[1])
×
599
        return x
×
600
    elseif isexpr(x, :block)
158✔
601
        docarg = 0
7✔
602
        for i = 1:length(x.args)
7✔
603
            isa(x.args[i], LineNumberNode) && continue
26✔
604
            if docarg == 0
13✔
605
                docarg = i
×
606
                continue
7✔
607
            end
608
            # More than one documentable expression in the block, treat it as a whole
609
            # expression, which will fall through and look for (Expr(:meta, doc))
610
            docarg = 0
×
611
            break
6✔
612
        end
39✔
613
        if docarg != 0
7✔
614
            x.args[docarg] = _docm(source, mod, meta, x.args[docarg], define)
1✔
615
            return x
1✔
616
        end
617
    end
618

619
    # Don't try to redefine expressions. This is only needed for `Base` img gen since
620
    # otherwise calling `loaddocs` would redefine all documented functions and types.
621
    def = define ? x : nothing
157✔
622
    if isa(x, GlobalRef) && (x::GlobalRef).mod == mod
157✔
623
        x = (x::GlobalRef).name
×
624
    end
625

626
    # Keywords using the `@kw_str` macro in `base/docs/basedocs.jl`.
627
    #
628
    #   "..."
629
    #   kw"if", kw"else"
630
    #
631
    doc =
645✔
632
    isa(x, Base.BaseDocs.Keyword) ? keyworddoc(source, mod, meta, x) :
633

634
    # Method / macro definitions and "call" syntax.
635
    #
636
    #   function f(...) ... end
637
    #   f(...) = ...
638
    #   macro m(...) end
639
    #   function f end
640
    #   f(...)
641
    #
642
    # Including if the "call" expression is wrapped in "where" expression(s) (#32960), i.e.
643
    #
644
    #   f(::T) where T
645
    #   f(::T, ::U) where T where U
646
    #
647
    isexpr(x, FUNC_HEADS) && is_signature((x::Expr).args[1]) ? objectdoc(source, mod, meta, def, x::Expr, signature(x::Expr)) :
648
    isexpr(x, [:function, :macro])  && !isexpr((x::Expr).args[1], :call) ? objectdoc(source, mod, meta, def, x::Expr) :
649
    iscallexpr(x) ? calldoc(source, mod, meta, x::Expr) :
650

651
    # Type definitions.
652
    #
653
    #   struct T ... end
654
    #   abstract type T end
655
    #   primitive type T N end
656
    #
657
    isexpr(x, [:struct, :abstract, :primitive]) ? objectdoc(source, mod, meta, def, x::Expr) :
658

659
    # "Bindings". Names that resolve to objects with different names, ie.
660
    #
661
    #   const T = S
662
    #   T = S
663
    #   global T = S
664
    #
665
    isexpr(x, BINDING_HEADS) && !isexpr((x::Expr).args[1], :call) ? objectdoc(source, mod, meta, def, x::Expr) :
666

667
    # Quoted macrocall syntax. `:@time` / `:(Base.@time)`.
668
    isquotedmacrocall(x) ? objectdoc(source, mod, meta, def, x) :
669
    # Modules and baremodules.
670
    isexpr(x, :module) ? moduledoc(source, mod, meta, def, x::Expr) :
671
    # Document several expressions with the same docstring. `a, b, c`.
672
    isexpr(x, :tuple) ? multidoc(source, mod, meta, x::Expr, define) :
673
    # Errors generated by calling `macroexpand` are passed back to the call site.
674
    isexpr(x, :error) ? esc(x) :
675
    # When documenting macro-generated code we look for embedded `@__doc__` calls.
676
    __doc__!(source, mod, meta, x, define) ? esc(x) :
677
    # Any "basic" expression such as a bare function or module name or numeric literal.
678
    isbasicdoc(x) ? objectdoc(source, mod, meta, nothing, x) :
679

680
    # All other expressions are undocumentable and should be handled on a case-by-case basis
681
    # with `@__doc__`. Unbound string literals are also undocumentable since they cannot be
682
    # retrieved from the module's metadata `IdDict` without a reference to the string.
683
    docerror(x)
684

685
    return doc
×
686
end
687

688
function docerror(@nospecialize ex)
5✔
689
    txt = """
6✔
690
    cannot document the following expression:
691

692
    $(isa(ex, AbstractString) ? repr(ex) : ex)"""
693
    if isexpr(ex, :macrocall)
5✔
694
        txt *= "\n\n'$(ex.args[1])' not documentable. See 'Base.@__doc__' docs for details."
×
695
    end
696
    return :($(error)($txt, "\n"))
5✔
697
end
698

699
include("utils.jl")
700

701
# Swap out the bootstrap macro with the real one.
702
Core.atdoc!(docm)
703

704
function loaddocs(docs::Vector{Core.SimpleVector})
×
705
    for (mod, ex, str, file, line) in docs
×
706
        data = Dict{Symbol,Any}(:path => string(file), :linenumber => line)
×
707
        doc = docstr(str, data)
×
708
        lno = LineNumberNode(line, file)
×
709
        docstring = docm(lno, mod, doc, ex, false) # expand the real @doc macro now
×
710
        Core.eval(mod, Expr(:var"hygienic-scope", docstring, Docs, lno))
×
711
    end
×
712
    empty!(docs)
×
713
    nothing
×
714
end
715

716
# FIXME: formatdoc, parsedoc, apropos, and doc are defined here (but only doc is exported)
717
# for historical reasons (#25738), but are *implemented* in REPL/src/docview.jl, while
718
# apropos is *exported* by InteractiveUtils and doc is exported by Docs.  Seems
719
# like a more sensible refactoring should be possible.
720

721
function formatdoc end
722
function parsedoc end
723

724
"""
725
    apropos([io::IO=stdout], pattern::Union{AbstractString,Regex})
726

727
Search available docstrings for entries containing `pattern`.
728

729
When `pattern` is a string, case is ignored. Results are printed to `io`.
730

731
`apropos` can be called from the help mode in the REPL by wrapping the query in double quotes:
732
```
733
help?> "pattern"
734
```
735
"""
736
function apropos end
737

738
"""
739
    Docs.doc(binding, sig)
740

741
Return all documentation that matches both `binding` and `sig`.
742

743
If `getdoc` returns a non-`nothing` result on the value of the binding, then a
744
dynamic docstring is returned instead of one based on the binding itself.
745
"""
746
function doc end
747

748
"""
749
    Docs.hasdoc(mod::Module, sym::Symbol)::Bool
750

751
Return `true` if `sym` in `mod` has a docstring and `false` otherwise.
752
"""
753
hasdoc(mod::Module, sym::Symbol) = hasdoc(Docs.Binding(mod, sym))
3,783✔
754
function hasdoc(binding::Docs.Binding, sig::Type = Union{})
3,839✔
755
    # this function is based on the Base.Docs.doc method implemented
756
    # in REPL/src/docview.jl.  TODO: refactor and unify these methods.
757
    defined(binding) && !isnothing(getdoc(resolve(binding), sig)) && return true
7,622✔
758
    for mod in modules
3,839✔
759
        dict = meta(mod; autoinit=false)
545,386✔
760
        !isnothing(dict) && haskey(dict, binding) && return true
545,386✔
761
    end
272,071✔
762
    alias = aliasof(binding)
3,217✔
763
    return alias == binding ? false : hasdoc(alias, sig)
3,217✔
764
end
765

766

767
"""
768
    undocumented_names(mod::Module; private=false)
769

770
Return a sorted vector of undocumented symbols in `module` (that is, lacking docstrings).
771
`private=false` (the default) returns only identifiers declared with `public` and/or
772
`export`, whereas `private=true` returns all symbols in the module (excluding
773
compiler-generated hidden symbols starting with `#`).
774

775
See also: [`names`](@ref), [`Docs.hasdoc`](@ref), [`Base.ispublic`](@ref).
776
"""
777
function undocumented_names(mod::Module; private::Bool=false)
30✔
778
    filter!(names(mod; all=true)) do sym
30✔
779
        !hasdoc(mod, sym) && !startswith(string(sym), '#') &&
3,779✔
780
            (private || Base.ispublic(mod, sym))
781
    end
782
end
783

784
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc