mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] clang-tools: Resolve compilation database paths before changing directory
@ 2026-09-23 15:26 houtinghang
  0 siblings, 0 replies; only message in thread
From: houtinghang @ 2026-09-23 15:26 UTC (permalink / raw)
  To: Nathan Chancellor, Nicolas Schier
  Cc: llvm, linux-kbuild, linux-kernel, Nick Desaulniers,
	Bill Wendling, Justin Stitt

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.

Fixes: 6ad7cbc01527 ("Makefile: Add clang-tidy and static analyzer support to makefile")
Assisted-by: LLM
Signed-off-by: houtinghang <ue081723@gmail.com>
---
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

diff --git a/scripts/clang-tools/run-clang-tools-path-test.py b/scripts/clang-tools/run-clang-tools-path-test.py
new file mode 100644
index 0000000..87baba5
--- /dev/null
+++ b/scripts/clang-tools/run-clang-tools-path-test.py
@@ -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")
+
+
+@unittest.skipUnless(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()
diff --git a/scripts/clang-tools/run-clang-tools.py b/scripts/clang-tools/run-clang-tools.py
index e78be82..230db50 100755
--- a/scripts/clang-tools/run-clang-tools.py
+++ b/scripts/clang-tools/run-clang-tools.py
@@ -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)
-- 
2.52.0.windows.1


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-09-23 15:27 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-23 15:26 [PATCH] clang-tools: Resolve compilation database paths before changing directory houtinghang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®