From c0dc76c2519b5700f5f9c081ed5a094775dd8780 Mon Sep 17 00:00:00 2001 From: Aaron Ballman Date: Mon, 31 Aug 2015 21:54:42 +0000 Subject: [PATCH] Allow the static assert clang-tidy checker to run over C code. llvm-svn: 246495 --- .../clang-tidy/misc/StaticAssertCheck.cpp | 7 +++-- .../test/clang-tidy/misc-static-assert.c | 27 +++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/misc-static-assert.c diff --git a/clang-tools-extra/clang-tidy/misc/StaticAssertCheck.cpp b/clang-tools-extra/clang-tidy/misc/StaticAssertCheck.cpp index 0828059f494a..7b596cdf6a72 100644 --- a/clang-tools-extra/clang-tidy/misc/StaticAssertCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/StaticAssertCheck.cpp @@ -27,10 +27,9 @@ StaticAssertCheck::StaticAssertCheck(StringRef Name, ClangTidyContext *Context) : 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 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( diff --git a/clang-tools-extra/test/clang-tidy/misc-static-assert.c b/clang-tools-extra/test/clang-tidy/misc-static-assert.c new file mode 100644 index 000000000000..aa0b7ee2b9f5 --- /dev/null +++ b/clang-tools-extra/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, ""); +}