From 7b8b3de9d1c8ee042929d0b3b5d873e58ca29415 Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Sun, 13 Jun 2021 22:22:15 +0900 Subject: [PATCH 1/3] Fix #9330: versionchanged causes error during pdf build The versionchanged directive breaks doctree if its contents start with non-paragraph element (ex. lists, tables, and so on). It causes build error in LaTeX. This inserts a paragraph if the first child of the contents is not a paragraph not to break doctree. --- CHANGES | 2 ++ sphinx/domains/changeset.py | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/CHANGES b/CHANGES index 6710a63a5..9d7733187 100644 --- a/CHANGES +++ b/CHANGES @@ -16,6 +16,8 @@ Features added Bugs fixed ---------- +* #9330: changeset domain: :rst:dir:`versionchanged` with contents being a list + will cause error during pdf build * #9313: LaTeX: complex table with merged cells broken since 4.0 Testing diff --git a/sphinx/domains/changeset.py b/sphinx/domains/changeset.py index 1b31e0248..81c81c288 100644 --- a/sphinx/domains/changeset.py +++ b/sphinx/domains/changeset.py @@ -74,8 +74,10 @@ class VersionChange(SphinxDirective): if self.content: self.state.nested_parse(self.content, self.content_offset, node) classes = ['versionmodified', versionlabel_classes[self.name]] - if len(node): - if isinstance(node[0], nodes.paragraph) and node[0].rawsource: + if len(node) > 0 and isinstance(node[0], nodes.paragraph): + # the contents start with a paragraph + if node[0].rawsource: + # make the first paragraph translatable content = nodes.inline(node[0].rawsource, translatable=True) content.source = node[0].source content.line = node[0].line @@ -84,10 +86,16 @@ class VersionChange(SphinxDirective): para = cast(nodes.paragraph, node[0]) para.insert(0, nodes.inline('', '%s: ' % text, classes=classes)) - else: + elif len(node) > 0: + # the contents do not starts with a paragraph para = nodes.paragraph('', '', - nodes.inline('', '%s.' % text, - classes=classes), + nodes.inline('', '%s: ' % text, classes=classes), + translatable=False) + node.insert(0, para) + else: + # the contents are empty + para = nodes.paragraph('', '', + nodes.inline('', '%s.' % text, classes=classes), translatable=False) node.append(para) From ef9b55c9616b957bda1431e331bb17a2edab36e2 Mon Sep 17 00:00:00 2001 From: jfbu Date: Sun, 13 Jun 2021 18:34:06 +0200 Subject: [PATCH 2/3] Fix #9305 LaTeX: backslash in sphinxupquote error with Japanese --- CHANGES | 2 ++ sphinx/texinputs/sphinxlatexliterals.sty | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index 9d7733187..9a7f854e3 100644 --- a/CHANGES +++ b/CHANGES @@ -19,6 +19,8 @@ Bugs fixed * #9330: changeset domain: :rst:dir:`versionchanged` with contents being a list will cause error during pdf build * #9313: LaTeX: complex table with merged cells broken since 4.0 +* #9305: LaTeX: backslash may cause Improper discretionary list pdf build error + with Japanese engines Testing -------- diff --git a/sphinx/texinputs/sphinxlatexliterals.sty b/sphinx/texinputs/sphinxlatexliterals.sty index 4d0312fc6..d2ba89ea7 100644 --- a/sphinx/texinputs/sphinxlatexliterals.sty +++ b/sphinx/texinputs/sphinxlatexliterals.sty @@ -765,7 +765,8 @@ % break at . , ; ? ! / \sphinxbreaksviaactive % break also at \ - \let\sphinx@textbackslash\textbackslash + \setbox8=\hbox{\textbackslash}% + \def\sphinx@textbackslash{\copy8}% \let\textbackslash\sphinxtextbackslash % by default, no continuation symbol on next line but may be added \let\sphinxafterbreak\sphinxafterbreakofinlineliteral From 50f5280603fa9c66253f13e53dd7e1b4a0263447 Mon Sep 17 00:00:00 2001 From: Jakob Lykke Andersen Date: Wed, 16 Jun 2021 22:08:30 +0200 Subject: [PATCH 3/3] C, keyword changes - Add new keywords from C23. - Add c_extra_keywords as confval. - Move macro names from keywords to c_extra_keywords. Fixes sphinx-doc/sphinx#9354 --- CHANGES | 6 ++++++ doc/usage/configuration.rst | 8 ++++++++ sphinx/domains/c.py | 26 ++++++++++++++++++++++---- tests/test_domain_c.py | 25 ++++++++++++++++++------- 4 files changed, 54 insertions(+), 11 deletions(-) diff --git a/CHANGES b/CHANGES index 9a7f854e3..62e1f60ee 100644 --- a/CHANGES +++ b/CHANGES @@ -13,6 +13,10 @@ Deprecated Features added -------------- +* C, add C23 keywords ``_Decimal32``, ``_Decimal64``, and ``_Decimal128``. +* #9354: C, add :confval:`c_extra_keywords` to allow user-defined keywords + during parsing. + Bugs fixed ---------- @@ -21,6 +25,8 @@ Bugs fixed * #9313: LaTeX: complex table with merged cells broken since 4.0 * #9305: LaTeX: backslash may cause Improper discretionary list pdf build error with Japanese engines +* #9354: C, remove special macro names from the keyword list. + See also :confval:`c_extra_keywords`. Testing -------- diff --git a/doc/usage/configuration.rst b/doc/usage/configuration.rst index a233e0fda..3c58da308 100644 --- a/doc/usage/configuration.rst +++ b/doc/usage/configuration.rst @@ -2670,6 +2670,14 @@ Options for the C domain .. versionadded:: 3.0 +.. confval:: c_extra_keywords + + A list of identifiers to be recognized as keywords by the C parser. + It defaults to ``['alignas', 'alignof', 'bool', 'complex', 'imaginary', + 'noreturn', 'static_assert', 'thread_local']``. + + .. versionadded:: 4.0.3 + .. confval:: c_allow_pre_v3 A boolean (default ``False``) controlling whether to parse and try to diff --git a/sphinx/domains/c.py b/sphinx/domains/c.py index b0711f68e..6ebfe04c4 100644 --- a/sphinx/domains/c.py +++ b/sphinx/domains/c.py @@ -54,10 +54,15 @@ _keywords = [ 'else', 'enum', 'extern', 'float', 'for', 'goto', 'if', 'inline', 'int', 'long', 'register', 'restrict', 'return', 'short', 'signed', 'sizeof', 'static', 'struct', 'switch', 'typedef', 'union', 'unsigned', 'void', 'volatile', 'while', - '_Alignas', 'alignas', '_Alignof', 'alignof', '_Atomic', '_Bool', 'bool', - '_Complex', 'complex', '_Generic', '_Imaginary', 'imaginary', - '_Noreturn', 'noreturn', '_Static_assert', 'static_assert', - '_Thread_local', 'thread_local', + '_Alignas', '_Alignof', '_Atomic', '_Bool', '_Complex', + '_Decimal32', '_Decimal64', '_Decimal128', + '_Generic', '_Imaginary', '_Noreturn', '_Static_assert', '_Thread_local', +] +# These are only keyword'y when the corresponding headers are included. +# They are used as default value for c_extra_keywords. +_macroKeywords = [ + 'alignas', 'alignof', 'bool', 'complex', 'imaginary', 'noreturn', 'static_assert', + 'thread_local', ] # these are ordered by preceedence @@ -2535,6 +2540,12 @@ class DefinitionParser(BaseParser): if identifier in _keywords: self.fail("Expected identifier in nested name, " "got keyword: %s" % identifier) + if self.matched_text in self.config.c_extra_keywords: + msg = "Expected identifier, got user-defined keyword: %s." \ + + " Remove it from c_extra_keywords to allow it as identifier.\n" \ + + "Currently c_extra_keywords is %s." + self.fail(msg % (self.matched_text, + str(self.config.c_extra_keywords))) ident = ASTIdentifier(identifier) names.append(ident) @@ -2711,6 +2722,12 @@ class DefinitionParser(BaseParser): if self.matched_text in _keywords: self.fail("Expected identifier, " "got keyword: %s" % self.matched_text) + if self.matched_text in self.config.c_extra_keywords: + msg = "Expected identifier, got user-defined keyword: %s." \ + + " Remove it from c_extra_keywords to allow it as identifier.\n" \ + + "Currently c_extra_keywords is %s." + self.fail(msg % (self.matched_text, + str(self.config.c_extra_keywords))) identifier = ASTIdentifier(self.matched_text) declId = ASTNestedName([identifier], rooted=False) else: @@ -3877,6 +3894,7 @@ def setup(app: Sphinx) -> Dict[str, Any]: app.add_domain(CDomain) app.add_config_value("c_id_attributes", [], 'env') app.add_config_value("c_paren_attributes", [], 'env') + app.add_config_value("c_extra_keywords", _macroKeywords, 'env') app.add_post_transform(AliasTransform) app.add_config_value("c_allow_pre_v3", False, 'env') diff --git a/tests/test_domain_c.py b/tests/test_domain_c.py index ef4858786..a891e18e2 100644 --- a/tests/test_domain_c.py +++ b/tests/test_domain_c.py @@ -15,16 +15,20 @@ import pytest from sphinx import addnodes from sphinx.addnodes import desc -from sphinx.domains.c import DefinitionError, DefinitionParser, Symbol, _id_prefix, _max_id +from sphinx.domains.c import (DefinitionError, DefinitionParser, Symbol, _id_prefix, + _macroKeywords, _max_id) from sphinx.ext.intersphinx import load_mappings, normalize_intersphinx_mapping from sphinx.testing import restructuredtext from sphinx.testing.util import assert_node +class Config: + c_id_attributes = ["id_attr", 'LIGHTGBM_C_EXPORT'] + c_paren_attributes = ["paren_attr"] + c_extra_keywords = _macroKeywords + + def parse(name, string): - class Config: - c_id_attributes = ["id_attr", 'LIGHTGBM_C_EXPORT'] - c_paren_attributes = ["paren_attr"] parser = DefinitionParser(string, location=None, config=Config()) parser.allowFallbackExpressionParsing = False ast = parser.parse_declaration(name, name) @@ -114,9 +118,6 @@ def check(name, input, idDict, output=None, key=None, asTextOutput=None): def test_expressions(): def exprCheck(expr, output=None): - class Config: - c_id_attributes = ["id_attr"] - c_paren_attributes = ["paren_attr"] parser = DefinitionParser(expr, location=None, config=Config()) parser.allowFallbackExpressionParsing = False ast = parser.parse_expression() @@ -522,6 +523,16 @@ def test_attributes(): check('function', 'LIGHTGBM_C_EXPORT int LGBM_BoosterFree(int handle)', {1: 'LGBM_BoosterFree'}) + +def test_extra_keywords(): + with pytest.raises(DefinitionError, + match='Expected identifier, got user-defined keyword: complex.'): + parse('function', 'void f(int complex)') + with pytest.raises(DefinitionError, + match='Expected identifier, got user-defined keyword: complex.'): + parse('function', 'void complex(void)') + + # def test_print(): # # used for getting all the ids out for checking # for a in ids: