New -assume-filename=param to check_clang_tidy.py (like clang-format)
authorHaojian Wu <hokein@google.com>
Wed, 18 Oct 2017 07:48:40 +0000 (07:48 +0000)
committerHaojian Wu <hokein@google.com>
Wed, 18 Oct 2017 07:48:40 +0000 (07:48 +0000)
Summary:
Currently, check_clang_tidy.py includes logic to select default
clang flags based on the extension of the source filename passed
as the first argument.

Since the source filename might be a temporary or test file with an
arbitrary extension unrelated to the file type, this adds the ability
to override the logic the same way `clang-format`'s -assume-filename=
parameter does.

I included a test with a nonstandard file extension. I confirmed
when I modified the warning message that the new test failed,
and that it passed again when I restored the warning message.

Ran tests with:

% cmake -G Ninja /path/to/llvm
% ninja check-clang-tools

Patch by Ben Hamilton!

Reviewers: hokein, alexfh

Reviewed By: hokein

Subscribers: alexfh

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

llvm-svn: 316066

clang-tools-extra/test/clang-tidy/check_clang_tidy.py
clang-tools-extra/test/clang-tidy/nonstandard-file-extension.test [new file with mode: 0644]

index 114f0d55946d5896f41a7545f5e1cec5eb4b9786..986b70be5e47f7e4e6e2ce447f87d07e5e276775 100755 (executable)
@@ -17,6 +17,7 @@ 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>] \
     <source-file> <check-name> <temp-file> \
     -- [optional clang-tidy arguments]
 
@@ -38,6 +39,7 @@ def write_file(file_name, text):
 def main():
   parser = argparse.ArgumentParser()
   parser.add_argument('-resource-dir')
+  parser.add_argument('-assume-filename')
   parser.add_argument('input_file_name')
   parser.add_argument('check_name')
   parser.add_argument('temp_file_name')
@@ -45,14 +47,17 @@ def main():
   args, extra_args = parser.parse_known_args()
 
   resource_dir = args.resource_dir
+  assume_file_name = args.assume_filename
   input_file_name = args.input_file_name
   check_name = args.check_name
   temp_file_name = args.temp_file_name
 
+  file_name_with_extension = assume_file_name or input_file_name
+
   extension = '.cpp'
-  if (input_file_name.endswith('.c')):
+  if (file_name_with_extension.endswith('.c')):
     extension = '.c'
-  if (input_file_name.endswith('.hpp')):
+  if (file_name_with_extension.endswith('.hpp')):
     extension = '.hpp'
   temp_file_name = temp_file_name + extension
 
diff --git a/clang-tools-extra/test/clang-tidy/nonstandard-file-extension.test b/clang-tools-extra/test/clang-tidy/nonstandard-file-extension.test
new file mode 100644 (file)
index 0000000..4cb4d11
--- /dev/null
@@ -0,0 +1,6 @@
+// RUN: %check_clang_tidy -assume-filename=const-cast.cpp %s cppcoreguidelines-pro-type-const-cast %t
+
+const int *i;
+int *j;
+void f() { j = const_cast<int *>(i); }
+// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: do not use const_cast [cppcoreguidelines-pro-type-const-cast]