Clang-tidy
The `clang-tidy` Python package provides pre-built binaries of the LLVM-based code analyser tool `clang-tidy`, making the executable available in Python environments. It is currently at version 22.1.0.1, primarily reflecting the packaged LLVM version. Releases typically follow major LLVM releases, with additional patches for packaging updates.
Common errors
-
clang-tidy: command not found
cause The `clang-tidy` executable is not in your system's PATH, or the pip installation failed to link it correctly.fixEnsure `pip install clang-tidy` completed successfully. If using a virtual environment, ensure it's activated. Alternatively, invoke the tool using `python -m clang_tidy` which directly calls the installed Python module. -
Error: no input files
cause You ran `clang-tidy` without specifying any C++ source files to analyze.fixProvide one or more C++ source files as arguments (e.g., `clang-tidy my_file.cpp`). For a whole project, use a compilation database and target files: `clang-tidy path/to/source.cpp -p path/to/build_dir`. -
No compilation database found in directory [directory path]
cause Clang-tidy requires a `compile_commands.json` file to understand how to build and analyze your C++ project, and it couldn't find one in the current directory or the specified path.fixGenerate `compile_commands.json` using your build system (e.g., CMake, Meson, etc.). Place it in your project's build directory or root, and then call `clang-tidy -p <path_to_directory_containing_compile_commands.json>`.
Warnings
- gotcha The `clang-tidy` Python package primarily serves as a wrapper to distribute the `clang-tidy` executable. It does NOT provide a direct Python API for programmatic analysis. If you expect to import `clang_tidy` and call functions, this package is not for that purpose.
- gotcha The package version (e.g., `22.1.0.1`) reflects the underlying LLVM/Clang version it ships, not a distinct Python package versioning scheme. The first three digits (e.g., `22.1.0`) directly correspond to the bundled `clang-tidy` tool's version. This can be confusing if you expect standard Python semantic versioning.
- gotcha For `clang-tidy` to perform meaningful code analysis on C++ projects, it typically requires a compilation database (a `compile_commands.json` file) in the project root or specified via the `-p` flag. This file lists compilation commands for each source file, allowing `clang-tidy` to correctly resolve headers and build settings.
Install
-
pip install clang-tidy
Imports
- No direct Python API
import subprocess # Or use: python -m clang_tidy
Quickstart
import subprocess
import os
def run_clang_tidy(args):
try:
# Option 1: Using subprocess directly with the executable
# Requires 'clang-tidy' to be in PATH or specified with full path
# For this package, 'clang-tidy' should be in PATH after install
result = subprocess.run(['clang-tidy'] + args, capture_output=True, text=True, check=True)
print(f"Stdout:\n{result.stdout}")
if result.stderr:
print(f"Stderr:\n{result.stderr}")
return result.returncode
except FileNotFoundError:
print("Error: 'clang-tidy' command not found. Ensure the package is installed and accessible.")
return 1
except subprocess.CalledProcessError as e:
print(f"Error running clang-tidy: {e.returncode}")
print(f"Stdout:\n{e.stdout}")
print(f"Stderr:\n{e.stderr}")
return e.returncode
# Example 1: Check clang-tidy version
print("\n--- Running clang-tidy --version ---")
run_clang_tidy(['--version'])
# Example 2: Run clang-tidy as a Python module (alternative)
# This is often more reliable in environments where PATH might be tricky
def run_clang_tidy_module(args):
try:
# Ensure the script running this has access to the python executable
# that installed clang-tidy.
result = subprocess.run(['python', '-m', 'clang_tidy'] + args, capture_output=True, text=True, check=True)
print(f"Stdout:\n{result.stdout}")
if result.stderr:
print(f"Stderr:\n{result.stderr}")
return result.returncode
except FileNotFoundError:
print("Error: 'python' command not found, or module path issue.")
return 1
except subprocess.CalledProcessError as e:
print(f"Error running clang-tidy via module: {e.returncode}")
print(f"Stdout:\n{e.stdout}")
print(f"Stderr:\n{e.stderr}")
return e.returncode
print("\n--- Running clang-tidy via 'python -m clang_tidy --version' ---")
run_clang_tidy_module(['--version'])
# To run actual analysis, you typically need a 'compile_commands.json'
# This example is illustrative and won't work without a compiled C++ project.
# For example:
# if os.path.exists('compile_commands.json'):
# print("\n--- Running clang-tidy on a C++ file (illustrative) ---")
# run_clang_tidy(['my_source.cpp', '-p', '.'])
# else:
# print("\nSkipping C++ file analysis: 'compile_commands.json' not found.")