[clang-tools-extra] [run-clang-tidy] Add option -export-directory (PR #69453)

Amadeus Gebauer via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 18 07:05:36 PDT 2023


https://github.com/amgebauer updated https://github.com/llvm/llvm-project/pull/69453

>From d63de8e3d8e71df445983516a57cc41cf47f1a6e Mon Sep 17 00:00:00 2001
From: Amadeus Gebauer <amadeus.gebauer at tum.de>
Date: Wed, 18 Oct 2023 14:02:44 +0200
Subject: [PATCH] [run-clang-tidy] Accept directory for -export-fixes

---
 .../clang-tidy/tool/run-clang-tidy.py         | 42 ++++++++++++-------
 1 file changed, 28 insertions(+), 14 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py b/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
index 312d9241cfa57c5..1cce241a031c6d7 100755
--- a/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
+++ b/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
@@ -308,10 +308,12 @@ def main():
     if yaml:
         parser.add_argument(
             "-export-fixes",
-            metavar="filename",
+            metavar="file_or_directory",
             dest="export_fixes",
-            help="Create a yaml file to store suggested fixes in, "
-            "which can be applied with clang-apply-replacements.",
+            help="A directory or a yaml file to store suggested fixes in, "
+            "which can be applied with clang-apply-replacements. If the "
+            "parameter is a directory, the fixes of each compilation unit are "
+            "stored in individual yaml files in the directory."
         )
     parser.add_argument(
         "-j",
@@ -384,14 +386,26 @@ def main():
 
     clang_tidy_binary = find_binary(args.clang_tidy_binary, "clang-tidy", build_path)
 
-    tmpdir = None
     if args.fix:
         clang_apply_replacements_binary = find_binary(
             args.clang_apply_replacements_binary, "clang-apply-replacements", build_path
         )
 
-    if args.fix or (yaml and args.export_fixes):
-        tmpdir = tempfile.mkdtemp()
+    combine_fixes = False
+    export_fixes_dir = None
+    delete_fixes_dir = True
+    if args.export_fixes is not None:
+        assert not args.export_fixes.endswith(os.path.sep) or os.path.isdir(args.export_fixes), "The export fixes directory does not exist."
+
+        if not os.path.isdir(args.export_fixes) and yaml:
+            combine_fixes = True
+        
+        if os.path.isdir(args.export_fixes):
+            export_fixes_dir = args.export_fixes
+            delete_fixes_dir = False
+        
+    if export_fixes_dir is None and (args.fix or combine_fixes):
+        export_fixes_dir = tempfile.mkdtemp()
 
     try:
         invocation = get_tidy_invocation(
@@ -450,7 +464,7 @@ def main():
                 args=(
                     args,
                     clang_tidy_binary,
-                    tmpdir,
+                    export_fixes_dir,
                     build_path,
                     task_queue,
                     lock,
@@ -474,14 +488,14 @@ def main():
         # This is a sad hack. Unfortunately subprocess goes
         # bonkers with ctrl-c and we start forking merrily.
         print("\nCtrl-C detected, goodbye.")
-        if tmpdir:
-            shutil.rmtree(tmpdir)
+        if export_fixes_dir is not None and delete_fixes_dir:
+            shutil.rmtree(export_fixes_dir)
         os.kill(0, 9)
 
-    if yaml and args.export_fixes:
+    if combine_fixes:
         print("Writing fixes to " + args.export_fixes + " ...")
         try:
-            merge_replacement_files(tmpdir, args.export_fixes)
+            merge_replacement_files(export_fixes_dir, args.export_fixes)
         except:
             print("Error exporting fixes.\n", file=sys.stderr)
             traceback.print_exc()
@@ -490,14 +504,14 @@ def main():
     if args.fix:
         print("Applying fixes ...")
         try:
-            apply_fixes(args, clang_apply_replacements_binary, tmpdir)
+            apply_fixes(args, clang_apply_replacements_binary, export_fixes_dir)
         except:
             print("Error applying fixes.\n", file=sys.stderr)
             traceback.print_exc()
             return_code = 1
 
-    if tmpdir:
-        shutil.rmtree(tmpdir)
+    if export_fixes_dir is not None and delete_fixes_dir:
+        shutil.rmtree(export_fixes_dir)
     sys.exit(return_code)
 
 



More information about the cfe-commits mailing list