3 +----------------------------------------------------------------------+
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");
29 function parse_php_functions(string $file):
30 ConstMap
<string, Pair
<string, ConstVector
<string>>> {
31 $source = file_get_contents($file);
36 // Don't handle methods yet, so function can't be indented
37 static $function_regex =
38 "#<<[^>]*__Native([^>]*)>>\nfunction +([^(]*)\(([^)]+)\) *: *(.+?);#m";
42 if (preg_match_all($function_regex, $source, $matches, PREG_SET_ORDER
)) {
43 foreach($matches as $match) {
44 $nativeArgs = $match[1];
46 if (strpos($nativeArgs, '"ActRec"') !== false) {
47 // ActRec functions have a specific structure
49 $argTypes = Vector
{'actrec'};
52 $retType = explode('<', $match[4], 2)[0];
53 $argTypes = Vector
{};
55 $args = preg_split('/\s*,\s*/', $argList);
56 if (count($args) > 5) {
58 $argTypes = Vector
{'actrec'};
60 foreach($args as $arg) {
61 $type = preg_split('/\s*\$/', $arg)[0];
62 $type = explode('<', $type, 2)[0];
64 // Special case varargs
65 $vargTypes = Vector
{'int'};
66 $vargTypes->addAll($argTypes);
67 $vargTypes[] = 'array';
68 $argTypes = $vargTypes;
76 $functions[$name] = Pair
{ $retType, $argTypes };
82 function parse_cpp_functions(string $file):
83 ConstMap
<string, Pair
<string, ConstVector
<string>>> {
84 $source = file_get_contents($file);
89 // Don't handle methods yet, so function can't be indented
90 static $function_regex =
91 "#^(?:static )?(\S+) +HHVM_FUNCTION\(([^,)]+)(?:, *)?([^)]*)\)#m";
95 if (preg_match_all($function_regex, $source, $matches, PREG_SET_ORDER
)) {
96 foreach($matches as $match) {
100 $argTypes = Vector
{};
102 $args = preg_split('/\s*,\s*/', $argList);
103 foreach($args as $arg) {
104 $type = preg_split('# */ *#', $arg)[0];
105 $type = implode(' ', explode(' ', $type, -1));
109 $functions[$name] = Pair
{ $retType, $argTypes };
115 function match_return_type(string $php, string $cpp): bool {
116 if ($php[0] == '?') {
117 $expected = 'Variant';
119 switch (strtolower($php)) {
126 $expected = 'int64_t';
130 $expected = 'double';
136 $expected = 'String';
142 $expected = 'Resource';
146 $expected = 'Variant';
149 $expected = 'TypedValue*';
153 $expected = 'Object';
157 // Special case for ints
161 return $cpp == $expected;
164 function match_arg_type(string $php, string $cpp): bool {
165 if ($php[0] == '@') {
166 $php = substr($php, 1);
168 if ($php[0] == '?') {
169 $expected = 'const Variant&';
171 switch (strtolower($php)) {
178 $expected = 'int64_t';
182 $expected = 'double';
185 // Shouldn't have void as an argument type
188 $expected = 'const String&';
191 $expected = 'const Array&';
194 $expected = 'const Resource&';
198 $expected = 'const Variant&';
201 $expected = 'ActRec*';
205 $expected = 'const Object&';
209 // References must be a variant type
210 if ($php[strlen($php)-1] == '&') {
211 if ($expected != 'const Variant&') {
214 $expected = 'VRefParam';
218 // Special case for ints
222 return $cpp == $expected;
225 function check_types(ConstMap
<string, Pair
<string, ConstVector
<string>>> $php,
226 ConstMap
<string, Pair
<string, ConstVector
<string>>> $cpp):
229 foreach($php as $name => $types) {
230 if (!isset($cpp[$name])) {
232 printf("Unimplemented native function '%s'\n", $name);
235 $cppTypes = $cpp[$name];
236 if (!match_return_type($types[0], $cppTypes[0])) {
238 printf("Mismatched return type for function '%s'. PHP: %s C++: %s\n",
239 $name, $types[0], $cppTypes[0]);
241 if ($types[1]->count() != $cppTypes[1]->count()) {
243 printf("Unequal number of arguments for function '%s'\n", $name);
246 foreach($types[1] as $idx => $t) {
247 if (!match_arg_type($t, $cppTypes[1][$idx])) {
249 printf("Mismatched argument type for function '%s' at index '%d'."
250 . " PHP: %s C++: %s\n", $name, $idx, $t, $cppTypes[1][$idx]);
257 $phpFuncs = parse_php_functions($_SERVER['argv'][2]);
258 $cppFuncs = parse_cpp_functions($_SERVER['argv'][1]);
260 if (check_types($phpFuncs, $cppFuncs)) {
261 echo "See https://github.com/facebook/hhvm/wiki/Extension-API for what types",