bugprone-multiple-statement-macro.cpp
1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// RUN: %check_clang_tidy %s bugprone-multiple-statement-macro %t
void F();
#define BAD_MACRO(x) \
F(); \
F()
#define GOOD_MACRO(x) \
do { \
F(); \
F(); \
} while (0)
#define GOOD_MACRO2(x) F()
#define GOOD_MACRO3(x) F();
#define MACRO_ARG_MACRO(X) \
if (54) \
X(2)
#define ALL_IN_MACRO(X) \
if (43) \
F(); \
F()
#define GOOD_NESTED(x) \
if (x) \
GOOD_MACRO3(x); \
F();
#define IF(x) if(x)
void positives() {
if (1)
BAD_MACRO(1);
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: multiple statement macro used without braces; some statements will be unconditionally executed [bugprone-multiple-statement-macro]
if (1) {
} else
BAD_MACRO(1);
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: multiple statement macro used
while (1)
BAD_MACRO(1);
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: multiple statement macro used
for (;;)
BAD_MACRO(1);
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: multiple statement macro used
MACRO_ARG_MACRO(BAD_MACRO);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: multiple statement macro used
MACRO_ARG_MACRO(F(); int);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: multiple statement macro used
IF(1) BAD_MACRO(1);
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: multiple statement macro used
}
void negatives() {
if (1) {
BAD_MACRO(1);
} else {
BAD_MACRO(1);
}
while (1) {
BAD_MACRO(1);
}
for (;;) {
BAD_MACRO(1);
}
if (1)
GOOD_MACRO(1);
if (1) {
GOOD_MACRO(1);
}
if (1)
GOOD_MACRO2(1);
if (1)
GOOD_MACRO3(1);
MACRO_ARG_MACRO(GOOD_MACRO);
ALL_IN_MACRO(1);
IF(1) GOOD_MACRO(1);
}