diff options
author | Zinovy Nis <zinovy.nis@gmail.com> | 2018-04-21 15:23:56 +0000 |
---|---|---|
committer | Zinovy Nis <zinovy.nis@gmail.com> | 2018-04-21 15:23:56 +0000 |
commit | eba2857bed15028b6e2d2be3640b1de4c6957037 (patch) | |
tree | a0be7527e04fb191ea9f22f6ab0b84baa43e4c60 | |
parent | 93b102cd459e60b4c60648f17f956bb3f66b0c97 (diff) | |
download | bcm5719-llvm-eba2857bed15028b6e2d2be3640b1de4c6957037.tar.gz bcm5719-llvm-eba2857bed15028b6e2d2be3640b1de4c6957037.zip |
[clang-tidy] Customize FileCheck prefix in check_clang-tidy.py
The patch introduces a new command line option '-check-suffix' for check_clang_tidy.py
to allow multiple %check_clang_tidy% in a single test file.
Sample:
// RUN: %check_clang_tidy -check-suffix=FLAG-1 %s misc-unused-using-decls %t -- -- <options-set-1>
// RUN: %check_clang_tidy -check-suffix=FLAG-2 %s misc-unused-using-decls %t -- -- <options-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;$
Differential Revision: https://reviews.llvm.org/D45776
llvm-svn: 330511
-rw-r--r-- | clang-tools-extra/docs/clang-tidy/index.rst | 21 | ||||
-rw-r--r-- | clang-tools-extra/test/clang-tidy/check_clang_tidy.cpp | 21 | ||||
-rwxr-xr-x | clang-tools-extra/test/clang-tidy/check_clang_tidy.py | 25 |
3 files changed, 59 insertions, 8 deletions
diff --git a/clang-tools-extra/docs/clang-tidy/index.rst b/clang-tools-extra/docs/clang-tidy/index.rst index dc601ec345e..91c697711fd 100644 --- a/clang-tools-extra/docs/clang-tidy/index.rst +++ b/clang-tools-extra/docs/clang-tidy/index.rst @@ -673,6 +673,27 @@ source code is at `test/clang-tidy/google-readability-casting.cpp`_): // CHECK-FIXES: int b = a; } +To check more than one scenario in the same test file use +``-check-suffix=SUFFIX-NAME`` on ``check_clang_tidy.py`` command line. +With ``-check-suffix=SUFFIX-NAME`` you need to replace your ``CHECK-*`` +directives with ``CHECK-MESSAGES-SUFFIX-NAME`` and ``CHECK-FIXES-SUFFIX-NAME``. + +Here's an example: + +.. code-block:: c++ + + // RUN: %check_clang_tidy -check-suffix=USING-A %s misc-unused-using-decls %t -- -- -DUSING_A + // RUN: %check_clang_tidy -check-suffix=USING-B %s misc-unused-using-decls %t -- -- -DUSING_B + // RUN: %check_clang_tidy %s misc-unused-using-decls %t + ... + // CHECK-MESSAGES-USING-A: :[[@LINE-8]]:10: warning: using decl 'A' {{.*}} + // CHECK-MESSAGES-USING-B: :[[@LINE-7]]:10: warning: using decl 'B' {{.*}} + // CHECK-MESSAGES: :[[@LINE-6]]:10: warning: using decl 'C' {{.*}} + // CHECK-FIXES-USING-A-NOT: using a::A;$ + // CHECK-FIXES-USING-B-NOT: using a::B;$ + // CHECK-FIXES-NOT: using a::C;$ + + There are many dark corners in the C++ language, and it may be difficult to make your check work perfectly in all cases, especially if it issues fix-it hints. The most frequent pitfalls are macros and templates: diff --git a/clang-tools-extra/test/clang-tidy/check_clang_tidy.cpp b/clang-tools-extra/test/clang-tidy/check_clang_tidy.cpp new file mode 100644 index 00000000000..5c1bf92eecd --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/check_clang_tidy.cpp @@ -0,0 +1,21 @@ +// RUN: %check_clang_tidy -check-suffix=USING-A %s misc-unused-using-decls %t -- -- -DUSING_A +// RUN: %check_clang_tidy -check-suffix=USING-B %s misc-unused-using-decls %t -- -- -DUSING_B +// RUN: %check_clang_tidy %s misc-unused-using-decls %t + +namespace a {class A {}; class B {}; class C {}; } +namespace b { +#if defined(USING_A) +using a::A; +#elif defined(USING_B) +using a::B; +#else +using a::C; +#endif +} +namespace c {} +// CHECK-MESSAGES-USING-A: :[[@LINE-8]]:10: warning: using decl 'A' {{.*}} +// CHECK-MESSAGES-USING-B: :[[@LINE-7]]:10: warning: using decl 'B' {{.*}} +// CHECK-MESSAGES: :[[@LINE-6]]:10: warning: using decl 'C' {{.*}} +// CHECK-FIXES-USING-A-NOT: using a::A;$ +// CHECK-FIXES-USING-B-NOT: using a::B;$ +// CHECK-FIXES-NOT: using a::C;$ diff --git a/clang-tools-extra/test/clang-tidy/check_clang_tidy.py b/clang-tools-extra/test/clang-tidy/check_clang_tidy.py index 7fa03992bb7..bb2c5a26052 100755 --- a/clang-tools-extra/test/clang-tidy/check_clang_tidy.py +++ b/clang-tools-extra/test/clang-tidy/check_clang_tidy.py @@ -16,8 +16,9 @@ ClangTidy Test Helper This script runs clang-tidy in fix mode and verify fixes, messages or both. Usage: - check_clang_tidy.py [-resource-dir <resource-dir>] \ - [-assume-filename <file-with-source-extension>] \ + check_clang_tidy.py [-resource-dir=<resource-dir>] \ + [-assume-filename=<file-with-source-extension>] \ + [-check-suffix=<file-check-suffix>] \ <source-file> <check-name> <temp-file> \ -- [optional clang-tidy arguments] @@ -42,6 +43,7 @@ def main(): 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 +72,13 @@ def main(): 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)) + + file_check_suffix = ('-' + args.check_suffix) if args.check_suffix else '' + check_fixes_prefix = 'CHECK-FIXES' + file_check_suffix + check_messages_prefix = 'CHECK-MESSAGES' + file_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 +89,17 @@ def main(): 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) write_file(temp_file_name, cleaned_test) @@ -128,7 +137,7 @@ def main(): 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 @@ def main(): 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: |