diff --git a/.github/scripts/warnings_report.py b/.github/scripts/warnings_report.py new file mode 100644 index 0000000..4627ca2 --- /dev/null +++ b/.github/scripts/warnings_report.py @@ -0,0 +1,136 @@ +#!/usr/bin/env python3 +############################################################################### +# License: BSD Zero Clause License file +# Copyright: +# (C) 2026 Alexander Shaduri +############################################################################### + +""" +Generate a Markdown report of compiler / clang-tidy warnings from build logs. + +Warnings are deduplicated (the same header warning is printed once per +translation unit), restricted to project files (dependencies, system headers +and generated files are skipped), and split into "in changed files" and "total". + +Usage: + warnings_report.py --workspace DIR [--changed-files FILE] [--build-dir NAME] + --log "Title=path/to/build.log" [--log ...] > report.md +""" + +import argparse +import collections +import posixpath +import re +import sys + +# file:line:col: warning: message [flag-or-check] +WARNING_RE = re.compile( + r"^(?P[^\s:][^:]*):(?P\d+):(?P\d+): warning: (?P.*?)(?: \[(?P[^\]]+)\])?$") + +# Paths (relative to the workspace) whose warnings are not ours. +EXCLUDED_PREFIXES = ("dependencies/",) + +# Maximum number of warnings listed per section, to keep the comment within GitHub's size limit. +MAX_LISTED = 150 + +MARKER = "" + + +Warning = collections.namedtuple("Warning", "file line col msg flag") + + +def parse_log(path, workspace, build_dirs): + """Return a sorted list of unique project warnings found in the log.""" + found = set() + try: + with open(path, encoding="utf-8", errors="replace") as f: + lines = f.readlines() + except FileNotFoundError: + return None + + for raw in lines: + m = WARNING_RE.match(raw.rstrip("\r\n")) + if not m: + continue + file = posixpath.normpath(m.group("file")) + if posixpath.isabs(file): + file = posixpath.relpath(file, workspace) + if file.startswith("../") or file.startswith(EXCLUDED_PREFIXES): + continue # system headers, dependencies + if any(file == d or file.startswith(d + "/") for d in build_dirs): + continue # generated files + found.add(Warning(file, int(m.group("line")), int(m.group("col")), + m.group("msg").strip(), (m.group("flag") or "").replace(",-warnings-as-errors", ""))) + + return sorted(found) + + +def format_list(warnings): + out = [] + for w in warnings[:MAX_LISTED]: + flag = f" `[{w.flag}]`" if w.flag else "" + msg = w.msg.replace("|", "\\|").replace("`", "'") + out.append(f"- `{w.file}:{w.line}:{w.col}`: {msg}{flag}") + if len(warnings) > MAX_LISTED: + out.append(f"- ... and {len(warnings) - MAX_LISTED} more") + return "\n".join(out) + + +def format_top_flags(warnings, count=15): + counter = collections.Counter(w.flag or "(no flag)" for w in warnings) + rows = ["| Warning | Count |", "|---|---:|"] + rows += [f"| `{flag}` | {n} |" for flag, n in counter.most_common(count)] + return "\n".join(rows) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + parser.add_argument("--workspace", required=True, help="Source root; warnings are reported relative to it") + parser.add_argument("--build-dir", action="append", default=[], help="Build directory (relative), skipped") + parser.add_argument("--changed-files", help="File with one changed path (relative) per line") + parser.add_argument("--log", action="append", required=True, help="Title=path of a build log") + args = parser.parse_args() + + workspace = posixpath.abspath(args.workspace) + changed = None + if args.changed_files: + with open(args.changed_files, encoding="utf-8") as f: + changed = {line.strip() for line in f if line.strip()} + + reports = [] + for spec in args.log: + title, _, path = spec.partition("=") + reports.append((title, parse_log(path, workspace, args.build_dir))) + + out = [MARKER, "## Warnings report", ""] + + header = "| | " + " | ".join(title for title, _ in reports) + " |" + out += [header, "|---" + "|---:" * len(reports) + "|"] + if changed is not None: + cells = [] + for _, warnings in reports: + cells.append("n/a" if warnings is None else str(sum(1 for w in warnings if w.file in changed))) + out.append("| In changed files | " + " | ".join(cells) + " |") + cells = ["build log missing" if w is None else str(len(w)) for _, w in reports] + out.append("| Total (deduplicated) | " + " | ".join(cells) + " |") + out.append("") + + for title, warnings in reports: + if not warnings: + continue + if changed is not None: + in_changed = [w for w in warnings if w.file in changed] + if in_changed: + out += [f"
{title}: {len(in_changed)} in changed files", "", + format_list(in_changed), "", "
", ""] + out += [f"
{title}: all {len(warnings)} warnings by type", "", + format_top_flags(warnings), "", "
", ""] + + out.append("Warnings in `dependencies/`, system headers and generated files are excluded. " + "Updated automatically on each push.") + print("\n".join(out)) + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/.github/workflows/clang-tidy.yml b/.github/workflows/clang-tidy.yml deleted file mode 100644 index e602b7b..0000000 --- a/.github/workflows/clang-tidy.yml +++ /dev/null @@ -1,70 +0,0 @@ -name: clang-tidy - -# Builds with clang-tidy enabled (using the checks in .clang-tidy). -# clang-tidy warnings are reported in the log and the job summary, but do not fail the build. - -on: - push: - branches: [ main ] - pull_request: - branches: [ main ] - workflow_dispatch: - -permissions: - contents: read - -concurrency: - group: ${{ github.workflow }}-${{ github.ref }} - cancel-in-progress: ${{ github.event_name == 'pull_request' }} - -env: - # Keep in sync with the job name below. - LLVM_VERSION: 22 - -jobs: - - clang-tidy: - name: clang-tidy (clang-22) - runs-on: ubuntu-26.04 - timeout-minutes: 90 - - steps: - - uses: actions/checkout@v7 - - - name: Install Dependencies - run: | - sudo apt-get update - sudo apt-get install libgtkmm-3.0-dev gettext - - # toolchains/linux-dev-with-tidy.cmake hardcodes an unversioned "clang-tidy", - # so we use linux-dev.cmake and pass the versioned binary. - - name: Configure CMake - env: - CC: clang-${{ env.LLVM_VERSION }} - CXX: clang++-${{ env.LLVM_VERSION }} - run: > - cmake -B build -S "$GITHUB_WORKSPACE" - -DCMAKE_BUILD_TYPE=Debug - -DCMAKE_TOOLCHAIN_FILE="$GITHUB_WORKSPACE/toolchains/linux-dev.cmake" - -DCMAKE_CXX_CLANG_TIDY="clang-tidy-${LLVM_VERSION}" - - - name: Build with clang-tidy - run: | - set -o pipefail - cmake --build build --parallel "$(nproc)" 2>&1 | tee build.log - - - name: Summary - if: ${{ always() }} - run: | - count=$(grep -cE '^[^ ]+:[0-9]+:[0-9]+: warning: .*\[[a-z0-9.,-]+\]$' build.log || true) - { - echo "### clang-tidy" - echo - echo "Warnings: ${count}" - echo - echo "Top checks:" - echo - echo '```' - grep -oE '\[[a-z0-9.-]+(,[a-z0-9.-]+)*\]$' build.log | sort | uniq -c | sort -rn | head -20 || true - echo '```' - } >> "$GITHUB_STEP_SUMMARY" diff --git a/.github/workflows/warnings-report.yml b/.github/workflows/warnings-report.yml new file mode 100644 index 0000000..ced1ab6 --- /dev/null +++ b/.github/workflows/warnings-report.yml @@ -0,0 +1,122 @@ +name: Warnings Report + +# Builds with clang-tidy (checks from .clang-tidy) and with GCC (project warning flags), +# then posts a deduplicated warnings report as a PR comment (updated on each push). +# Warnings do not fail the build. + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} + +jobs: + + build: + name: ${{ matrix.name }} + runs-on: ubuntu-26.04 + timeout-minutes: 90 + strategy: + fail-fast: false + matrix: + include: [ + { name: "clang-tidy (clang-22)", id: clang-tidy, cc: clang-22, cxx: clang++-22, tidy: clang-tidy-22 }, + { name: "gcc-15 warnings", id: gcc, cc: gcc-15, cxx: g++-15, tidy: "" }, + ] + + steps: + - uses: actions/checkout@v7 + + - name: Install Dependencies + run: | + sudo apt-get update + sudo apt-get install libgtkmm-3.0-dev gettext + + # toolchains/linux-dev-with-tidy.cmake hardcodes an unversioned "clang-tidy", + # so we use linux-dev.cmake and pass the versioned binary. + - name: Configure CMake + env: + CC: ${{ matrix.cc }} + CXX: ${{ matrix.cxx }} + run: > + cmake -B build -S "$GITHUB_WORKSPACE" + -DCMAKE_BUILD_TYPE=Debug + -DCMAKE_TOOLCHAIN_FILE="$GITHUB_WORKSPACE/toolchains/linux-dev.cmake" + -DCMAKE_CXX_CLANG_TIDY="${{ matrix.tidy }}" + + - name: Build + run: | + set -o pipefail + cmake --build build --parallel "$(nproc)" 2>&1 | tee build.log + + - name: Upload build log + if: ${{ always() }} + uses: actions/upload-artifact@v7 + with: + name: build-log-${{ matrix.id }} + path: build.log + if-no-files-found: ignore + retention-days: 7 + + + report: + needs: build + if: ${{ always() }} + runs-on: ubuntu-latest + timeout-minutes: 10 + permissions: + contents: read + pull-requests: write + + steps: + - uses: actions/checkout@v7 + + - name: Download build logs + uses: actions/download-artifact@v8 + with: + pattern: build-log-* + path: logs + + - name: Get changed files + if: ${{ github.event_name == 'pull_request' }} + env: + GH_TOKEN: ${{ github.token }} + PR_NUMBER: ${{ github.event.pull_request.number }} + run: > + gh api --paginate "repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER/files" -q '.[].filename' + > changed-files.txt + + - name: Generate report + run: | + changed=() + if [ -f changed-files.txt ]; then changed=(--changed-files changed-files.txt); fi + python3 .github/scripts/warnings_report.py \ + --workspace "$GITHUB_WORKSPACE" --build-dir build "${changed[@]}" \ + --log "clang-tidy (clang-22)=logs/build-log-clang-tidy/build.log" \ + --log "GCC 15=logs/build-log-gcc/build.log" \ + > report.md + cat report.md >> "$GITHUB_STEP_SUMMARY" + + # The token is read-only for PRs from forks, so the report is only in the job summary there. + - name: Post or update PR comment + if: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository }} + env: + GH_TOKEN: ${{ github.token }} + PR_NUMBER: ${{ github.event.pull_request.number }} + run: | + marker='' + comment_id=$(gh api --paginate "repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER/comments" \ + -q ".[] | select(.user.login == \"github-actions[bot]\" and (.body | startswith(\"$marker\"))) | .id" | head -n 1) + if [ -n "$comment_id" ]; then + gh api -X PATCH "repos/$GITHUB_REPOSITORY/issues/comments/$comment_id" -F body=@report.md > /dev/null + else + gh api -X POST "repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER/comments" -F body=@report.md > /dev/null + fi