clang-tools: Resolve compilation database paths before changing directory
Subsystems (2)
PATCH 1/1 clang-tools: Resolve compilation database paths before changing directory
run-clang-tools.py opens the compilation database relative to the caller, but passes the same path to clang-tidy while running it in each entry's build directory. If these directories differ, a relative database path no longer points to the selected database. clang-tidy then falls back to running without flags, or can pick up a different database.
For example, invoking the script with database/compile_commands.json from the parent of the build directory makes clang-tidy look under build/database/compile_commands.json. A source file that requires a macro defined in the selected database then reports a compiler error.
Convert the database argument to an absolute path when parsing it, before starting the worker pool. This preserves the caller's selected database when subprocess.run() changes the working directory.
Add a regression test using real clang-tidy with C and C++ inputs. Cover both analysis modes and absolute, relative, dot-prefixed and parent component paths, including a database directory containing spaces.
---
Testing with Python 3.12.3 and clang-tidy 18.1.3 on Ubuntu:
- Eight CLI invocations cover both analysis modes and four path forms, each processing C and C++ inputs. Six fail before the fix; all pass after it.
- Real x86 and ARM64 Clang init/main.c analyses match the output from the original script with an absolute database path, with no compiler errors. Existing analyzer warnings remain identical.
- GCC database diagnostics also match, but clang-tidy cannot fully process the GCC configuration; this is not a clean analysis result.
AI disclosure: Codex found and reproduced the path bug, wrote the fix and regression test, reviewed the changes, ran the tests and drafted this patch in response to a request to find another kernel issue.
.../clang-tools/run-clang-tools-path-test.py | 56 +++++++++++++++++++ scripts/clang-tools/run-clang-tools.py | 3 +- 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 scripts/clang-tools/run-clang-tools-path-test.py
scripts/clang-tools/run-clang-tools-path-test.py new file +56 -0
@@ -0,0 +1,56 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0 + +"""Exercise compilation database paths with the real clang-tidy executable.""" + +import json +from pathlib import Path +import shutil +import subprocess +import sys +import tempfile +import unittest + + +SCRIPT = Path(__file__).with_name("run-clang-tools.py") + + [email protected](shutil.which("clang-tidy"), "clang-tidy is required") +class TestDatabasePath(unittest.TestCase): + def test_database_path(self): + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + build = root / "build" + database = root / "database with spaces" + build.mkdir() + database.mkdir() + entries = [] + for suffix in ("c", "cpp"): + source = build / ("test." + suffix) + source.write_text( + "#ifndef REQUIRED\n#error compilation flags lost\n" + "#endif\nint test;\n", encoding="utf-8") + entries.append({ + "directory": str(build), "file": str(source), + "arguments": ["clang", "-DREQUIRED", "-c", str(source)], + }) + path = database / "compile_commands.json" + path.write_text(json.dumps(entries), encoding="utf-8") + paths = [str(path), str(path.relative_to(root)), + "./" + str(path.relative_to(root)), + "build/../" + str(path.relative_to(root))] + for analysis in ("clang-tidy", "clang-analyzer"): + for value in paths: + with self.subTest(analysis=analysis, path=value): + result = subprocess.run( + [sys.executable, str(SCRIPT.resolve()), analysis, + value], cwd=root, capture_output=True, text=True) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertNotIn("compilation flags lost", result.stderr) + self.assertNotIn("Running without flags", result.stderr) + self.assertNotIn("Error while trying to load", + result.stderr) + + +if __name__ == "__main__": + unittest.main()
scripts/clang-tools/run-clang-tools.py +2 -1
@@ -12,6 +12,7 @@ compile_commands.json. import argparse import json import multiprocessing +import os import subprocess import sys
@@ -31,7 +32,7 @@ def parse_arguments(): choices=["clang-tidy", "clang-analyzer"], help=type_help) path_help = "Path to the compilation database to parse" - parser.add_argument("path", type=str, help=path_help) + parser.add_argument("path", type=os.path.abspath, help=path_help) checks_help = "Checks to pass to the analysis" parser.add_argument("-checks", type=str, default=None, help=checks_help)
Signature · 1 line
2.52.0.windows.1