[llvm] [Code Coverage] Add a tool to check test coverage of a patch (PR #71841)

Aiden Grossman via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 13 19:16:58 PST 2023


================
@@ -0,0 +1,722 @@
+#!/usr/bin/env python3
+#
+# ===- git-check-coverage - CheckCoverage Git Integration ---------*- 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
+#
+# ===------------------------------------------------------------------------===#
+
+r"""
+code-coverage git integration
+============================
+This file provides a code-coverage integration for git. Put it in your
+llvm-project root directory and ensure that it is executable. Code
+coverage information will be provided for the last commit/HEAD by
+runing below command.
+Example uses -
+          git check-coverage -b build bin/opt llvm/test
+Here b is build directory (optional, default is build)
+next we have binray
+and then test suite path
+"""
+
+import argparse
+import logging
+import os
+import subprocess
+import re
+import sys
+from unidiff import PatchSet
+
+
+# Configure the logging module
+def configure_logging(build_dir):
+    logging.basicConfig(
+        filename=os.path.join(
+            build_dir, "patch_coverage.log"
+        ),  # Specify the log file in the build directory
+        level=logging.INFO,  # Set the logging level to INFO
+        format="%(message)s",  # Specify the log message format
+    )
+
+
+# Define a custom print function that writes to both the log file and the terminal
+def custom_print(*args):
+    message = " ".join(map(str, args))
+    logging.info(message)  # Write to the log file
+
+
+def create_patch_from_last_commit(output_path):
+    """Create a patch file from the last commit in the Git repository."""
+
+    try:
+        # Create the patch from the last commit
+        patch_cmd = ["git", "format-patch", "-1", "--stdout"]
+        patch_output = subprocess.check_output(patch_cmd).decode("utf-8", "ignore")
+
+        # Write the patch to the output file in binary mode
+        with open(output_path, "wb") as patch_file:
+            patch_file.write(patch_output.encode("utf-8"))
+
+        print("Patch file '{}' created successfully.".format(output_path))
+        print("")
+
+    except subprocess.CalledProcessError as e:
+        print("Error while creating the patch from the last commit:", e)
+        sys.exit(1)
+
+
+def extract_source_files_from_patch(patch_path):
+    """Read the patch file and extract the names of .cpp and .h files that
+    have been modified or added in the patch."""
+
+    try:
+        source_files = []
+        with open(patch_path, "rb") as patch_file:
+            patch_diff = patch_file.read().decode("utf-8", "ignore")
+
+        # Use regular expression to find .cpp files in the patch
+        source_file_matches = re.findall(r"\+{3} b/(\S+\.(?:cpp|c))", patch_diff)
----------------
boomanaiden154 wrote:

It says in the function description that this grabs header files too? Not sure if we actually to grab header files though. In addition, we also want to capture more extensions:
https://github.com/llvm/llvm-project/blob/1f3d13c415a2a92e5a3811740ed55c4bcff3f539/llvm/utils/git/code-format-helper.py#L172

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


More information about the llvm-commits mailing list