[PATCH] D158929: [clang-tidy] Add exit code support to clang-tidy-diff.py

Piotr Zegar via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Aug 26 05:09:23 PDT 2023


PiotrZSL created this revision.
PiotrZSL added reviewers: njames93, carlosgalvezp.
Herald added a subscriber: xazax.hun.
Herald added a project: All.
PiotrZSL requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

Modify script to fail when some run clang-tidy
command fails. Based on run_clang-tidy.

Fixes: #65000


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D158929

Files:
  clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py
  clang-tools-extra/docs/ReleaseNotes.rst


Index: clang-tools-extra/docs/ReleaseNotes.rst
===================================================================
--- clang-tools-extra/docs/ReleaseNotes.rst
+++ clang-tools-extra/docs/ReleaseNotes.rst
@@ -113,6 +113,10 @@
 
 - Improved `--dump-config` to print check options in alphabetical order.
 
+- Improved :program:`clang-tidy-diff.py` script. It now returns exit code `1`
+  if any :program:`clang-tidy` subprocess exits with a non-zero code or if
+  exporting fixes fails.
+
 New checks
 ^^^^^^^^^^
 
Index: clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py
===================================================================
--- clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py
+++ clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py
@@ -49,7 +49,7 @@
     import queue as queue
 
 
-def run_tidy(task_queue, lock, timeout):
+def run_tidy(task_queue, lock, timeout, failed_files):
     watchdog = None
     while True:
         command = task_queue.get()
@@ -63,6 +63,14 @@
                 watchdog.start()
 
             stdout, stderr = proc.communicate()
+            if proc.returncode != 0:
+                if proc.returncode < 0:
+                    msg = "Terminated by signal %d : %s\n" % (
+                        -proc.returncode,
+                        " ".join(command),
+                    )
+                    stderr += msg.encode("utf-8")
+                failed_files.append(command)
 
             with lock:
                 sys.stdout.write(stdout.decode("utf-8") + "\n")
@@ -84,9 +92,11 @@
             task_queue.task_done()
 
 
-def start_workers(max_tasks, tidy_caller, task_queue, lock, timeout):
+def start_workers(max_tasks, tidy_caller, task_queue, lock, timeout, failed_files):
     for _ in range(max_tasks):
-        t = threading.Thread(target=tidy_caller, args=(task_queue, lock, timeout))
+        t = threading.Thread(
+            target=tidy_caller, args=(task_queue, lock, timeout, failed_files)
+        )
         t.daemon = True
         t.start()
 
@@ -259,8 +269,13 @@
     # A lock for console output.
     lock = threading.Lock()
 
+    # List of files with a non-zero return code.
+    failed_files = []
+
     # Run a pool of clang-tidy workers.
-    start_workers(max_task_count, run_tidy, task_queue, lock, args.timeout)
+    start_workers(
+        max_task_count, run_tidy, task_queue, lock, args.timeout, failed_files
+    )
 
     # Form the common args list.
     common_clang_tidy_args = []
@@ -301,8 +316,15 @@
 
         task_queue.put(command)
 
+    # Application return code
+    return_code = 0
+
     # Wait for all threads to be done.
     task_queue.join()
+    # Application return code
+    return_code = 0
+    if len(failed_files):
+        return_code = 1
 
     if yaml and args.export_fixes:
         print("Writing fixes to " + args.export_fixes + " ...")
@@ -311,9 +333,11 @@
         except:
             sys.stderr.write("Error exporting fixes.\n")
             traceback.print_exc()
+            return_code = 1
 
     if tmpdir:
         shutil.rmtree(tmpdir)
+    sys.exit(return_code)
 
 
 if __name__ == "__main__":


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D158929.553721.patch
Type: text/x-patch
Size: 3123 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230826/c682fb1e/attachment-0001.bin>


More information about the cfe-commits mailing list