[clang-tools-extra] r246495 - Allow the static assert clang-tidy checker to run over C code.
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Mon Aug 31 14:54:42 PDT 2015
Author: aaronballman
Date: Mon Aug 31 16:54:42 2015
New Revision: 246495
URL: http://llvm.org/viewvc/llvm-project?rev=246495&view=rev
Log:
Allow the static assert clang-tidy checker to run over C code.
Added:
clang-tools-extra/trunk/test/clang-tidy/misc-static-assert.c
Modified:
clang-tools-extra/trunk/clang-tidy/misc/StaticAssertCheck.cpp
Modified: clang-tools-extra/trunk/clang-tidy/misc/StaticAssertCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/StaticAssertCheck.cpp?rev=246495&r1=246494&r2=246495&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/StaticAssertCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/StaticAssertCheck.cpp Mon Aug 31 16:54:42 2015
@@ -27,10 +27,9 @@ StaticAssertCheck::StaticAssertCheck(Str
: 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(
Added: clang-tools-extra/trunk/test/clang-tidy/misc-static-assert.c
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/misc-static-assert.c?rev=246495&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/misc-static-assert.c (added)
+++ clang-tools-extra/trunk/test/clang-tidy/misc-static-assert.c Mon Aug 31 16:54:42 2015
@@ -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, "");
+}
More information about the cfe-commits
mailing list