[clang-tools-extra] r301365 - [clang-tidy] run-clang-tidy.py: check if clang-apply-replacements succeeds

Jakub Kuderski via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 25 15:38:40 PDT 2017


Author: kuhar
Date: Tue Apr 25 17:38:39 2017
New Revision: 301365

URL: http://llvm.org/viewvc/llvm-project?rev=301365&view=rev
Log:
[clang-tidy] run-clang-tidy.py: check if clang-apply-replacements succeeds

Summary:
When running run-clang-tidy.py with -fix it tries to apply found replacements at the end.
If there are errors running clang-apply-replacements, the script currently crashes or displays no error at all.

This patch checks for errors running clang-apply-replacements the same way clang-tidy binary is handled.

Another option would be probably checking for clang-apply-replacements (when -fix is passed) even before running clang-tidy.

Reviewers: Prazek, alexfh, bkramer, mfherbst

Reviewed By: Prazek, alexfh

Subscribers: kimgr, cfe-commits

Tags: #clang-tools-extra

Differential Revision: https://reviews.llvm.org/D32294

Modified:
    clang-tools-extra/trunk/clang-tidy/tool/run-clang-tidy.py

Modified: clang-tools-extra/trunk/clang-tidy/tool/run-clang-tidy.py
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/tool/run-clang-tidy.py?rev=301365&r1=301364&r2=301365&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/tool/run-clang-tidy.py (original)
+++ clang-tools-extra/trunk/clang-tidy/tool/run-clang-tidy.py Tue Apr 25 17:38:39 2017
@@ -34,6 +34,7 @@ Compilation database setup:
 http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html
 """
 
+from __future__ import print_function
 import argparse
 import json
 import multiprocessing
@@ -45,6 +46,7 @@ import subprocess
 import sys
 import tempfile
 import threading
+import traceback
 
 
 def find_compilation_database(path):
@@ -52,7 +54,7 @@ def find_compilation_database(path):
   result = './'
   while not os.path.isfile(os.path.join(result, path)):
     if os.path.realpath(result) == '/':
-      print 'Error: could not find compilation database.'
+      print('Error: could not find compilation database.')
       sys.exit(1)
     result += '../'
   return os.path.realpath(result)
@@ -87,6 +89,17 @@ def get_tidy_invocation(f, clang_tidy_bi
   return start
 
 
+def check_clang_apply_replacements_binary(args):
+  """Checks if invoking supplied clang-apply-replacements binary works."""
+  try:
+    subprocess.check_call([args.clang_apply_replacements_binary, '--version'])
+  except:
+    print('Unable to run clang-apply-replacements. Is clang-apply-replacements '
+          'binary correctly specified?', file=sys.stderr)
+    traceback.print_exc()
+    sys.exit(1)
+
+
 def apply_fixes(args, tmpdir):
   """Calls clang-apply-fixes on a given directory. Deletes the dir when done."""
   invocation = [args.clang_apply_replacements_binary]
@@ -94,7 +107,6 @@ def apply_fixes(args, tmpdir):
     invocation.append('-format')
   invocation.append(tmpdir)
   subprocess.call(invocation)
-  shutil.rmtree(tmpdir)
 
 
 def run_tidy(args, tmpdir, build_path, queue):
@@ -164,9 +176,9 @@ def main():
     if args.checks:
       invocation.append('-checks=' + args.checks)
     invocation.append('-')
-    print subprocess.check_output(invocation)
+    print(subprocess.check_output(invocation))
   except:
-    print >>sys.stderr, "Unable to run clang-tidy."
+    print("Unable to run clang-tidy.", file=sys.stderr)
     sys.exit(1)
 
   # Load the database and extract all files.
@@ -179,6 +191,7 @@ def main():
 
   tmpdir = None
   if args.fix:
+    check_clang_apply_replacements_binary(args)
     tmpdir = tempfile.mkdtemp()
 
   # Build up a big regexy filter from all command line arguments.
@@ -204,14 +217,25 @@ def main():
   except KeyboardInterrupt:
     # This is a sad hack. Unfortunately subprocess goes
     # bonkers with ctrl-c and we start forking merrily.
-    print '\nCtrl-C detected, goodbye.'
+    print('\nCtrl-C detected, goodbye.')
     if args.fix:
       shutil.rmtree(tmpdir)
     os.kill(0, 9)
 
   if args.fix:
-    print 'Applying fixes ...'
-    apply_fixes(args, tmpdir)
+    print('Applying fixes ...')
+    successfully_applied = False
+
+    try:
+      apply_fixes(args, tmpdir)
+      successfully_applied = True
+    except:
+      print('Error applying fixes.\n', file=sys.stderr)
+      traceback.print_exc()
+
+    shutil.rmtree(tmpdir)
+    if not successfully_applied:
+      sys.exit(1)
 
 if __name__ == '__main__':
   main()




More information about the cfe-commits mailing list