1// RUN: %check_clang_tidy %s readability-simplify-boolean-expr %t
2
3// Check when we can convert !(A Op B) -> !A InvOp !B.
4// RUN: %check_clang_tidy -check-suffixes=",RELAXED" %s \
5// RUN: readability-simplify-boolean-expr %t -- -config="{CheckOptions: { \
6// RUN: readability-simplify-boolean-expr.SimplifyDeMorganRelaxed: true}}" --
7
8// Verify warning issued when invalid options are specified.
9// RUN: clang-tidy %s -checks=-*,readability-simplify-boolean-expr -config="{CheckOptions: { \
10// RUN: readability-simplify-boolean-expr.SimplifyDeMorgan: false, \
11// RUN: readability-simplify-boolean-expr.SimplifyDeMorganRelaxed: true}}" \
12// RUN: -- 2>&1 | FileCheck %s -check-prefix=CHECK-BAD-CONFIG \
13// RUN: -implicit-check-not="{{warning|error}}:"
14
15// CHECK-BAD-CONFIG: warning: readability-simplify-boolean-expr: 'SimplifyDeMorganRelaxed' cannot be enabled without 'SimplifyDeMorgan' enabled
16void eat(bool);
17
18void foo(bool A1, bool A2, bool A3, bool A4) {
19 bool X;
20
21 X = !(A1 && A2);
22 X = !(A1 || A2);
23 // CHECK-MESSAGES-RELAXED: :[[@LINE-2]]:7: warning: boolean expression can be simplified by DeMorgan's theorem
24 // CHECK-MESSAGES-RELAXED: :[[@LINE-2]]:7: warning: boolean expression can be simplified by DeMorgan's theorem
25 // CHECK-FIXES-RELAXED: X = !A1 || !A2;
26 // CHECK-FIXES-NEXT-RELAXED: X = !A1 && !A2;
27
28 X = !(!A1 || A2);
29 X = !(A1 || !A2);
30 X = !(!A1 || !A2);
31 // CHECK-MESSAGES: :[[@LINE-3]]:7: warning: boolean expression can be simplified by DeMorgan's theorem
32 // CHECK-MESSAGES: :[[@LINE-3]]:7: warning: boolean expression can be simplified by DeMorgan's theorem
33 // CHECK-MESSAGES: :[[@LINE-3]]:7: warning: boolean expression can be simplified by DeMorgan's theorem
34 // CHECK-FIXES: X = A1 && !A2;
35 // CHECK-FIXES-NEXT: X = !A1 && A2;
36 // CHECK-FIXES-NEXT: X = A1 && A2;
37
38 X = !(!A1 && A2);
39 X = !(A1 && !A2);
40 X = !(!A1 && !A2);
41 // CHECK-MESSAGES: :[[@LINE-3]]:7: warning: boolean expression can be simplified by DeMorgan's theorem
42 // CHECK-MESSAGES: :[[@LINE-3]]:7: warning: boolean expression can be simplified by DeMorgan's theorem
43 // CHECK-MESSAGES: :[[@LINE-3]]:7: warning: boolean expression can be simplified by DeMorgan's theorem
44 // CHECK-FIXES: X = A1 || !A2;
45 // CHECK-FIXES-NEXT: X = !A1 || A2;
46 // CHECK-FIXES-NEXT: X = A1 || A2;
47
48 X = !(!A1 && !A2 && !A3);
49 X = !(!A1 && (!A2 && !A3));
50 X = !(!A1 && (A2 && A3));
51 // CHECK-MESSAGES: :[[@LINE-3]]:7: warning: boolean expression can be simplified by DeMorgan's theorem
52 // CHECK-MESSAGES: :[[@LINE-3]]:7: warning: boolean expression can be simplified by DeMorgan's theorem
53 // CHECK-MESSAGES: :[[@LINE-3]]:7: warning: boolean expression can be simplified by DeMorgan's theorem
54 // CHECK-FIXES: X = A1 || A2 || A3;
55 // CHECK-FIXES-NEXT: X = A1 || A2 || A3;
56 // CHECK-FIXES-NEXT: X = A1 || !A2 || !A3;
57
58 X = !(A1 && A2 == A3);
59 X = !(!A1 && A2 > A3);
60 // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: boolean expression can be simplified by DeMorgan's theorem
61 // CHECK-MESSAGES: :[[@LINE-2]]:7: warning: boolean expression can be simplified by DeMorgan's theorem
62 // CHECK-FIXES: X = !A1 || A2 != A3;
63 // CHECK-FIXES-NEXT: X = A1 || A2 <= A3;
64
65 // Ensure the check doesn't try to combine fixes for the inner and outer demorgan simplification.
66 X = !(!A1 && !(!A2 && !A3));
67 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: boolean expression can be simplified by DeMorgan's theorem
68 // CHECK-MESSAGES: :[[@LINE-2]]:16: warning: boolean expression can be simplified by DeMorgan's theorem
69 // CHECK-FIXES: X = A1 || (!A2 && !A3);
70
71 // Testing to see how it handles parens
72 X = !(A1 && !A2 && !A3);
73 X = !(A1 && !A2 || !A3);
74 X = !(!A1 || A2 && !A3);
75 X = !((A1 || !A2) && !A3);
76 X = !((A1 || !A2) || !A3);
77 // CHECK-MESSAGES: :[[@LINE-5]]:7: warning: boolean expression can be simplified by DeMorgan's theorem
78 // CHECK-MESSAGES: :[[@LINE-5]]:7: warning: boolean expression can be simplified by DeMorgan's theorem
79 // CHECK-MESSAGES: :[[@LINE-5]]:7: warning: boolean expression can be simplified by DeMorgan's theorem
80 // CHECK-MESSAGES: :[[@LINE-5]]:7: warning: boolean expression can be simplified by DeMorgan's theorem
81 // CHECK-MESSAGES: :[[@LINE-5]]:7: warning: boolean expression can be simplified by DeMorgan's theorem
82 // CHECK-FIXES: X = !A1 || A2 || A3;
83 // CHECK-FIXES-NEXT: X = (!A1 || A2) && A3;
84 // CHECK-FIXES-NEXT: X = A1 && (!A2 || A3);
85 // CHECK-FIXES-NEXT: X = (!A1 && A2) || A3;
86 // CHECK-FIXES-NEXT: X = !A1 && A2 && A3;
87 X = !((A1 || A2) && (!A3 || A4));
88 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: boolean expression can be simplified by DeMorgan's theorem
89 // CHECK-FIXES: X = (!A1 && !A2) || (A3 && !A4);
90
91 eat(!(!A1 && !A2));
92 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: boolean expression can be simplified by DeMorgan's theorem
93 // CHECK-FIXES: eat(A1 || A2);
94
95 bool Init = !(!A1 || !A2);
96 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: boolean expression can be simplified by DeMorgan's theorem
97 // CHECK-FIXES: bool Init = A1 && A2;
98
99 X = A1 && !(!A2 || !A3);
100 X = A1 || !(!A2 || !A3);
101 X = A1 && !(!A2 && !A3);
102 // CHECK-MESSAGES: :[[@LINE-3]]:13: warning: boolean expression can be simplified by DeMorgan's theorem
103 // CHECK-MESSAGES: :[[@LINE-3]]:13: warning: boolean expression can be simplified by DeMorgan's theorem
104 // CHECK-MESSAGES: :[[@LINE-3]]:13: warning: boolean expression can be simplified by DeMorgan's theorem
105 // CHECK-FIXES: X = A1 && A2 && A3;
106 // CHECK-FIXES-NEXT: X = A1 || (A2 && A3);
107 // CHECK-FIXES-NEXT: X = A1 && (A2 || A3);
108}
109

source code of clang-tools-extra/test/clang-tidy/checkers/readability/simplify-boolean-expr-demorgan.cpp