1 // RUN: %check_clang_tidy %s bugprone-assert-side-effect %t -- -config="{CheckOptions: [{key: bugprone-assert-side-effect.CheckFunctionCalls, value: true}, {key: bugprone-assert-side-effect.AssertMacros, value: 'assert,assert2,my_assert,convoluted_assert,msvc_assert'}, {key: bugprone-assert-side-effect.IgnoredFunctions, value: 'MyClass::badButIgnoredFunc'}]}" -- -fexceptions
2 
3 //===--- assert definition block ------------------------------------------===//
abort()4 int abort() { return 0; }
5 
6 #ifdef NDEBUG
7 #define assert(x) 1
8 #else
9 #define assert(x)                                                              \
10   if (!(x))                                                                    \
11   (void)abort()
12 #endif
13 
14 void print(...);
15 #define assert2(e) (__builtin_expect(!(e), 0) ?                                \
16                        print (#e, __FILE__, __LINE__) : (void)0)
17 
18 #ifdef NDEBUG
19 #define my_assert(x) 1
20 #else
21 #define my_assert(x)                                                           \
22   ((void)((x) ? 1 : abort()))
23 #endif
24 
25 #ifdef NDEBUG
26 #define not_my_assert(x) 1
27 #else
28 #define not_my_assert(x)                                                       \
29   if (!(x))                                                                    \
30   (void)abort()
31 #endif
32 
33 #define real_assert(x) ((void)((x) ? 1 : abort()))
34 #define wrap1(x) real_assert(x)
35 #define wrap2(x) wrap1(x)
36 #define convoluted_assert(x) wrap2(x)
37 
38 #define msvc_assert(expression) (void)(                                        \
39             (!!(expression)) ||                                                \
40             (abort(), 0)                                                       \
41         )
42 
43 
44 //===----------------------------------------------------------------------===//
45 
badButIgnoredFunc(int a,int b)46 bool badButIgnoredFunc(int a, int b) { return a * b > 0; }
47 
48 class MyClass {
49 public:
badFunc(int a,int b)50   bool badFunc(int a, int b) { return a * b > 0; }
badButIgnoredFunc(int a,int b)51   bool badButIgnoredFunc(int a, int b) { return a * b > 0; }
goodFunc(int a,int b) const52   bool goodFunc(int a, int b) const { return a * b > 0; }
53 
operator =(const MyClass & rhs)54   MyClass &operator=(const MyClass &rhs) { return *this; }
55 
operator -()56   int operator-() { return 1; }
57 
operator bool() const58   operator bool() const { return true; }
59 
operator delete(void * p)60   void operator delete(void *p) {}
61 };
62 
63 class SomeoneElseClass {
64 public:
badButIgnoredFunc(int a,int b)65   bool badButIgnoredFunc(int a, int b) { return a * b > 0; }
66 };
67 
freeFunction()68 bool freeFunction() {
69   return true;
70 }
71 
main()72 int main() {
73 
74   int X = 0;
75   bool B = false;
76   assert(X == 1);
77 
78   assert(X = 1);
79   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: side effect in assert() condition discarded in release builds [bugprone-assert-side-effect]
80   my_assert(X = 1);
81   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: side effect in my_assert() condition discarded in release builds
82   convoluted_assert(X = 1);
83   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: side effect in convoluted_assert() condition discarded in release builds
84   not_my_assert(X = 1);
85 
86   assert(++X);
87   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: side effect in assert() condition discarded in release builds
88   assert(!B);
89 
90   assert(B || true);
91 
92   assert(freeFunction());
93   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: side effect in assert() condition discarded in release builds
94 
95   MyClass mc;
96   SomeoneElseClass sec;
97   assert(mc.badFunc(0, 1));
98   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: side effect in assert() condition discarded in release builds
99   assert(mc.badButIgnoredFunc(0, 1));
100   // badButIgnoredFunc is not ignored as only class members are ignored by the config
101   assert(badButIgnoredFunc(0, 1));
102   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: side effect in assert() condition discarded in release builds
103   // sec.badButIgnoredFunc is not ignored as only MyClass members are ignored by the config
104   assert(sec.badButIgnoredFunc(0, 1));
105   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: side effect in assert() condition discarded in release builds
106   assert(mc.goodFunc(0, 1));
107 
108   MyClass mc2;
109   assert(mc2 = mc);
110   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: side effect in assert() condition discarded in release builds
111 
112   assert(-mc > 0);
113 
114   MyClass *mcp;
115   assert(mcp = new MyClass);
116   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: side effect in assert() condition discarded in release builds
117 
118   assert((delete mcp, false));
119   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: side effect in assert() condition discarded in release builds
120 
121   assert((throw 1, false));
122   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: side effect in assert() condition discarded in release builds
123 
124   assert2(1 == 2 - 1);
125 
126   msvc_assert(mc2 = mc);
127   // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: side effect in msvc_assert() condition discarded in release builds
128 
129   return 0;
130 }
131