Skip to contentSkip to conversation

run-clang-tools: Resolve the compilation database before changing directory

PATCH In review Marcus 2 participants 2 messages Nov 9 – Nov 11

Subsystems (2)
lore
Share & tools
lore

Marcus

author
#0 lore raw

PATCH 1/1 run-clang-tools: Resolve the compilation database before changing directory

The script reads the compilation database relative to its working directory, but passes the same path to clang-tidy after switching to each entry's directory. When the database is outside the build directory, a relative path can therefore point to a nonexistent location. clang-tidy then runs without the recorded compilation flags or finds an unrelated database in a parent directory.

For example, invoking the script with database/compile_commands.json from the parent of database/ and build/ fails to load the database when its entries use build/ as their directory.

After reading the JSON file, resolve its containing directory to an absolute path for clang-tidy's -p option. This preserves the caller's path resolution when the subprocess changes directory.

The issue, fix, and reproducer were developed with OpenAI Codex assistance. Verified with Python 3.13 and clang-tidy 23.1.4 using a C file requiring a definition from the database, with relative and absolute database paths outside the build directory and a database in the working directory.

Fixes 6ad7cbc01527 ("Makefile: Add clang-tidy and static analyzer support to makefile")
---
 scripts/clang-tools/run-clang-tools.py | 2 ++
 1 file changed, 2 insertions(+)
scripts/clang-tools/run-clang-tools.py +2 -0
index 79d58b34..cc19f8bb 100755
@@ -86,6 +86,8 @@ def main():
     with open(args.path) as f:
         datastore = json.load(f)
 
+    args.path = os.path.dirname(os.path.abspath(args.path))
+
     lock = multiprocessing.Lock()
     try:
         with multiprocessing.Pool(initializer=init, initargs=(lock, args)) as pool:

base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb

Signature · 1 line
2.51.2.windows.1
#1 lore raw
Quoted 10 lines

The script reads the compilation database relative to its working directory, but passes the same path to clang-tidy after switching to each entry's directory. When the database is outside the build directory, a relative path can therefore point to a nonexistent location. clang-tidy then runs without the recorded compilation flags or finds an unrelated database in a parent directory.

For example, invoking the script with database/compile_commands.json from the parent of database/ and build/ fails to load the database when its entries use build/ as their directory.

I do not believe this script is meant to be called arbitrarily by a user. It is only designed to be called within Kbuild directly via the clang-tidy and clang-analyzer targets. The rules for those targets mandate using the current build's compile_commands.json through their prerequisites, so the script's current working directory must be the same as the directory of every entry in compile_commands.json, so I do not think your reproducer can happen in practice. If it can happen through using one of those targets, please demonstrate how.

Signature · 2 lines
Cheers,
Nathan