2016-05-19 04:30:30 +08:00
|
|
|
# coding=utf-8
|
|
|
|
#
|
|
|
|
# Copyright © 2016 Intel Corporation
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
# copy of this software and associated documentation files (the "Software"),
|
|
|
|
# to deal in the Software without restriction, including without limitation
|
|
|
|
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
# and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
# Software is furnished to do so, subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice (including the next
|
|
|
|
# paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
# Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
# IN THE SOFTWARE.
|
|
|
|
#
|
|
|
|
# Authors:
|
|
|
|
# Jani Nikula <jani.nikula@intel.com>
|
2016-05-31 23:09:12 +08:00
|
|
|
#
|
|
|
|
# Please make sure this works on both python2 and python3.
|
|
|
|
#
|
2016-05-19 04:30:30 +08:00
|
|
|
|
2017-09-01 03:21:29 +08:00
|
|
|
import codecs
|
2016-05-19 04:30:30 +08:00
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
doc/sphinx: Track line-number of starting blocks
Design is pretty simple: kernel-doc inserts breadcrumbs with line
numbers, and sphinx picks them up. At first I went with a sphinx
comment, but inserting those at random places seriously upsets the
parser, and must be filtered. Hence why this version now uses "#define
LINEO " since one of these ever escape into output it's pretty clear
there is a bug.
It seems to work well, and at least the 2-3 errors where sphinx
complained about something that was not correct in kernel-doc text the
line numbers matched up perfectly.
v2: Instead of noodling around in the parser state machine, create
a ViewList and parse it ourselves. This seems to be the recommended
way, per Jani's suggestion.
v3:
- Split out ViewList pach. Splitting the kernel-doc changes from the
sphinx ones isn't possible, since emitting the LINENO lines wreaks
havoc with the rst formatting. We must filter them.
- Improve the regex per Jani's suggestions, and compile it just once
for speed.
- Now that LINENO lines are eaten, also add them to function parameter
descriptions. Much less content and offset than for in-line struct
member descriptions, but still nice to know which exact continuation
line upsets sphinx.
- Simplify/clarify the line +/-1 business a bit.
v4: Split out the scripts/kernel-doc changes and make line-numbers
opt-in, as suggested by Jani.
Cc: Jani Nikula <jani.nikula@intel.com>
Cc: linux-doc@vger.kernel.org
Cc: Jonathan Corbet <corbet@lwn.net>
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Signed-off-by: Jani Nikula <jani.nikula@intel.com>
2016-06-04 04:21:35 +08:00
|
|
|
import re
|
2016-06-07 17:13:56 +08:00
|
|
|
import glob
|
2016-05-19 04:30:30 +08:00
|
|
|
|
|
|
|
from docutils import nodes, statemachine
|
2016-06-02 20:59:18 +08:00
|
|
|
from docutils.statemachine import ViewList
|
2018-03-03 02:40:14 +08:00
|
|
|
from docutils.parsers.rst import directives, Directive
|
2019-05-22 04:42:34 +08:00
|
|
|
import sphinx
|
2021-02-02 07:26:25 +08:00
|
|
|
from sphinx.util.docutils import switch_source_input
|
2019-05-22 04:23:43 +08:00
|
|
|
import kernellog
|
|
|
|
|
2016-08-24 21:35:24 +08:00
|
|
|
__version__ = '1.0'
|
|
|
|
|
2016-05-19 04:30:30 +08:00
|
|
|
class KernelDocDirective(Directive):
|
|
|
|
"""Extract kernel-doc comments from the specified file"""
|
|
|
|
required_argument = 1
|
|
|
|
optional_arguments = 4
|
|
|
|
option_spec = {
|
|
|
|
'doc': directives.unchanged_required,
|
2016-06-07 17:13:56 +08:00
|
|
|
'export': directives.unchanged,
|
|
|
|
'internal': directives.unchanged,
|
2019-10-31 21:52:45 +08:00
|
|
|
'identifiers': directives.unchanged,
|
2020-09-29 17:32:18 +08:00
|
|
|
'no-identifiers': directives.unchanged,
|
2019-10-31 21:52:45 +08:00
|
|
|
'functions': directives.unchanged,
|
2016-05-19 04:30:30 +08:00
|
|
|
}
|
|
|
|
has_content = False
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
env = self.state.document.settings.env
|
doc/sphinx: Track line-number of starting blocks
Design is pretty simple: kernel-doc inserts breadcrumbs with line
numbers, and sphinx picks them up. At first I went with a sphinx
comment, but inserting those at random places seriously upsets the
parser, and must be filtered. Hence why this version now uses "#define
LINEO " since one of these ever escape into output it's pretty clear
there is a bug.
It seems to work well, and at least the 2-3 errors where sphinx
complained about something that was not correct in kernel-doc text the
line numbers matched up perfectly.
v2: Instead of noodling around in the parser state machine, create
a ViewList and parse it ourselves. This seems to be the recommended
way, per Jani's suggestion.
v3:
- Split out ViewList pach. Splitting the kernel-doc changes from the
sphinx ones isn't possible, since emitting the LINENO lines wreaks
havoc with the rst formatting. We must filter them.
- Improve the regex per Jani's suggestions, and compile it just once
for speed.
- Now that LINENO lines are eaten, also add them to function parameter
descriptions. Much less content and offset than for in-line struct
member descriptions, but still nice to know which exact continuation
line upsets sphinx.
- Simplify/clarify the line +/-1 business a bit.
v4: Split out the scripts/kernel-doc changes and make line-numbers
opt-in, as suggested by Jani.
Cc: Jani Nikula <jani.nikula@intel.com>
Cc: linux-doc@vger.kernel.org
Cc: Jonathan Corbet <corbet@lwn.net>
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Signed-off-by: Jani Nikula <jani.nikula@intel.com>
2016-06-04 04:21:35 +08:00
|
|
|
cmd = [env.config.kerneldoc_bin, '-rst', '-enable-lineno']
|
2016-05-19 04:30:30 +08:00
|
|
|
|
2020-10-04 15:44:28 +08:00
|
|
|
# Pass the version string to kernel-doc, as it needs to use a different
|
|
|
|
# dialect, depending what the C domain supports for each specific
|
|
|
|
# Sphinx versions
|
|
|
|
cmd += ['-sphinx-version', sphinx.__version__]
|
|
|
|
|
2016-05-19 04:30:30 +08:00
|
|
|
filename = env.config.kerneldoc_srctree + '/' + self.arguments[0]
|
2016-06-07 17:13:56 +08:00
|
|
|
export_file_patterns = []
|
2016-05-19 04:30:30 +08:00
|
|
|
|
|
|
|
# Tell sphinx of the dependency
|
|
|
|
env.note_dependency(os.path.abspath(filename))
|
|
|
|
|
|
|
|
tab_width = self.options.get('tab-width', self.state.document.settings.tab_width)
|
|
|
|
|
2019-10-31 21:52:45 +08:00
|
|
|
# 'function' is an alias of 'identifiers'
|
|
|
|
if 'functions' in self.options:
|
|
|
|
self.options['identifiers'] = self.options.get('functions')
|
|
|
|
|
2016-05-19 04:30:30 +08:00
|
|
|
# FIXME: make this nicer and more robust against errors
|
|
|
|
if 'export' in self.options:
|
|
|
|
cmd += ['-export']
|
2016-06-07 17:13:56 +08:00
|
|
|
export_file_patterns = str(self.options.get('export')).split()
|
2016-05-19 04:30:30 +08:00
|
|
|
elif 'internal' in self.options:
|
|
|
|
cmd += ['-internal']
|
2016-06-07 17:13:56 +08:00
|
|
|
export_file_patterns = str(self.options.get('internal')).split()
|
2016-05-19 04:30:30 +08:00
|
|
|
elif 'doc' in self.options:
|
|
|
|
cmd += ['-function', str(self.options.get('doc'))]
|
2019-10-31 21:52:45 +08:00
|
|
|
elif 'identifiers' in self.options:
|
|
|
|
identifiers = self.options.get('identifiers').split()
|
|
|
|
if identifiers:
|
|
|
|
for i in identifiers:
|
|
|
|
cmd += ['-function', i]
|
2018-06-30 05:05:10 +08:00
|
|
|
else:
|
|
|
|
cmd += ['-no-doc-sections']
|
2016-05-19 04:30:30 +08:00
|
|
|
|
2020-09-29 17:32:18 +08:00
|
|
|
if 'no-identifiers' in self.options:
|
|
|
|
no_identifiers = self.options.get('no-identifiers').split()
|
|
|
|
if no_identifiers:
|
|
|
|
for i in no_identifiers:
|
|
|
|
cmd += ['-nosymbol', i]
|
|
|
|
|
2016-06-07 17:13:56 +08:00
|
|
|
for pattern in export_file_patterns:
|
|
|
|
for f in glob.glob(env.config.kerneldoc_srctree + '/' + pattern):
|
|
|
|
env.note_dependency(os.path.abspath(f))
|
|
|
|
cmd += ['-export-file', f]
|
|
|
|
|
2016-05-19 04:30:30 +08:00
|
|
|
cmd += [filename]
|
|
|
|
|
|
|
|
try:
|
2019-05-22 04:23:43 +08:00
|
|
|
kernellog.verbose(env.app,
|
|
|
|
'calling kernel-doc \'%s\'' % (" ".join(cmd)))
|
2016-05-19 04:30:30 +08:00
|
|
|
|
2017-09-01 03:21:29 +08:00
|
|
|
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
2016-05-19 04:30:30 +08:00
|
|
|
out, err = p.communicate()
|
|
|
|
|
2017-09-01 03:21:29 +08:00
|
|
|
out, err = codecs.decode(out, 'utf-8'), codecs.decode(err, 'utf-8')
|
2016-05-19 04:30:30 +08:00
|
|
|
|
|
|
|
if p.returncode != 0:
|
|
|
|
sys.stderr.write(err)
|
|
|
|
|
2019-05-22 04:23:43 +08:00
|
|
|
kernellog.warn(env.app,
|
|
|
|
'kernel-doc \'%s\' failed with return code %d' % (" ".join(cmd), p.returncode))
|
2016-05-19 04:30:30 +08:00
|
|
|
return [nodes.error(None, nodes.paragraph(text = "kernel-doc missing"))]
|
|
|
|
elif env.config.kerneldoc_verbosity > 0:
|
|
|
|
sys.stderr.write(err)
|
|
|
|
|
|
|
|
lines = statemachine.string2lines(out, tab_width, convert_whitespace=True)
|
doc/sphinx: Track line-number of starting blocks
Design is pretty simple: kernel-doc inserts breadcrumbs with line
numbers, and sphinx picks them up. At first I went with a sphinx
comment, but inserting those at random places seriously upsets the
parser, and must be filtered. Hence why this version now uses "#define
LINEO " since one of these ever escape into output it's pretty clear
there is a bug.
It seems to work well, and at least the 2-3 errors where sphinx
complained about something that was not correct in kernel-doc text the
line numbers matched up perfectly.
v2: Instead of noodling around in the parser state machine, create
a ViewList and parse it ourselves. This seems to be the recommended
way, per Jani's suggestion.
v3:
- Split out ViewList pach. Splitting the kernel-doc changes from the
sphinx ones isn't possible, since emitting the LINENO lines wreaks
havoc with the rst formatting. We must filter them.
- Improve the regex per Jani's suggestions, and compile it just once
for speed.
- Now that LINENO lines are eaten, also add them to function parameter
descriptions. Much less content and offset than for in-line struct
member descriptions, but still nice to know which exact continuation
line upsets sphinx.
- Simplify/clarify the line +/-1 business a bit.
v4: Split out the scripts/kernel-doc changes and make line-numbers
opt-in, as suggested by Jani.
Cc: Jani Nikula <jani.nikula@intel.com>
Cc: linux-doc@vger.kernel.org
Cc: Jonathan Corbet <corbet@lwn.net>
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Signed-off-by: Jani Nikula <jani.nikula@intel.com>
2016-06-04 04:21:35 +08:00
|
|
|
result = ViewList()
|
|
|
|
|
|
|
|
lineoffset = 0;
|
|
|
|
line_regex = re.compile("^#define LINENO ([0-9]+)$")
|
|
|
|
for line in lines:
|
|
|
|
match = line_regex.search(line)
|
|
|
|
if match:
|
|
|
|
# sphinx counts lines from 0
|
|
|
|
lineoffset = int(match.group(1)) - 1
|
|
|
|
# we must eat our comments since the upset the markup
|
|
|
|
else:
|
2020-09-28 17:22:12 +08:00
|
|
|
doc = env.srcdir + "/" + env.docname + ":" + str(self.lineno)
|
|
|
|
result.append(line, doc + ": " + filename, lineoffset)
|
doc/sphinx: Track line-number of starting blocks
Design is pretty simple: kernel-doc inserts breadcrumbs with line
numbers, and sphinx picks them up. At first I went with a sphinx
comment, but inserting those at random places seriously upsets the
parser, and must be filtered. Hence why this version now uses "#define
LINEO " since one of these ever escape into output it's pretty clear
there is a bug.
It seems to work well, and at least the 2-3 errors where sphinx
complained about something that was not correct in kernel-doc text the
line numbers matched up perfectly.
v2: Instead of noodling around in the parser state machine, create
a ViewList and parse it ourselves. This seems to be the recommended
way, per Jani's suggestion.
v3:
- Split out ViewList pach. Splitting the kernel-doc changes from the
sphinx ones isn't possible, since emitting the LINENO lines wreaks
havoc with the rst formatting. We must filter them.
- Improve the regex per Jani's suggestions, and compile it just once
for speed.
- Now that LINENO lines are eaten, also add them to function parameter
descriptions. Much less content and offset than for in-line struct
member descriptions, but still nice to know which exact continuation
line upsets sphinx.
- Simplify/clarify the line +/-1 business a bit.
v4: Split out the scripts/kernel-doc changes and make line-numbers
opt-in, as suggested by Jani.
Cc: Jani Nikula <jani.nikula@intel.com>
Cc: linux-doc@vger.kernel.org
Cc: Jonathan Corbet <corbet@lwn.net>
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Signed-off-by: Jani Nikula <jani.nikula@intel.com>
2016-06-04 04:21:35 +08:00
|
|
|
lineoffset += 1
|
2016-06-02 20:59:18 +08:00
|
|
|
|
|
|
|
node = nodes.section()
|
2019-05-22 04:42:34 +08:00
|
|
|
self.do_parse(result, node)
|
2016-06-02 20:59:18 +08:00
|
|
|
|
|
|
|
return node.children
|
|
|
|
|
2016-07-20 18:38:58 +08:00
|
|
|
except Exception as e: # pylint: disable=W0703
|
2019-05-22 04:23:43 +08:00
|
|
|
kernellog.warn(env.app, 'kernel-doc \'%s\' processing failed with: %s' %
|
|
|
|
(" ".join(cmd), str(e)))
|
2016-05-19 04:30:30 +08:00
|
|
|
return [nodes.error(None, nodes.paragraph(text = "kernel-doc missing"))]
|
|
|
|
|
2019-05-22 04:42:34 +08:00
|
|
|
def do_parse(self, result, node):
|
2021-02-02 07:26:25 +08:00
|
|
|
with switch_source_input(self.state, result):
|
|
|
|
self.state.nested_parse(result, 0, node, match_titles=1)
|
2019-05-22 04:42:34 +08:00
|
|
|
|
2016-05-19 04:30:30 +08:00
|
|
|
def setup(app):
|
|
|
|
app.add_config_value('kerneldoc_bin', None, 'env')
|
|
|
|
app.add_config_value('kerneldoc_srctree', None, 'env')
|
|
|
|
app.add_config_value('kerneldoc_verbosity', 1, 'env')
|
|
|
|
|
|
|
|
app.add_directive('kernel-doc', KernelDocDirective)
|
2016-08-24 21:35:24 +08:00
|
|
|
|
|
|
|
return dict(
|
|
|
|
version = __version__,
|
|
|
|
parallel_read_safe = True,
|
|
|
|
parallel_write_safe = True
|
|
|
|
)
|