2015-10-22 19:31:44 +08:00
|
|
|
// RUN: %check_clang_tidy %s misc-static-assert %t
|
2015-03-02 18:46:43 +08:00
|
|
|
|
|
|
|
void abort() {}
|
|
|
|
#ifdef NDEBUG
|
|
|
|
#define assert(x) 1
|
|
|
|
#else
|
|
|
|
#define assert(x) \
|
|
|
|
if (!(x)) \
|
|
|
|
abort()
|
|
|
|
#endif
|
|
|
|
|
2015-05-29 17:49:59 +08:00
|
|
|
void print(...);
|
|
|
|
|
2015-03-02 18:46:43 +08:00
|
|
|
#define ZERO_MACRO 0
|
|
|
|
|
2015-03-15 10:19:37 +08:00
|
|
|
#define False false
|
|
|
|
#define FALSE 0
|
|
|
|
|
2015-03-02 18:46:43 +08:00
|
|
|
#define my_macro() assert(0 == 1)
|
|
|
|
// CHECK-FIXES: #define my_macro() assert(0 == 1)
|
|
|
|
|
|
|
|
constexpr bool myfunc(int a, int b) { return a * b == 0; }
|
|
|
|
|
2015-05-23 22:21:01 +08:00
|
|
|
typedef __SIZE_TYPE__ size_t;
|
|
|
|
extern "C" size_t strlen(const char *s);
|
|
|
|
|
2015-03-02 18:46:43 +08:00
|
|
|
class A {
|
|
|
|
public:
|
|
|
|
bool method() { return true; }
|
|
|
|
};
|
|
|
|
|
|
|
|
class B {
|
|
|
|
public:
|
|
|
|
constexpr bool method() { return true; }
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T> void doSomething(T t) {
|
|
|
|
assert(myfunc(1, 2));
|
|
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be replaced by static_assert() [misc-static-assert]
|
|
|
|
// CHECK-FIXES: {{^ }}static_assert(myfunc(1, 2), "");
|
|
|
|
|
|
|
|
assert(t.method());
|
|
|
|
// CHECK-FIXES: {{^ }}assert(t.method());
|
2015-03-09 10:27:57 +08:00
|
|
|
|
|
|
|
assert(sizeof(T) == 123);
|
2015-03-02 18:46:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
my_macro();
|
|
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
|
|
|
|
// CHECK-FIXES: {{^ }}my_macro();
|
|
|
|
|
|
|
|
assert(myfunc(1, 2) && (3 == 4));
|
|
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
|
|
|
|
// CHECK-FIXES: {{^ }}static_assert(myfunc(1, 2) && (3 == 4), "");
|
|
|
|
|
|
|
|
int x = 1;
|
|
|
|
assert(x == 0);
|
|
|
|
// CHECK-FIXES: {{^ }}assert(x == 0);
|
|
|
|
|
|
|
|
A a;
|
|
|
|
B b;
|
|
|
|
|
|
|
|
doSomething<A>(a);
|
|
|
|
doSomething<B>(b);
|
|
|
|
|
|
|
|
assert(false);
|
|
|
|
// CHECK-FIXES: {{^ }}assert(false);
|
|
|
|
|
2015-03-15 10:19:37 +08:00
|
|
|
assert(False);
|
|
|
|
// CHECK-FIXES: {{^ }}assert(False);
|
|
|
|
assert(FALSE);
|
|
|
|
// CHECK-FIXES: {{^ }}assert(FALSE);
|
|
|
|
|
2015-03-02 18:46:43 +08:00
|
|
|
assert(ZERO_MACRO);
|
|
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
|
|
|
|
// CHECK-FIXES: {{^ }}static_assert(ZERO_MACRO, "");
|
|
|
|
|
|
|
|
assert(0 && "Don't report me!");
|
|
|
|
// CHECK-FIXES: {{^ }}assert(0 && "Don't report me!");
|
|
|
|
|
|
|
|
assert(false && "Don't report me!");
|
|
|
|
// CHECK-FIXES: {{^ }}assert(false && "Don't report me!");
|
|
|
|
|
2015-04-10 21:55:39 +08:00
|
|
|
#define NULL ((void*)0)
|
|
|
|
assert(NULL && "Don't report me!");
|
|
|
|
// CHECK-FIXES: {{^ }}assert(NULL && "Don't report me!");
|
|
|
|
|
|
|
|
assert(NULL == "Don't report me!");
|
|
|
|
// CHECK-FIXES: {{^ }}assert(NULL == "Don't report me!");
|
|
|
|
|
|
|
|
assert("Don't report me!" == NULL);
|
|
|
|
// CHECK-FIXES: {{^ }}assert("Don't report me!" == NULL);
|
|
|
|
|
|
|
|
assert(0 == "Don't report me!");
|
|
|
|
// CHECK-FIXES: {{^ }}assert(0 == "Don't report me!");
|
|
|
|
|
|
|
|
#define NULL ((unsigned int)0)
|
|
|
|
assert(NULL && "Report me!");
|
|
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
|
|
|
|
// CHECK-FIXES: {{^ }}static_assert(NULL , "Report me!");
|
2015-05-08 15:56:24 +08:00
|
|
|
|
|
|
|
#define NULL __null
|
|
|
|
assert(__null == "Don't report me!");
|
|
|
|
// CHECK-FIXES: {{^ }}assert(__null == "Don't report me!");
|
|
|
|
assert(NULL == "Don't report me!");
|
|
|
|
// CHECK-FIXES: {{^ }}assert(NULL == "Don't report me!");
|
2015-04-10 21:55:39 +08:00
|
|
|
#undef NULL
|
|
|
|
|
2015-03-02 18:46:43 +08:00
|
|
|
assert(ZERO_MACRO && "Report me!");
|
|
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
|
|
|
|
// CHECK-FIXES: {{^ }}static_assert(ZERO_MACRO , "Report me!");
|
|
|
|
|
2015-04-04 22:54:53 +08:00
|
|
|
assert(0);
|
|
|
|
|
|
|
|
#define false false
|
|
|
|
assert(false);
|
|
|
|
|
|
|
|
#define false 0
|
|
|
|
assert(false);
|
|
|
|
#undef false
|
|
|
|
|
2015-03-02 18:46:43 +08:00
|
|
|
assert(10==5 && "Report me!");
|
|
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
|
|
|
|
// CHECK-FIXES: {{^ }}static_assert(10==5 , "Report me!");
|
|
|
|
|
2015-05-23 22:21:01 +08:00
|
|
|
assert(strlen("12345") == 5);
|
|
|
|
// CHECK-FIXES: {{^ }}assert(strlen("12345") == 5);
|
|
|
|
|
2015-05-29 17:49:59 +08:00
|
|
|
#define assert(e) (__builtin_expect(!(e), 0) ? print (#e, __FILE__, __LINE__) : (void)0)
|
|
|
|
assert(false);
|
|
|
|
// CHECK-FIXES: {{^ }}assert(false);
|
|
|
|
|
|
|
|
assert(10 == 5 + 5);
|
|
|
|
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() that could be
|
|
|
|
// CHECK-FIXES: {{^ }}static_assert(10 == 5 + 5, "");
|
|
|
|
#undef assert
|
|
|
|
|
2015-03-02 18:46:43 +08:00
|
|
|
return 0;
|
|
|
|
}
|