1 // REQUIRES: static-analyzer
2 // RUN: %check_clang_tidy %s mpi-type-mismatch %t -- -- -I %S/Inputs/mpi-type-mismatch
3 
4 #include "mpimock.h"
5 
charNegativeTest()6 void charNegativeTest() {
7   int buf;
8   MPI_Send(&buf, 1, MPI_CHAR, 0, 0, MPI_COMM_WORLD);
9   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'int' does not match the MPI datatype 'MPI_CHAR'
10 
11   short buf2;
12   MPI_Send(&buf2, 1, MPI_CHAR, 0, 0, MPI_COMM_WORLD);
13   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'short' does not match the MPI datatype 'MPI_CHAR'
14 
15   long buf3;
16   MPI_Send(&buf3, 1, MPI_CHAR, 0, 0, MPI_COMM_WORLD);
17   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'long' does not match the MPI datatype 'MPI_CHAR'
18 
19   int8_t buf4;
20   MPI_Send(&buf4, 1, MPI_CHAR, 0, 0, MPI_COMM_WORLD);
21   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'int8_t' does not match the MPI datatype 'MPI_CHAR'
22 
23   uint16_t buf5;
24   MPI_Send(&buf5, 1, MPI_CHAR, 0, 0, MPI_COMM_WORLD);
25   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'uint16_t' does not match the MPI datatype 'MPI_CHAR'
26 
27   long double _Complex buf6;
28   MPI_Send(&buf6, 1, MPI_CHAR, 0, 0, MPI_COMM_WORLD);
29   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'long double _Complex' does not match the MPI datatype 'MPI_CHAR'
30 
31   std::complex<float> buf7;
32   MPI_Send(&buf7, 1, MPI_CHAR, 0, 0, MPI_COMM_WORLD);
33   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'complex<float>' does not match the MPI datatype 'MPI_CHAR'
34 }
35 
intNegativeTest()36 void intNegativeTest() {
37   unsigned char buf;
38   MPI_Send(&buf, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
39   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'unsigned char' does not match the MPI datatype 'MPI_INT'
40 
41   unsigned buf2;
42   MPI_Send(&buf2, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
43   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'unsigned int' does not match the MPI datatype 'MPI_INT'
44 
45   short buf3;
46   MPI_Send(&buf3, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
47   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'short' does not match the MPI datatype 'MPI_INT'
48 
49   long buf4;
50   MPI_Send(&buf4, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
51   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'long' does not match the MPI datatype 'MPI_INT'
52 
53   int8_t buf5;
54   MPI_Send(&buf5, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
55   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'int8_t' does not match the MPI datatype 'MPI_INT'
56 
57   uint16_t buf6;
58   MPI_Send(&buf6, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
59   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'uint16_t' does not match the MPI datatype 'MPI_INT'
60 
61   long double _Complex buf7;
62   MPI_Send(&buf7, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
63   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'long double _Complex' does not match the MPI datatype 'MPI_INT'
64 
65   std::complex<float> buf8;
66   MPI_Send(&buf8, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
67   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'complex<float>' does not match the MPI datatype 'MPI_INT'
68 }
69 
longNegativeTest()70 void longNegativeTest() {
71   char buf;
72   MPI_Send(&buf, 1, MPI_LONG, 0, 0, MPI_COMM_WORLD);
73   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'char' does not match the MPI datatype 'MPI_LONG'
74 
75   unsigned buf2;
76   MPI_Send(&buf2, 1, MPI_LONG, 0, 0, MPI_COMM_WORLD);
77   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'unsigned int' does not match the MPI datatype 'MPI_LONG'
78 
79   unsigned short buf3;
80   MPI_Send(&buf3, 1, MPI_LONG, 0, 0, MPI_COMM_WORLD);
81   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'unsigned short' does not match the MPI datatype 'MPI_LONG'
82 
83   unsigned long buf4;
84   MPI_Send(&buf4, 1, MPI_LONG, 0, 0, MPI_COMM_WORLD);
85   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'unsigned long' does not match the MPI datatype 'MPI_LONG'
86 
87   int8_t buf5;
88   MPI_Send(&buf5, 1, MPI_LONG, 0, 0, MPI_COMM_WORLD);
89   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'int8_t' does not match the MPI datatype 'MPI_LONG'
90 
91   uint16_t buf6;
92   MPI_Send(&buf6, 1, MPI_LONG, 0, 0, MPI_COMM_WORLD);
93   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'uint16_t' does not match the MPI datatype 'MPI_LONG'
94 
95   long double _Complex buf7;
96   MPI_Send(&buf7, 1, MPI_LONG, 0, 0, MPI_COMM_WORLD);
97   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'long double _Complex' does not match the MPI datatype 'MPI_LONG'
98 
99   std::complex<float> buf8;
100   MPI_Send(&buf8, 1, MPI_LONG, 0, 0, MPI_COMM_WORLD);
101   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'complex<float>' does not match the MPI datatype 'MPI_LONG'
102 }
103 
int8_tNegativeTest()104 void int8_tNegativeTest() {
105   char buf;
106   MPI_Send(&buf, 1, MPI_INT8_T, 0, 0, MPI_COMM_WORLD);
107   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'char' does not match the MPI datatype 'MPI_INT8_T'
108 
109   unsigned buf2;
110   MPI_Send(&buf2, 1, MPI_INT8_T, 0, 0, MPI_COMM_WORLD);
111   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'unsigned int' does not match the MPI datatype 'MPI_INT8_T'
112 
113   short buf3;
114   MPI_Send(&buf3, 1, MPI_INT8_T, 0, 0, MPI_COMM_WORLD);
115   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'short' does not match the MPI datatype 'MPI_INT8_T'
116 
117   unsigned long buf4;
118   MPI_Send(&buf4, 1, MPI_INT8_T, 0, 0, MPI_COMM_WORLD);
119   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'unsigned long' does not match the MPI datatype 'MPI_INT8_T'
120 
121   uint8_t buf5;
122   MPI_Send(&buf5, 1, MPI_INT8_T, 0, 0, MPI_COMM_WORLD);
123   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'uint8_t' does not match the MPI datatype 'MPI_INT8_T'
124 
125   uint16_t buf6;
126   MPI_Send(&buf6, 1, MPI_INT8_T, 0, 0, MPI_COMM_WORLD);
127   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'uint16_t' does not match the MPI datatype 'MPI_INT8_T'
128 
129   long double _Complex buf7;
130   MPI_Send(&buf7, 1, MPI_INT8_T, 0, 0, MPI_COMM_WORLD);
131   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'long double _Complex' does not match the MPI datatype 'MPI_INT8_T'
132 
133   std::complex<float> buf8;
134   MPI_Send(&buf8, 1, MPI_INT8_T, 0, 0, MPI_COMM_WORLD);
135   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'complex<float>' does not match the MPI datatype 'MPI_INT8_T'
136 }
137 
complex_c_long_double_complexNegativeTest()138 void complex_c_long_double_complexNegativeTest() {
139   char buf;
140   MPI_Send(&buf, 1, MPI_C_LONG_DOUBLE_COMPLEX, 0, 0, MPI_COMM_WORLD);
141   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'char' does not match the MPI datatype 'MPI_C_LONG_DOUBLE_COMPLEX'
142 
143   unsigned buf2;
144   MPI_Send(&buf2, 1, MPI_C_LONG_DOUBLE_COMPLEX, 0, 0, MPI_COMM_WORLD);
145   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'unsigned int' does not match the MPI datatype 'MPI_C_LONG_DOUBLE_COMPLEX'
146 
147   short buf3;
148   MPI_Send(&buf3, 1, MPI_C_LONG_DOUBLE_COMPLEX, 0, 0, MPI_COMM_WORLD);
149   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'short' does not match the MPI datatype 'MPI_C_LONG_DOUBLE_COMPLEX'
150 
151   unsigned long buf4;
152   MPI_Send(&buf4, 1, MPI_C_LONG_DOUBLE_COMPLEX, 0, 0, MPI_COMM_WORLD);
153   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'unsigned long' does not match the MPI datatype 'MPI_C_LONG_DOUBLE_COMPLEX'
154 
155   uint8_t buf5;
156   MPI_Send(&buf5, 1, MPI_C_LONG_DOUBLE_COMPLEX, 0, 0, MPI_COMM_WORLD);
157   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'uint8_t' does not match the MPI datatype 'MPI_C_LONG_DOUBLE_COMPLEX'
158 
159   uint16_t buf6;
160   MPI_Send(&buf6, 1, MPI_C_LONG_DOUBLE_COMPLEX, 0, 0, MPI_COMM_WORLD);
161   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'uint16_t' does not match the MPI datatype 'MPI_C_LONG_DOUBLE_COMPLEX'
162 
163   double _Complex buf7;
164   MPI_Send(&buf7, 1, MPI_C_LONG_DOUBLE_COMPLEX, 0, 0, MPI_COMM_WORLD);
165   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'double _Complex' does not match the MPI datatype 'MPI_C_LONG_DOUBLE_COMPLEX'
166 
167   std::complex<float> buf8;
168   MPI_Send(&buf8, 1, MPI_C_LONG_DOUBLE_COMPLEX, 0, 0, MPI_COMM_WORLD);
169   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'complex<float>' does not match the MPI datatype 'MPI_C_LONG_DOUBLE_COMPLEX'
170 }
171 
complex_cxx_float_complexNegativeTest()172 void complex_cxx_float_complexNegativeTest() {
173   char buf;
174   MPI_Send(&buf, 1, MPI_CXX_FLOAT_COMPLEX, 0, 0, MPI_COMM_WORLD);
175   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'char' does not match the MPI datatype 'MPI_CXX_FLOAT_COMPLEX'
176 
177   unsigned buf2;
178   MPI_Send(&buf2, 1, MPI_CXX_FLOAT_COMPLEX, 0, 0, MPI_COMM_WORLD);
179   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'unsigned int' does not match the MPI datatype 'MPI_CXX_FLOAT_COMPLEX'
180 
181   short buf3;
182   MPI_Send(&buf3, 1, MPI_CXX_FLOAT_COMPLEX, 0, 0, MPI_COMM_WORLD);
183   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'short' does not match the MPI datatype 'MPI_CXX_FLOAT_COMPLEX'
184 
185   unsigned long buf4;
186   MPI_Send(&buf4, 1, MPI_CXX_FLOAT_COMPLEX, 0, 0, MPI_COMM_WORLD);
187   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'unsigned long' does not match the MPI datatype 'MPI_CXX_FLOAT_COMPLEX'
188 
189   uint8_t buf5;
190   MPI_Send(&buf5, 1, MPI_CXX_FLOAT_COMPLEX, 0, 0, MPI_COMM_WORLD);
191   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'uint8_t' does not match the MPI datatype 'MPI_CXX_FLOAT_COMPLEX'
192 
193   uint16_t buf6;
194   MPI_Send(&buf6, 1, MPI_CXX_FLOAT_COMPLEX, 0, 0, MPI_COMM_WORLD);
195   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'uint16_t' does not match the MPI datatype 'MPI_CXX_FLOAT_COMPLEX'
196 
197   double _Complex buf7;
198   MPI_Send(&buf7, 1, MPI_CXX_FLOAT_COMPLEX, 0, 0, MPI_COMM_WORLD);
199   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'double _Complex' does not match the MPI datatype 'MPI_CXX_FLOAT_COMPLEX'
200 
201   std::complex<double> buf8;
202   MPI_Send(&buf8, 1, MPI_CXX_FLOAT_COMPLEX, 0, 0, MPI_COMM_WORLD);
203   // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: buffer type 'complex<double>' does not match the MPI datatype 'MPI_CXX_FLOAT_COMPLEX'
204 }
205 
skippedTypesTests()206 void skippedTypesTests() {
207   // typedefs, user defined MPI and nullptr types are skipped
208   typedef char CHAR;
209   CHAR buf;
210   MPI_Send(&buf, 1, MPI_CXX_FLOAT_COMPLEX, 0, 0, MPI_COMM_WORLD);
211 
212   typedef unsigned UNSIGNED;
213   UNSIGNED buf2;
214   MPI_Send(&buf2, 1, MPI_CXX_FLOAT_COMPLEX, 0, 0, MPI_COMM_WORLD);
215 
216 #define _MPI_LONG MPI_LONG
217   int buf3;
218   MPI_Send(&buf3, 1, _MPI_LONG, 0, 0, MPI_COMM_WORLD);
219 
220 #define _MPI_CXX_FLOAT_COMPLEX MPI_CXX_FLOAT_COMPLEX
221   short buf4;
222   MPI_Send(&buf4, 1, _MPI_CXX_FLOAT_COMPLEX, 0, 0, MPI_COMM_WORLD);
223 
224   MPI_Send(NULL, 1, MPI_LONG, 0, 0, MPI_COMM_WORLD);
225 }
226 
positiveTests()227 void positiveTests() {
228   char buf;
229   MPI_Send(&buf, 1, MPI_CHAR, 0, 0, MPI_COMM_WORLD);
230 
231   int buf2;
232   MPI_Send(&buf2, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
233 
234   long buf3;
235   MPI_Send(&buf3, 1, MPI_LONG, 0, 0, MPI_COMM_WORLD);
236 
237   int8_t buf4;
238   MPI_Send(&buf4, 1, MPI_INT8_T, 0, 0, MPI_COMM_WORLD);
239 
240   long double _Complex buf5;
241   MPI_Send(&buf5, 1, MPI_C_LONG_DOUBLE_COMPLEX, 0, 0, MPI_COMM_WORLD);
242 
243   std::complex<float> buf6;
244   MPI_Send(&buf6, 1, MPI_CXX_FLOAT_COMPLEX, 0, 0, MPI_COMM_WORLD);
245 
246   uint8_t buf7;
247   MPI_Send(&buf7, 1, MPI_UINT8_T, 0, 0, MPI_COMM_WORLD);
248 
249   uint16_t buf8;
250   MPI_Send(&buf8, 1, MPI_UINT16_T, 0, 0, MPI_COMM_WORLD);
251 
252   // On some systems like PPC or ARM, 'char' is unsigned by default which is why
253   // distinct signedness for the buffer and MPI type is tolerated.
254   unsigned char buf9;
255   MPI_Send(&buf9, 1, MPI_CHAR, 0, 0, MPI_COMM_WORLD);
256 }
257