2 * Prototype search and parsing functions
4 * Copyright 2000 Jon Griffiths
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
26 static char *grep_buff = NULL;
27 static char *fgrep_buff = NULL;
29 static int symbol_from_prototype (parsed_symbol *sym, const char *prototype);
30 static const char *get_type (parsed_symbol *sym, const char *proto, int arg);
33 /*******************************************************************
36 * Call Patrik Stridvall's 'function_grep.pl' script to retrieve a
37 * function prototype from include file(s)
39 int symbol_search (parsed_symbol *sym)
41 static const size_t MAX_RESULT_LEN = 1024;
45 assert (globals.do_code);
46 assert (globals.directory);
47 assert (sym && sym->symbol);
49 if (!symbol_is_valid_c (sym))
53 grep_buff = (char *) malloc (MAX_RESULT_LEN);
56 fgrep_buff = (char *) malloc (MAX_RESULT_LEN);
58 if (!grep_buff || !fgrep_buff)
59 fatal ("Out of Memory");
61 /* Use 'grep' to tell us which possible files the function is in,
62 * then use 'function_grep.pl' to get the prototype. If this fails the
63 * first time then give grep a more general query (that doesn't
64 * require an opening argument brace on the line with the function name).
69 char *cmd = str_create (4, "grep -d recurse -l \"", sym->symbol,
70 !attempt ? "[:blank:]*(\" " : "\" ", globals.directory);
75 fflush (NULL); /* See 'man popen' */
77 if (!(grep = popen (cmd, "r")))
78 fatal ("Cannot execute grep -l");
81 while (fgets (grep_buff, MAX_RESULT_LEN, grep))
84 for (i = 0; grep_buff[i] && grep_buff[i] != '\n' ; i++)
91 cmd = str_create (5, "function_grep.pl ", sym->symbol,
92 " \"", grep_buff, "\"");
97 fflush (NULL); /* See 'man popen' */
99 if (!(f_grep = popen (cmd, "r")))
100 fatal ("Cannot execute function_grep.pl");
103 while (fgets (grep_buff, MAX_RESULT_LEN, f_grep))
105 char *iter = grep_buff;
107 /* Keep only the first line */
108 symbol_clean_string(grep_buff);
110 for (i = 0; grep_buff[i] && grep_buff[i] != '\n' ; i++)
117 while ((iter = strstr (iter, sym->symbol)))
119 if (iter > grep_buff && iter[-1] == ' ' &&
120 (iter[strlen (sym->symbol)] == ' ' ||
121 iter[strlen (sym->symbol)] == '('))
124 printf ("Prototype '%s' looks OK, processing\n", grep_buff);
126 if (!symbol_from_prototype (sym, grep_buff))
133 puts ("Failed, trying next");
136 iter += strlen (sym->symbol);
145 return -1; /* Not found */
149 /*******************************************************************
150 * symbol_from_prototype
152 * Convert a C prototype into a symbol
154 static int symbol_from_prototype (parsed_symbol *sym, const char *proto)
159 proto = get_type (sym, proto, -1); /* Get return type */
163 iter = (char *)str_match (proto, sym->symbol, &found);
168 /* Calling Convention */
169 iter = strchr (iter, ' ');
173 call = str_substring (proto, iter);
175 if (!strcasecmp (call, "cdecl") || !strcasecmp (call, "__cdecl"))
176 sym->flags |= SYM_CDECL;
178 sym->flags |= SYM_STDCALL;
180 iter = (char *)str_match (iter, sym->symbol, &found);
186 printf ("Using %s calling convention\n",
187 sym->flags & SYM_CDECL ? "cdecl" : "stdcall");
190 sym->flags = CALLING_CONVENTION;
192 sym->function_name = strdup (sym->symbol);
195 /* Now should be the arguments */
199 for (; *proto == ' '; proto++);
201 if (!strncmp (proto, "void", 4))
206 /* Process next argument */
207 str_match (proto, "...", &sym->varargs);
211 if (!(proto = get_type (sym, proto, sym->argc)))
218 else if (*proto != ')')
221 } while (*proto != ')');
227 /*******************************************************************
230 * Read a type from a prototype
232 static const char *get_type (parsed_symbol *sym, const char *proto, int arg)
234 int is_const, is_volatile, is_struct, is_signed, is_unsigned, ptrs = 0;
235 char *iter, *type_str, *base_type, *catch_unsigned, dest_type;
237 assert (sym && sym->symbol);
238 assert (proto && *proto);
239 assert (arg < 0 || (unsigned)arg == sym->argc);
241 type_str = (char *)proto;
243 proto = str_match (proto, "const", &is_const);
244 proto = str_match (proto, "volatile", &is_volatile);
245 proto = str_match (proto, "struct", &is_struct);
247 proto = str_match (proto, "union", &is_struct);
249 catch_unsigned = (char *)proto;
251 proto = str_match (proto, "unsigned", &is_unsigned);
252 proto = str_match (proto, "signed", &is_signed);
254 /* Can have 'unsigned const' or 'const unsigned' etc */
256 proto = str_match (proto, "const", &is_const);
258 proto = str_match (proto, "volatile", &is_volatile);
260 base_type = (char *)proto;
261 iter = (char *)str_find_set (proto, " ,*)");
265 if (arg < 0 && (is_signed || is_unsigned))
267 /* Prevent calling convention from being swallowed by 'un/signed' alone */
268 if (strncmp (base_type, "int", 3) && strncmp (base_type, "long", 4) &&
269 strncmp (base_type, "short", 5) && strncmp (base_type, "char", 4))
271 iter = (char *)proto;
272 base_type = catch_unsigned;
276 catch_unsigned = NULL;
278 /* FIXME: skip const/volatile here too */
279 for (proto = iter; *proto; proto++)
282 else if (*proto != ' ')
288 type_str = str_substring (type_str, proto);
289 if (iter == base_type || catch_unsigned)
291 /* 'unsigned' with no type */
292 char *tmp = str_create (2, type_str, " int");
296 symbol_clean_string (type_str);
298 dest_type = symbol_get_type (type_str);
302 sym->return_text = type_str;
303 sym->return_type = dest_type;
307 sym->arg_type [arg] = dest_type;
308 sym->arg_flag [arg] = is_const ? CT_CONST : is_volatile ? CT_VOLATILE : 0;
310 if (*proto == ',' || *proto == ')')
311 sym->arg_name [arg] = str_create_num (1, arg, "arg");
314 iter = (char *)str_find_set (proto, " ,)");
320 sym->arg_name [arg] = str_substring (proto, iter);
323 sym->arg_text [arg] = type_str;
331 /*******************************************************************
334 * Free memory used while searching (a niceity)
336 void search_cleanup (void) __attribute__ ((destructor));
337 void search_cleanup (void)