[clang-tools-extra] [clang-tidy] Implement alphabetical order test (PR #166072)
Baranov Victor via cfe-commits
cfe-commits at lists.llvm.org
Sun Nov 2 12:58:51 PST 2025
================
@@ -0,0 +1,298 @@
+#!/usr/bin/env python3
+#
+# ===-----------------------------------------------------------------------===#
+#
+# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+#
+# ===-----------------------------------------------------------------------===#
+
+"""
+
+ClangTidy Alphabetical Order Checker
+====================================
+
+Normalize clang-tidy docs with deterministic sorting for linting/tests.
+
+Subcommands:
+ - checks-list: Sort entries in docs/clang-tidy/checks/list.rst csv-table.
+ - release-notes: Sort key sections in docs/ReleaseNotes.rst and de-duplicate
+ entries in "Changes in existing checks".
+
+Usage:
+ clang-tidy-alphabetical-order-check.py <subcommand> [-i <input rst>] [-o <output rst>] [--fix]
+
+Flags:
+ -i/--input Input file.
+ -o/--output Write normalized content here; omit to write to stdout.
+ --fix Rewrite the input file in place. Cannot be combined with -o/--output.
+"""
+
+import argparse
+import io
+import os
+import re
+import sys
+from typing import List, Optional, Sequence, Tuple
+
+DOC_LABEL_RN_RE = re.compile(r":doc:`(?P<label>[^`<]+)\s*(?:<[^>]+>)?`")
+DOC_LINE_RE = re.compile(r"^\s*:doc:`(?P<label>[^`<]+?)\s*<[^>]+>`.*$")
+
+
+def script_dir() -> str:
+ return os.path.dirname(os.path.abspath(__file__))
+
+
+def read_text(path: str) -> List[str]:
+ with io.open(path, "r", encoding="utf-8") as f:
+ return f.read().splitlines(True)
+
+
+def write_text(path: str, content: str) -> None:
+ with io.open(path, "w", encoding="utf-8", newline="") as f:
+ f.write(content)
+
+
+def normalize_list_rst(lines: List[str]) -> str:
+ out: List[str] = []
+ i = 0
+ n = len(lines)
+ while i < n:
+ out.append(lines[i])
+ if lines[i].lstrip().startswith(".. csv-table::"):
+ i += 1
+ break
+ i += 1
+
+ while i < n and (lines[i].startswith(" ") or lines[i].strip() == ""):
+ if DOC_LINE_RE.match(lines[i]):
+ break
+ out.append(lines[i])
+ i += 1
+
+ entries: List[str] = []
+ while i < n and lines[i].startswith(" "):
+ if DOC_LINE_RE.match(lines[i]):
+ entries.append(lines[i])
+ else:
+ entries.append(lines[i])
+ i += 1
+
+ def key_for(line: str):
+ m = DOC_LINE_RE.match(line)
+ if not m:
+ return (1, "")
+ return (0, m.group("label"))
+
+ entries_sorted = sorted(entries, key=key_for)
+ out.extend(entries_sorted)
+ out.extend(lines[i:])
+
+ return "".join(out)
+
+
+def run_checks_list(inp: Optional[str], out_path: Optional[str], fix: bool) -> int:
+ if not inp:
+ inp = os.path.normpath(
+ os.path.join(
+ script_dir(),
+ "..",
+ "..",
+ "docs",
+ "clang-tidy",
+ "checks",
+ "list.rst",
+ )
+ )
+ lines = read_text(inp)
+ normalized = normalize_list_rst(lines)
+ if fix and out_path:
+ sys.stderr.write("error: --fix cannot be used together with --output\n")
+ return 2
+ if fix:
+ original = "".join(lines)
+ if original != normalized:
+ write_text(inp, normalized)
+ return 0
+ if out_path:
+ write_text(out_path, normalized)
+ return 0
+ sys.stdout.write(normalized)
+ return 0
+
+
+def find_heading(lines: Sequence[str], title: str) -> Optional[int]:
----------------
vbvictor wrote:
Please add comment what heading styly do we look for
https://github.com/llvm/llvm-project/pull/166072
More information about the cfe-commits
mailing list