Skip to content

livery.extensions.clang.tidy._steps

livery.extensions.clang.tidy._steps

clang-tidy's step: each cpp package's sources, over its compilation database.

The extension's extension.toml declares it and names the body here. lint.clang-tidy judges one package at a time, after the package's build is configured: the files its claims reach in the package, or the files a run names (livery.workshop.scoped_files), against the compilation database the package's build writes, its extensions' answer to livery.workshop.COMPILE_COMMANDS; a package whose build writes none skips, saying so. The checks are the package's own .clang-tidy, which the extension writes. The step hands clang-tidy the words after -- on its own verb (fm lint.clang-tidy -- --checks=-*,bugprone-*). A body resolves its runner on this module when it runs, so a test that replaces run_lint here sees its replacement called.

Functions:

Name Description
toolchain_arguments

What the standalone clang-tidy needs, and why it cannot run.

run_lint

Run clang-tidy over files of package against database; a finding refuses.

judge_lint

Run clang-tidy over the package's files the run reaches, against its database.

toolchain_arguments

toolchain_arguments() -> tuple[list[str], str]

What the standalone clang-tidy needs, and why it cannot run.

The static build is one binary. It carries no resource directory of its own, so the compiler's builtin headers (stddef.h and its kin) come from the host's compiler, and on macOS the standard library comes from the SDK xcrun names. Returns the arguments and an empty reason, or no arguments and the reason the lint cannot run, which the caller prints as a skip.

Source code in packages/extensions/clang-tidy/src/livery/extensions/clang/tidy/_steps.py
def toolchain_arguments() -> tuple[list[str], str]:
    """What the standalone clang-tidy needs, and why it cannot run.

    The static build is one binary. It carries no resource directory of
    its own, so the compiler's builtin headers (`stddef.h` and its kin)
    come from the host's compiler, and on macOS the standard library
    comes from the SDK xcrun names. Returns the arguments and an empty
    reason, or no arguments and the reason the lint cannot run, which
    the caller prints as a skip.
    """
    resources = _asked(["clang", "-print-resource-dir"]) or _asked(
        ["cc", "-print-file-name=include"]
    )
    if not resources:
        return [], "no compiler here answers where its builtin headers are"
    arguments = [f"--extra-arg=-resource-dir={resources.removesuffix('/include')}"]
    if sys.platform == "darwin":
        sdk = _asked(["xcrun", "--show-sdk-path"])
        if not sdk:
            return [], "xcrun names no SDK, where this platform keeps its headers"
        arguments.append(f"--extra-arg=-isysroot{sdk}")
    return arguments, ""

run_lint

run_lint(package: Package, files: tuple[Path, ...], database: Path, arguments: tuple[str, ...] = ()) -> None

Run clang-tidy over files of package against database; a finding refuses.

A host that cannot give the standalone binary its headers skips, saying why. arguments go to clang-tidy before the files.

Raises:

Type Description
Failed

when clang-tidy finds anything, with its own output.

Source code in packages/extensions/clang-tidy/src/livery/extensions/clang/tidy/_steps.py
def run_lint(
    package: Package,
    files: tuple[Path, ...],
    database: Path,
    arguments: tuple[str, ...] = (),
) -> None:
    """Run clang-tidy over *files* of *package* against *database*; a finding refuses.

    A host that cannot give the standalone binary its headers skips,
    saying why. *arguments* go to clang-tidy before the files.

    Raises:
        Failed: when clang-tidy finds anything, with its own output.
    """
    if not files:
        return
    toolchain, reason = toolchain_arguments()
    if reason:
        print(f"  {package.name}: clang-tidy skips, {reason}")
        return
    result = tools.clang_tidy.opts(cwd=package.directory, nofail=True, recorded=False)(
        "-p",
        str(database.parent),
        *toolchain,
        *arguments,
        *(str(path) for path in files),
    )
    if result.code != 0:
        fail(
            f"{package.name}: clang-tidy found something:\n"
            f"{result.stdout[-4000:]}{result.stderr[-2000:]}"
        )

judge_lint

judge_lint(ctx: PhaseContext) -> None

Run clang-tidy over the package's files the run reaches, against its database.

Source code in packages/extensions/clang-tidy/src/livery/extensions/clang/tidy/_steps.py
def judge_lint(ctx: PhaseContext) -> None:
    """Run clang-tidy over the package's files the run reaches, against its database."""
    package = ctx.member
    database = answer(package, COMPILE_COMMANDS)
    if database is None or not database.is_file():
        print(
            f"  {package.name}: clang-tidy skips, its build writes no"
            " compilation database"
        )
        return
    run_lint(package, scoped_files(ctx, "lint.clang-tidy"), database, ctx.arguments)