Clint (Clang-Tidy)

The clint macro runs clang-tidy on C/C++ translation units from a compilation database and uses cloc to count code lines. It converts clang-tidy diagnostics into cikit LintIssue objects and produces a Pylint-like quality score.

Dependencies

The clang-tidy and cloc executables must be available in the CI environment. PyYAML is needed to read the structured YAML diagnostics. It may be installed independently.

A compile_commands.json compilation database is required. It can be generated by CMake (with CMAKE_EXPORT_COMPILE_COMMANDS=ON) or by PlatformIO.

Configuration

The analysis rules are configured by clang-tidy itself, not by cikit. Place a .clang-tidy file in the project tree to specify which checks are enabled, which headers to include in diagnostic output, and whether warnings should fail the build. For example:

Checks: '-*,clang-analyzer-*,bugprone-*,performance-*,readability-*'
HeaderFilterRegex: '.*/(src|include)/.*'
WarningsAsErrors: ''

clang-tidy finds .clang-tidy by searching upward from the directory of each source file. Adjust HeaderFilterRegex to the project’s actual header paths. If no configuration file is present, clang-tidy uses its built-in defaults.

Features

  • analysis of all C/C++ translation units or an explicitly selected subset

  • cloc-based source code line count

  • YAML diagnostic export and conversion to LintIssue objects

  • Pylint-like 0-to-10 score, badge, and run-data metrics

  • reusable RST lint reports, including individual diagnostics and artifact links

  • command log registration and optional timeline integration

Typical usage

from pathlib import Path

from cikit.macros import clint

clint.clint(env, Path("build/compile_commands.json"))
clint.badge(env)
clint.write_rst(env)

The optional sources argument restricts the analysis to files already listed in the compilation database:

clint.clint(env, Path("build/compile_commands.json"),
            sources=[Path("src/main.cpp")])

The built-in clint and platformio_lint commands generate their own compilation databases without requiring a preceding build. See Built-in CI Commands for their CLI options. To analyze an existing compilation database, call the macro directly.

Scoring

clang-tidy does not provide its own numeric quality score. cikit computes a Pylint-like score from the weighted diagnostics and the number of code lines reported by cloc:

\[\mathrm{score} = \max\left(0, 10 - 10 \cdot \frac{\mathrm{weighted\_issues}}{\mathrm{lines\_of\_code}}\right)\]

The following weights are defined by cikit; they are not clang-tidy severities:

Check family

Weight

LintIssue severity

clang-diagnostic-error

5.0

error

bugprone-*, cert-*

3.0

error

performance-*, cppcoreguidelines-*

1.5

warning

modernize-*

0.5

refactor

readability-*

0.3

convention

Other checks

1.0

warning

Only diagnostics located in the selected translation units contribute to the weighted total and the code-line denominator. Findings in headers or other files remain visible in the RST report but are excluded from the score. A clang-diagnostic-error reported anywhere forces the score to zero.

Metrics and reports

The macro stores score, lines_of_code, weighted_issues, issues, issues_scored, issues_excluded, files_analyzed, analysis_time, and exit_code in the current run data when available. The full clang-tidy log is registered as a LOG artifact; a non-empty YAML fixes report is registered as a LINT artifact.

write_rst() reads the diagnostics from the registered YAML artifact, so a report can also be generated in a separate CI step belonging to the same run. The RST report includes the summary, individual issues, logs, and links to registered artifacts. The built-in command can add a score timeline chart.

A non-zero clang-tidy exit code is stored in the run data before reports are created. The built-in command marks the CI run as failed after generating the available reports. If the tool fails without usable diagnostics, no score is published instead of incorrectly reporting 10/10.

Filtered reports

Use filter_code to generate a separate report for a specific diagnostic code from existing lint results, without rerunning the analysis:

clint.write_rst(env, name="filtered", report_title="Filtered Report",
                 filter_code="bugprone-use-after-move")

Filtered reports omit the timeline chart. Use a distinct output name to retain the unfiltered report.