[PATCH] D12446: [PATCH] Enable clang-tidy misc-static-assert for C11

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 31 09:06:29 PDT 2015


aaron.ballman updated this revision to Diff 33584.
aaron.ballman added a comment.

Only use a single test file with two RUN lines.


http://reviews.llvm.org/D12446

Files:
  clang-tidy/misc/StaticAssertCheck.cpp
  test/clang-tidy/check_clang_tidy.py
  test/clang-tidy/misc-static-assert.c

Index: test/clang-tidy/misc-static-assert.c
===================================================================
--- test/clang-tidy/misc-static-assert.c
+++ test/clang-tidy/misc-static-assert.c
@@ -0,0 +1,27 @@
+// RUN: %python %S/check_clang_tidy.py %s misc-static-assert %t -- -std=c11
+// RUN: clang-tidy %s -checks=-*,misc-static-assert -- -std=c99 | count 0
+
+void abort() {}
+#ifdef NDEBUG
+#define assert(x) 1
+#else
+#define assert(x)                                                              \
+  if (!(x))                                                                    \
+  abort()
+#endif
+
+void f(void) {
+  int x = 1;
+  assert(x == 0);
+  // CHECK-FIXES: {{^  }}assert(x == 0);
+
+  #define static_assert(x, msg) _Static_assert(x, msg)
+  assert(11 == 5 + 6);
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
+  // CHECK-FIXES: {{^  }}static_assert(11 == 5 + 6, "");
+  #undef static_assert
+
+  assert(10 == 5 + 5);
+  // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
+  // CHECK-FIXES: {{^  }}static_assert(10 == 5 + 5, "");
+}
Index: test/clang-tidy/check_clang_tidy.py
===================================================================
--- test/clang-tidy/check_clang_tidy.py
+++ test/clang-tidy/check_clang_tidy.py
@@ -38,12 +38,16 @@
     sys.exit('Not enough arguments.')
 
   input_file_name = sys.argv[1]
+  extension = '.cpp'
+  if (input_file_name.endswith('.c')):
+    extension = '.c'
+    
   check_name = sys.argv[2]
-  temp_file_name = sys.argv[3] + '.cpp'
+  temp_file_name = sys.argv[3] + extension
 
   clang_tidy_extra_args = sys.argv[4:]
   if len(clang_tidy_extra_args) == 0:
-    clang_tidy_extra_args = ['--', '--std=c++11']
+    clang_tidy_extra_args = ['--', '--std=c++11'] if extension == '.cpp' else ['--']
 
   with open(input_file_name, 'r') as input_file:
     input_text = input_file.read()
Index: clang-tidy/misc/StaticAssertCheck.cpp
===================================================================
--- clang-tidy/misc/StaticAssertCheck.cpp
+++ clang-tidy/misc/StaticAssertCheck.cpp
@@ -27,10 +27,9 @@
     : ClangTidyCheck(Name, Context) {}
 
 void StaticAssertCheck::registerMatchers(MatchFinder *Finder) {
-  // FIXME: I don't see why this checker couldn't also be interesting for
-  // _Static_assert in C11, or static_assert if <assert.h> has been included, 
-  // but it is currently only enabled for C++11. Investigate.
-  if (!getLangOpts().CPlusPlus11)
+  // This checker only makes sense for languages that have static assertion
+  // capabilities: C++11 and C11.
+  if (!(getLangOpts().CPlusPlus11 || getLangOpts().C11))
     return;
 
   auto IsAlwaysFalse = expr(ignoringParenImpCasts(


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D12446.33584.patch
Type: text/x-patch
Size: 2738 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150831/920fac72/attachment-0001.bin>


More information about the cfe-commits mailing list