[PATCH] D45776: [clang-tidy] Customize FileCheck prefix in check_clang-tidy.py

Zinovy Nis via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 18 09:38:24 PDT 2018


zinovy.nis created this revision.
zinovy.nis added a project: clang-tools-extra.
Herald added subscribers: cfe-commits, xazax.hun.

The patch introduces a new command line option `-check_suffix` to allow multiple %check_clang_tidy% in tests.

Sample:

  // RUN: %check_clang_tidy %s misc-unused-using-decls %t -check_suffix=-FLAG_1--  <option set 1>
  // RUN: %check_clang_tidy %s misc-unused-using-decls %t -check_suffix=-FLAG_2 -- <option set 2> 
  ...
  +// CHECK-MESSAGES-FLAG_1: :[[@LINE-4]]:10: warning: using decl 'B' is unused [misc-unused-using-decls]
  +// CHECK-MESSAGES-FLAG_2: :[[@LINE-7]]:10: warning: using decl 'A' is unused [misc-unused-using-decls]
  +// CHECK-FIXES-FLAG_1-NOT: using a::A;$
  +// CHECK-FIXES-FLAG_2-NOT: using a::B;$


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D45776

Files:
  test/clang-tidy/check_clang_tidy.cpp
  test/clang-tidy/check_clang_tidy.py


Index: test/clang-tidy/check_clang_tidy.py
===================================================================
--- test/clang-tidy/check_clang_tidy.py
+++ test/clang-tidy/check_clang_tidy.py
@@ -42,6 +42,7 @@
   parser.add_argument('-expect-clang-tidy-error', action='store_true')
   parser.add_argument('-resource-dir')
   parser.add_argument('-assume-filename')
+  parser.add_argument('-check_suffix', default='')
   parser.add_argument('input_file_name')
   parser.add_argument('check_name')
   parser.add_argument('temp_file_name')
@@ -70,6 +71,12 @@
       clang_tidy_extra_args.extend(
           ['-fobjc-abi-version=2', '-fobjc-arc'])
 
+  if args.check_suffix and not re.match('^[A-Z0-9-_]+$', args.check_suffix):
+    sys.exit('Only A..Z, 0..9, "-" and "_" are allowed in check suffix, but "%s" was given' % (args.check_suffix))
+
+  check_fixes_prefix = 'CHECK-FIXES' + args.check_suffix
+  check_messages_prefix = 'CHECK-MESSAGES' + args.check_suffix
+
   # Tests should not rely on STL being available, and instead provide mock
   # implementations of relevant APIs.
   clang_tidy_extra_args.append('-nostdinc++')
@@ -80,17 +87,19 @@
   with open(input_file_name, 'r') as input_file:
     input_text = input_file.read()
 
-  has_check_fixes = input_text.find('CHECK-FIXES') >= 0
-  has_check_messages = input_text.find('CHECK-MESSAGES') >= 0
+  has_check_fixes = check_fixes_prefix in input_text
+  has_check_messages = check_messages_prefix in input_text
 
   if not has_check_fixes and not has_check_messages:
-    sys.exit('Neither CHECK-FIXES nor CHECK-MESSAGES found in the input')
+    sys.exit('Neither %s nor %s found in the input' % (check_fixes_prefix, check_messages_prefix) )
 
   # Remove the contents of the CHECK lines to avoid CHECKs matching on
   # themselves.  We need to keep the comments to preserve line numbers while
   # avoiding empty lines which could potentially trigger formatting-related
   # checks.
-  cleaned_test = re.sub('// *CHECK-[A-Z-]*:[^\r\n]*', '//', input_text)
+  cleaned_test = re.sub('// *CHECK-[A-Z0-9\-_]*:[^\r\n]*', '//', input_text)
+
+  print "\n\n\n---->>>",input_text, "<<-----\n\n"
 
   write_file(temp_file_name, cleaned_test)
 
@@ -128,7 +137,7 @@
     try:
       subprocess.check_output(
           ['FileCheck', '-input-file=' + temp_file_name, input_file_name,
-           '-check-prefix=CHECK-FIXES', '-strict-whitespace'],
+           '-check-prefix=' + check_fixes_prefix, '-strict-whitespace'],
           stderr=subprocess.STDOUT)
     except subprocess.CalledProcessError as e:
       print('FileCheck failed:\n' + e.output.decode())
@@ -140,7 +149,7 @@
     try:
       subprocess.check_output(
           ['FileCheck', '-input-file=' + messages_file, input_file_name,
-           '-check-prefix=CHECK-MESSAGES',
+           '-check-prefix=' + check_messages_prefix,
            '-implicit-check-not={{warning|error}}:'],
           stderr=subprocess.STDOUT)
     except subprocess.CalledProcessError as e:
Index: test/clang-tidy/check_clang_tidy.cpp
===================================================================
--- /dev/null
+++ test/clang-tidy/check_clang_tidy.cpp
@@ -0,0 +1,15 @@
+// RUN: %check_clang_tidy %s misc-unused-using-decls %t
+// RUN: %check_clang_tidy %s misc-unused-using-decls %t -check_suffix=-FLAG -- -DFLAG
+namespace a { class A {}; class B {};}
+namespace b {
+#if defined(FLAG)
+using a::A;
+#else
+using a::B;
+#endif
+}
+namespace c {}
+// CHECK-MESSAGES: :[[@LINE-4]]:10: warning: using decl 'B' is unused [misc-unused-using-decls]
+// CHECK-MESSAGES-FLAG: :[[@LINE-7]]:10: warning: using decl 'A' is unused [misc-unused-using-decls]
+// CHECK-FIXES-FLAG-NOT: using a::A;$
+// CHECK-FIXES-NOT: using a::B;$
\ No newline at end of file


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D45776.142953.patch
Type: text/x-patch
Size: 3747 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180418/9061873a/attachment.bin>


More information about the cfe-commits mailing list