[libc-commits] [libc] [libc][docs] Link C23 sections to cppreference in docgen (PR #240746)
via libc-commits
libc-commits at lists.llvm.org
Tue Nov 10 00:10:06 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Jeff Bailey (kaladron)
<details>
<summary>Changes</summary>
Header status tables currently render C23 section numbers as static badges, forcing readers to look up standard definitions manually while POSIX columns already link directly to the Open Group specification.
Add c-reference-root and optional per-symbol c-reference paths to the C23 docgen YAML catalogues so numeric C standard sections render as links to their corresponding cppreference.com pages. Non-section annotations such as glibc extension notes remain plain badges.
Assisted-by: Automated tooling, human reviewed.
---
Patch is 45.38 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/240746.diff
21 Files Affected:
- (modified) libc/docs/_static/status.css (+20-3)
- (modified) libc/utils/docgen/assert.yaml (+1)
- (modified) libc/utils/docgen/ctype.yaml (+1)
- (modified) libc/utils/docgen/docgen.py (+88-21)
- (modified) libc/utils/docgen/docgen_test.py (+125-4)
- (modified) libc/utils/docgen/errno.yaml (+4)
- (modified) libc/utils/docgen/fenv.yaml (+30)
- (modified) libc/utils/docgen/float.yaml (+3)
- (modified) libc/utils/docgen/inttypes.yaml (+7)
- (modified) libc/utils/docgen/locale.yaml (+7)
- (modified) libc/utils/docgen/setjmp.yaml (+1)
- (modified) libc/utils/docgen/signal.yaml (+10)
- (modified) libc/utils/docgen/stdbit.yaml (+88)
- (modified) libc/utils/docgen/stdio.yaml (+19)
- (modified) libc/utils/docgen/stdlib.yaml (+48)
- (modified) libc/utils/docgen/string.yaml (+2)
- (modified) libc/utils/docgen/threads.yaml (+3)
- (modified) libc/utils/docgen/time.yaml (+1)
- (modified) libc/utils/docgen/uchar.yaml (+1)
- (modified) libc/utils/docgen/wchar.yaml (+39)
- (modified) libc/utils/docgen/wctype.yaml (+1)
``````````diff
diff --git a/libc/docs/_static/status.css b/libc/docs/_static/status.css
index f1828153559c8d..1b4a30df5e8b40 100644
--- a/libc/docs/_static/status.css
+++ b/libc/docs/_static/status.css
@@ -132,21 +132,38 @@ body[data-theme="dark"] {
white-space: nowrap;
}
+.hc-c-link,
.hc-posix-link {
display: inline-flex;
align-items: center;
gap: 3px;
font-size: 0.76rem;
- font-weight: 500;
color: var(--hc-blue-text);
background: var(--hc-blue-bg);
border: 1px solid var(--hc-blue-border);
- padding: 1px 7px;
- border-radius: 999px;
text-decoration: none;
white-space: nowrap;
}
+.hc-c-link {
+ font-family: var(--font-stack--monospace, monospace);
+ padding: 1px 6px;
+ border-radius: 4px;
+}
+
+.hc-posix-link {
+ font-weight: 500;
+ padding: 1px 7px;
+ border-radius: 999px;
+}
+
+.hc-c-link:hover,
.hc-posix-link:hover {
text-decoration: underline;
}
+
+.hc-c-link:focus-visible,
+.hc-posix-link:focus-visible {
+ outline: 2px solid var(--hc-blue-text);
+ outline-offset: 2px;
+}
diff --git a/libc/utils/docgen/assert.yaml b/libc/utils/docgen/assert.yaml
index 0afd4e2d3c063f..58175aceb2fc3d 100644
--- a/libc/utils/docgen/assert.yaml
+++ b/libc/utils/docgen/assert.yaml
@@ -1,3 +1,4 @@
+c-reference-root: error/assert
macros:
__STDC_VERSION_ASSERT_H__:
c-definition: 7.2.1
diff --git a/libc/utils/docgen/ctype.yaml b/libc/utils/docgen/ctype.yaml
index 027d8f38c71f97..2f855dc2b6e242 100644
--- a/libc/utils/docgen/ctype.yaml
+++ b/libc/utils/docgen/ctype.yaml
@@ -1,3 +1,4 @@
+c-reference-root: string/byte
functions:
isalnum:
c-definition: 7.4.1.1
diff --git a/libc/utils/docgen/docgen.py b/libc/utils/docgen/docgen.py
index 3714eca6254d8c..f9f019eb595025 100755
--- a/libc/utils/docgen/docgen.py
+++ b/libc/utils/docgen/docgen.py
@@ -8,13 +8,16 @@
# ===----------------------------------------------------------------------===##
from argparse import ArgumentParser, Namespace
import html
-from pathlib import Path
-from typing import Callable, Dict
import os
+from pathlib import Path
+import re
import sys
+from typing import Callable, Dict, Optional
from header import Header
+_CPPREFERENCE_BASE = "https://en.cppreference.com/w/c"
+
class DocgenAPIFormatError(Exception):
"""Raised on fatal formatting errors with a description of a formatting error"""
@@ -51,6 +54,8 @@ def check_api(header: Header, api: Dict):
:param api: docgen yaml file contents parsed into a dict
"""
errors = []
+ if "c-reference-root" in api and not isinstance(api["c-reference-root"], str):
+ errors.append("error: c-reference-root must be a string")
# We require entries to have at least one of these.
possible_keys = [
"c-definition",
@@ -75,6 +80,8 @@ def check_api(header: Header, api: Dict):
if not any(k in obj for k in possible_keys):
err = f"error: Macro {name} does not contain at least one required property: {possible_keys}"
errors.append(err)
+ if "c-reference" in obj and not isinstance(obj["c-reference"], str):
+ errors.append(f"error: Macro {name} c-reference must be a string")
# Validate functions
if "functions" in api:
@@ -91,6 +98,8 @@ def check_api(header: Header, api: Dict):
if not any(k in obj for k in possible_keys):
err = f"error: function {name} does not contain at least one required property: {possible_keys}"
errors.append(err)
+ if "c-reference" in obj and not isinstance(obj["c-reference"], str):
+ errors.append(f"error: function {name} c-reference must be a string")
if errors:
raise DocgenAPIFormatError("\n".join(errors))
@@ -110,29 +119,84 @@ def _status_badge_html(implemented: bool) -> str:
)
-def _c_section_html(item: Dict) -> str:
- sec = item.get("c-definition")
- if sec:
- return f'<span class="hc-sec-badge">{html.escape(str(sec))}</span>'
- return '<span class="hc-status-no" aria-label="Not in C23">–</span>'
-
-
-def _posix_link_html(header: Header, name: str, kind: str, item: Dict) -> str:
- base = (
- f"functions/{name}.html"
- if kind == "function"
+def cppreference_url(
+ header: Header,
+ symbol_name: str = "",
+ kind: str = "function",
+ item: Optional[Dict] = None,
+ api: Optional[Dict] = None,
+) -> str:
+ if item and "c-reference" in item:
+ ref = item["c-reference"]
+ return f"{_CPPREFERENCE_BASE}/{ref}" if ref else ""
+ root = (api if api is not None else header.api).get("c-reference-root")
+ if not root:
+ return ""
+ if (
+ not symbol_name
+ or symbol_name.startswith("__")
+ or kind not in ("function", "family")
+ ):
+ return f"{_CPPREFERENCE_BASE}/{root}"
+ return f"{_CPPREFERENCE_BASE}/{root}/{symbol_name}"
+
+
+def posix_url(header: Header, symbol_name: str, kind: str, item: Dict) -> str:
+ subpath = (
+ f"functions/{symbol_name}.html"
+ if kind in ("function", "family") and symbol_name
else f"basedefs/{header.name.replace('/', '_')}.html"
)
if "in-latest-posix" in item:
- url = f"https://pubs.opengroup.org/onlinepubs/9799919799/{base}"
- return f'<a class="hc-posix-link" href="{url}">POSIX.1-2024 ↗</a>'
+ return f"https://pubs.opengroup.org/onlinepubs/9799919799/{subpath}"
if "removed-in-posix-2008" in item:
- url = f"https://pubs.opengroup.org/onlinepubs/007904875/{base}"
- return f'<a class="hc-posix-link" href="{url}">Removed (2008) ↗</a>'
+ return f"https://pubs.opengroup.org/onlinepubs/007904875/{subpath}"
if "removed-in-posix-2024" in item:
- url = f"https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/{base}"
- return f'<a class="hc-posix-link" href="{url}">Removed (2024) ↗</a>'
- return '<span class="hc-status-no" aria-label="Not in POSIX">–</span>'
+ return f"https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/{subpath}"
+ return ""
+
+
+_C_SECTION_RE = re.compile(r"^([0-9]+|[FGHJK])\.")
+
+
+def _c_section_html(
+ header: Header,
+ name: str,
+ kind: str,
+ item: Dict,
+ api: Optional[Dict] = None,
+) -> str:
+ sec = item.get("c-definition")
+ if not sec:
+ return '<span class="hc-status-no" aria-label="Not in C23">–</span>'
+ label = html.escape(str(sec))
+ if _C_SECTION_RE.match(str(sec)):
+ url = cppreference_url(header, name, kind, item, api)
+ if url:
+ href = html.escape(url, quote=True)
+ return (
+ f'<a class="hc-c-link" href="{href}" '
+ f'title="C23 section {label} on cppreference.com">'
+ f'{label} <span aria-hidden="true">↗</span></a>'
+ )
+ return f'<span class="hc-sec-badge">{label}</span>'
+
+
+def _posix_link_html(header: Header, name: str, kind: str, item: Dict) -> str:
+ url = posix_url(header, name, kind, item)
+ if not url:
+ return '<span class="hc-status-no" aria-label="Not in POSIX">–</span>'
+ if "removed-in-posix-2008" in item:
+ label = "Removed (2008)"
+ elif "removed-in-posix-2024" in item:
+ label = "Removed (2024)"
+ else:
+ label = "POSIX.1-2024"
+ href = html.escape(url, quote=True)
+ return (
+ f'<a class="hc-posix-link" href="{href}">'
+ f'{label} <span aria-hidden="true">↗</span></a>'
+ )
def print_symbol_table_md(
@@ -142,6 +206,7 @@ def print_symbol_table_md(
items: Dict,
is_implemented_fn: Callable[[str], bool],
kind: str,
+ api: Optional[Dict] = None,
):
print(
f"## {section_title}\n\n```{{raw}} html\n"
@@ -156,7 +221,7 @@ def print_symbol_table_md(
obj = items[name] or {}
row_id = html.escape(f"{kind}-{name.replace(' ', '-')}", quote=True)
status = _status_badge_html(is_implemented_fn(name))
- c_sec = _c_section_html(obj)
+ c_sec = _c_section_html(header, name, kind, obj, api)
posix = _posix_link_html(header, name, kind, obj)
print(
f' <tr id="{row_id}">'
@@ -179,6 +244,7 @@ def print_header_status_md(header: Header, api: Dict):
api["macros"],
header.implements_macro,
"macro",
+ api,
)
if api.get("functions"):
@@ -189,6 +255,7 @@ def print_header_status_md(header: Header, api: Dict):
api["functions"],
header.implements_fn,
"function",
+ api,
)
diff --git a/libc/utils/docgen/docgen_test.py b/libc/utils/docgen/docgen_test.py
index 27a3af3c10bea3..d68344817703a8 100644
--- a/libc/utils/docgen/docgen_test.py
+++ b/libc/utils/docgen/docgen_test.py
@@ -8,7 +8,7 @@
# ===----------------------------------------------------------------------===##
"""Unit tests for docgen and header implementation status evaluation."""
-from contextlib import redirect_stdout
+from contextlib import redirect_stderr, redirect_stdout
import io
from pathlib import Path
import sys
@@ -16,7 +16,7 @@
import unittest
sys.path.insert(0, str(Path(__file__).parent))
-from docgen import print_header_status_md
+from docgen import check_api, cppreference_url, posix_url, print_header_status_md
from header import Header, _is_stub_file
@@ -78,10 +78,20 @@ def test_implements_macro(self):
def test_md_output_generation(self):
hdr = Header("stdio.h")
api = {
+ "c-reference-root": "io",
"macros": {"EOF": {"c-definition": "7.23.1", "in-latest-posix": None}},
"functions": {
- "printf": {"c-definition": "7.23.6.3", "in-latest-posix": None},
+ "printf": {
+ "c-definition": "7.23.6.3",
+ "c-reference": "io/fprintf",
+ "in-latest-posix": None,
+ },
+ "printf_s": {
+ "c-definition": "K.3.5.3.3",
+ "c-reference": "io/fprintf",
+ },
"missing_fn": {"c-definition": "7.23.6.99"},
+ "gnu_fn": {"c-definition": "TODO: glibc extension"},
},
}
buf = io.StringIO()
@@ -97,7 +107,9 @@ def test_md_output_generation(self):
any(
'id="macro-EOF"' in l
and 'title="Implemented"' in l
+ and 'href="https://en.cppreference.com/w/c/io"' in l
and "basedefs/stdio.h.html" in l
+ and '<span aria-hidden="true">↗</span>' in l
for l in lines
)
)
@@ -105,16 +117,125 @@ def test_md_output_generation(self):
any(
'id="function-printf"' in l
and 'title="Implemented"' in l
+ and 'href="https://en.cppreference.com/w/c/io/fprintf"' in l
and "functions/printf.html" in l
for l in lines
)
)
self.assertTrue(
any(
- 'id="function-missing_fn"' in l and 'title="Not Implemented"' in l
+ 'id="function-printf_s"' in l
+ and 'href="https://en.cppreference.com/w/c/io/fprintf"' in l
for l in lines
)
)
+ self.assertTrue(
+ any(
+ 'id="function-missing_fn"' in l
+ and 'title="Not Implemented"' in l
+ and 'href="https://en.cppreference.com/w/c/io/missing_fn"' in l
+ for l in lines
+ )
+ )
+ self.assertTrue(
+ any(
+ 'id="function-gnu_fn"' in l
+ and '<span class="hc-sec-badge">TODO: glibc extension</span>' in l
+ for l in lines
+ )
+ )
+
+ def test_external_urls(self):
+ cases = [
+ (
+ "stdio.h",
+ "fopen",
+ "function",
+ "functions",
+ "https://en.cppreference.com/w/c/io/fopen",
+ ),
+ (
+ "stdio.h",
+ "printf",
+ "function",
+ "functions",
+ "https://en.cppreference.com/w/c/io/fprintf",
+ ),
+ (
+ "stdio.h",
+ "SEEK_SET",
+ "macro",
+ "macros",
+ "https://en.cppreference.com/w/c/io/fseek",
+ ),
+ (
+ "stdlib.h",
+ "malloc",
+ "function",
+ "functions",
+ "https://en.cppreference.com/w/c/memory/malloc",
+ ),
+ (
+ "stdlib.h",
+ "strfromd128",
+ "function",
+ "functions",
+ "https://en.cppreference.com/w/c/string/byte/strfromf",
+ ),
+ (
+ "stdbit.h",
+ "stdc_bit_ceil_ui",
+ "function",
+ "functions",
+ "https://en.cppreference.com/w/c/numeric/bit/stdc_bit_ceil",
+ ),
+ (
+ "time.h",
+ "clock",
+ "function",
+ "functions",
+ "https://en.cppreference.com/w/c/chrono/clock",
+ ),
+ ("unistd.h", "fork", "function", "functions", ""),
+ ]
+ for hname, sym, kind, sec, expected in cases:
+ hdr = Header(hname)
+ item = (hdr.api.get(sec) or {}).get(sym, {})
+ with self.subTest(header=hname, symbol=sym):
+ self.assertEqual(cppreference_url(hdr, sym, kind, item), expected)
+ with redirect_stderr(io.StringIO()):
+ for yf in sorted(Path(__file__).parent.rglob("*.yaml")):
+ rel = yf.relative_to(Path(__file__).parent).with_suffix(".h").as_posix()
+ hdr = Header(rel)
+ check_api(hdr, hdr.api)
+ self.assertEqual(
+ posix_url(
+ Header("sys/stat.h"), "chmod", "function", {"in-latest-posix": None}
+ ),
+ "https://pubs.opengroup.org/onlinepubs/9799919799/functions/chmod.html",
+ )
+ self.assertEqual(
+ posix_url(
+ Header("sys/stat.h"), "S_IRUSR", "macro", {"in-latest-posix": None}
+ ),
+ "https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_stat.h.html",
+ )
+ self.assertEqual(
+ posix_url(
+ Header("strings.h"), "bcmp", "function", {"removed-in-posix-2008": None}
+ ),
+ "https://pubs.opengroup.org/onlinepubs/007904875/functions/bcmp.html",
+ )
+ self.assertEqual(
+ posix_url(
+ Header("sys/time.h"),
+ "gettimeofday",
+ "function",
+ {"removed-in-posix-2024": None},
+ ),
+ "https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/functions/gettimeofday.html",
+ )
+ self.assertEqual(posix_url(Header("stdio.h"), "printf", "function", {}), "")
if __name__ == "__main__":
diff --git a/libc/utils/docgen/errno.yaml b/libc/utils/docgen/errno.yaml
index da41552bc3a180..d2bdeaa4daae75 100644
--- a/libc/utils/docgen/errno.yaml
+++ b/libc/utils/docgen/errno.yaml
@@ -1,12 +1,16 @@
+c-reference-root: error/errno
macros:
EDOM:
c-definition: '7.5'
+ c-reference: error/errno_macros
in-latest-posix: ''
EILSEQ:
c-definition: '7.5'
+ c-reference: error/errno_macros
in-latest-posix: ''
ERANGE:
c-definition: '7.5'
+ c-reference: error/errno_macros
in-latest-posix: ''
errno:
c-definition: '7.5'
diff --git a/libc/utils/docgen/fenv.yaml b/libc/utils/docgen/fenv.yaml
index 1d73697f36becf..48d5e408b3c7bb 100644
--- a/libc/utils/docgen/fenv.yaml
+++ b/libc/utils/docgen/fenv.yaml
@@ -1,52 +1,71 @@
+c-reference-root: numeric/fenv
functions:
fe_dec_getround:
c-definition: 7.6.5.3
+ c-reference: numeric/fenv
fe_dec_setround:
c-definition: 7.6.5.6
+ c-reference: numeric/fenv
feclearexcept:
c-definition: 7.6.4.1
+ c-reference: numeric/fenv/feexcept
in-latest-posix: ''
fegetenv:
c-definition: 7.6.6.1
+ c-reference: numeric/fenv/feenv
in-latest-posix: ''
fegetexceptflag:
c-definition: 7.6.4.2
+ c-reference: numeric/fenv/feexcept
in-latest-posix: ''
fegetmode:
c-definition: 7.6.5.1
+ c-reference: numeric/fenv
fegetround:
c-definition: 7.6.5.2
+ c-reference: numeric/fenv/feround
in-latest-posix: ''
feholdexcept:
c-definition: 7.6.6.2
+ c-reference: numeric/fenv/feenv
in-latest-posix: ''
feraiseexcept:
c-definition: 7.6.4.3
+ c-reference: numeric/fenv/feexcept
in-latest-posix: ''
fesetenv:
c-definition: 7.6.6.3
+ c-reference: numeric/fenv/feenv
in-latest-posix: ''
fesetexcept:
c-definition: 7.6.4.4
+ c-reference: numeric/fenv/feexcept
fesetexceptflag:
c-definition: 7.6.4.5
+ c-reference: numeric/fenv/feexcept
in-latest-posix: ''
fesetmode:
c-definition: 7.6.5.4
+ c-reference: numeric/fenv
fesetround:
c-definition: 7.6.5.5
+ c-reference: numeric/fenv/feround
in-latest-posix: ''
fetestexcept:
c-definition: 7.6.4.7
+ c-reference: numeric/fenv/feexcept
in-latest-posix: ''
fetestexceptflag:
c-definition: 7.6.4.6
+ c-reference: numeric/fenv/feexcept
feupdateenv:
c-definition: 7.6.6.4
+ c-reference: numeric/fenv/feenv
in-latest-posix: ''
macros:
FE_ALL_EXCEPT:
c-definition: 7.6.12
+ c-reference: numeric/fenv/FE_exceptions
in-latest-posix: ''
FE_DEC_DOWNWARD:
c-definition: 7.6.14
@@ -60,37 +79,48 @@ macros:
c-definition: 7.6.14
FE_DFL_ENV:
c-definition: 7.6.17
+ c-reference: numeric/fenv/FE_DFL_ENV
in-latest-posix: ''
FE_DFL_MODE:
c-definition: 7.6.11
FE_DIVBYZERO:
c-definition: 7.6.9
+ c-reference: numeric/fenv/FE_exceptions
in-latest-posix: ''
FE_DOWNWARD:
c-definition: 7.6.13
+ c-reference: numeric/fenv/FE_round
in-latest-posix: ''
FE_INEXACT:
c-definition: 7.6.9
+ c-reference: numeric/fenv/FE_exceptions
in-latest-posix: ''
FE_INVALID:
c-definition: 7.6.9
+ c-reference: numeric/fenv/FE_exceptions
in-latest-posix: ''
FE_OVERFLOW:
c-definition: 7.6.9
+ c-reference: numeric/fenv/FE_exceptions
in-latest-posix: ''
FE_TONEAREST:
c-definition: 7.6.13
+ c-reference: numeric/fenv/FE_round
in-latest-posix: ''
FE_TONEARESTFROMZERO:
c-definition: 7.6.13
+ c-reference: numeric/fenv/FE_round
FE_TOWARDZERO:
c-definition: 7.6.13
+ c-reference: numeric/fenv/FE_round
in-latest-posix: ''
FE_UNDERFLOW:
c-definition: 7.6.9
+ c-reference: numeric/fenv/FE_exceptions
in-latest-posix: ''
FE_UPWARD:
c-definition: 7.6.13
+ c-reference: numeric/fenv/FE_round
in-latest-posix: ''
__STDC_VERSION_FENV_H__:
c-definition: 7.6.5
diff --git a/libc/utils/docgen/float.yaml b/libc/utils/docgen/float.yaml
index a8840b91be55b4..af2539f30c48dc 100644
--- a/libc/utils/docgen/float.yaml
+++ b/libc/utils/docgen/float.yaml
@@ -1,3 +1,4 @@
+c-reference-root: types/limits
macros:
DBL_DECIMAL_DIG:
c-definition: 5.3.5.3.3
@@ -95,6 +96,7 @@ macros:
in-latest-posix: ''
INFINITY:
c-definition: 5.3.5.3.3
+ c-reference: numeric/math/INFINITY
LDBL_DECIMAL_DIG:
c-definition: 5.3.5.3.3
in-latest-posix: ''
@@ -138,6 +140,7 @@ macros:
in-latest-posix: ''
NAN:
c-definition: 5.3.5.3.3
+ c-reference: numeric/math/NAN
__STDC_VERSION_FLOAT_H__:
c-definition: '7.7'
diff --git a/libc/utils/docgen/inttypes.yaml b/libc/utils/docgen/inttypes.yaml
index cbf50592ef072d..7927ef11efc3b4 100644
--- a/libc/utils/docgen/inttypes.yaml
+++ b/libc/utils/docgen/inttypes.yaml
@@ -1,20 +1,27 @@
+c-reference-root: types/integer
functions:
imaxabs:
c-definition: 7.8.2.1
+ c-reference: numeric/math/abs
in-latest-posix: ''
imaxdiv:
c-definition: 7.8.2.2
+ c-reference: numeric/math/div
in-latest-posix: ''
strtoimax:
...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/240746
More information about the libc-commits
mailing list