[clang-tools-extra] r316090 - Support Objective-C/C++ source files in check_clang_tidy.py

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 18 08:56:39 PDT 2017


Author: hokein
Date: Wed Oct 18 08:56:39 2017
New Revision: 316090

URL: http://llvm.org/viewvc/llvm-project?rev=316090&view=rev
Log:
Support Objective-C/C++ source files in check_clang_tidy.py

check_clang_tidy.py currently only handles C and C++ source files.

This extends the logic to also handle Objective-C (.m) and
Objective-C++ (.mm) files.

However, by default, clang compiles .m/.mm files using Objective-C 1.0
syntax. Objective-C 2.0 has been the default in Xcode for about 10
years, and Objective-C Automatic Reference Counting (ARC) for about 6
years, so this enables both by default.

(Clients which actually want to test clang-tidy checks for Objective-C
 1.0 or non-ARC files can pass custom flags to check_clang_tidy.py
 after --, which will disable the Objective-C 2.0 and ARC flags).

I did not add logic to handle running clang-tidy on Objective-C header
files alone; they also use the .h file extension, so we'd need to
look inside their contents.

I included a new test to confirm the new behavior.

Depends On D38963

Patch by Ben Hamilton!

Modified:
    clang-tools-extra/trunk/test/clang-tidy/check_clang_tidy.py

Modified: clang-tools-extra/trunk/test/clang-tidy/check_clang_tidy.py
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/check_clang_tidy.py?rev=316090&r1=316089&r2=316090&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/check_clang_tidy.py (original)
+++ clang-tools-extra/trunk/test/clang-tidy/check_clang_tidy.py Wed Oct 18 08:56:39 2017
@@ -26,6 +26,7 @@ Example:
 """
 
 import argparse
+import os
 import re
 import subprocess
 import sys
@@ -53,18 +54,19 @@ def main():
   temp_file_name = args.temp_file_name
 
   file_name_with_extension = assume_file_name or input_file_name
-
-  extension = '.cpp'
-  if (file_name_with_extension.endswith('.c')):
-    extension = '.c'
-  if (file_name_with_extension.endswith('.hpp')):
-    extension = '.hpp'
+  _, extension = os.path.splitext(file_name_with_extension)
+  if extension not in ['.c', '.hpp', '.m', '.mm']:
+    extension = '.cpp'
   temp_file_name = temp_file_name + extension
 
   clang_tidy_extra_args = extra_args
   if len(clang_tidy_extra_args) == 0:
-    clang_tidy_extra_args = ['--', '--std=c++11'] \
-        if extension == '.cpp' or extension == '.hpp' else ['--']
+    clang_tidy_extra_args = ['--']
+    if extension in ['.cpp', '.hpp', '.mm']:
+      clang_tidy_extra_args.append('--std=c++11')
+    if extension in ['.m', '.mm']:
+      clang_tidy_extra_args.extend(
+          ['-fobjc-abi-version=2', '-fobjc-arc'])
 
   # Tests should not rely on STL being available, and instead provide mock
   # implementations of relevant APIs.




More information about the cfe-commits mailing list