Clang-tidy

22.1.0.1 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to execute the `clang-tidy` command-line tool after installing the Python package. Since there's no direct Python API, you interact with `clang-tidy` using `subprocess` or by running it as a Python module. A common use case is to check its version or to run analysis on C++ code, which usually requires a `compile_commands.json` file generated by a build system like CMake.

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.")

view raw JSON →