xref: /llvm-project/clang-tools-extra/test/clang-tidy/checkers/readability/math-missing-parentheses.cpp (revision d211abc625cc7bbc8616885bb8eaf4a69a9a3853)
1 // RUN: %check_clang_tidy %s readability-math-missing-parentheses %t
2 
3 #define MACRO_AND &
4 #define MACRO_ADD +
5 #define MACRO_OR |
6 #define MACRO_MULTIPLY *
7 #define MACRO_XOR ^
8 #define MACRO_SUBTRACT -
9 #define MACRO_DIVIDE /
10 
foo()11 int foo(){
12     return 5;
13 }
14 
bar()15 int bar(){
16     return 4;
17 }
18 
19 int sink(int);
20 #define FUN(ARG) (sink(ARG))
21 #define FUN2(ARG) sink((ARG))
22 #define FUN3(ARG) sink(ARG)
23 #define FUN4(ARG) sink(1 + ARG)
24 #define FUN5(ARG) sink(4 * ARG)
25 
26 class fun{
27 public:
28     int A;
29     double B;
fun()30     fun(){
31         A = 5;
32         B = 5.4;
33     }
34 };
35 
f()36 void f(){
37     //CHECK-MESSAGES: :[[@LINE+2]]:17: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
38     //CHECK-FIXES: int a = 1 + (2 * 3);
39     int a = 1 + 2 * 3;
40 
41     int a_negative = 1 + (2 * 3); // No warning
42 
43     int b = 1 + 2 + 3; // No warning
44 
45     int c = 1 * 2 * 3; // No warning
46 
47     //CHECK-MESSAGES: :[[@LINE+3]]:17: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
48     //CHECK-MESSAGES: :[[@LINE+2]]:25: warning: '/' has higher precedence than '-'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
49     //CHECK-FIXES: int d = 1 + (2 * 3) - (4 / 5);
50     int d = 1 + 2 * 3 - 4 / 5;
51 
52     int d_negative = 1 + (2 * 3) - (4 / 5); // No warning
53 
54     //CHECK-MESSAGES: :[[@LINE+4]]:13: warning: '&' has higher precedence than '|'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
55     //CHECK-MESSAGES: :[[@LINE+3]]:17: warning: '+' has higher precedence than '&'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
56     //CHECK-MESSAGES: :[[@LINE+2]]:25: warning: '*' has higher precedence than '|'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
57     //CHECK-FIXES: int e = (1 & (2 + 3)) | (4 * 5);
58     int e = 1 & 2 + 3 | 4 * 5;
59 
60     int e_negative = (1 & (2 + 3)) | (4 * 5); // No warning
61 
62     //CHECK-MESSAGES: :[[@LINE+2]]:13: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
63     //CHECK-FIXES: int f = (1 * -2) + 4;
64     int f = 1 * -2 + 4;
65 
66     int f_negative = (1 * -2) + 4; // No warning
67 
68     //CHECK-MESSAGES: :[[@LINE+2]]:13: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
69     //CHECK-FIXES: int g = (1 * 2 * 3) + 4 + 5;
70     int g = 1 * 2 * 3 + 4 + 5;
71 
72     int g_negative = (1 * 2 * 3) + 4 + 5; // No warning
73 
74     //CHECK-MESSAGES: :[[@LINE+4]]:13: warning: '&' has higher precedence than '|'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
75     //CHECK-MESSAGES: :[[@LINE+3]]:19: warning: '+' has higher precedence than '&'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
76     //CHECK-MESSAGES: :[[@LINE+2]]:27: warning: '*' has higher precedence than '|'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
77     //CHECK-FIXES: int h = (120 & (2 + 3)) | (22 * 5);
78     int h = 120 & 2 + 3 | 22 * 5;
79 
80     int h_negative = (120 & (2 + 3)) | (22 * 5); // No warning
81 
82     int i = 1 & 2 & 3; // No warning
83 
84     int j = 1 | 2 | 3; // No warning
85 
86     int k = 1 ^ 2 ^ 3; // No warning
87 
88     //CHECK-MESSAGES: :[[@LINE+2]]:13: warning: '+' has higher precedence than '^'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
89     //CHECK-FIXES: int l = (1 + 2) ^ 3;
90     int l = 1 + 2 ^ 3;
91 
92     int l_negative = (1 + 2) ^ 3; // No warning
93 
94     //CHECK-MESSAGES: :[[@LINE+2]]:13: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
95     //CHECK-FIXES: int m = (2 * foo()) + bar();
96     int m = 2 * foo() + bar();
97 
98     int m_negative = (2 * foo()) + bar(); // No warning
99 
100     //CHECK-MESSAGES: :[[@LINE+2]]:13: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
101     //CHECK-FIXES: int n = (1.05 * foo()) + double(bar());
102     int n = 1.05 * foo() + double(bar());
103 
104     int n_negative = (1.05 * foo()) + double(bar()); // No warning
105 
106     //CHECK-MESSAGES: :[[@LINE+3]]:17: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
107     //CHECK-FIXES: int o = 1 + (obj.A * 3) + obj.B;
108     fun obj;
109     int o = 1 + obj.A * 3 + obj.B;
110 
111     int o_negative = 1 + (obj.A * 3) + obj.B; // No warning
112 
113     //CHECK-MESSAGES: :[[@LINE+2]]:18: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
114     //CHECK-FIXES: int p = 1U + (2 * 3);
115     int p = 1U + 2 * 3;
116 
117     int p_negative = 1U + (2 * 3); // No warning
118 
119     //CHECK-MESSAGES: :[[@LINE+7]]:13: warning: '+' has higher precedence than '|'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
120     //CHECK-MESSAGES: :[[@LINE+6]]:25: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
121     //CHECK-MESSAGES: :[[@LINE+5]]:53: warning: '&' has higher precedence than '^'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
122     //CHECK-MESSAGES: :[[@LINE+4]]:53: warning: '^' has higher precedence than '|'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
123     //CHECK-MESSAGES: :[[@LINE+3]]:77: warning: '-' has higher precedence than '^'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
124     //CHECK-MESSAGES: :[[@LINE+2]]:94: warning: '/' has higher precedence than '-'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
125     //CHECK-FIXES: int q = (1 MACRO_ADD (2 MACRO_MULTIPLY 3)) MACRO_OR ((4 MACRO_AND 5) MACRO_XOR (6 MACRO_SUBTRACT (7 MACRO_DIVIDE 8)));
126     int q = 1 MACRO_ADD 2 MACRO_MULTIPLY 3 MACRO_OR 4 MACRO_AND 5 MACRO_XOR 6 MACRO_SUBTRACT 7 MACRO_DIVIDE 8; // No warning
127 
128     //CHECK-MESSAGES: :[[@LINE+1]]:21: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
129     int r = FUN(0 + 1 * 2);
130 
131     //CHECK-MESSAGES: :[[@LINE+1]]:22: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
132     int s = FUN2(0 + 1 * 2);
133 
134     //CHECK-MESSAGES: :[[@LINE+1]]:22: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
135     int t = FUN3(0 + 1 * 2);
136 
137     //CHECK-MESSAGES: :[[@LINE+1]]:18: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
138     int u = FUN4(1 * 2);
139 
140     //CHECK-MESSAGES: :[[@LINE+1]]:13: warning: '*' has higher precedence than '+'; add parentheses to explicitly specify the order of operations [readability-math-missing-parentheses]
141     int v = FUN5(0 + 1);
142 }
143 
144 namespace PR92516 {
f(int i)145   void f(int i) {
146     int j, k;
147     for (j = i + 1, k = 0; j < 1; ++j) {}
148   }
149 
f2(int i)150   void f2(int i) {
151     int j;
152     for (j = i + 1; j < 1; ++j) {}
153   }
154 
f3(int i)155   void f3(int i) {
156     int j;
157     for (j = i + 1, 2; j < 1; ++j) {}
158   }
159 }
160