2 * Prototype search and parsing functions
4 * Copyright 2000 Jon Griffiths
8 static char *grep_buff = NULL;
9 static char *fgrep_buff = NULL;
11 static int symbol_from_prototype (parsed_symbol *sym, const char *prototype);
12 static const char *get_type (parsed_symbol *sym, const char *proto, int arg);
15 /*******************************************************************
18 * Call Patrik Stridvall's 'function_grep.pl' script to retrieve a
19 * function prototype from include file(s)
21 int symbol_search (parsed_symbol *sym)
23 static const size_t MAX_RESULT_LEN = 1024;
27 assert (globals.do_code);
28 assert (globals.directory);
29 assert (sym && sym->symbol);
31 if (!symbol_is_valid_c (sym))
35 grep_buff = (char *) malloc (MAX_RESULT_LEN);
38 fgrep_buff = (char *) malloc (MAX_RESULT_LEN);
40 if (!grep_buff || !fgrep_buff)
41 fatal ("Out of Memory");
43 /* Use 'grep' to tell us which possible files the function is in,
44 * then use 'function_grep.pl' to get the prototype. If this fails the
45 * first time then give grep a more general query (that doesn't
46 * require an opening argument brace on the line with the function name).
51 char *cmd = str_create (4, "grep -d recurse -l \"", sym->symbol,
52 !attempt ? "[:blank:]*(\" " : "\" ", globals.directory);
57 fflush (NULL); /* See 'man popen' */
59 if (!(grep = popen (cmd, "r")))
60 fatal ("Cannot execute grep -l");
63 while (fgets (grep_buff, MAX_RESULT_LEN, grep))
66 for (i = 0; grep_buff[i] && grep_buff[i] != '\n' ; i++)
73 cmd = str_create (5, "function_grep.pl ", sym->symbol,
74 " \"", grep_buff, "\"");
79 fflush (NULL); /* See 'man popen' */
81 if (!(f_grep = popen (cmd, "r")))
82 fatal ("Cannot execute function_grep.pl");
85 while (fgets (grep_buff, MAX_RESULT_LEN, f_grep))
87 char *iter = grep_buff;
89 /* Keep only the first line */
90 symbol_clean_string(grep_buff);
92 for (i = 0; grep_buff[i] && grep_buff[i] != '\n' ; i++)
99 while ((iter = strstr (iter, sym->symbol)))
101 if (iter > grep_buff && iter[-1] == ' ' &&
102 (iter[strlen (sym->symbol)] == ' ' ||
103 iter[strlen (sym->symbol)] == '('))
106 printf ("Prototype '%s' looks OK, processing\n", grep_buff);
108 if (!symbol_from_prototype (sym, grep_buff))
115 puts ("Failed, trying next");
118 iter += strlen (sym->symbol);
127 return -1; /* Not found */
131 /*******************************************************************
132 * symbol_from_prototype
134 * Convert a C prototype into a symbol
136 static int symbol_from_prototype (parsed_symbol *sym, const char *proto)
141 proto = get_type (sym, proto, -1); /* Get return type */
145 iter = (char *)str_match (proto, sym->symbol, &found);
150 /* Calling Convention */
151 iter = strchr (iter, ' ');
155 call = str_substring (proto, iter);
157 if (!strcasecmp (call, "cdecl") || !strcasecmp (call, "__cdecl"))
158 sym->flags |= SYM_CDECL;
160 sym->flags |= SYM_STDCALL;
162 iter = (char *)str_match (iter, sym->symbol, &found);
168 printf ("Using %s calling convention\n",
169 sym->flags & SYM_CDECL ? "cdecl" : "stdcall");
172 sym->flags = CALLING_CONVENTION;
174 sym->function_name = strdup (sym->symbol);
177 /* Now should be the arguments */
181 for (; *proto == ' '; proto++);
183 if (!strncmp (proto, "void", 4))
188 /* Process next argument */
189 str_match (proto, "...", &sym->varargs);
193 if (!(proto = get_type (sym, proto, sym->argc)))
200 else if (*proto != ')')
203 } while (*proto != ')');
209 /*******************************************************************
212 * Read a type from a prototype
214 static const char *get_type (parsed_symbol *sym, const char *proto, int arg)
216 int is_const, is_volatile, is_struct, is_signed, is_unsigned, ptrs = 0;
217 char *iter, *type_str, *base_type, *catch_unsigned, dest_type;
219 assert (sym && sym->symbol);
220 assert (proto && *proto);
221 assert (arg < 0 || (unsigned)arg == sym->argc);
223 type_str = (char *)proto;
225 proto = str_match (proto, "const", &is_const);
226 proto = str_match (proto, "volatile", &is_volatile);
227 proto = str_match (proto, "struct", &is_struct);
229 proto = str_match (proto, "union", &is_struct);
231 catch_unsigned = (char *)proto;
233 proto = str_match (proto, "unsigned", &is_unsigned);
234 proto = str_match (proto, "signed", &is_signed);
236 /* Can have 'unsigned const' or 'const unsigned' etc */
238 proto = str_match (proto, "const", &is_const);
240 proto = str_match (proto, "volatile", &is_volatile);
242 base_type = (char *)proto;
243 iter = (char *)str_find_set (proto, " ,*)");
247 if (arg < 0 && (is_signed || is_unsigned))
249 /* Prevent calling convention from being swallowed by 'un/signed' alone */
250 if (strncmp (base_type, "int", 3) && strncmp (base_type, "long", 4) &&
251 strncmp (base_type, "short", 5) && strncmp (base_type, "char", 4))
253 iter = (char *)proto;
254 base_type = catch_unsigned;
258 catch_unsigned = NULL;
260 /* FIXME: skip const/volatile here too */
261 for (proto = iter; *proto; proto++)
264 else if (*proto != ' ')
270 type_str = str_substring (type_str, proto);
271 if (iter == base_type || catch_unsigned)
273 /* 'unsigned' with no type */
274 char *tmp = str_create (2, type_str, " int");
278 symbol_clean_string (type_str);
280 dest_type = symbol_get_type (type_str);
284 sym->return_text = type_str;
285 sym->return_type = dest_type;
289 sym->arg_type [arg] = dest_type;
290 sym->arg_flag [arg] = is_const ? CT_CONST : is_volatile ? CT_VOLATILE : 0;
292 if (*proto == ',' || *proto == ')')
293 sym->arg_name [arg] = str_create_num (1, arg, "arg");
296 iter = (char *)str_find_set (proto, " ,)");
302 sym->arg_name [arg] = str_substring (proto, iter);
305 sym->arg_text [arg] = type_str;
313 /*******************************************************************
316 * Free memory used while searching (a niceity)
318 void search_cleanup (void) __attribute__ ((destructor));
319 void search_cleanup (void)