[clang-tools-extra] [clang-tidy] Scaffold lit tests in per-check directories (PR #244400)
Daniil Dudkin via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 19 13:51:28 PST 2026
https://github.com/unterumarmung created https://github.com/llvm/llvm-project/pull/244400
Make `add_new_check.py` generate the lit test at `checkers/<module>/<check>/test.<ext>`. Teach `rename_check.py` to move a check’s test directory with its private fixtures, and document the Ninja targets for running a module or a single check.
This is the tooling and documentation patch for the proposed per-check lit layout. The migration pilots can land independently because the existing lit and CMake configuration already handles nested directories.
### Testing
- Generated test skeletons for C, C++, Objective-C, and Objective-C++.
- Renamed a check in each layout and moved a check between modules.
- Built the contribution guide with Sphinx warnings treated as errors.
>From ccc141503f19f6a3233472f7f433c250ce56568e Mon Sep 17 00:00:00 2001
From: Daniil Dudkin <unterumarmung at yandex.ru>
Date: Fri, 20 Nov 2026 00:01:45 +0300
Subject: [PATCH] [clang-tidy] Scaffold lit tests in per-check directories
Generate test.<ext> in the check directory and teach rename_check.py to move that directory with its fixtures. Document how to run per-check directories alongside tests in the existing flat layout.
---
clang-tools-extra/clang-tidy/add_new_check.py | 6 ++--
clang-tools-extra/clang-tidy/rename_check.py | 28 ++++++++++-----
.../docs/clang-tidy/Contributing.rst | 34 +++++++++++++++----
3 files changed, 50 insertions(+), 18 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/add_new_check.py b/clang-tools-extra/clang-tidy/add_new_check.py
index 0e6692038fbf12..2119c9bffbd4a7 100755
--- a/clang-tools-extra/clang-tidy/add_new_check.py
+++ b/clang-tools-extra/clang-tidy/add_new_check.py
@@ -310,7 +310,7 @@ def write_test(
) -> None:
test_standard = f"-std={test_standard}-or-later " if test_standard else ""
check_name_dashes = f"{module}-{check_name}"
- filename = os.path.normpath(
+ test_dir = os.path.normpath(
os.path.join(
module_path,
"..",
@@ -319,9 +319,11 @@ def write_test(
"clang-tidy",
"checkers",
module,
- f"{check_name}.{test_extension}",
+ check_name,
)
)
+ os.makedirs(test_dir, exist_ok=True)
+ filename = os.path.join(test_dir, f"test.{test_extension}")
print(f"Creating {filename}...")
with open(filename, "w", encoding="utf8", newline="\n") as f:
f.write(
diff --git a/clang-tools-extra/clang-tidy/rename_check.py b/clang-tools-extra/clang-tidy/rename_check.py
index 18c998e839994f..b373c1141a6b00 100755
--- a/clang-tools-extra/clang-tidy/rename_check.py
+++ b/clang-tools-extra/clang-tidy/rename_check.py
@@ -77,15 +77,13 @@ def deleteMatchingLines(fileName: str, pattern: str) -> bool:
return True
-def getListOfFiles(clang_tidy_path: str) -> List[str]:
+def getListOfFiles(clang_tidy_path: str, checkers_path: str) -> List[str]:
files = glob.glob(os.path.join(clang_tidy_path, "**"), recursive=True)
files += [
os.path.normpath(os.path.join(clang_tidy_path, "../docs/ReleaseNotes.md"))
]
- files += glob.glob(
- os.path.join(clang_tidy_path, "..", "test", "clang-tidy", "checkers", "**"),
- recursive=True,
- )
+ files += glob.glob(os.path.join(checkers_path, "**"), recursive=True)
+ files += glob.glob(os.path.join(checkers_path, "**", ".clang-tidy"), recursive=True)
docs_path = os.path.join(clang_tidy_path, "..", "docs", "clang-tidy", "checks")
# TODO: Stop discovering reST files once all clang-tidy check
# documentation has been migrated to MyST.
@@ -277,6 +275,12 @@ def main() -> None:
old_module_path = os.path.join(clang_tidy_path, old_module)
new_module_path = os.path.join(clang_tidy_path, new_module)
+ checkers_path = os.path.join(clang_tidy_path, "..", "test", "clang-tidy", "checkers")
+ old_test_dir = os.path.join(checkers_path, old_module, old_name)
+ new_test_dir = os.path.join(checkers_path, new_module, new_name)
+ move_test_dir = old_test_dir != new_test_dir and os.path.isdir(old_test_dir)
+ if move_test_dir and os.path.lexists(new_test_dir):
+ sys.exit(f"Cannot move tests to {new_test_dir}: the path already exists.")
if old_module != new_module:
# Remove the check from the old module.
@@ -302,10 +306,12 @@ def main() -> None:
"\\b" + check_name_camel + "|\\b" + args.old_check_name,
)
- for filename in getListOfFiles(clang_tidy_path):
- filename = fileRename(
- filename, old_module + "/" + old_name, new_module + "/" + new_name
- )
+ for filename in getListOfFiles(clang_tidy_path, checkers_path):
+ # Move per-check test directories as a unit after updating their files.
+ if not filename.startswith(old_test_dir + os.sep):
+ filename = fileRename(
+ filename, old_module + "/" + old_name, new_module + "/" + new_name
+ )
filename = fileRename(filename, args.old_check_name, args.new_check_name)
filename = fileRename(filename, check_name_camel, new_check_name_camel)
for header_guard in header_guard_variants:
@@ -336,6 +342,10 @@ def main() -> None:
)
replaceInFile(filename, check_name_camel, new_check_name_camel)
+ if move_test_dir:
+ os.makedirs(os.path.dirname(new_test_dir), exist_ok=True)
+ fileRename(old_test_dir, old_test_dir, new_test_dir)
+
if old_module != new_module or new_module == "llvm":
if new_module == "llvm":
new_namespace = new_module + "_check"
diff --git a/clang-tools-extra/docs/clang-tidy/Contributing.rst b/clang-tools-extra/docs/clang-tidy/Contributing.rst
index 71c13344288141..26e06d70e87b6d 100644
--- a/clang-tools-extra/docs/clang-tidy/Contributing.rst
+++ b/clang-tools-extra/docs/clang-tidy/Contributing.rst
@@ -164,7 +164,8 @@ If we want to create a `readability-awesome-function-names`, we would run:
The ``add_new_check.py`` script will:
* create the class for your check inside the specified module's directory and
register it in the module and in the build system;
- * create a lit test file in the ``test/clang-tidy/`` directory;
+ * create a lit test file at
+ ``test/clang-tidy/checkers/<module>/<check>/test.<ext>``;
* create a documentation file and include it into the
``docs/clang-tidy/checks/list.md``.
@@ -581,6 +582,22 @@ generator, use the command:
$ ninja check-clang-tools
+Use the following Ninja targets for a module or for a check with its own test
+directory.
+
+.. code-block:: console
+
+ $ ninja check-clang-extra-clang-tidy-checkers-<module>
+ $ ninja check-clang-extra-clang-tidy-checkers-<module>-<check>
+
+For example, these commands run the ``readability`` module and the
+``readability-redundant-control-flow`` check, respectively.
+
+.. code-block:: console
+
+ $ ninja check-clang-extra-clang-tidy-checkers-readability
+ $ ninja check-clang-extra-clang-tidy-checkers-readability-redundant-control-flow
+
:program:`clang-tidy` checks can be tested using either unit tests or
`lit`_ tests. Unit tests may be more convenient to test complex replacements
with strict checks. `Lit`_ tests allow using partial text matching and regular
@@ -611,10 +628,14 @@ must have an associated CHECK in that file. Or, you can use ``CHECK-NOTES:``
instead, if you want to **also** ensure that all the notes are checked.
To use the ``check_clang_tidy.py`` script, put a .cpp file with the
-appropriate ``RUN`` line in the ``test/clang-tidy`` directory. Use
-``CHECK-MESSAGES:`` and ``CHECK-FIXES:`` lines to write checks against
+appropriate ``RUN`` line in ``test/clang-tidy/checkers/<module>/<check>/``.
+For checks using the flat layout, add new tests alongside the existing files.
+Use ``CHECK-MESSAGES:`` and ``CHECK-FIXES:`` lines to write checks against
diagnostic messages and fixed code.
+Use ``test.<ext>`` for a general test and descriptive names such as
+``macros.cpp`` for individual scenarios.
+
It's advised to make the checks as specific as possible to avoid checks
matching incorrect parts of the input. Use ``[[@LINE+X]]``/``[[@LINE-X]]``
substitutions and distinct function and variable names in the test code.
@@ -690,10 +711,9 @@ hints. The most frequent pitfalls are macros and templates:
some macro expansions/template instantiations, but easily break some other
expansions/instantiations.
-If you need multiple files to exercise all the aspects of your check, it is
-recommended you place them in a subdirectory named for the check under the
-``Inputs`` directory for the module containing your check. This keeps the
-test directory from getting cluttered.
+Place private fixtures in the check's ``Inputs`` directory. For checks using
+the flat layout, use the module's ``Inputs/<check>`` directory. Shared fixtures
+can stay in the module's ``Inputs`` directory.
If you need to validate how your check interacts with system header files, a
set of simulated system header files is located in the
More information about the cfe-commits
mailing list