[llvm] [GitHub][CI] Add clang-tidy premerge workflow (PR #154829)

Baranov Victor via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 24 03:08:44 PDT 2025


================
@@ -0,0 +1,345 @@
+#!/usr/bin/env python3
+#
+# ====- clang-tidy-helper, runs clang-tidy from the ci --*- python -*-------==#
+#
+# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for license information.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+#
+# ==-------------------------------------------------------------------------==#
+
+import argparse
+import os
+import subprocess
+import sys
+from typing import List, Optional
+
+"""
+This script is run by GitHub actions to ensure that the code in PR's conform to
+the coding style of LLVM. The canonical source of this script is in the LLVM
+source tree under llvm/utils/git.
+
+You can learn more about the LLVM coding style on llvm.org:
+https://llvm.org/docs/CodingStandards.html
+"""
+
+
+class LintArgs:
+    start_rev: str = None
+    end_rev: str = None
+    repo: str = None
+    changed_files: List[str] = []
+    token: str = None
+    verbose: bool = True
+    issue_number: int = 0
+    build_path: str = "build"
+    clang_tidy_binary: str = "clang-tidy"
+
+    def __init__(self, args: argparse.Namespace = None) -> None:
+        if not args is None:
+            self.start_rev = args.start_rev
+            self.end_rev = args.end_rev
+            self.repo = args.repo
+            self.token = args.token
+            self.changed_files = args.changed_files
+            self.issue_number = args.issue_number
+            self.verbose = args.verbose
+            self.build_path = args.build_path
+            self.clang_tidy_binary = args.clang_tidy_binary
+
+
+COMMENT_TAG = "<!--LLVM CODE LINT COMMENT: clang-tidy-->"
+
+
+def get_instructions(cpp_files: List[str]) -> str:
+    files_str = " ".join(cpp_files)
+    return f"""
+git diff -U0 origin/main..HEAD -- {files_str} |
+python3 clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py \\
+  -path build -p1 -quiet"""
+
+
+def clean_clang_tidy_output(output: str) -> Optional[str]:
+    """
+    - Remove 'Running clang-tidy in X threads...' line
+    - Remove 'N warnings generated.' line
+    - Strip leading workspace path from file paths
+    """
+    if not output or output == "No relevant changes found.":
+        return None
+
+    lines = output.split("\n")
+    cleaned_lines = []
+
+    for line in lines:
+        if line.startswith("Running clang-tidy in") or line.endswith("generated."):
+            continue
+
+        # Remove everything up to rightmost "llvm-project/" for correct files names
+        idx = line.rfind("llvm-project/")
+        if idx != -1:
+            line = line[idx + len("llvm-project/") :]
+
+        cleaned_lines.append(line)
+
+    if cleaned_lines:
+        return "\n".join(cleaned_lines)
+    return None
+
+
+def should_lint_file(filepath: str) -> bool:
+    # For add other paths/files to this function
+    return filepath.startswith("clang-tools-extra/clang-tidy/")
+
+
+def filter_changed_files(changed_files: List[str]) -> List[str]:
+    filtered_files = []
+    for filepath in changed_files:
+        _, ext = os.path.splitext(filepath)
+        if ext not in (".cpp", ".c", ".h", ".hpp", ".hxx", ".cxx"):
+            continue
+        if not should_lint_file(filepath):
+            continue
+        if os.path.exists(filepath):
+            filtered_files.append(filepath)
+
+    return filtered_files
+
+
+def has_clang_tidy(clang_tidy_binary: str) -> bool:
+    cmd = [clang_tidy_binary, "--version"]
+    proc = None
+    try:
+        proc = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+    except:
+        return False
+    return proc.returncode == 0
+
+
+def create_comment_text(warning: str, cpp_files: List[str]) -> str:
+    instructions = get_instructions(cpp_files)
+    return f"""
----------------
vbvictor wrote:

Using `textwrap.dedent` gave incorrect formatting.
Since the readability isn't dramatically worse without `textwrap`, I'd leave as is for now.

<img width="1597" height="1008" alt="image" src="https://github.com/user-attachments/assets/3215bd8a-a343-4d77-8566-10a995603820" />


https://github.com/llvm/llvm-project/pull/154829


More information about the llvm-commits mailing list