[clang-tools-extra] r323919 - [clang-tidy] New argument --language to add_new_check.py

Ben Hamilton via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 31 13:52:39 PST 2018


Author: benhamilton
Date: Wed Jan 31 13:52:39 2018
New Revision: 323919

URL: http://llvm.org/viewvc/llvm-project?rev=323919&view=rev
Log:
[clang-tidy] New argument --language to add_new_check.py

Summary:
Currently, add_new_check.py assumes all checks are for C++ code.

This adds a new argument --language=[LANG] to add_new_check.py
so authors of new checks can specify that the test file should
be in a different language.

For example, authors can pass --language=objc for Objective-C
clang-tidy checks.

Reviewers: hokein, alexfh

Reviewed By: alexfh

Subscribers: Wizard, xazax.hun

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

Modified:
    clang-tools-extra/trunk/clang-tidy/add_new_check.py

Modified: clang-tools-extra/trunk/clang-tidy/add_new_check.py
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/add_new_check.py?rev=323919&r1=323918&r2=323919&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/add_new_check.py (original)
+++ clang-tools-extra/trunk/clang-tidy/add_new_check.py Wed Jan 31 13:52:39 2018
@@ -9,6 +9,7 @@
 #
 #===------------------------------------------------------------------------===#
 
+import argparse
 import os
 import re
 import sys
@@ -221,10 +222,10 @@ def add_release_notes(module_path, modul
 
 
 # Adds a test for the check.
-def write_test(module_path, module, check_name):
+def write_test(module_path, module, check_name, test_extension):
   check_name_dashes = module + '-' + check_name
   filename = os.path.normpath(os.path.join(module_path, '../../test/clang-tidy',
-                                           check_name_dashes + '.cpp'))
+                                           check_name_dashes + '.' + test_extension))
   print('Creating %s...' % filename)
   with open(filename, 'wb') as f:
     f.write("""// RUN: %%check_clang_tidy %%s %(check_name_dashes)s %%t
@@ -300,22 +301,37 @@ FIXME: Describe what patterns does the c
 
 
 def main():
-  if len(sys.argv) == 2 and sys.argv[1] == '--update-docs':
-    update_checks_list(os.path.dirname(sys.argv[0]))
-    return
-
-  if len(sys.argv) != 3:
-    print """\
-Usage: add_new_check.py <module> <check>, e.g.
-  add_new_check.py misc awesome-functions
-
-Alternatively, run 'add_new_check.py --update-docs' to just update the list of
-documentation files."""
+  language_to_extension = {
+      'c': 'c',
+      'c++': 'cpp',
+      'objc': 'm',
+      'objc++': 'mm',
+  }
+  parser = argparse.ArgumentParser()
+  parser.add_argument(
+      '--update-docs',
+      action='store_true',
+      help='just update the list of documentation files, then exit')
+  parser.add_argument(
+      '--language',
+      help='language to use for new check (defaults to c++)',
+      choices=language_to_extension.keys(),
+      default='c++',
+      metavar='LANG')
+  parser.add_argument(
+      'module',
+      help='module directory under which to place the new tidy check (e.g., misc)')
+  parser.add_argument(
+      'check',
+      help='name of new tidy check to add (e.g. foo-do-the-stuff)')
+  args = parser.parse_args()
 
+  if args.update_docs:
+    update_checks_list(os.path.dirname(sys.argv[0]))
     return
 
-  module = sys.argv[1]
-  check_name = sys.argv[2]
+  module = args.module
+  check_name = args.check
 
   if check_name.startswith(module):
     print 'Check name "%s" must not start with the module "%s". Exiting.' % (
@@ -332,7 +348,8 @@ documentation files."""
   write_implementation(module_path, module, check_name_camel)
   adapt_module(module_path, module, check_name, check_name_camel)
   add_release_notes(module_path, module, check_name)
-  write_test(module_path, module, check_name)
+  test_extension = language_to_extension.get(args.language)
+  write_test(module_path, module, check_name, test_extension)
   write_docs(module_path, module, check_name)
   update_checks_list(clang_tidy_path)
   print('Done. Now it\'s your turn!')




More information about the cfe-commits mailing list