Index file line lengths
[hiphop-php.git] / hphp / tools / check_native_signatures.php
blob9bd34b0ad0dca0bae504c4ad5d4ad8175a0df26d
1 <?hh
2 /*
3 +----------------------------------------------------------------------+
4 | HipHop for PHP |
5 +----------------------------------------------------------------------+
6 | Copyright (c) 2014 Facebook, Inc. (http://www.facebook.com) |
7 +----------------------------------------------------------------------+
8 | This source file is subject to version 3.01 of the PHP license, |
9 | that is bundled with this package in the file LICENSE, and is |
10 | available through the world-wide-web at the following url: |
11 | http://www.php.net/license/3_01.txt |
12 | If you did not receive a copy of the PHP license and are unable to |
13 | obtain it through the world-wide-web, please send a note to |
14 | license@php.net so we can mail you a copy immediately. |
15 +----------------------------------------------------------------------+
18 A helper script to check that HNI signatures in a PHP file match how the
19 functions are defined in the C++ file.
21 Currently only matches functions, so won't match classes/methods.
23 // expects argv[1] to be the C++ file, argv[2] to be the PHP file
24 if (empty($_SERVER['argv'][2])) {
25 fwrite(STDERR, "Usage: {$_SERVER['argv'][0]} <extfile.cpp> <extfile.php>\n");
26 exit(1);
29 function parse_php_functions(string $file):
30 ConstMap<string, Pair<string, ConstVector<string>>> {
31 $source = file_get_contents($file);
32 if (!$source) {
33 return ImmMap {};
36 // Don't handle methods yet, so function can't be indented
37 $function_regex =
38 "#<<[^>]*__Native([^>]*)>>\nfunction +([^(]*)\(([^)]*)\) *: *(.+?);#m";
40 $functions = Map {};
41 $matches = [];
42 if (preg_match_all($function_regex, $source, $matches, PREG_SET_ORDER)) {
43 foreach($matches as $match) {
44 $nativeArgs = $match[1];
45 $name = $match[2];
46 if (strpos($nativeArgs, '"ActRec"') !== false) {
47 // ActRec functions have a specific structure
48 $retType = 'actrec';
49 $argTypes = Vector {'actrec'};
50 } else {
51 $argList = $match[3];
52 $retType = explode('<', $match[4], 2)[0];
53 $argTypes = Vector {};
54 if ($argList) {
55 $args = preg_split('/\s*,\s*/', $argList);
56 if (count($args) > 7 && (in_array('float', $args)
57 || in_array('double', $args))) {
58 $retType = 'actrec';
59 $argTypes = Vector {'actrec'};
60 } else if (count($args) > 15) {
61 $retType = 'actrec';
62 $argTypes = Vector {'actrec'};
63 } else {
64 foreach($args as $arg) {
65 $type = preg_split('/\s*\$/', $arg)[0];
66 $type = explode('<', $type, 2)[0];
67 if ($type == '...') {
68 // Special case varargs
69 $vargTypes = Vector {'int'};
70 $vargTypes->addAll($argTypes);
71 $vargTypes[] = 'array';
72 $argTypes = $vargTypes;
73 } else {
74 $argTypes[] = $type;
80 $functions[strtolower($name)] = Pair { $retType, $argTypes };
83 return $functions;
86 function parse_cpp_functions(string $file):
87 ConstMap<string, Pair<string, ConstVector<string>>> {
88 $source = file_get_contents($file);
89 if (!$source) {
90 return ImmMap {};
93 // Don't handle methods yet, so function can't be indented
94 $function_regex =
95 "#^(?:static )?(\S+) +HHVM_FUNCTION\(([^,)]+)(?:, *)?([^)]*)\)#m";
97 $functions = Map {};
98 $matches = [];
100 if (preg_match_all($function_regex, $source, $matches, PREG_SET_ORDER)) {
101 foreach($matches as $match) {
102 $name = $match[2];
103 $argList = $match[3];
104 $retType = $match[1];
105 $argTypes = Vector {};
106 if ($argList) {
107 $args = preg_split('/\s*,\s*/', $argList);
108 foreach($args as $arg) {
109 $type = preg_split('# */ *#', $arg)[0];
110 $type = implode(' ', explode(' ', $type, -1));
111 $argTypes[] = $type;
114 $functions[strtolower($name)] = Pair { $retType, $argTypes };
117 return $functions;
120 function parse_php_methods(string $file):
121 ConstMap<string, Pair<string, ConstVector<string>>> {
122 $source = file_get_contents($file);
123 if (!$source) {
124 return ImmMap {};
127 $class_regex = "#class ([^\\s{/]+)[^{/\\)]*\\{(.*?)\n\\}#ms";
128 $method_regex =
129 "#<<[^>]*__Native([^>]*)>>\n\\s*.*?function +([^(]*)\(([^)]*)\) *: *(.+?);#m";
131 $methods = Map {};
132 $classes = [];
134 if (preg_match_all($class_regex, $source, $classes, PREG_SET_ORDER)) {
135 foreach ($classes as $class) {
136 $cname = $class[1];
137 $source = $class[2];
138 $matches = [];
139 if (preg_match_all($method_regex, $source, $matches, PREG_SET_ORDER)) {
140 foreach($matches as $match) {
141 $nativeArgs = $match[1];
142 $mname = $match[2];
143 if (strpos($nativeArgs, '"ActRec"') !== false) {
144 // ActRec functions have a specific structure
145 $retType = 'actrec';
146 $argTypes = Vector {'actrec'};
147 } else {
148 $argList = $match[3];
149 $retType = explode('<', $match[4], 2)[0];
150 $argTypes = Vector {};
151 if ($argList) {
152 $args = preg_split('/\s*,\s*/', $argList);
153 if (count($args) > 7 && (in_array('float', $args)
154 || in_array('double', $args))) {
155 $retType = 'actrec';
156 $argTypes = Vector {'actrec'};
157 } else if (count($args) > 15) {
158 $retType = 'actrec';
159 $argTypes = Vector {'actrec'};
160 } else {
161 foreach($args as $arg) {
162 $type = preg_split('/\s*\$/', $arg)[0];
163 $type = explode('<', $type, 2)[0];
164 if ($type == '...') {
165 // Special case varargs
166 $vargTypes = Vector {'int'};
167 $vargTypes->addAll($argTypes);
168 $vargTypes[] = 'array';
169 $argTypes = $vargTypes;
170 } else {
171 $argTypes[] = $type;
177 $methods[strtolower("$cname::$mname")] = Pair { $retType, $argTypes };
183 return $methods;
186 function parse_cpp_methods(string $file):
187 ConstMap<string, Pair<string, ConstVector<string>>> {
188 $source = file_get_contents($file);
189 if (!$source) {
190 return ImmMap {};
193 // Don't handle methods yet, so function can't be indented
194 $method_regex =
195 "#^(?:static )?(\S+) +HHVM_(?:STATIC_)?METHOD\(([^,)]+),\s+([^,)]+)(?:, *)?([^)]*)\)#m";
197 $methods = Map {};
198 $matches = [];
200 if (preg_match_all($method_regex, $source, $matches, PREG_SET_ORDER)) {
201 foreach($matches as $match) {
202 $cname = $match[2];
203 $mname = $match[3];
204 $argList = $match[4];
205 $retType = $match[1];
206 $argTypes = Vector {};
207 if ($argList) {
208 $args = preg_split('/\s*,\s*/', $argList);
209 foreach($args as $arg) {
210 $type = preg_split('# */ *#', $arg)[0];
211 $type = implode(' ', explode(' ', $type, -1));
212 $argTypes[] = $type;
215 $methods[strtolower("$cname::$mname")] = Pair { $retType, $argTypes };
218 return $methods;
221 function match_return_type(string $php, string $cpp): bool {
222 if ($php[0] == '?') {
223 $expected = 'Variant';
224 } else {
225 switch (strtolower($php)) {
226 case 'bool':
227 case 'boolean':
228 $expected = 'bool';
229 break;
230 case 'int':
231 case 'long':
232 $expected = 'int64_t';
233 break;
234 case 'float':
235 case 'double':
236 $expected = 'double';
237 break;
238 case 'void':
239 $expected = 'void';
240 break;
241 case 'string':
242 $expected = 'String';
243 break;
244 case 'array':
245 $expected = 'Array';
246 break;
247 case 'resource':
248 $expected = 'Resource';
249 break;
250 case 'mixed':
251 case 'callable':
252 $expected = 'Variant';
253 break;
254 case 'actrec':
255 $expected = 'TypedValue*';
256 break;
257 case 'object':
258 default:
259 $expected = 'Object';
260 break;
263 // Special case for ints
264 if ($cpp == 'int') {
265 $cpp = 'int64_t';
267 return $cpp == $expected;
270 function match_arg_type(string $php, string $cpp): bool {
271 if ($php[0] == '@') {
272 $php = substr($php, 1);
274 if ($php[0] == '?') {
275 $expected = 'const Variant&';
276 } else {
277 switch (strtolower(strtok($php, ' &'))) {
278 case 'bool':
279 case 'boolean':
280 $expected = 'bool';
281 break;
282 case 'int':
283 case 'long':
284 $expected = 'int64_t';
285 break;
286 case 'float':
287 case 'double':
288 $expected = 'double';
289 break;
290 case 'void':
291 // Shouldn't have void as an argument type
292 return false;
293 case 'string':
294 $expected = 'const String&';
295 break;
296 case 'array':
297 $expected = 'const Array&';
298 break;
299 case 'resource':
300 $expected = 'const Resource&';
301 break;
302 case 'mixed':
303 case 'callable':
304 $expected = 'const Variant&';
305 break;
306 case 'actrec':
307 $expected = 'ActRec*';
308 break;
309 case 'object':
310 default:
311 $expected = 'const Object&';
312 break;
315 $cpp = trim($cpp);
316 // Special case for ints
317 if ($cpp == 'int') {
318 $cpp = 'int64_t';
320 return $cpp == $expected;
323 function check_types(ConstMap<string, Pair<string, ConstVector<string>>> $php,
324 ConstMap<string, Pair<string, ConstVector<string>>> $cpp):
325 bool {
326 $errored = false;
327 foreach($php as $name => $types) {
328 if (!isset($cpp[$name])) {
329 $errored = true;
330 printf("Unimplemented native function '%s'\n", $name);
331 continue;
333 $cppTypes = $cpp[$name];
334 if (!match_return_type($types[0], $cppTypes[0])) {
335 $errored = true;
336 printf("Mismatched return type for function '%s'. PHP: %s C++: %s\n",
337 $name, $types[0], $cppTypes[0]);
339 if ($types[1]->count() != $cppTypes[1]->count()) {
340 $errored = true;
341 printf("Unequal number of arguments for function '%s'\n", $name);
342 continue;
344 foreach($types[1] as $idx => $t) {
345 if (!match_arg_type($t, $cppTypes[1][$idx])) {
346 $errored = true;
347 printf("Mismatched argument type for function '%s' at index '%d'."
348 . " PHP: %s C++: %s\n", $name, $idx, $t, $cppTypes[1][$idx]);
352 return $errored;
355 $phpFuncs = parse_php_functions($_SERVER['argv'][2]);
356 $cppFuncs = parse_cpp_functions($_SERVER['argv'][1]);
358 $cppMeths = parse_cpp_methods($_SERVER['argv'][1]);
359 $phpMeths = parse_php_methods($_SERVER['argv'][2]);
361 $funcs = check_types($phpFuncs, $cppFuncs);
362 $meths = check_types($phpMeths, $cppMeths);
364 if ($funcs || $meths) {
365 echo "See https://github.com/facebook/hhvm/wiki/Extension-API for what types",
366 " map to what\n";