[clang-tidy] Handle missing yaml module in run-clang-tidy.py
The Yaml module is missing on some systems and on many of clang buildbots.
But the test for run-clang-tidy.py doesn't fail due to 'NOT' statement masking a python runtime error.
This patch conditionally imports and enables the yaml module only if it's present in the system.
If not, then '-export-fixes' is disabled.
Differential Revision: https://reviews.llvm.org/D59734
llvm-svn: 357114
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 5ec40f2..cfd4f41 100755
--- a/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
+++ b/clang-tools-extra/clang-tidy/tool/run-clang-tidy.py
@@ -47,7 +47,11 @@
import tempfile
import threading
import traceback
-import yaml
+
+try:
+ import yaml
+except ImportError:
+ yaml = None
is_py2 = sys.version[0] == '2'
@@ -199,9 +203,10 @@
'headers to output diagnostics from. Diagnostics from '
'the main file of each translation unit are always '
'displayed.')
- parser.add_argument('-export-fixes', metavar='filename', dest='export_fixes',
- help='Create a yaml file to store suggested fixes in, '
- 'which can be applied with clang-apply-replacements.')
+ if yaml:
+ parser.add_argument('-export-fixes', metavar='filename', dest='export_fixes',
+ help='Create a yaml file to store suggested fixes in, '
+ 'which can be applied with clang-apply-replacements.')
parser.add_argument('-j', type=int, default=0,
help='number of tidy instances to be run in parallel.')
parser.add_argument('files', nargs='*', default=['.*'],
@@ -254,7 +259,7 @@
max_task = multiprocessing.cpu_count()
tmpdir = None
- if args.fix or args.export_fixes:
+ if args.fix or (yaml and args.export_fixes):
check_clang_apply_replacements_binary(args)
tmpdir = tempfile.mkdtemp()
@@ -292,7 +297,7 @@
shutil.rmtree(tmpdir)
os.kill(0, 9)
- if args.export_fixes:
+ if yaml and args.export_fixes:
print('Writing fixes to ' + args.export_fixes + ' ...')
try:
merge_replacement_files(tmpdir, args.export_fixes)