1// RUN: %check_clang_tidy %s bugprone-macro-parentheses %t
2
3#define BAD1 -1
4// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: macro replacement list should be enclosed in parentheses [bugprone-macro-parentheses]
5#define BAD2 1+2
6// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: macro replacement list should be enclosed in parentheses [bugprone-macro-parentheses]
7#define BAD3(A) (A+1)
8// CHECK-MESSAGES: :[[@LINE-1]]:28: warning: macro argument should be enclosed in parentheses [bugprone-macro-parentheses]
9#define BAD4(x) ((unsigned char)(x & 0xff))
10// CHECK-MESSAGES: :[[@LINE-1]]:44: warning: macro argument should be enclosed in parentheses [bugprone-macro-parentheses]
11#define BAD5(X) A*B=(C*)X+2
12// CHECK-MESSAGES: :[[@LINE-1]]:35: warning: macro argument should be enclosed in parentheses [bugprone-macro-parentheses]
13#define BAD6(x) goto *x;
14// CHECK-MESSAGES: :[[@LINE-1]]:33: warning: macro argument should be enclosed in parentheses [bugprone-macro-parentheses]
15#define BAD7(x, y) if (x) goto y; else x;
16// CHECK-MESSAGES: :[[@LINE-1]]:47: warning: macro argument should be enclosed in parentheses [bugprone-macro-parentheses]
17
18#define GOOD1 1
19#define GOOD2 (1+2)
20#define GOOD3(A) #A
21#define GOOD4(A,B) A ## B
22#define GOOD5(T) ((T*)0)
23#define GOOD6(B) "A" B "C"
24#define GOOD7(b) A b
25#define GOOD8(a) a B
26#define GOOD9(type) (type(123))
27#define GOOD10(car, ...) car
28#define GOOD11 a[b+c]
29#define GOOD12(x) a[x]
30#define GOOD13(x) a.x
31#define GOOD14(x) a->x
32#define GOOD15(x) ({ int a = x; a+4; })
33#define GOOD16(x) a_ ## x, b_ ## x = c_ ## x - 1,
34#define GOOD17 case 123: x=4+5; break;
35#define GOOD18(x) ;x;
36#define GOOD19 ;-2;
37#define GOOD20 void*
38#define GOOD21(a) case Fred::a:
39#define GOOD22(a) if (verbose) return a;
40#define GOOD23(type) (type::Field)
41#define GOOD24(t) std::set<t> s
42#define GOOD25(t) std::set<t,t,t> s
43#define GOOD26(x) (a->*x)
44#define GOOD27(x) (a.*x)
45#define GOOD28(x) namespace x {int b;}
46#define GOOD29(...) std::cout << __VA_ARGS__;
47#define GOOD30(args...) std::cout << args;
48#define GOOD31(X) A*X=2
49#define GOOD32(X) std::vector<X>
50#define GOOD33(x) if (!a__##x) a_##x = &f(#x)
51#define GOOD34(x, y) if (x) goto y;
52#define GOOD35(x, y) if (x) goto *(y);
53
54// These are allowed for now..
55#define MAYBE1 *12.34
56#define MAYBE2 <<3
57

source code of clang-tools-extra/test/clang-tidy/checkers/bugprone/macro-parentheses.cpp