]>
Commit | Line | Data |
---|---|---|
d4bb0623 | 1 | /* Protoize program - Original version by Ron Guilmette (rfg@segfault.us.com). |
af841dbd JL |
2 | Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, |
3 | 1999, 2000 Free Software Foundation, Inc. | |
5f8037c4 RS |
4 | |
5 | This file is part of GNU CC. | |
6 | ||
7 | GNU CC is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2, or (at your option) | |
10 | any later version. | |
11 | ||
12 | GNU CC is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GNU CC; see the file COPYING. If not, write to | |
e99215a3 RK |
19 | the Free Software Foundation, 59 Temple Place - Suite 330, |
20 | Boston, MA 02111-1307, USA. */ | |
5f8037c4 | 21 | |
34e56753 | 22 | #include "config.h" |
944fc8ab | 23 | #include "system.h" |
38d9d130 NS |
24 | #include "intl.h" |
25 | ||
5f8037c4 | 26 | #include <setjmp.h> |
ffb9f2f1 | 27 | #include <signal.h> |
ee77eda5 MK |
28 | #ifdef HAVE_UNISTD_H |
29 | #include <unistd.h> | |
30 | #endif | |
ffb9f2f1 | 31 | #undef abort |
9f8f4efe | 32 | #include "version.h" |
b0e87872 | 33 | |
ffb9f2f1 | 34 | /* Include getopt.h for the sake of getopt_long. */ |
5f8037c4 RS |
35 | #include "getopt.h" |
36 | ||
ee77eda5 MK |
37 | /* Macro to see if the path elements match. */ |
38 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM | |
ce1cc601 | 39 | #define IS_SAME_PATH_CHAR(a,b) (TOUPPER (a) == TOUPPER (b)) |
ee77eda5 MK |
40 | #else |
41 | #define IS_SAME_PATH_CHAR(a,b) ((a) == (b)) | |
42 | #endif | |
43 | ||
44 | /* Macro to see if the paths match. */ | |
45 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM | |
46 | #define IS_SAME_PATH(a,b) (strcasecmp (a, b) == 0) | |
47 | #else | |
48 | #define IS_SAME_PATH(a,b) (strcmp (a, b) == 0) | |
49 | #endif | |
50 | ||
a7db8bbb MK |
51 | /* Suffix for aux-info files. */ |
52 | #ifdef __MSDOS__ | |
53 | #define AUX_INFO_SUFFIX "X" | |
54 | #else | |
55 | #define AUX_INFO_SUFFIX ".X" | |
56 | #endif | |
57 | ||
58 | /* Suffix for saved files. */ | |
59 | #ifdef __MSDOS__ | |
60 | #define SAVE_SUFFIX "sav" | |
61 | #else | |
62 | #define SAVE_SUFFIX ".save" | |
63 | #endif | |
64 | ||
ee77eda5 MK |
65 | /* Suffix for renamed C++ files. */ |
66 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM | |
67 | #define CPLUS_FILE_SUFFIX "cc" | |
68 | #else | |
69 | #define CPLUS_FILE_SUFFIX "C" | |
70 | #endif | |
71 | ||
d059a239 FF |
72 | static void usage PARAMS ((void)) ATTRIBUTE_NORETURN; |
73 | static void aux_info_corrupted PARAMS ((void)) ATTRIBUTE_NORETURN; | |
74 | static void declare_source_confusing PARAMS ((const char *)) ATTRIBUTE_NORETURN; | |
ffb9f2f1 KG |
75 | static const char *shortpath PARAMS ((const char *, const char *)); |
76 | extern void fancy_abort PARAMS ((void)) ATTRIBUTE_NORETURN; | |
77 | static void notice PARAMS ((const char *, ...)) ATTRIBUTE_PRINTF_1; | |
78 | static char *savestring PARAMS ((const char *, unsigned int)); | |
79 | static char *dupnstr PARAMS ((const char *, size_t)); | |
80 | static const char *substr PARAMS ((const char *, const char * const)); | |
81 | static int safe_read PARAMS ((int, PTR, int)); | |
82 | static void safe_write PARAMS ((int, PTR, int, const char *)); | |
83 | static void save_pointers PARAMS ((void)); | |
84 | static void restore_pointers PARAMS ((void)); | |
85 | static int is_id_char PARAMS ((int)); | |
86 | static int in_system_include_dir PARAMS ((const char *)); | |
87 | static int directory_specified_p PARAMS ((const char *)); | |
88 | static int file_excluded_p PARAMS ((const char *)); | |
89 | static char *unexpand_if_needed PARAMS ((const char *)); | |
90 | static char *abspath PARAMS ((const char *, const char *)); | |
13536812 | 91 | static int is_abspath PARAMS ((const char *)); |
ffb9f2f1 KG |
92 | static void check_aux_info PARAMS ((int)); |
93 | static const char *find_corresponding_lparen PARAMS ((const char *)); | |
94 | static int referenced_file_is_newer PARAMS ((const char *, time_t)); | |
95 | static void save_def_or_dec PARAMS ((const char *, int)); | |
96 | static void munge_compile_params PARAMS ((const char *)); | |
97 | static int gen_aux_info_file PARAMS ((const char *)); | |
98 | static void process_aux_info_file PARAMS ((const char *, int, int)); | |
99 | static int identify_lineno PARAMS ((const char *)); | |
100 | static void check_source PARAMS ((int, const char *)); | |
101 | static const char *seek_to_line PARAMS ((int)); | |
102 | static const char *forward_to_next_token_char PARAMS ((const char *)); | |
103 | static void output_bytes PARAMS ((const char *, size_t)); | |
104 | static void output_string PARAMS ((const char *)); | |
105 | static void output_up_to PARAMS ((const char *)); | |
106 | static int other_variable_style_function PARAMS ((const char *)); | |
107 | static const char *find_rightmost_formals_list PARAMS ((const char *)); | |
108 | static void do_cleaning PARAMS ((char *, const char *)); | |
109 | static const char *careful_find_l_paren PARAMS ((const char *)); | |
110 | static void do_processing PARAMS ((void)); | |
5f8037c4 RS |
111 | |
112 | /* Look for these where the `const' qualifier is intentionally cast aside. */ | |
5f8037c4 RS |
113 | #define NONCONST |
114 | ||
5f8037c4 RS |
115 | /* Define a default place to find the SYSCALLS.X file. */ |
116 | ||
d059a239 FF |
117 | #ifndef UNPROTOIZE |
118 | ||
119 | #ifndef STANDARD_EXEC_PREFIX | |
120 | #define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc-lib/" | |
121 | #endif /* !defined STANDARD_EXEC_PREFIX */ | |
122 | ||
ffb9f2f1 KG |
123 | static const char * const standard_exec_prefix = STANDARD_EXEC_PREFIX; |
124 | static const char * const target_machine = DEFAULT_TARGET_MACHINE; | |
125 | static const char * const target_version = DEFAULT_TARGET_VERSION; | |
d059a239 FF |
126 | |
127 | #ifndef GET_ENV_PATH_LIST | |
128 | #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0) | |
129 | #endif | |
130 | ||
131 | #endif /* !defined (UNPROTOIZE) */ | |
5f8037c4 | 132 | |
5f8037c4 RS |
133 | /* Suffix of aux_info files. */ |
134 | ||
a7db8bbb | 135 | static const char * const aux_info_suffix = AUX_INFO_SUFFIX; |
5f8037c4 | 136 | |
a2b22788 | 137 | /* String to attach to filenames for saved versions of original files. */ |
5f8037c4 | 138 | |
a7db8bbb | 139 | static const char * const save_suffix = SAVE_SUFFIX; |
5f8037c4 | 140 | |
ee77eda5 MK |
141 | /* String to attach to C filenames renamed to C++. */ |
142 | ||
143 | static const char * const cplus_suffix = CPLUS_FILE_SUFFIX; | |
144 | ||
5f8037c4 RS |
145 | #ifndef UNPROTOIZE |
146 | ||
147 | /* File name of the file which contains descriptions of standard system | |
148 | routines. Note that we never actually do anything with this file per se, | |
149 | but we do read in its corresponding aux_info file. */ | |
150 | ||
d742f26c | 151 | static const char syscalls_filename[] = "SYSCALLS.c"; |
5f8037c4 RS |
152 | |
153 | /* Default place to find the above file. */ | |
154 | ||
ffb9f2f1 | 155 | static const char * default_syscalls_dir; |
5f8037c4 | 156 | |
a2b22788 | 157 | /* Variable to hold the complete absolutized filename of the SYSCALLS.c.X |
5f8037c4 RS |
158 | file. */ |
159 | ||
a2b22788 | 160 | static char * syscalls_absolute_filename; |
5f8037c4 | 161 | |
a019653e | 162 | #endif /* !defined (UNPROTOIZE) */ |
5f8037c4 | 163 | |
0f41302f | 164 | /* Type of the structure that holds information about macro unexpansions. */ |
5f8037c4 RS |
165 | |
166 | struct unexpansion_struct { | |
167 | const char *expanded; | |
168 | const char *contracted; | |
169 | }; | |
170 | typedef struct unexpansion_struct unexpansion; | |
171 | ||
172 | /* A table of conversions that may need to be made for some (stupid) older | |
173 | operating systems where these types are preprocessor macros rather than | |
174 | typedefs (as they really ought to be). | |
175 | ||
176 | WARNING: The contracted forms must be as small (or smaller) as the | |
177 | expanded forms, or else havoc will ensue. */ | |
178 | ||
179 | static const unexpansion unexpansions[] = { | |
180 | { "struct _iobuf", "FILE" }, | |
181 | { 0, 0 } | |
182 | }; | |
183 | ||
184 | /* The number of "primary" slots in the hash tables for filenames and for | |
185 | function names. This can be as big or as small as you like, except that | |
186 | it must be a power of two. */ | |
187 | ||
188 | #define HASH_TABLE_SIZE (1 << 9) | |
189 | ||
190 | /* Bit mask to use when computing hash values. */ | |
191 | ||
192 | static const int hash_mask = (HASH_TABLE_SIZE - 1); | |
193 | ||
194 | /* Make a table of default system include directories | |
86702e31 | 195 | just as it is done in cpp. */ |
5f8037c4 RS |
196 | |
197 | #ifndef STANDARD_INCLUDE_DIR | |
198 | #define STANDARD_INCLUDE_DIR "/usr/include" | |
199 | #endif | |
200 | ||
201 | #ifndef LOCAL_INCLUDE_DIR | |
202 | #define LOCAL_INCLUDE_DIR "/usr/local/include" | |
203 | #endif | |
204 | ||
956d6950 JL |
205 | struct default_include { const char *fname; |
206 | const char *component; | |
207 | int x1, x2; } include_defaults[] | |
5f8037c4 RS |
208 | #ifdef INCLUDE_DEFAULTS |
209 | = INCLUDE_DEFAULTS; | |
210 | #else | |
211 | = { | |
212 | /* Pick up GNU C++ specific include files. */ | |
956d6950 | 213 | { GPLUSPLUS_INCLUDE_DIR, "G++", 1, 1 }, |
5f8037c4 | 214 | #ifdef CROSS_COMPILE |
e1139a5e RS |
215 | /* This is the dir for fixincludes. Put it just before |
216 | the files that we fix. */ | |
956d6950 | 217 | { GCC_INCLUDE_DIR, "GCC", 0, 0 }, |
5f8037c4 RS |
218 | /* For cross-compilation, this dir name is generated |
219 | automatically in Makefile.in. */ | |
956d6950 | 220 | { CROSS_INCLUDE_DIR, 0, 0, 0 }, |
e1139a5e | 221 | /* This is another place that the target system's headers might be. */ |
956d6950 | 222 | { TOOL_INCLUDE_DIR, "BINUTILS", 0, 0 }, |
5f8037c4 | 223 | #else /* not CROSS_COMPILE */ |
e1139a5e RS |
224 | /* This should be /use/local/include and should come before |
225 | the fixincludes-fixed header files. */ | |
956d6950 | 226 | { LOCAL_INCLUDE_DIR, 0, 0, 1 }, |
e1139a5e RS |
227 | /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here. |
228 | Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */ | |
956d6950 | 229 | { TOOL_INCLUDE_DIR, "BINUTILS", 0, 0 }, |
e1139a5e RS |
230 | /* This is the dir for fixincludes. Put it just before |
231 | the files that we fix. */ | |
956d6950 | 232 | { GCC_INCLUDE_DIR, "GCC", 0, 0 }, |
5f8037c4 RS |
233 | /* Some systems have an extra dir of include files. */ |
234 | #ifdef SYSTEM_INCLUDE_DIR | |
956d6950 | 235 | { SYSTEM_INCLUDE_DIR, 0, 0, 0 }, |
5f8037c4 | 236 | #endif |
956d6950 | 237 | { STANDARD_INCLUDE_DIR, 0, 0, 0}, |
5f8037c4 | 238 | #endif /* not CROSS_COMPILE */ |
956d6950 | 239 | { 0, 0, 0, 0} |
5f8037c4 RS |
240 | }; |
241 | #endif /* no INCLUDE_DEFAULTS */ | |
242 | ||
243 | /* Datatype for lists of directories or filenames. */ | |
244 | struct string_list | |
245 | { | |
ffb9f2f1 | 246 | const char *name; |
5f8037c4 RS |
247 | struct string_list *next; |
248 | }; | |
249 | ||
ffb9f2f1 KG |
250 | static struct string_list *string_list_cons PARAMS ((const char *, |
251 | struct string_list *)); | |
252 | ||
5f8037c4 RS |
253 | /* List of directories in which files should be converted. */ |
254 | ||
255 | struct string_list *directory_list; | |
256 | ||
257 | /* List of file names which should not be converted. | |
258 | A file is excluded if the end of its name, following a /, | |
259 | matches one of the names in this list. */ | |
260 | ||
261 | struct string_list *exclude_list; | |
262 | ||
263 | /* The name of the other style of variable-number-of-parameters functions | |
264 | (i.e. the style that we want to leave unconverted because we don't yet | |
265 | know how to convert them to this style. This string is used in warning | |
266 | messages. */ | |
267 | ||
268 | /* Also define here the string that we can search for in the parameter lists | |
269 | taken from the .X files which will unambiguously indicate that we have | |
270 | found a varargs style function. */ | |
271 | ||
272 | #ifdef UNPROTOIZE | |
273 | static const char * const other_var_style = "stdarg"; | |
a019653e | 274 | #else /* !defined (UNPROTOIZE) */ |
5f8037c4 | 275 | static const char * const other_var_style = "varargs"; |
a2b22788 RS |
276 | /* Note that this is a string containing the expansion of va_alist. |
277 | But in `main' we discard all but the first token. */ | |
a019653e RS |
278 | static const char *varargs_style_indicator = STRINGIFY (va_alist); |
279 | #endif /* !defined (UNPROTOIZE) */ | |
5f8037c4 RS |
280 | |
281 | /* The following two types are used to create hash tables. In this program, | |
282 | there are two hash tables which are used to store and quickly lookup two | |
283 | different classes of strings. The first type of strings stored in the | |
a2b22788 | 284 | first hash table are absolute filenames of files which protoize needs to |
5f8037c4 RS |
285 | know about. The second type of strings (stored in the second hash table) |
286 | are function names. It is this second class of strings which really | |
287 | inspired the use of the hash tables, because there may be a lot of them. */ | |
288 | ||
289 | typedef struct hash_table_entry_struct hash_table_entry; | |
290 | ||
291 | /* Do some typedefs so that we don't have to write "struct" so often. */ | |
292 | ||
293 | typedef struct def_dec_info_struct def_dec_info; | |
294 | typedef struct file_info_struct file_info; | |
295 | typedef struct f_list_chain_item_struct f_list_chain_item; | |
296 | ||
ffb9f2f1 KG |
297 | #ifndef UNPROTOIZE |
298 | static int is_syscalls_file PARAMS ((const file_info *)); | |
299 | static void rename_c_file PARAMS ((const hash_table_entry *)); | |
300 | static const def_dec_info *find_extern_def PARAMS ((const def_dec_info *, | |
301 | const def_dec_info *)); | |
302 | static const def_dec_info *find_static_definition PARAMS ((const def_dec_info *)); | |
303 | static void connect_defs_and_decs PARAMS ((const hash_table_entry *)); | |
304 | static void add_local_decl PARAMS ((const def_dec_info *, const char *)); | |
305 | static void add_global_decls PARAMS ((const file_info *, const char *)); | |
306 | #endif /* ! UNPROTOIZE */ | |
307 | static int needs_to_be_converted PARAMS ((const file_info *)); | |
308 | static void visit_each_hash_node PARAMS ((const hash_table_entry *, | |
309 | void (*)(const hash_table_entry *))); | |
310 | static hash_table_entry *add_symbol PARAMS ((hash_table_entry *, const char *)); | |
311 | static hash_table_entry *lookup PARAMS ((hash_table_entry *, const char *)); | |
312 | static void free_def_dec PARAMS ((def_dec_info *)); | |
313 | static file_info *find_file PARAMS ((const char *, int)); | |
314 | static void reverse_def_dec_list PARAMS ((const hash_table_entry *)); | |
315 | static void edit_fn_declaration PARAMS ((const def_dec_info *, const char *)); | |
316 | static int edit_formals_lists PARAMS ((const char *, unsigned int, | |
317 | const def_dec_info *)); | |
318 | static void edit_fn_definition PARAMS ((const def_dec_info *, const char *)); | |
319 | static void scan_for_missed_items PARAMS ((const file_info *)); | |
320 | static void edit_file PARAMS ((const hash_table_entry *)); | |
321 | ||
5f8037c4 | 322 | /* In the struct below, note that the "_info" field has two different uses |
a2b22788 RS |
323 | depending on the type of hash table we are in (i.e. either the filenames |
324 | hash table or the function names hash table). In the filenames hash table | |
5f8037c4 | 325 | the info fields of the entries point to the file_info struct which is |
a2b22788 | 326 | associated with each filename (1 per filename). In the function names |
5f8037c4 RS |
327 | hash table, the info field points to the head of a singly linked list of |
328 | def_dec_info entries which are all defs or decs of the function whose | |
329 | name is pointed to by the "symbol" field. Keeping all of the defs/decs | |
330 | for a given function name on a special list specifically for that function | |
331 | name makes it quick and easy to find out all of the important information | |
332 | about a given (named) function. */ | |
333 | ||
334 | struct hash_table_entry_struct { | |
335 | hash_table_entry * hash_next; /* -> to secondary entries */ | |
336 | const char * symbol; /* -> to the hashed string */ | |
337 | union { | |
338 | const def_dec_info * _ddip; | |
339 | file_info * _fip; | |
340 | } _info; | |
341 | }; | |
342 | #define ddip _info._ddip | |
343 | #define fip _info._fip | |
344 | ||
345 | /* Define a type specifically for our two hash tables. */ | |
346 | ||
347 | typedef hash_table_entry hash_table[HASH_TABLE_SIZE]; | |
348 | ||
349 | /* The following struct holds all of the important information about any | |
a2b22788 | 350 | single filename (e.g. file) which we need to know about. */ |
5f8037c4 RS |
351 | |
352 | struct file_info_struct { | |
353 | const hash_table_entry * hash_entry; /* -> to associated hash entry */ | |
354 | const def_dec_info * defs_decs; /* -> to chain of defs/decs */ | |
355 | time_t mtime; /* Time of last modification. */ | |
356 | }; | |
357 | ||
358 | /* Due to the possibility that functions may return pointers to functions, | |
359 | (which may themselves have their own parameter lists) and due to the | |
360 | fact that returned pointers-to-functions may be of type "pointer-to- | |
361 | function-returning-pointer-to-function" (ad nauseum) we have to keep | |
362 | an entire chain of ANSI style formal parameter lists for each function. | |
363 | ||
364 | Normally, for any given function, there will only be one formals list | |
365 | on the chain, but you never know. | |
366 | ||
367 | Note that the head of each chain of formals lists is pointed to by the | |
368 | `f_list_chain' field of the corresponding def_dec_info record. | |
369 | ||
370 | For any given chain, the item at the head of the chain is the *leftmost* | |
371 | parameter list seen in the actual C language function declaration. If | |
372 | there are other members of the chain, then these are linked in left-to-right | |
373 | order from the head of the chain. */ | |
374 | ||
375 | struct f_list_chain_item_struct { | |
376 | const f_list_chain_item * chain_next; /* -> to next item on chain */ | |
377 | const char * formals_list; /* -> to formals list string */ | |
378 | }; | |
379 | ||
380 | /* The following struct holds all of the important information about any | |
381 | single function definition or declaration which we need to know about. | |
382 | Note that for unprotoize we don't need to know very much because we | |
383 | never even create records for stuff that we don't intend to convert | |
384 | (like for instance defs and decs which are already in old K&R format | |
385 | and "implicit" function declarations). */ | |
386 | ||
387 | struct def_dec_info_struct { | |
388 | const def_dec_info * next_in_file; /* -> to rest of chain for file */ | |
389 | file_info * file; /* -> file_info for containing file */ | |
390 | int line; /* source line number of def/dec */ | |
391 | const char * ansi_decl; /* -> left end of ansi decl */ | |
392 | hash_table_entry * hash_entry; /* -> hash entry for function name */ | |
393 | unsigned int is_func_def; /* = 0 means this is a declaration */ | |
394 | const def_dec_info * next_for_func; /* -> to rest of chain for func name */ | |
395 | unsigned int f_list_count; /* count of formals lists we expect */ | |
396 | char prototyped; /* = 0 means already prototyped */ | |
397 | #ifndef UNPROTOIZE | |
398 | const f_list_chain_item * f_list_chain; /* -> chain of formals lists */ | |
399 | const def_dec_info * definition; /* -> def/dec containing related def */ | |
6dc42e49 | 400 | char is_static; /* = 0 means visibility is "extern" */ |
5f8037c4 RS |
401 | char is_implicit; /* != 0 for implicit func decl's */ |
402 | char written; /* != 0 means written for implicit */ | |
a019653e | 403 | #else /* !defined (UNPROTOIZE) */ |
5f8037c4 | 404 | const char * formal_names; /* -> to list of names of formals */ |
6dc42e49 | 405 | const char * formal_decls; /* -> to string of formal declarations */ |
a019653e | 406 | #endif /* !defined (UNPROTOIZE) */ |
5f8037c4 RS |
407 | }; |
408 | ||
a2b22788 | 409 | /* Pointer to the tail component of the filename by which this program was |
5f8037c4 RS |
410 | invoked. Used everywhere in error and warning messages. */ |
411 | ||
412 | static const char *pname; | |
413 | ||
414 | /* Error counter. Will be non-zero if we should give up at the next convenient | |
415 | stopping point. */ | |
416 | ||
417 | static int errors = 0; | |
418 | ||
419 | /* Option flags. */ | |
420 | /* ??? These comments should say what the flag mean as well as the options | |
421 | that set them. */ | |
422 | ||
a019653e RS |
423 | /* File name to use for running gcc. Allows GCC 2 to be named |
424 | something other than gcc. */ | |
8241a41f | 425 | static const char *compiler_file_name = "gcc"; |
ef91d7e2 | 426 | |
34e56753 RS |
427 | static int version_flag = 0; /* Print our version number. */ |
428 | static int quiet_flag = 0; /* Don't print messages normally. */ | |
429 | static int nochange_flag = 0; /* Don't convert, just say what files | |
430 | we would have converted. */ | |
431 | static int nosave_flag = 0; /* Don't save the old version. */ | |
432 | static int keep_flag = 0; /* Don't delete the .X files. */ | |
433 | static const char ** compile_params = 0; /* Option string for gcc. */ | |
5f8037c4 | 434 | #ifdef UNPROTOIZE |
34e56753 RS |
435 | static const char *indent_string = " "; /* Indentation for newly |
436 | inserted parm decls. */ | |
a019653e | 437 | #else /* !defined (UNPROTOIZE) */ |
34e56753 | 438 | static int local_flag = 0; /* Insert new local decls (when?). */ |
5f8037c4 | 439 | static int global_flag = 0; /* set by -g option */ |
34e56753 | 440 | static int cplusplus_flag = 0; /* Rename converted files to *.C. */ |
0f41302f | 441 | static const char *nondefault_syscalls_dir = 0; /* Dir to look for |
34e56753 | 442 | SYSCALLS.c.X in. */ |
a019653e | 443 | #endif /* !defined (UNPROTOIZE) */ |
5f8037c4 | 444 | |
bd0725f3 RS |
445 | /* An index into the compile_params array where we should insert the source |
446 | file name when we are ready to exec the C compiler. A zero value indicates | |
a019653e | 447 | that we have not yet called munge_compile_params. */ |
5f8037c4 | 448 | |
bd0725f3 RS |
449 | static int input_file_name_index = 0; |
450 | ||
451 | /* An index into the compile_params array where we should insert the filename | |
452 | for the aux info file, when we run the C compiler. */ | |
453 | static int aux_info_file_name_index = 0; | |
5f8037c4 RS |
454 | |
455 | /* Count of command line arguments which were "filename" arguments. */ | |
456 | ||
a2b22788 | 457 | static int n_base_source_files = 0; |
5f8037c4 RS |
458 | |
459 | /* Points to a malloc'ed list of pointers to all of the filenames of base | |
460 | source files which were specified on the command line. */ | |
461 | ||
a2b22788 | 462 | static const char **base_source_filenames; |
5f8037c4 RS |
463 | |
464 | /* Line number of the line within the current aux_info file that we | |
465 | are currently processing. Used for error messages in case the prototypes | |
466 | info file is corrupted somehow. */ | |
467 | ||
468 | static int current_aux_info_lineno; | |
469 | ||
470 | /* Pointer to the name of the source file currently being converted. */ | |
471 | ||
a2b22788 | 472 | static const char *convert_filename; |
5f8037c4 RS |
473 | |
474 | /* Pointer to relative root string (taken from aux_info file) which indicates | |
475 | where directory the user was in when he did the compilation step that | |
0f41302f | 476 | produced the containing aux_info file. */ |
5f8037c4 | 477 | |
a2b22788 | 478 | static const char *invocation_filename; |
5f8037c4 RS |
479 | |
480 | /* Pointer to the base of the input buffer that holds the original text for the | |
481 | source file currently being converted. */ | |
482 | ||
483 | static const char *orig_text_base; | |
484 | ||
485 | /* Pointer to the byte just beyond the end of the input buffer that holds the | |
486 | original text for the source file currently being converted. */ | |
487 | ||
488 | static const char *orig_text_limit; | |
489 | ||
490 | /* Pointer to the base of the input buffer that holds the cleaned text for the | |
491 | source file currently being converted. */ | |
492 | ||
493 | static const char *clean_text_base; | |
494 | ||
495 | /* Pointer to the byte just beyond the end of the input buffer that holds the | |
496 | cleaned text for the source file currently being converted. */ | |
497 | ||
498 | static const char *clean_text_limit; | |
499 | ||
500 | /* Pointer to the last byte in the cleaned text buffer that we have already | |
501 | (virtually) copied to the output buffer (or decided to ignore). */ | |
502 | ||
503 | static const char * clean_read_ptr; | |
504 | ||
505 | /* Pointer to the base of the output buffer that holds the replacement text | |
506 | for the source file currently being converted. */ | |
507 | ||
508 | static char *repl_text_base; | |
509 | ||
510 | /* Pointer to the byte just beyond the end of the output buffer that holds the | |
511 | replacement text for the source file currently being converted. */ | |
512 | ||
513 | static char *repl_text_limit; | |
514 | ||
515 | /* Pointer to the last byte which has been stored into the output buffer. | |
516 | The next byte to be stored should be stored just past where this points | |
517 | to. */ | |
518 | ||
519 | static char * repl_write_ptr; | |
520 | ||
521 | /* Pointer into the cleaned text buffer for the source file we are currently | |
522 | converting. This points to the first character of the line that we last | |
a019653e | 523 | did a "seek_to_line" to (see below). */ |
5f8037c4 RS |
524 | |
525 | static const char *last_known_line_start; | |
526 | ||
527 | /* Number of the line (in the cleaned text buffer) that we last did a | |
a019653e | 528 | "seek_to_line" to. Will be one if we just read a new source file |
5f8037c4 RS |
529 | into the cleaned text buffer. */ |
530 | ||
531 | static int last_known_line_number; | |
532 | ||
a2b22788 | 533 | /* The filenames hash table. */ |
5f8037c4 | 534 | |
a2b22788 | 535 | static hash_table filename_primary; |
5f8037c4 RS |
536 | |
537 | /* The function names hash table. */ | |
538 | ||
539 | static hash_table function_name_primary; | |
540 | ||
541 | /* The place to keep the recovery address which is used only in cases where | |
542 | we get hopelessly confused by something in the cleaned original text. */ | |
543 | ||
544 | static jmp_buf source_confusion_recovery; | |
545 | ||
a2b22788 | 546 | /* A pointer to the current directory filename (used by abspath). */ |
5f8037c4 RS |
547 | |
548 | static char *cwd_buffer; | |
549 | ||
550 | /* A place to save the read pointer until we are sure that an individual | |
551 | attempt at editing will succeed. */ | |
552 | ||
553 | static const char * saved_clean_read_ptr; | |
554 | ||
555 | /* A place to save the write pointer until we are sure that an individual | |
556 | attempt at editing will succeed. */ | |
557 | ||
558 | static char * saved_repl_write_ptr; | |
ab87f8c8 JL |
559 | \f |
560 | /* Translate and output an error message. */ | |
ab87f8c8 | 561 | static void |
d059a239 | 562 | notice VPARAMS ((const char *msgid, ...)) |
ab87f8c8 JL |
563 | { |
564 | #ifndef ANSI_PROTOTYPES | |
565 | const char *msgid; | |
566 | #endif | |
567 | va_list ap; | |
568 | ||
569 | VA_START (ap, msgid); | |
570 | ||
571 | #ifndef ANSI_PROTOTYPES | |
572 | msgid = va_arg (ap, const char *); | |
573 | #endif | |
574 | ||
575 | vfprintf (stderr, _(msgid), ap); | |
576 | va_end (ap); | |
577 | } | |
578 | ||
5f8037c4 | 579 | \f |
5f8037c4 RS |
580 | /* Make a copy of a string INPUT with size SIZE. */ |
581 | ||
582 | static char * | |
34e56753 RS |
583 | savestring (input, size) |
584 | const char *input; | |
6f4870cf | 585 | unsigned int size; |
5f8037c4 RS |
586 | { |
587 | char *output = (char *) xmalloc (size + 1); | |
588 | strcpy (output, input); | |
589 | return output; | |
590 | } | |
591 | ||
592 | /* More 'friendly' abort that prints the line and file. | |
593 | config.h can #define abort fancy_abort if you like that sort of thing. */ | |
594 | ||
595 | void | |
596 | fancy_abort () | |
597 | { | |
ab87f8c8 | 598 | notice ("%s: internal abort\n", pname); |
2e3f9f3d | 599 | exit (FATAL_EXIT_CODE); |
5f8037c4 RS |
600 | } |
601 | \f | |
5f8037c4 RS |
602 | /* Make a duplicate of the first N bytes of a given string in a newly |
603 | allocated area. */ | |
604 | ||
605 | static char * | |
34e56753 RS |
606 | dupnstr (s, n) |
607 | const char *s; | |
608 | size_t n; | |
5f8037c4 | 609 | { |
600ceaa9 | 610 | char *ret_val = (char *) xmalloc (n + 1); |
5f8037c4 | 611 | |
600ceaa9 | 612 | strncpy (ret_val, s, n); |
5f8037c4 RS |
613 | ret_val[n] = '\0'; |
614 | return ret_val; | |
615 | } | |
616 | ||
6dc42e49 | 617 | /* Return a pointer to the first occurrence of s2 within s1 or NULL if s2 |
5f8037c4 RS |
618 | does not occur within s1. Assume neither s1 nor s2 are null pointers. */ |
619 | ||
620 | static const char * | |
34e56753 RS |
621 | substr (s1, s2) |
622 | const char *s1; | |
623 | const char *const s2; | |
5f8037c4 RS |
624 | { |
625 | for (; *s1 ; s1++) | |
626 | { | |
627 | const char *p1; | |
628 | const char *p2; | |
a2b22788 | 629 | int c; |
5f8037c4 | 630 | |
51723711 | 631 | for (p1 = s1, p2 = s2; (c = *p2); p1++, p2++) |
5f8037c4 RS |
632 | if (*p1 != c) |
633 | goto outer; | |
634 | return s1; | |
635 | outer: | |
636 | ; | |
637 | } | |
638 | return 0; | |
639 | } | |
640 | \f | |
c7bfb646 RS |
641 | /* Read LEN bytes at PTR from descriptor DESC, for file FILENAME, |
642 | retrying if necessary. Return the actual number of bytes read. */ | |
643 | ||
644 | static int | |
645 | safe_read (desc, ptr, len) | |
646 | int desc; | |
ffb9f2f1 | 647 | PTR ptr; |
c7bfb646 RS |
648 | int len; |
649 | { | |
650 | int left = len; | |
651 | while (left > 0) { | |
55abdd3c | 652 | int nchars = read (desc, ptr, left); |
c7bfb646 | 653 | if (nchars < 0) |
c9a8a295 RS |
654 | { |
655 | #ifdef EINTR | |
656 | if (errno == EINTR) | |
657 | continue; | |
658 | #endif | |
659 | return nchars; | |
660 | } | |
c7bfb646 RS |
661 | if (nchars == 0) |
662 | break; | |
9c592305 KG |
663 | /* Arithmetic on void pointers is a gcc extention. */ |
664 | ptr = (char *) ptr + nchars; | |
c7bfb646 RS |
665 | left -= nchars; |
666 | } | |
667 | return len - left; | |
668 | } | |
669 | ||
670 | /* Write LEN bytes at PTR to descriptor DESC, | |
671 | retrying if necessary, and treating any real error as fatal. */ | |
672 | ||
673 | static void | |
674 | safe_write (desc, ptr, len, out_fname) | |
675 | int desc; | |
ffb9f2f1 | 676 | PTR ptr; |
c7bfb646 | 677 | int len; |
ffb9f2f1 | 678 | const char *out_fname; |
c7bfb646 RS |
679 | { |
680 | while (len > 0) { | |
55abdd3c | 681 | int written = write (desc, ptr, len); |
c7bfb646 | 682 | if (written < 0) |
c9a8a295 | 683 | { |
e5e809f4 | 684 | int errno_val = errno; |
c9a8a295 | 685 | #ifdef EINTR |
e5e809f4 | 686 | if (errno_val == EINTR) |
c9a8a295 RS |
687 | continue; |
688 | #endif | |
ab87f8c8 JL |
689 | notice ("%s: error writing file `%s': %s\n", |
690 | pname, shortpath (NULL, out_fname), xstrerror (errno_val)); | |
c4434aaa | 691 | return; |
c9a8a295 | 692 | } |
9c592305 KG |
693 | /* Arithmetic on void pointers is a gcc extention. */ |
694 | ptr = (char *) ptr + written; | |
c7bfb646 RS |
695 | len -= written; |
696 | } | |
697 | } | |
698 | \f | |
5f8037c4 RS |
699 | /* Get setup to recover in case the edit we are about to do goes awry. */ |
700 | ||
ffb9f2f1 | 701 | static void |
34e56753 | 702 | save_pointers () |
5f8037c4 RS |
703 | { |
704 | saved_clean_read_ptr = clean_read_ptr; | |
705 | saved_repl_write_ptr = repl_write_ptr; | |
706 | } | |
707 | ||
708 | /* Call this routine to recover our previous state whenever something looks | |
709 | too confusing in the source code we are trying to edit. */ | |
710 | ||
ffb9f2f1 | 711 | static void |
34e56753 | 712 | restore_pointers () |
5f8037c4 RS |
713 | { |
714 | clean_read_ptr = saved_clean_read_ptr; | |
715 | repl_write_ptr = saved_repl_write_ptr; | |
716 | } | |
717 | ||
3826a3da | 718 | /* Return true if the given character is a valid identifier character. */ |
5f8037c4 | 719 | |
34e56753 RS |
720 | static int |
721 | is_id_char (ch) | |
ffb9f2f1 | 722 | int ch; |
5f8037c4 | 723 | { |
e9a780ec | 724 | return (ISALNUM (ch) || (ch == '_') || (ch == '$')); |
5f8037c4 RS |
725 | } |
726 | ||
727 | /* Give a message indicating the proper way to invoke this program and then | |
728 | exit with non-zero status. */ | |
729 | ||
730 | static void | |
34e56753 | 731 | usage () |
5f8037c4 RS |
732 | { |
733 | #ifdef UNPROTOIZE | |
ab87f8c8 JL |
734 | notice ("%s: usage '%s [ -VqfnkN ] [ -i <istring> ] [ filename ... ]'\n", |
735 | pname, pname); | |
a019653e | 736 | #else /* !defined (UNPROTOIZE) */ |
ab87f8c8 JL |
737 | notice ("%s: usage '%s [ -VqfnkNlgC ] [ -B <dirname> ] [ filename ... ]'\n", |
738 | pname, pname); | |
a019653e | 739 | #endif /* !defined (UNPROTOIZE) */ |
2e3f9f3d | 740 | exit (FATAL_EXIT_CODE); |
5f8037c4 RS |
741 | } |
742 | ||
a2b22788 | 743 | /* Return true if the given filename (assumed to be an absolute filename) |
5f8037c4 RS |
744 | designates a file residing anywhere beneath any one of the "system" |
745 | include directories. */ | |
746 | ||
747 | static int | |
34e56753 RS |
748 | in_system_include_dir (path) |
749 | const char *path; | |
5f8037c4 RS |
750 | { |
751 | struct default_include *p; | |
752 | ||
ee77eda5 | 753 | if (! is_abspath (path)) |
a2b22788 | 754 | abort (); /* Must be an absolutized filename. */ |
5f8037c4 RS |
755 | |
756 | for (p = include_defaults; p->fname; p++) | |
757 | if (!strncmp (path, p->fname, strlen (p->fname)) | |
ee77eda5 | 758 | && IS_DIR_SEPARATOR (path[strlen (p->fname)])) |
5f8037c4 RS |
759 | return 1; |
760 | return 0; | |
761 | } | |
762 | \f | |
763 | #if 0 | |
a2b22788 | 764 | /* Return true if the given filename designates a file that the user has |
5f8037c4 RS |
765 | read access to and for which the user has write access to the containing |
766 | directory. */ | |
767 | ||
768 | static int | |
769 | file_could_be_converted (const char *path) | |
770 | { | |
771 | char *const dir_name = (char *) alloca (strlen (path) + 1); | |
772 | ||
ffb9f2f1 | 773 | if (access (path, R_OK)) |
5f8037c4 RS |
774 | return 0; |
775 | ||
776 | { | |
777 | char *dir_last_slash; | |
778 | ||
779 | strcpy (dir_name, path); | |
ee77eda5 MK |
780 | dir_last_slash = strrchr (dir_name, DIR_SEPARATOR); |
781 | #ifdef DIR_SEPARATOR_2 | |
782 | { | |
783 | char *slash; | |
784 | ||
785 | slash = strrchr (dir_last_slash ? dir_last_slash : dir_name, | |
786 | DIR_SEPARATOR_2); | |
787 | if (slash) | |
788 | dir_last_slash = slash; | |
789 | } | |
790 | #endif | |
5f8037c4 RS |
791 | if (dir_last_slash) |
792 | *dir_last_slash = '\0'; | |
793 | else | |
a2b22788 | 794 | abort (); /* Should have been an absolutized filename. */ |
5f8037c4 RS |
795 | } |
796 | ||
ffb9f2f1 | 797 | if (access (path, W_OK)) |
5f8037c4 RS |
798 | return 0; |
799 | ||
800 | return 1; | |
801 | } | |
802 | ||
a2b22788 | 803 | /* Return true if the given filename designates a file that we are allowed |
5f8037c4 RS |
804 | to modify. Files which we should not attempt to modify are (a) "system" |
805 | include files, and (b) files which the user doesn't have write access to, | |
806 | and (c) files which reside in directories which the user doesn't have | |
807 | write access to. Unless requested to be quiet, give warnings about | |
808 | files that we will not try to convert for one reason or another. An | |
809 | exception is made for "system" include files, which we never try to | |
810 | convert and for which we don't issue the usual warnings. */ | |
811 | ||
812 | static int | |
6dc42e49 | 813 | file_normally_convertible (const char *path) |
5f8037c4 RS |
814 | { |
815 | char *const dir_name = alloca (strlen (path) + 1); | |
816 | ||
817 | if (in_system_include_dir (path)) | |
818 | return 0; | |
819 | ||
820 | { | |
821 | char *dir_last_slash; | |
822 | ||
823 | strcpy (dir_name, path); | |
ee77eda5 MK |
824 | dir_last_slash = strrchr (dir_name, DIR_SEPARATOR); |
825 | #ifdef DIR_SEPARATOR_2 | |
826 | { | |
827 | char *slash; | |
828 | ||
829 | slash = strrchr (dir_last_slash ? dir_last_slash : dir_name, | |
830 | DIR_SEPARATOR_2); | |
831 | if (slash) | |
832 | dir_last_slash = slash; | |
833 | } | |
834 | #endif | |
5f8037c4 RS |
835 | if (dir_last_slash) |
836 | *dir_last_slash = '\0'; | |
837 | else | |
a2b22788 | 838 | abort (); /* Should have been an absolutized filename. */ |
5f8037c4 RS |
839 | } |
840 | ||
ffb9f2f1 | 841 | if (access (path, R_OK)) |
5f8037c4 RS |
842 | { |
843 | if (!quiet_flag) | |
ab87f8c8 JL |
844 | notice ("%s: warning: no read access for file `%s'\n", |
845 | pname, shortpath (NULL, path)); | |
5f8037c4 RS |
846 | return 0; |
847 | } | |
848 | ||
ffb9f2f1 | 849 | if (access (path, W_OK)) |
5f8037c4 RS |
850 | { |
851 | if (!quiet_flag) | |
ab87f8c8 JL |
852 | notice ("%s: warning: no write access for file `%s'\n", |
853 | pname, shortpath (NULL, path)); | |
5f8037c4 RS |
854 | return 0; |
855 | } | |
856 | ||
ffb9f2f1 | 857 | if (access (dir_name, W_OK)) |
5f8037c4 RS |
858 | { |
859 | if (!quiet_flag) | |
ab87f8c8 JL |
860 | notice ("%s: warning: no write access for dir containing `%s'\n", |
861 | pname, shortpath (NULL, path)); | |
5f8037c4 RS |
862 | return 0; |
863 | } | |
864 | ||
865 | return 1; | |
866 | } | |
867 | #endif /* 0 */ | |
868 | \f | |
869 | #ifndef UNPROTOIZE | |
870 | ||
871 | /* Return true if the given file_info struct refers to the special SYSCALLS.c.X | |
872 | file. Return false otherwise. */ | |
873 | ||
874 | static int | |
34e56753 RS |
875 | is_syscalls_file (fi_p) |
876 | const file_info *fi_p; | |
5f8037c4 | 877 | { |
d742f26c RS |
878 | char const *f = fi_p->hash_entry->symbol; |
879 | size_t fl = strlen (f), sysl = sizeof (syscalls_filename) - 1; | |
880 | return sysl <= fl && strcmp (f + fl - sysl, syscalls_filename) == 0; | |
5f8037c4 RS |
881 | } |
882 | ||
a019653e | 883 | #endif /* !defined (UNPROTOIZE) */ |
5f8037c4 RS |
884 | |
885 | /* Check to see if this file will need to have anything done to it on this | |
886 | run. If there is nothing in the given file which both needs conversion | |
887 | and for which we have the necessary stuff to do the conversion, return | |
888 | false. Otherwise, return true. | |
889 | ||
890 | Note that (for protoize) it is only valid to call this function *after* | |
891 | the connections between declarations and definitions have all been made | |
a019653e | 892 | by connect_defs_and_decs. */ |
5f8037c4 RS |
893 | |
894 | static int | |
34e56753 RS |
895 | needs_to_be_converted (file_p) |
896 | const file_info *file_p; | |
5f8037c4 RS |
897 | { |
898 | const def_dec_info *ddp; | |
899 | ||
900 | #ifndef UNPROTOIZE | |
901 | ||
902 | if (is_syscalls_file (file_p)) | |
903 | return 0; | |
904 | ||
a019653e | 905 | #endif /* !defined (UNPROTOIZE) */ |
5f8037c4 RS |
906 | |
907 | for (ddp = file_p->defs_decs; ddp; ddp = ddp->next_in_file) | |
908 | ||
909 | if ( | |
910 | ||
911 | #ifndef UNPROTOIZE | |
912 | ||
0f41302f | 913 | /* ... and if we a protoizing and this function is in old style ... */ |
5f8037c4 | 914 | !ddp->prototyped |
0f41302f | 915 | /* ... and if this a definition or is a decl with an associated def ... */ |
5f8037c4 RS |
916 | && (ddp->is_func_def || (!ddp->is_func_def && ddp->definition)) |
917 | ||
a019653e | 918 | #else /* defined (UNPROTOIZE) */ |
5f8037c4 | 919 | |
0f41302f | 920 | /* ... and if we are unprotoizing and this function is in new style ... */ |
5f8037c4 RS |
921 | ddp->prototyped |
922 | ||
a019653e | 923 | #endif /* defined (UNPROTOIZE) */ |
5f8037c4 RS |
924 | ) |
925 | /* ... then the containing file needs converting. */ | |
926 | return -1; | |
927 | return 0; | |
928 | } | |
929 | ||
930 | /* Return 1 if the file name NAME is in a directory | |
931 | that should be converted. */ | |
932 | ||
933 | static int | |
34e56753 RS |
934 | directory_specified_p (name) |
935 | const char *name; | |
5f8037c4 RS |
936 | { |
937 | struct string_list *p; | |
938 | ||
939 | for (p = directory_list; p; p = p->next) | |
940 | if (!strncmp (name, p->name, strlen (p->name)) | |
ee77eda5 | 941 | && IS_DIR_SEPARATOR (name[strlen (p->name)])) |
37114d0d RS |
942 | { |
943 | const char *q = name + strlen (p->name) + 1; | |
944 | ||
945 | /* If there are more slashes, it's in a subdir, so | |
946 | this match doesn't count. */ | |
ee77eda5 MK |
947 | while (*q++) |
948 | if (IS_DIR_SEPARATOR (*(q-1))) | |
37114d0d RS |
949 | goto lose; |
950 | return 1; | |
951 | ||
952 | lose: ; | |
953 | } | |
5f8037c4 RS |
954 | |
955 | return 0; | |
956 | } | |
957 | ||
958 | /* Return 1 if the file named NAME should be excluded from conversion. */ | |
959 | ||
960 | static int | |
34e56753 RS |
961 | file_excluded_p (name) |
962 | const char *name; | |
5f8037c4 RS |
963 | { |
964 | struct string_list *p; | |
965 | int len = strlen (name); | |
966 | ||
967 | for (p = exclude_list; p; p = p->next) | |
968 | if (!strcmp (name + len - strlen (p->name), p->name) | |
ee77eda5 | 969 | && IS_DIR_SEPARATOR (name[len - strlen (p->name) - 1])) |
5f8037c4 RS |
970 | return 1; |
971 | ||
972 | return 0; | |
973 | } | |
974 | ||
975 | /* Construct a new element of a string_list. | |
976 | STRING is the new element value, and REST holds the remaining elements. */ | |
977 | ||
978 | static struct string_list * | |
34e56753 | 979 | string_list_cons (string, rest) |
ffb9f2f1 | 980 | const char *string; |
8241a41f | 981 | struct string_list *rest; |
5f8037c4 | 982 | { |
34e56753 RS |
983 | struct string_list *temp |
984 | = (struct string_list *) xmalloc (sizeof (struct string_list)); | |
985 | ||
5f8037c4 RS |
986 | temp->next = rest; |
987 | temp->name = string; | |
988 | return temp; | |
989 | } | |
990 | \f | |
991 | /* ??? The GNU convention for mentioning function args in its comments | |
992 | is to capitalize them. So change "hash_tab_p" to HASH_TAB_P below. | |
993 | Likewise for all the other functions. */ | |
994 | ||
995 | /* Given a hash table, apply some function to each node in the table. The | |
996 | table to traverse is given as the "hash_tab_p" argument, and the | |
997 | function to be applied to each node in the table is given as "func" | |
998 | argument. */ | |
999 | ||
1000 | static void | |
34e56753 RS |
1001 | visit_each_hash_node (hash_tab_p, func) |
1002 | const hash_table_entry *hash_tab_p; | |
ffb9f2f1 | 1003 | void (*func) PARAMS ((const hash_table_entry *)); |
5f8037c4 RS |
1004 | { |
1005 | const hash_table_entry *primary; | |
1006 | ||
1007 | for (primary = hash_tab_p; primary < &hash_tab_p[HASH_TABLE_SIZE]; primary++) | |
1008 | if (primary->symbol) | |
1009 | { | |
1010 | hash_table_entry *second; | |
1011 | ||
1012 | (*func)(primary); | |
1013 | for (second = primary->hash_next; second; second = second->hash_next) | |
1014 | (*func) (second); | |
1015 | } | |
1016 | } | |
1017 | ||
1018 | /* Initialize all of the fields of a new hash table entry, pointed | |
1019 | to by the "p" parameter. Note that the space to hold the entry | |
1020 | is assumed to have already been allocated before this routine is | |
1021 | called. */ | |
1022 | ||
1023 | static hash_table_entry * | |
34e56753 RS |
1024 | add_symbol (p, s) |
1025 | hash_table_entry *p; | |
1026 | const char *s; | |
5f8037c4 RS |
1027 | { |
1028 | p->hash_next = NULL; | |
ad85216e | 1029 | p->symbol = xstrdup (s); |
5f8037c4 RS |
1030 | p->ddip = NULL; |
1031 | p->fip = NULL; | |
1032 | return p; | |
1033 | } | |
1034 | ||
a2b22788 | 1035 | /* Look for a particular function name or filename in the particular |
5f8037c4 RS |
1036 | hash table indicated by "hash_tab_p". If the name is not in the |
1037 | given hash table, add it. Either way, return a pointer to the | |
1038 | hash table entry for the given name. */ | |
1039 | ||
1040 | static hash_table_entry * | |
34e56753 RS |
1041 | lookup (hash_tab_p, search_symbol) |
1042 | hash_table_entry *hash_tab_p; | |
1043 | const char *search_symbol; | |
5f8037c4 RS |
1044 | { |
1045 | int hash_value = 0; | |
1046 | const char *search_symbol_char_p = search_symbol; | |
1047 | hash_table_entry *p; | |
1048 | ||
1049 | while (*search_symbol_char_p) | |
1050 | hash_value += *search_symbol_char_p++; | |
1051 | hash_value &= hash_mask; | |
1052 | p = &hash_tab_p[hash_value]; | |
1053 | if (! p->symbol) | |
1054 | return add_symbol (p, search_symbol); | |
1055 | if (!strcmp (p->symbol, search_symbol)) | |
1056 | return p; | |
1057 | while (p->hash_next) | |
1058 | { | |
1059 | p = p->hash_next; | |
1060 | if (!strcmp (p->symbol, search_symbol)) | |
1061 | return p; | |
1062 | } | |
1063 | p->hash_next = (hash_table_entry *) xmalloc (sizeof (hash_table_entry)); | |
1064 | p = p->hash_next; | |
1065 | return add_symbol (p, search_symbol); | |
1066 | } | |
1067 | \f | |
1068 | /* Throw a def/dec record on the junk heap. | |
1069 | ||
1070 | Also, since we are not using this record anymore, free up all of the | |
1071 | stuff it pointed to. */ | |
1072 | ||
34e56753 RS |
1073 | static void |
1074 | free_def_dec (p) | |
1075 | def_dec_info *p; | |
5f8037c4 | 1076 | { |
ffb9f2f1 | 1077 | free ((NONCONST PTR) p->ansi_decl); |
5f8037c4 RS |
1078 | |
1079 | #ifndef UNPROTOIZE | |
1080 | { | |
1081 | const f_list_chain_item * curr; | |
1082 | const f_list_chain_item * next; | |
1083 | ||
1084 | for (curr = p->f_list_chain; curr; curr = next) | |
1085 | { | |
1086 | next = curr->chain_next; | |
ffb9f2f1 | 1087 | free ((NONCONST PTR) curr); |
5f8037c4 RS |
1088 | } |
1089 | } | |
a019653e | 1090 | #endif /* !defined (UNPROTOIZE) */ |
5f8037c4 | 1091 | |
ad85216e | 1092 | free (p); |
5f8037c4 RS |
1093 | } |
1094 | ||
1095 | /* Unexpand as many macro symbol as we can find. | |
1096 | ||
1097 | If the given line must be unexpanded, make a copy of it in the heap and | |
1098 | return a pointer to the unexpanded copy. Otherwise return NULL. */ | |
1099 | ||
1100 | static char * | |
34e56753 RS |
1101 | unexpand_if_needed (aux_info_line) |
1102 | const char *aux_info_line; | |
5f8037c4 RS |
1103 | { |
1104 | static char *line_buf = 0; | |
1105 | static int line_buf_size = 0; | |
0f41302f | 1106 | const unexpansion *unexp_p; |
5f8037c4 RS |
1107 | int got_unexpanded = 0; |
1108 | const char *s; | |
1109 | char *copy_p = line_buf; | |
1110 | ||
1111 | if (line_buf == 0) | |
1112 | { | |
1113 | line_buf_size = 1024; | |
1114 | line_buf = (char *) xmalloc (line_buf_size); | |
1115 | } | |
1116 | ||
1117 | copy_p = line_buf; | |
1118 | ||
1119 | /* Make a copy of the input string in line_buf, expanding as necessary. */ | |
1120 | ||
1121 | for (s = aux_info_line; *s != '\n'; ) | |
1122 | { | |
1123 | for (unexp_p = unexpansions; unexp_p->expanded; unexp_p++) | |
1124 | { | |
1125 | const char *in_p = unexp_p->expanded; | |
1126 | size_t len = strlen (in_p); | |
1127 | ||
1128 | if (*s == *in_p && !strncmp (s, in_p, len) && !is_id_char (s[len])) | |
1129 | { | |
1130 | int size = strlen (unexp_p->contracted); | |
1131 | got_unexpanded = 1; | |
1132 | if (copy_p + size - line_buf >= line_buf_size) | |
1133 | { | |
1134 | int offset = copy_p - line_buf; | |
1135 | line_buf_size *= 2; | |
1136 | line_buf_size += size; | |
1137 | line_buf = (char *) xrealloc (line_buf, line_buf_size); | |
1138 | copy_p = line_buf + offset; | |
1139 | } | |
1140 | strcpy (copy_p, unexp_p->contracted); | |
1141 | copy_p += size; | |
1142 | ||
1143 | /* Assume the there will not be another replacement required | |
1144 | within the text just replaced. */ | |
1145 | ||
1146 | s += len; | |
1147 | goto continue_outer; | |
1148 | } | |
1149 | } | |
1150 | if (copy_p - line_buf == line_buf_size) | |
1151 | { | |
1152 | int offset = copy_p - line_buf; | |
1153 | line_buf_size *= 2; | |
1154 | line_buf = (char *) xrealloc (line_buf, line_buf_size); | |
1155 | copy_p = line_buf + offset; | |
1156 | } | |
1157 | *copy_p++ = *s++; | |
1158 | continue_outer: ; | |
1159 | } | |
1160 | if (copy_p + 2 - line_buf >= line_buf_size) | |
1161 | { | |
1162 | int offset = copy_p - line_buf; | |
1163 | line_buf_size *= 2; | |
1164 | line_buf = (char *) xrealloc (line_buf, line_buf_size); | |
1165 | copy_p = line_buf + offset; | |
1166 | } | |
1167 | *copy_p++ = '\n'; | |
8fc5db4e | 1168 | *copy_p = '\0'; |
5f8037c4 | 1169 | |
8fc5db4e | 1170 | return (got_unexpanded ? savestring (line_buf, copy_p - line_buf) : 0); |
5f8037c4 RS |
1171 | } |
1172 | \f | |
ee77eda5 MK |
1173 | /* Return 1 if pathname is absolute. */ |
1174 | ||
1175 | static int | |
1176 | is_abspath (path) | |
1177 | const char *path; | |
1178 | { | |
1179 | return (IS_DIR_SEPARATOR (path[0]) | |
1180 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM | |
1181 | /* Check for disk name on MS-DOS-based systems. */ | |
1182 | || (path[0] && path[1] == ':' && IS_DIR_SEPARATOR (path[2])) | |
1183 | #endif | |
1184 | ); | |
1185 | } | |
1186 | \f | |
a2b22788 RS |
1187 | /* Return the absolutized filename for the given relative |
1188 | filename. Note that if that filename is already absolute, it may | |
5f8037c4 RS |
1189 | still be returned in a modified form because this routine also |
1190 | eliminates redundant slashes and single dots and eliminates double | |
a2b22788 RS |
1191 | dots to get a shortest possible filename from the given input |
1192 | filename. The absolutization of relative filenames is made by | |
1193 | assuming that the given filename is to be taken as relative to | |
5f8037c4 RS |
1194 | the first argument (cwd) or to the current directory if cwd is |
1195 | NULL. */ | |
1196 | ||
1197 | static char * | |
34e56753 RS |
1198 | abspath (cwd, rel_filename) |
1199 | const char *cwd; | |
1200 | const char *rel_filename; | |
5f8037c4 RS |
1201 | { |
1202 | /* Setup the current working directory as needed. */ | |
1203 | const char *cwd2 = (cwd) ? cwd : cwd_buffer; | |
1204 | char *const abs_buffer | |
d45cf215 | 1205 | = (char *) alloca (strlen (cwd2) + strlen (rel_filename) + 2); |
5f8037c4 RS |
1206 | char *endp = abs_buffer; |
1207 | char *outp, *inp; | |
1208 | ||
d45cf215 | 1209 | /* Copy the filename (possibly preceded by the current working |
5f8037c4 RS |
1210 | directory name) into the absolutization buffer. */ |
1211 | ||
1212 | { | |
1213 | const char *src_p; | |
1214 | ||
ee77eda5 | 1215 | if (! is_abspath (rel_filename)) |
5f8037c4 RS |
1216 | { |
1217 | src_p = cwd2; | |
51723711 | 1218 | while ((*endp++ = *src_p++)) |
5f8037c4 | 1219 | continue; |
ee77eda5 | 1220 | *(endp-1) = DIR_SEPARATOR; /* overwrite null */ |
5f8037c4 | 1221 | } |
ee77eda5 MK |
1222 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM |
1223 | else if (IS_DIR_SEPARATOR (rel_filename[0])) | |
1224 | { | |
1225 | /* A path starting with a directory separator is considered absolute | |
1226 | for dos based filesystems, but it's really not -- it's just the | |
1227 | convention used throughout GCC and it works. However, in this | |
1228 | case, we still need to prepend the drive spec from cwd_buffer. */ | |
1229 | *endp++ = cwd2[0]; | |
1230 | *endp++ = cwd2[1]; | |
1231 | } | |
1232 | #endif | |
a2b22788 | 1233 | src_p = rel_filename; |
51723711 | 1234 | while ((*endp++ = *src_p++)) |
5f8037c4 | 1235 | continue; |
5f8037c4 RS |
1236 | } |
1237 | ||
1238 | /* Now make a copy of abs_buffer into abs_buffer, shortening the | |
a2b22788 | 1239 | filename (by taking out slashes and dots) as we go. */ |
5f8037c4 RS |
1240 | |
1241 | outp = inp = abs_buffer; | |
1242 | *outp++ = *inp++; /* copy first slash */ | |
8ebf19db | 1243 | #if defined (apollo) || defined (_WIN32) || defined (__INTERIX) |
ee77eda5 | 1244 | if (IS_DIR_SEPARATOR (inp[0])) |
d742f26c RS |
1245 | *outp++ = *inp++; /* copy second slash */ |
1246 | #endif | |
5f8037c4 RS |
1247 | for (;;) |
1248 | { | |
1249 | if (!inp[0]) | |
1250 | break; | |
ee77eda5 | 1251 | else if (IS_DIR_SEPARATOR (inp[0]) && IS_DIR_SEPARATOR (outp[-1])) |
5f8037c4 RS |
1252 | { |
1253 | inp++; | |
1254 | continue; | |
1255 | } | |
ee77eda5 | 1256 | else if (inp[0] == '.' && IS_DIR_SEPARATOR (outp[-1])) |
5f8037c4 RS |
1257 | { |
1258 | if (!inp[1]) | |
1259 | break; | |
ee77eda5 | 1260 | else if (IS_DIR_SEPARATOR (inp[1])) |
5f8037c4 RS |
1261 | { |
1262 | inp += 2; | |
1263 | continue; | |
1264 | } | |
ee77eda5 MK |
1265 | else if ((inp[1] == '.') && (inp[2] == 0 |
1266 | || IS_DIR_SEPARATOR (inp[2]))) | |
5f8037c4 | 1267 | { |
ee77eda5 | 1268 | inp += (IS_DIR_SEPARATOR (inp[2])) ? 3 : 2; |
5f8037c4 | 1269 | outp -= 2; |
ee77eda5 | 1270 | while (outp >= abs_buffer && ! IS_DIR_SEPARATOR (*outp)) |
5f8037c4 RS |
1271 | outp--; |
1272 | if (outp < abs_buffer) | |
2e3f9f3d RK |
1273 | { |
1274 | /* Catch cases like /.. where we try to backup to a | |
1275 | point above the absolute root of the logical file | |
1276 | system. */ | |
1277 | ||
ab87f8c8 JL |
1278 | notice ("%s: invalid file name: %s\n", |
1279 | pname, rel_filename); | |
2e3f9f3d RK |
1280 | exit (FATAL_EXIT_CODE); |
1281 | } | |
5f8037c4 RS |
1282 | *++outp = '\0'; |
1283 | continue; | |
2e3f9f3d | 1284 | } |
5f8037c4 RS |
1285 | } |
1286 | *outp++ = *inp++; | |
1287 | } | |
1288 | ||
1289 | /* On exit, make sure that there is a trailing null, and make sure that | |
1290 | the last character of the returned string is *not* a slash. */ | |
1291 | ||
1292 | *outp = '\0'; | |
ee77eda5 | 1293 | if (IS_DIR_SEPARATOR (outp[-1])) |
5f8037c4 RS |
1294 | *--outp = '\0'; |
1295 | ||
1296 | /* Make a copy (in the heap) of the stuff left in the absolutization | |
1297 | buffer and return a pointer to the copy. */ | |
1298 | ||
8fc5db4e | 1299 | return savestring (abs_buffer, outp - abs_buffer); |
5f8037c4 RS |
1300 | } |
1301 | \f | |
a2b22788 | 1302 | /* Given a filename (and possibly a directory name from which the filename |
5f8037c4 | 1303 | is relative) return a string which is the shortest possible |
a2b22788 | 1304 | equivalent for the corresponding full (absolutized) filename. The |
5f8037c4 | 1305 | shortest possible equivalent may be constructed by converting the |
a2b22788 RS |
1306 | absolutized filename to be a relative filename (i.e. relative to |
1307 | the actual current working directory). However if a relative filename | |
1308 | is longer, then the full absolute filename is returned. | |
5f8037c4 RS |
1309 | |
1310 | KNOWN BUG: | |
1311 | ||
a2b22788 RS |
1312 | Note that "simple-minded" conversion of any given type of filename (either |
1313 | relative or absolute) may not result in a valid equivalent filename if any | |
1314 | subpart of the original filename is actually a symbolic link. */ | |
5f8037c4 RS |
1315 | |
1316 | static const char * | |
34e56753 RS |
1317 | shortpath (cwd, filename) |
1318 | const char *cwd; | |
1319 | const char *filename; | |
5f8037c4 RS |
1320 | { |
1321 | char *rel_buffer; | |
1322 | char *rel_buf_p; | |
1323 | char *cwd_p = cwd_buffer; | |
1324 | char *path_p; | |
1325 | int unmatched_slash_count = 0; | |
d742f26c | 1326 | size_t filename_len = strlen (filename); |
5f8037c4 | 1327 | |
a2b22788 | 1328 | path_p = abspath (cwd, filename); |
d742f26c | 1329 | rel_buf_p = rel_buffer = (char *) xmalloc (filename_len); |
5f8037c4 | 1330 | |
ee77eda5 | 1331 | while (*cwd_p && IS_SAME_PATH_CHAR (*cwd_p, *path_p)) |
5f8037c4 RS |
1332 | { |
1333 | cwd_p++; | |
1334 | path_p++; | |
1335 | } | |
ee77eda5 | 1336 | if (!*cwd_p && (!*path_p || IS_DIR_SEPARATOR (*path_p))) |
5f8037c4 | 1337 | { |
ee77eda5 | 1338 | /* whole pwd matched */ |
5f8037c4 RS |
1339 | if (!*path_p) /* input *is* the current path! */ |
1340 | return "."; | |
1341 | else | |
1342 | return ++path_p; | |
1343 | } | |
1344 | else | |
1345 | { | |
1346 | if (*path_p) | |
1347 | { | |
1348 | --cwd_p; | |
1349 | --path_p; | |
ee77eda5 | 1350 | while (! IS_DIR_SEPARATOR (*cwd_p)) /* backup to last slash */ |
5f8037c4 RS |
1351 | { |
1352 | --cwd_p; | |
1353 | --path_p; | |
1354 | } | |
1355 | cwd_p++; | |
1356 | path_p++; | |
1357 | unmatched_slash_count++; | |
1358 | } | |
526fef40 RS |
1359 | |
1360 | /* Find out how many directory levels in cwd were *not* matched. */ | |
ee77eda5 MK |
1361 | while (*cwd_p++) |
1362 | if (IS_DIR_SEPARATOR (*(cwd_p-1))) | |
526fef40 RS |
1363 | unmatched_slash_count++; |
1364 | ||
1365 | /* Now we know how long the "short name" will be. | |
1366 | Reject it if longer than the input. */ | |
1367 | if (unmatched_slash_count * 3 + strlen (path_p) >= filename_len) | |
1368 | return filename; | |
1369 | ||
1370 | /* For each of them, put a `../' at the beginning of the short name. */ | |
5f8037c4 RS |
1371 | while (unmatched_slash_count--) |
1372 | { | |
526fef40 RS |
1373 | /* Give up if the result gets to be longer |
1374 | than the absolute path name. */ | |
d742f26c RS |
1375 | if (rel_buffer + filename_len <= rel_buf_p + 3) |
1376 | return filename; | |
5f8037c4 RS |
1377 | *rel_buf_p++ = '.'; |
1378 | *rel_buf_p++ = '.'; | |
ee77eda5 | 1379 | *rel_buf_p++ = DIR_SEPARATOR; |
5f8037c4 | 1380 | } |
d742f26c | 1381 | |
526fef40 | 1382 | /* Then tack on the unmatched part of the desired file's name. */ |
d742f26c RS |
1383 | do |
1384 | { | |
1385 | if (rel_buffer + filename_len <= rel_buf_p) | |
1386 | return filename; | |
1387 | } | |
51723711 | 1388 | while ((*rel_buf_p++ = *path_p++)); |
d742f26c | 1389 | |
5f8037c4 | 1390 | --rel_buf_p; |
ee77eda5 | 1391 | if (IS_DIR_SEPARATOR (*(rel_buf_p-1))) |
5f8037c4 | 1392 | *--rel_buf_p = '\0'; |
5f8037c4 RS |
1393 | return rel_buffer; |
1394 | } | |
5f8037c4 RS |
1395 | } |
1396 | \f | |
a2b22788 | 1397 | /* Lookup the given filename in the hash table for filenames. If it is a |
5f8037c4 | 1398 | new one, then the hash table info pointer will be null. In this case, |
a2b22788 | 1399 | we create a new file_info record to go with the filename, and we initialize |
5f8037c4 RS |
1400 | that record with some reasonable values. */ |
1401 | ||
8241a41f RS |
1402 | /* FILENAME was const, but that causes a warning on AIX when calling stat. |
1403 | That is probably a bug in AIX, but might as well avoid the warning. */ | |
1404 | ||
5f8037c4 | 1405 | static file_info * |
34e56753 | 1406 | find_file (filename, do_not_stat) |
ffb9f2f1 | 1407 | const char *filename; |
34e56753 | 1408 | int do_not_stat; |
5f8037c4 RS |
1409 | { |
1410 | hash_table_entry *hash_entry_p; | |
1411 | ||
a2b22788 | 1412 | hash_entry_p = lookup (filename_primary, filename); |
5f8037c4 RS |
1413 | if (hash_entry_p->fip) |
1414 | return hash_entry_p->fip; | |
1415 | else | |
1416 | { | |
1417 | struct stat stat_buf; | |
1418 | file_info *file_p = (file_info *) xmalloc (sizeof (file_info)); | |
1419 | ||
1420 | /* If we cannot get status on any given source file, give a warning | |
1421 | and then just set its time of last modification to infinity. */ | |
1422 | ||
1423 | if (do_not_stat) | |
1424 | stat_buf.st_mtime = (time_t) 0; | |
1425 | else | |
1426 | { | |
ffb9f2f1 | 1427 | if (stat (filename, &stat_buf) == -1) |
5f8037c4 | 1428 | { |
e5e809f4 | 1429 | int errno_val = errno; |
ab87f8c8 JL |
1430 | notice ("%s: %s: can't get status: %s\n", |
1431 | pname, shortpath (NULL, filename), | |
1432 | xstrerror (errno_val)); | |
5f8037c4 RS |
1433 | stat_buf.st_mtime = (time_t) -1; |
1434 | } | |
1435 | } | |
1436 | ||
1437 | hash_entry_p->fip = file_p; | |
1438 | file_p->hash_entry = hash_entry_p; | |
1439 | file_p->defs_decs = NULL; | |
1440 | file_p->mtime = stat_buf.st_mtime; | |
1441 | return file_p; | |
1442 | } | |
1443 | } | |
1444 | ||
1445 | /* Generate a fatal error because some part of the aux_info file is | |
1446 | messed up. */ | |
1447 | ||
1448 | static void | |
34e56753 | 1449 | aux_info_corrupted () |
5f8037c4 | 1450 | { |
ab87f8c8 JL |
1451 | notice ("\n%s: fatal error: aux info file corrupted at line %d\n", |
1452 | pname, current_aux_info_lineno); | |
2e3f9f3d | 1453 | exit (FATAL_EXIT_CODE); |
5f8037c4 RS |
1454 | } |
1455 | ||
1456 | /* ??? This comment is vague. Say what the condition is for. */ | |
a019653e | 1457 | /* Check to see that a condition is true. This is kind of like an assert. */ |
5f8037c4 | 1458 | |
34e56753 RS |
1459 | static void |
1460 | check_aux_info (cond) | |
1461 | int cond; | |
5f8037c4 RS |
1462 | { |
1463 | if (! cond) | |
1464 | aux_info_corrupted (); | |
1465 | } | |
1466 | ||
1467 | /* Given a pointer to the closing right parenthesis for a particular formals | |
858a47b1 | 1468 | list (in an aux_info file) find the corresponding left parenthesis and |
5f8037c4 RS |
1469 | return a pointer to it. */ |
1470 | ||
1471 | static const char * | |
34e56753 RS |
1472 | find_corresponding_lparen (p) |
1473 | const char *p; | |
5f8037c4 RS |
1474 | { |
1475 | const char *q; | |
1476 | int paren_depth; | |
1477 | ||
1478 | for (paren_depth = 1, q = p-1; paren_depth; q--) | |
1479 | { | |
1480 | switch (*q) | |
1481 | { | |
1482 | case ')': | |
1483 | paren_depth++; | |
1484 | break; | |
1485 | case '(': | |
1486 | paren_depth--; | |
1487 | break; | |
1488 | } | |
1489 | } | |
1490 | return ++q; | |
1491 | } | |
1492 | \f | |
1493 | /* Given a line from an aux info file, and a time at which the aux info | |
1494 | file it came from was created, check to see if the item described in | |
1495 | the line comes from a file which has been modified since the aux info | |
1496 | file was created. If so, return non-zero, else return zero. */ | |
1497 | ||
1498 | static int | |
34e56753 RS |
1499 | referenced_file_is_newer (l, aux_info_mtime) |
1500 | const char *l; | |
1501 | time_t aux_info_mtime; | |
5f8037c4 RS |
1502 | { |
1503 | const char *p; | |
1504 | file_info *fi_p; | |
1505 | char *filename; | |
1506 | ||
1507 | check_aux_info (l[0] == '/'); | |
1508 | check_aux_info (l[1] == '*'); | |
1509 | check_aux_info (l[2] == ' '); | |
1510 | ||
1511 | { | |
1512 | const char *filename_start = p = l + 3; | |
1513 | ||
ee77eda5 MK |
1514 | while (*p != ':' |
1515 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM | |
1516 | || (*p == ':' && *p && *(p+1) && IS_DIR_SEPARATOR (*(p+1))) | |
1517 | #endif | |
1518 | ) | |
5f8037c4 RS |
1519 | p++; |
1520 | filename = (char *) alloca ((size_t) (p - filename_start) + 1); | |
1521 | strncpy (filename, filename_start, (size_t) (p - filename_start)); | |
1522 | filename[p-filename_start] = '\0'; | |
1523 | } | |
1524 | ||
1525 | /* Call find_file to find the file_info record associated with the file | |
1526 | which contained this particular def or dec item. Note that this call | |
1527 | may cause a new file_info record to be created if this is the first time | |
1528 | that we have ever known about this particular file. */ | |
1529 | ||
a2b22788 | 1530 | fi_p = find_file (abspath (invocation_filename, filename), 0); |
5f8037c4 RS |
1531 | |
1532 | return (fi_p->mtime > aux_info_mtime); | |
1533 | } | |
1534 | \f | |
1535 | /* Given a line of info from the aux_info file, create a new | |
1536 | def_dec_info record to remember all of the important information about | |
1537 | a function definition or declaration. | |
1538 | ||
1539 | Link this record onto the list of such records for the particular file in | |
d45cf215 | 1540 | which it occurred in proper (descending) line number order (for now). |
5f8037c4 RS |
1541 | |
1542 | If there is an identical record already on the list for the file, throw | |
1543 | this one away. Doing so takes care of the (useless and troublesome) | |
1544 | duplicates which are bound to crop up due to multiple inclusions of any | |
1545 | given individual header file. | |
1546 | ||
1547 | Finally, link the new def_dec record onto the list of such records | |
1548 | pertaining to this particular function name. */ | |
1549 | ||
1550 | static void | |
34e56753 RS |
1551 | save_def_or_dec (l, is_syscalls) |
1552 | const char *l; | |
1553 | int is_syscalls; | |
5f8037c4 RS |
1554 | { |
1555 | const char *p; | |
1556 | const char *semicolon_p; | |
1557 | def_dec_info *def_dec_p = (def_dec_info *) xmalloc (sizeof (def_dec_info)); | |
1558 | ||
1559 | #ifndef UNPROTOIZE | |
1560 | def_dec_p->written = 0; | |
a019653e | 1561 | #endif /* !defined (UNPROTOIZE) */ |
5f8037c4 RS |
1562 | |
1563 | /* Start processing the line by picking off 5 pieces of information from | |
1564 | the left hand end of the line. These are filename, line number, | |
1565 | new/old/implicit flag (new = ANSI prototype format), definition or | |
1566 | declaration flag, and extern/static flag). */ | |
1567 | ||
1568 | check_aux_info (l[0] == '/'); | |
1569 | check_aux_info (l[1] == '*'); | |
1570 | check_aux_info (l[2] == ' '); | |
1571 | ||
1572 | { | |
1573 | const char *filename_start = p = l + 3; | |
1574 | char *filename; | |
1575 | ||
ee77eda5 MK |
1576 | while (*p != ':' |
1577 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM | |
1578 | || (*p == ':' && *p && *(p+1) && IS_DIR_SEPARATOR (*(p+1))) | |
1579 | #endif | |
1580 | ) | |
5f8037c4 RS |
1581 | p++; |
1582 | filename = (char *) alloca ((size_t) (p - filename_start) + 1); | |
1583 | strncpy (filename, filename_start, (size_t) (p - filename_start)); | |
1584 | filename[p-filename_start] = '\0'; | |
1585 | ||
1586 | /* Call find_file to find the file_info record associated with the file | |
1587 | which contained this particular def or dec item. Note that this call | |
1588 | may cause a new file_info record to be created if this is the first time | |
1589 | that we have ever known about this particular file. | |
1590 | ||
a2b22788 | 1591 | Note that we started out by forcing all of the base source file names |
5f8037c4 | 1592 | (i.e. the names of the aux_info files with the .X stripped off) into the |
a2b22788 RS |
1593 | filenames hash table, and we simultaneously setup file_info records for |
1594 | all of these base file names (even if they may be useless later). | |
1595 | The file_info records for all of these "base" file names (properly) | |
5f8037c4 | 1596 | act as file_info records for the "original" (i.e. un-included) files |
bd0725f3 | 1597 | which were submitted to gcc for compilation (when the -aux-info |
5f8037c4 RS |
1598 | option was used). */ |
1599 | ||
a2b22788 | 1600 | def_dec_p->file = find_file (abspath (invocation_filename, filename), is_syscalls); |
5f8037c4 RS |
1601 | } |
1602 | ||
1603 | { | |
1604 | const char *line_number_start = ++p; | |
1605 | char line_number[10]; | |
1606 | ||
ee77eda5 MK |
1607 | while (*p != ':' |
1608 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM | |
1609 | || (*p == ':' && *p && *(p+1) && IS_DIR_SEPARATOR (*(p+1))) | |
1610 | #endif | |
1611 | ) | |
5f8037c4 RS |
1612 | p++; |
1613 | strncpy (line_number, line_number_start, (size_t) (p - line_number_start)); | |
1614 | line_number[p-line_number_start] = '\0'; | |
1615 | def_dec_p->line = atoi (line_number); | |
1616 | } | |
1617 | ||
1618 | /* Check that this record describes a new-style, old-style, or implicit | |
1619 | definition or declaration. */ | |
1620 | ||
0f41302f | 1621 | p++; /* Skip over the `:'. */ |
5f8037c4 RS |
1622 | check_aux_info ((*p == 'N') || (*p == 'O') || (*p == 'I')); |
1623 | ||
1624 | /* Is this a new style (ANSI prototyped) definition or declaration? */ | |
1625 | ||
1626 | def_dec_p->prototyped = (*p == 'N'); | |
1627 | ||
1628 | #ifndef UNPROTOIZE | |
1629 | ||
1630 | /* Is this an implicit declaration? */ | |
1631 | ||
1632 | def_dec_p->is_implicit = (*p == 'I'); | |
1633 | ||
a019653e | 1634 | #endif /* !defined (UNPROTOIZE) */ |
5f8037c4 RS |
1635 | |
1636 | p++; | |
1637 | ||
1638 | check_aux_info ((*p == 'C') || (*p == 'F')); | |
1639 | ||
1640 | /* Is this item a function definition (F) or a declaration (C). Note that | |
1641 | we treat item taken from the syscalls file as though they were function | |
1642 | definitions regardless of what the stuff in the file says. */ | |
1643 | ||
1644 | def_dec_p->is_func_def = ((*p++ == 'F') || is_syscalls); | |
1645 | ||
1646 | #ifndef UNPROTOIZE | |
1647 | def_dec_p->definition = 0; /* Fill this in later if protoizing. */ | |
a019653e | 1648 | #endif /* !defined (UNPROTOIZE) */ |
5f8037c4 RS |
1649 | |
1650 | check_aux_info (*p++ == ' '); | |
1651 | check_aux_info (*p++ == '*'); | |
1652 | check_aux_info (*p++ == '/'); | |
1653 | check_aux_info (*p++ == ' '); | |
1654 | ||
1655 | #ifdef UNPROTOIZE | |
1656 | check_aux_info ((!strncmp (p, "static", 6)) || (!strncmp (p, "extern", 6))); | |
a019653e | 1657 | #else /* !defined (UNPROTOIZE) */ |
5f8037c4 RS |
1658 | if (!strncmp (p, "static", 6)) |
1659 | def_dec_p->is_static = -1; | |
1660 | else if (!strncmp (p, "extern", 6)) | |
1661 | def_dec_p->is_static = 0; | |
1662 | else | |
1663 | check_aux_info (0); /* Didn't find either `extern' or `static'. */ | |
a019653e | 1664 | #endif /* !defined (UNPROTOIZE) */ |
5f8037c4 RS |
1665 | |
1666 | { | |
1667 | const char *ansi_start = p; | |
1668 | ||
1669 | p += 6; /* Pass over the "static" or "extern". */ | |
1670 | ||
1671 | /* We are now past the initial stuff. Search forward from here to find | |
1672 | the terminating semicolon that should immediately follow the entire | |
1673 | ANSI format function declaration. */ | |
1674 | ||
1675 | while (*++p != ';') | |
1676 | continue; | |
1677 | ||
1678 | semicolon_p = p; | |
1679 | ||
1680 | /* Make a copy of the ansi declaration part of the line from the aux_info | |
1681 | file. */ | |
1682 | ||
1683 | def_dec_p->ansi_decl | |
1684 | = dupnstr (ansi_start, (size_t) ((semicolon_p+1) - ansi_start)); | |
5f8037c4 | 1685 | |
0ef8d762 RS |
1686 | /* Backup and point at the final right paren of the final argument list. */ |
1687 | ||
1688 | p--; | |
5f8037c4 | 1689 | |
535e7983 RS |
1690 | #ifndef UNPROTOIZE |
1691 | def_dec_p->f_list_chain = NULL; | |
1692 | #endif /* !defined (UNPROTOIZE) */ | |
1693 | ||
0ef8d762 | 1694 | while (p != ansi_start && (p[-1] == ' ' || p[-1] == '\t')) p--; |
535e7983 | 1695 | if (*p != ')') |
0ef8d762 RS |
1696 | { |
1697 | free_def_dec (def_dec_p); | |
1698 | return; | |
1699 | } | |
1700 | } | |
5f8037c4 RS |
1701 | |
1702 | /* Now isolate a whole set of formal argument lists, one-by-one. Normally, | |
1703 | there will only be one list to isolate, but there could be more. */ | |
1704 | ||
1705 | def_dec_p->f_list_count = 0; | |
1706 | ||
5f8037c4 RS |
1707 | for (;;) |
1708 | { | |
1709 | const char *left_paren_p = find_corresponding_lparen (p); | |
1710 | #ifndef UNPROTOIZE | |
1711 | { | |
db3cf6fb MS |
1712 | f_list_chain_item *cip |
1713 | = (f_list_chain_item *) xmalloc (sizeof (f_list_chain_item)); | |
5f8037c4 RS |
1714 | |
1715 | cip->formals_list | |
1716 | = dupnstr (left_paren_p + 1, (size_t) (p - (left_paren_p+1))); | |
1717 | ||
1718 | /* Add the new chain item at the head of the current list. */ | |
1719 | ||
1720 | cip->chain_next = def_dec_p->f_list_chain; | |
1721 | def_dec_p->f_list_chain = cip; | |
1722 | } | |
a019653e | 1723 | #endif /* !defined (UNPROTOIZE) */ |
5f8037c4 RS |
1724 | def_dec_p->f_list_count++; |
1725 | ||
1726 | p = left_paren_p - 2; | |
1727 | ||
1728 | /* p must now point either to another right paren, or to the last | |
1729 | character of the name of the function that was declared/defined. | |
1730 | If p points to another right paren, then this indicates that we | |
1731 | are dealing with multiple formals lists. In that case, there | |
d45cf215 | 1732 | really should be another right paren preceding this right paren. */ |
5f8037c4 RS |
1733 | |
1734 | if (*p != ')') | |
1735 | break; | |
1736 | else | |
1737 | check_aux_info (*--p == ')'); | |
1738 | } | |
1739 | ||
1740 | ||
1741 | { | |
1742 | const char *past_fn = p + 1; | |
1743 | ||
1744 | check_aux_info (*past_fn == ' '); | |
1745 | ||
1746 | /* Scan leftwards over the identifier that names the function. */ | |
1747 | ||
1748 | while (is_id_char (*p)) | |
1749 | p--; | |
1750 | p++; | |
1751 | ||
1752 | /* p now points to the leftmost character of the function name. */ | |
1753 | ||
1754 | { | |
34e56753 | 1755 | char *fn_string = (char *) alloca (past_fn - p + 1); |
5f8037c4 RS |
1756 | |
1757 | strncpy (fn_string, p, (size_t) (past_fn - p)); | |
1758 | fn_string[past_fn-p] = '\0'; | |
1759 | def_dec_p->hash_entry = lookup (function_name_primary, fn_string); | |
1760 | } | |
1761 | } | |
1762 | ||
1763 | /* Look at all of the defs and decs for this function name that we have | |
1764 | collected so far. If there is already one which is at the same | |
1765 | line number in the same file, then we can discard this new def_dec_info | |
1766 | record. | |
1767 | ||
1768 | As an extra assurance that any such pair of (nominally) identical | |
1769 | function declarations are in fact identical, we also compare the | |
1770 | ansi_decl parts of the lines from the aux_info files just to be on | |
1771 | the safe side. | |
1772 | ||
1773 | This comparison will fail if (for instance) the user was playing | |
1774 | messy games with the preprocessor which ultimately causes one | |
1775 | function declaration in one header file to look differently when | |
1776 | that file is included by two (or more) other files. */ | |
1777 | ||
1778 | { | |
1779 | const def_dec_info *other; | |
1780 | ||
1781 | for (other = def_dec_p->hash_entry->ddip; other; other = other->next_for_func) | |
1782 | { | |
1783 | if (def_dec_p->line == other->line && def_dec_p->file == other->file) | |
1784 | { | |
1785 | if (strcmp (def_dec_p->ansi_decl, other->ansi_decl)) | |
1786 | { | |
ab87f8c8 JL |
1787 | notice ("%s:%d: declaration of function `%s' takes different forms\n", |
1788 | def_dec_p->file->hash_entry->symbol, | |
1789 | def_dec_p->line, | |
1790 | def_dec_p->hash_entry->symbol); | |
2e3f9f3d | 1791 | exit (FATAL_EXIT_CODE); |
5f8037c4 RS |
1792 | } |
1793 | free_def_dec (def_dec_p); | |
1794 | return; | |
1795 | } | |
1796 | } | |
1797 | } | |
1798 | ||
1799 | #ifdef UNPROTOIZE | |
1800 | ||
1801 | /* If we are doing unprotoizing, we must now setup the pointers that will | |
1802 | point to the K&R name list and to the K&R argument declarations list. | |
1803 | ||
1804 | Note that if this is only a function declaration, then we should not | |
1805 | expect to find any K&R style formals list following the ANSI-style | |
1806 | formals list. This is because GCC knows that such information is | |
1807 | useless in the case of function declarations (function definitions | |
1808 | are a different story however). | |
1809 | ||
1810 | Since we are unprotoizing, we don't need any such lists anyway. | |
1811 | All we plan to do is to delete all characters between ()'s in any | |
1812 | case. */ | |
1813 | ||
1814 | def_dec_p->formal_names = NULL; | |
1815 | def_dec_p->formal_decls = NULL; | |
1816 | ||
1817 | if (def_dec_p->is_func_def) | |
1818 | { | |
1819 | p = semicolon_p; | |
1820 | check_aux_info (*++p == ' '); | |
1821 | check_aux_info (*++p == '/'); | |
1822 | check_aux_info (*++p == '*'); | |
1823 | check_aux_info (*++p == ' '); | |
1824 | check_aux_info (*++p == '('); | |
1825 | ||
1826 | { | |
0f41302f | 1827 | const char *kr_names_start = ++p; /* Point just inside '('. */ |
5f8037c4 RS |
1828 | |
1829 | while (*p++ != ')') | |
1830 | continue; | |
1831 | p--; /* point to closing right paren */ | |
1832 | ||
1833 | /* Make a copy of the K&R parameter names list. */ | |
1834 | ||
1835 | def_dec_p->formal_names | |
1836 | = dupnstr (kr_names_start, (size_t) (p - kr_names_start)); | |
1837 | } | |
1838 | ||
1839 | check_aux_info (*++p == ' '); | |
1840 | p++; | |
1841 | ||
1842 | /* p now points to the first character of the K&R style declarations | |
1843 | list (if there is one) or to the star-slash combination that ends | |
1844 | the comment in which such lists get embedded. */ | |
1845 | ||
1846 | /* Make a copy of the K&R formal decls list and set the def_dec record | |
1847 | to point to it. */ | |
1848 | ||
1849 | if (*p == '*') /* Are there no K&R declarations? */ | |
1850 | { | |
1851 | check_aux_info (*++p == '/'); | |
1852 | def_dec_p->formal_decls = ""; | |
1853 | } | |
1854 | else | |
1855 | { | |
1856 | const char *kr_decls_start = p; | |
1857 | ||
1858 | while (p[0] != '*' || p[1] != '/') | |
1859 | p++; | |
1860 | p--; | |
1861 | ||
1862 | check_aux_info (*p == ' '); | |
1863 | ||
1864 | def_dec_p->formal_decls | |
1865 | = dupnstr (kr_decls_start, (size_t) (p - kr_decls_start)); | |
1866 | } | |
1867 | ||
1868 | /* Handle a special case. If we have a function definition marked as | |
9ec36da5 | 1869 | being in "old" style, and if its formal names list is empty, then |
5f8037c4 RS |
1870 | it may actually have the string "void" in its real formals list |
1871 | in the original source code. Just to make sure, we will get setup | |
1872 | to convert such things anyway. | |
1873 | ||
1874 | This kludge only needs to be here because of an insurmountable | |
1875 | problem with generating .X files. */ | |
1876 | ||
1877 | if (!def_dec_p->prototyped && !*def_dec_p->formal_names) | |
1878 | def_dec_p->prototyped = 1; | |
1879 | } | |
1880 | ||
1881 | /* Since we are unprotoizing, if this item is already in old (K&R) style, | |
1882 | we can just ignore it. If that is true, throw away the itme now. */ | |
1883 | ||
1884 | if (!def_dec_p->prototyped) | |
1885 | { | |
1886 | free_def_dec (def_dec_p); | |
1887 | return; | |
1888 | } | |
1889 | ||
a019653e | 1890 | #endif /* defined (UNPROTOIZE) */ |
5f8037c4 RS |
1891 | |
1892 | /* Add this record to the head of the list of records pertaining to this | |
1893 | particular function name. */ | |
1894 | ||
1895 | def_dec_p->next_for_func = def_dec_p->hash_entry->ddip; | |
1896 | def_dec_p->hash_entry->ddip = def_dec_p; | |
1897 | ||
1898 | /* Add this new def_dec_info record to the sorted list of def_dec_info | |
1899 | records for this file. Note that we don't have to worry about duplicates | |
1900 | (caused by multiple inclusions of header files) here because we have | |
1901 | already eliminated duplicates above. */ | |
1902 | ||
1903 | if (!def_dec_p->file->defs_decs) | |
1904 | { | |
1905 | def_dec_p->file->defs_decs = def_dec_p; | |
1906 | def_dec_p->next_in_file = NULL; | |
1907 | } | |
1908 | else | |
1909 | { | |
1910 | int line = def_dec_p->line; | |
1911 | const def_dec_info *prev = NULL; | |
1912 | const def_dec_info *curr = def_dec_p->file->defs_decs; | |
1913 | const def_dec_info *next = curr->next_in_file; | |
1914 | ||
1915 | while (next && (line < curr->line)) | |
1916 | { | |
1917 | prev = curr; | |
1918 | curr = next; | |
1919 | next = next->next_in_file; | |
1920 | } | |
1921 | if (line >= curr->line) | |
1922 | { | |
1923 | def_dec_p->next_in_file = curr; | |
1924 | if (prev) | |
1925 | ((NONCONST def_dec_info *) prev)->next_in_file = def_dec_p; | |
1926 | else | |
1927 | def_dec_p->file->defs_decs = def_dec_p; | |
1928 | } | |
1929 | else /* assert (next == NULL); */ | |
1930 | { | |
1931 | ((NONCONST def_dec_info *) curr)->next_in_file = def_dec_p; | |
1932 | /* assert (next == NULL); */ | |
1933 | def_dec_p->next_in_file = next; | |
1934 | } | |
1935 | } | |
1936 | } | |
1937 | \f | |
bd0725f3 RS |
1938 | /* Set up the vector COMPILE_PARAMS which is the argument list for running GCC. |
1939 | Also set input_file_name_index and aux_info_file_name_index | |
1940 | to the indices of the slots where the file names should go. */ | |
1941 | ||
1942 | /* We initialize the vector by removing -g, -O, -S, -c, and -o options, | |
1943 | and adding '-aux-info AUXFILE -S -o /dev/null INFILE' at the end. */ | |
5f8037c4 RS |
1944 | |
1945 | static void | |
34e56753 RS |
1946 | munge_compile_params (params_list) |
1947 | const char *params_list; | |
5f8037c4 | 1948 | { |
bd0725f3 RS |
1949 | /* Build up the contents in a temporary vector |
1950 | that is so big that to has to be big enough. */ | |
8241a41f | 1951 | const char **temp_params |
6f4870cf | 1952 | = (const char **) alloca ((strlen (params_list) + 8) * sizeof (char *)); |
5f8037c4 RS |
1953 | int param_count = 0; |
1954 | const char *param; | |
49009afd | 1955 | struct stat st; |
5f8037c4 | 1956 | |
a019653e | 1957 | temp_params[param_count++] = compiler_file_name; |
5f8037c4 RS |
1958 | for (;;) |
1959 | { | |
e51712db | 1960 | while (ISSPACE ((const unsigned char)*params_list)) |
5f8037c4 RS |
1961 | params_list++; |
1962 | if (!*params_list) | |
1963 | break; | |
1964 | param = params_list; | |
e51712db | 1965 | while (*params_list && !ISSPACE ((const unsigned char)*params_list)) |
5f8037c4 RS |
1966 | params_list++; |
1967 | if (param[0] != '-') | |
1968 | temp_params[param_count++] | |
1969 | = dupnstr (param, (size_t) (params_list - param)); | |
1970 | else | |
1971 | { | |
1972 | switch (param[1]) | |
1973 | { | |
1974 | case 'g': | |
1975 | case 'O': | |
1976 | case 'S': | |
1977 | case 'c': | |
1978 | break; /* Don't copy these. */ | |
1979 | case 'o': | |
e51712db | 1980 | while (ISSPACE ((const unsigned char)*params_list)) |
5f8037c4 | 1981 | params_list++; |
e51712db KG |
1982 | while (*params_list |
1983 | && !ISSPACE ((const unsigned char)*params_list)) | |
5f8037c4 RS |
1984 | params_list++; |
1985 | break; | |
1986 | default: | |
1987 | temp_params[param_count++] | |
1988 | = dupnstr (param, (size_t) (params_list - param)); | |
1989 | } | |
1990 | } | |
1991 | if (!*params_list) | |
1992 | break; | |
1993 | } | |
bd0725f3 RS |
1994 | temp_params[param_count++] = "-aux-info"; |
1995 | ||
1996 | /* Leave room for the aux-info file name argument. */ | |
1997 | aux_info_file_name_index = param_count; | |
1998 | temp_params[param_count++] = NULL; | |
1999 | ||
5f8037c4 RS |
2000 | temp_params[param_count++] = "-S"; |
2001 | temp_params[param_count++] = "-o"; | |
49009afd | 2002 | |
7db9125f JL |
2003 | if ((stat (HOST_BIT_BUCKET, &st) == 0) |
2004 | && (!S_ISDIR (st.st_mode)) | |
2005 | && (access (HOST_BIT_BUCKET, W_OK) == 0)) | |
49009afd JL |
2006 | temp_params[param_count++] = HOST_BIT_BUCKET; |
2007 | else | |
2008 | /* FIXME: This is hardly likely to be right, if HOST_BIT_BUCKET is not | |
2009 | writable. But until this is rejigged to use make_temp_file(), this | |
2010 | is the best we can do. */ | |
2011 | temp_params[param_count++] = "/dev/null"; | |
5f8037c4 | 2012 | |
bd0725f3 RS |
2013 | /* Leave room for the input file name argument. */ |
2014 | input_file_name_index = param_count; | |
2015 | temp_params[param_count++] = NULL; | |
2016 | /* Terminate the list. */ | |
5f8037c4 RS |
2017 | temp_params[param_count++] = NULL; |
2018 | ||
2019 | /* Make a copy of the compile_params in heap space. */ | |
2020 | ||
34e56753 | 2021 | compile_params |
ff57c94e | 2022 | = (const char **) xmalloc (sizeof (char *) * (param_count+1)); |
5f8037c4 RS |
2023 | memcpy (compile_params, temp_params, sizeof (char *) * param_count); |
2024 | } | |
2025 | ||
2026 | /* Do a recompilation for the express purpose of generating a new aux_info | |
b0e87872 DE |
2027 | file to go with a specific base source file. |
2028 | ||
2029 | The result is a boolean indicating success. */ | |
5f8037c4 RS |
2030 | |
2031 | static int | |
34e56753 RS |
2032 | gen_aux_info_file (base_filename) |
2033 | const char *base_filename; | |
5f8037c4 | 2034 | { |
bd0725f3 | 2035 | if (!input_file_name_index) |
5f8037c4 RS |
2036 | munge_compile_params (""); |
2037 | ||
bd0725f3 RS |
2038 | /* Store the full source file name in the argument vector. */ |
2039 | compile_params[input_file_name_index] = shortpath (NULL, base_filename); | |
2040 | /* Add .X to source file name to get aux-info file name. */ | |
ad85216e | 2041 | compile_params[aux_info_file_name_index] = |
a7db8bbb | 2042 | concat (compile_params[input_file_name_index], aux_info_suffix, NULL); |
ad85216e | 2043 | |
5f8037c4 | 2044 | if (!quiet_flag) |
ab87f8c8 JL |
2045 | notice ("%s: compiling `%s'\n", |
2046 | pname, compile_params[input_file_name_index]); | |
5f8037c4 | 2047 | |
b0e87872 DE |
2048 | { |
2049 | char *errmsg_fmt, *errmsg_arg; | |
2050 | int wait_status, pid; | |
5f8037c4 | 2051 | |
b0e87872 | 2052 | pid = pexecute (compile_params[0], (char * const *) compile_params, |
34640c87 | 2053 | pname, NULL, &errmsg_fmt, &errmsg_arg, |
b0e87872 | 2054 | PEXECUTE_FIRST | PEXECUTE_LAST | PEXECUTE_SEARCH); |
5f8037c4 | 2055 | |
b0e87872 | 2056 | if (pid == -1) |
5f8037c4 | 2057 | { |
b0e87872 DE |
2058 | int errno_val = errno; |
2059 | fprintf (stderr, "%s: ", pname); | |
2060 | fprintf (stderr, errmsg_fmt, errmsg_arg); | |
ed35cf6e | 2061 | fprintf (stderr, ": %s\n", xstrerror (errno_val)); |
b0e87872 DE |
2062 | return 0; |
2063 | } | |
5f8037c4 | 2064 | |
b0e87872 DE |
2065 | pid = pwait (pid, &wait_status, 0); |
2066 | if (pid == -1) | |
2067 | { | |
ab87f8c8 | 2068 | notice ("%s: wait: %s\n", pname, xstrerror (errno)); |
b0e87872 DE |
2069 | return 0; |
2070 | } | |
2071 | if (WIFSIGNALED (wait_status)) | |
2072 | { | |
ab87f8c8 JL |
2073 | notice ("%s: subprocess got fatal signal %d\n", |
2074 | pname, WTERMSIG (wait_status)); | |
b0e87872 DE |
2075 | return 0; |
2076 | } | |
2077 | if (WIFEXITED (wait_status)) | |
2078 | { | |
2079 | if (WEXITSTATUS (wait_status) != 0) | |
d742f26c | 2080 | { |
ab87f8c8 JL |
2081 | notice ("%s: %s exited with status %d\n", |
2082 | pname, compile_params[0], WEXITSTATUS (wait_status)); | |
d742f26c RS |
2083 | return 0; |
2084 | } | |
2085 | return 1; | |
5f8037c4 | 2086 | } |
b0e87872 DE |
2087 | abort (); |
2088 | } | |
5f8037c4 RS |
2089 | } |
2090 | \f | |
2091 | /* Read in all of the information contained in a single aux_info file. | |
2092 | Save all of the important stuff for later. */ | |
2093 | ||
2094 | static void | |
34e56753 RS |
2095 | process_aux_info_file (base_source_filename, keep_it, is_syscalls) |
2096 | const char *base_source_filename; | |
2097 | int keep_it; | |
2098 | int is_syscalls; | |
5f8037c4 | 2099 | { |
d742f26c RS |
2100 | size_t base_len = strlen (base_source_filename); |
2101 | char * aux_info_filename | |
2102 | = (char *) alloca (base_len + strlen (aux_info_suffix) + 1); | |
5f8037c4 RS |
2103 | char *aux_info_base; |
2104 | char *aux_info_limit; | |
d742f26c | 2105 | char *aux_info_relocated_name; |
5f8037c4 RS |
2106 | const char *aux_info_second_line; |
2107 | time_t aux_info_mtime; | |
2108 | size_t aux_info_size; | |
77f99bc7 | 2109 | int must_create; |
5f8037c4 | 2110 | |
a2b22788 | 2111 | /* Construct the aux_info filename from the base source filename. */ |
5f8037c4 | 2112 | |
a2b22788 RS |
2113 | strcpy (aux_info_filename, base_source_filename); |
2114 | strcat (aux_info_filename, aux_info_suffix); | |
5f8037c4 RS |
2115 | |
2116 | /* Check that the aux_info file exists and is readable. If it does not | |
2117 | exist, try to create it (once only). */ | |
2118 | ||
77f99bc7 RS |
2119 | /* If file doesn't exist, set must_create. |
2120 | Likewise if it exists and we can read it but it is obsolete. | |
2121 | Otherwise, report an error. */ | |
2122 | must_create = 0; | |
2cccceff RS |
2123 | |
2124 | /* Come here with must_create set to 1 if file is out of date. */ | |
2125 | start_over: ; | |
2126 | ||
ffb9f2f1 | 2127 | if (access (aux_info_filename, R_OK) == -1) |
77f99bc7 RS |
2128 | { |
2129 | if (errno == ENOENT) | |
2130 | { | |
2131 | if (is_syscalls) | |
2132 | { | |
ab87f8c8 JL |
2133 | notice ("%s: warning: missing SYSCALLS file `%s'\n", |
2134 | pname, aux_info_filename); | |
77f99bc7 RS |
2135 | return; |
2136 | } | |
2137 | must_create = 1; | |
2138 | } | |
2139 | else | |
2140 | { | |
e5e809f4 | 2141 | int errno_val = errno; |
ab87f8c8 JL |
2142 | notice ("%s: can't read aux info file `%s': %s\n", |
2143 | pname, shortpath (NULL, aux_info_filename), | |
2144 | xstrerror (errno_val)); | |
77f99bc7 RS |
2145 | errors++; |
2146 | return; | |
2147 | } | |
2148 | } | |
2149 | #if 0 /* There is code farther down to take care of this. */ | |
2150 | else | |
2151 | { | |
2152 | struct stat s1, s2; | |
2153 | stat (aux_info_file_name, &s1); | |
2154 | stat (base_source_file_name, &s2); | |
2155 | if (s2.st_mtime > s1.st_mtime) | |
2156 | must_create = 1; | |
2157 | } | |
2158 | #endif /* 0 */ | |
5f8037c4 | 2159 | |
77f99bc7 RS |
2160 | /* If we need a .X file, create it, and verify we can read it. */ |
2161 | if (must_create) | |
2162 | { | |
2163 | if (!gen_aux_info_file (base_source_filename)) | |
2164 | { | |
2165 | errors++; | |
2166 | return; | |
2167 | } | |
ffb9f2f1 | 2168 | if (access (aux_info_filename, R_OK) == -1) |
77f99bc7 | 2169 | { |
e5e809f4 | 2170 | int errno_val = errno; |
ab87f8c8 JL |
2171 | notice ("%s: can't read aux info file `%s': %s\n", |
2172 | pname, shortpath (NULL, aux_info_filename), | |
2173 | xstrerror (errno_val)); | |
77f99bc7 RS |
2174 | errors++; |
2175 | return; | |
2176 | } | |
2177 | } | |
5f8037c4 RS |
2178 | |
2179 | { | |
2180 | struct stat stat_buf; | |
2181 | ||
2182 | /* Get some status information about this aux_info file. */ | |
2183 | ||
ffb9f2f1 | 2184 | if (stat (aux_info_filename, &stat_buf) == -1) |
5f8037c4 | 2185 | { |
e5e809f4 | 2186 | int errno_val = errno; |
ab87f8c8 JL |
2187 | notice ("%s: can't get status of aux info file `%s': %s\n", |
2188 | pname, shortpath (NULL, aux_info_filename), | |
2189 | xstrerror (errno_val)); | |
5f8037c4 RS |
2190 | errors++; |
2191 | return; | |
2192 | } | |
2193 | ||
2194 | /* Check on whether or not this aux_info file is zero length. If it is, | |
2195 | then just ignore it and return. */ | |
2196 | ||
2197 | if ((aux_info_size = stat_buf.st_size) == 0) | |
2198 | return; | |
2199 | ||
2200 | /* Get the date/time of last modification for this aux_info file and | |
2201 | remember it. We will have to check that any source files that it | |
2202 | contains information about are at least this old or older. */ | |
2203 | ||
2204 | aux_info_mtime = stat_buf.st_mtime; | |
667cc897 | 2205 | |
2cccceff | 2206 | if (!is_syscalls) |
667cc897 | 2207 | { |
2cccceff RS |
2208 | /* Compare mod time with the .c file; update .X file if obsolete. |
2209 | The code later on can fail to check the .c file | |
2210 | if it did not directly define any functions. */ | |
2211 | ||
ffb9f2f1 | 2212 | if (stat (base_source_filename, &stat_buf) == -1) |
2cccceff | 2213 | { |
e5e809f4 | 2214 | int errno_val = errno; |
ab87f8c8 JL |
2215 | notice ("%s: can't get status of aux info file `%s': %s\n", |
2216 | pname, shortpath (NULL, base_source_filename), | |
2217 | xstrerror (errno_val)); | |
2cccceff RS |
2218 | errors++; |
2219 | return; | |
2220 | } | |
2221 | if (stat_buf.st_mtime > aux_info_mtime) | |
2222 | { | |
2223 | must_create = 1; | |
2224 | goto start_over; | |
2225 | } | |
667cc897 | 2226 | } |
5f8037c4 RS |
2227 | } |
2228 | ||
2229 | { | |
2230 | int aux_info_file; | |
ee77eda5 | 2231 | int fd_flags; |
5f8037c4 RS |
2232 | |
2233 | /* Open the aux_info file. */ | |
2234 | ||
ee77eda5 MK |
2235 | fd_flags = O_RDONLY; |
2236 | #ifdef O_BINARY | |
2237 | /* Use binary mode to avoid having to deal with different EOL characters. */ | |
2238 | fd_flags |= O_BINARY; | |
2239 | #endif | |
2240 | if ((aux_info_file = open (aux_info_filename, fd_flags, 0444 )) == -1) | |
5f8037c4 | 2241 | { |
e5e809f4 | 2242 | int errno_val = errno; |
ab87f8c8 JL |
2243 | notice ("%s: can't open aux info file `%s' for reading: %s\n", |
2244 | pname, shortpath (NULL, aux_info_filename), | |
2245 | xstrerror (errno_val)); | |
5f8037c4 RS |
2246 | return; |
2247 | } | |
2248 | ||
2249 | /* Allocate space to hold the aux_info file in memory. */ | |
2250 | ||
2251 | aux_info_base = xmalloc (aux_info_size + 1); | |
2252 | aux_info_limit = aux_info_base + aux_info_size; | |
2253 | *aux_info_limit = '\0'; | |
2254 | ||
2255 | /* Read the aux_info file into memory. */ | |
2256 | ||
e51712db KG |
2257 | if (safe_read (aux_info_file, aux_info_base, aux_info_size) != |
2258 | (int) aux_info_size) | |
5f8037c4 | 2259 | { |
e5e809f4 | 2260 | int errno_val = errno; |
ab87f8c8 JL |
2261 | notice ("%s: error reading aux info file `%s': %s\n", |
2262 | pname, shortpath (NULL, aux_info_filename), | |
2263 | xstrerror (errno_val)); | |
5f8037c4 RS |
2264 | free (aux_info_base); |
2265 | close (aux_info_file); | |
2266 | return; | |
2267 | } | |
2268 | ||
2269 | /* Close the aux info file. */ | |
2270 | ||
2271 | if (close (aux_info_file)) | |
2272 | { | |
e5e809f4 | 2273 | int errno_val = errno; |
ab87f8c8 JL |
2274 | notice ("%s: error closing aux info file `%s': %s\n", |
2275 | pname, shortpath (NULL, aux_info_filename), | |
2276 | xstrerror (errno_val)); | |
5f8037c4 RS |
2277 | free (aux_info_base); |
2278 | close (aux_info_file); | |
2279 | return; | |
2280 | } | |
2281 | } | |
2282 | ||
2283 | /* Delete the aux_info file (unless requested not to). If the deletion | |
2284 | fails for some reason, don't even worry about it. */ | |
2285 | ||
667cc897 | 2286 | if (must_create && !keep_it) |
ffb9f2f1 | 2287 | if (unlink (aux_info_filename) == -1) |
e5e809f4 JL |
2288 | { |
2289 | int errno_val = errno; | |
ab87f8c8 JL |
2290 | notice ("%s: can't delete aux info file `%s': %s\n", |
2291 | pname, shortpath (NULL, aux_info_filename), | |
2292 | xstrerror (errno_val)); | |
e5e809f4 | 2293 | } |
5f8037c4 RS |
2294 | |
2295 | /* Save a pointer into the first line of the aux_info file which | |
a2b22788 | 2296 | contains the filename of the directory from which the compiler |
5f8037c4 RS |
2297 | was invoked when the associated source file was compiled. |
2298 | This information is used later to help create complete | |
a2b22788 | 2299 | filenames out of the (potentially) relative filenames in |
5f8037c4 RS |
2300 | the aux_info file. */ |
2301 | ||
2302 | { | |
2303 | char *p = aux_info_base; | |
2304 | ||
ee77eda5 MK |
2305 | while (*p != ':' |
2306 | #ifdef HAVE_DOS_BASED_FILE_SYSTEM | |
2307 | || (*p == ':' && *p && *(p+1) && IS_DIR_SEPARATOR (*(p+1))) | |
2308 | #endif | |
2309 | ) | |
5f8037c4 RS |
2310 | p++; |
2311 | p++; | |
2312 | while (*p == ' ') | |
2313 | p++; | |
a2b22788 | 2314 | invocation_filename = p; /* Save a pointer to first byte of path. */ |
5f8037c4 RS |
2315 | while (*p != ' ') |
2316 | p++; | |
ee77eda5 | 2317 | *p++ = DIR_SEPARATOR; |
5f8037c4 RS |
2318 | *p++ = '\0'; |
2319 | while (*p++ != '\n') | |
2320 | continue; | |
2321 | aux_info_second_line = p; | |
d742f26c | 2322 | aux_info_relocated_name = 0; |
ee77eda5 | 2323 | if (! is_abspath (invocation_filename)) |
d742f26c RS |
2324 | { |
2325 | /* INVOCATION_FILENAME is relative; | |
2326 | append it to BASE_SOURCE_FILENAME's dir. */ | |
2327 | char *dir_end; | |
2328 | aux_info_relocated_name = xmalloc (base_len + (p-invocation_filename)); | |
2329 | strcpy (aux_info_relocated_name, base_source_filename); | |
ee77eda5 MK |
2330 | dir_end = strrchr (aux_info_relocated_name, DIR_SEPARATOR); |
2331 | #ifdef DIR_SEPARATOR_2 | |
2332 | { | |
2333 | char *slash; | |
2334 | ||
2335 | slash = strrchr (dir_end ? dir_end : aux_info_relocated_name, | |
2336 | DIR_SEPARATOR_2); | |
2337 | if (slash) | |
2338 | dir_end = slash; | |
2339 | } | |
2340 | #endif | |
d742f26c RS |
2341 | if (dir_end) |
2342 | dir_end++; | |
2343 | else | |
2344 | dir_end = aux_info_relocated_name; | |
2345 | strcpy (dir_end, invocation_filename); | |
2346 | invocation_filename = aux_info_relocated_name; | |
2347 | } | |
5f8037c4 RS |
2348 | } |
2349 | ||
2350 | ||
2351 | { | |
2352 | const char *aux_info_p; | |
2353 | ||
2354 | /* Do a pre-pass on the lines in the aux_info file, making sure that all | |
2355 | of the source files referenced in there are at least as old as this | |
2356 | aux_info file itself. If not, go back and regenerate the aux_info | |
2357 | file anew. Don't do any of this for the syscalls file. */ | |
2358 | ||
2359 | if (!is_syscalls) | |
2360 | { | |
2361 | current_aux_info_lineno = 2; | |
2362 | ||
2363 | for (aux_info_p = aux_info_second_line; *aux_info_p; ) | |
2364 | { | |
2365 | if (referenced_file_is_newer (aux_info_p, aux_info_mtime)) | |
2366 | { | |
2367 | free (aux_info_base); | |
ad85216e | 2368 | free (aux_info_relocated_name); |
ffb9f2f1 | 2369 | if (keep_it && unlink (aux_info_filename) == -1) |
5f8037c4 | 2370 | { |
e5e809f4 | 2371 | int errno_val = errno; |
ab87f8c8 JL |
2372 | notice ("%s: can't delete file `%s': %s\n", |
2373 | pname, shortpath (NULL, aux_info_filename), | |
2374 | xstrerror (errno_val)); | |
5f8037c4 RS |
2375 | return; |
2376 | } | |
2f397844 | 2377 | must_create = 1; |
5f8037c4 RS |
2378 | goto start_over; |
2379 | } | |
2380 | ||
2381 | /* Skip over the rest of this line to start of next line. */ | |
2382 | ||
2383 | while (*aux_info_p != '\n') | |
2384 | aux_info_p++; | |
2385 | aux_info_p++; | |
2386 | current_aux_info_lineno++; | |
2387 | } | |
2388 | } | |
2389 | ||
2390 | /* Now do the real pass on the aux_info lines. Save their information in | |
2391 | the in-core data base. */ | |
2392 | ||
2393 | current_aux_info_lineno = 2; | |
2394 | ||
2395 | for (aux_info_p = aux_info_second_line; *aux_info_p;) | |
2396 | { | |
2397 | char *unexpanded_line = unexpand_if_needed (aux_info_p); | |
2398 | ||
2399 | if (unexpanded_line) | |
2400 | { | |
2401 | save_def_or_dec (unexpanded_line, is_syscalls); | |
2402 | free (unexpanded_line); | |
2403 | } | |
2404 | else | |
2405 | save_def_or_dec (aux_info_p, is_syscalls); | |
2406 | ||
2407 | /* Skip over the rest of this line and get to start of next line. */ | |
2408 | ||
2409 | while (*aux_info_p != '\n') | |
2410 | aux_info_p++; | |
2411 | aux_info_p++; | |
2412 | current_aux_info_lineno++; | |
2413 | } | |
2414 | } | |
2415 | ||
2416 | free (aux_info_base); | |
ad85216e | 2417 | free (aux_info_relocated_name); |
5f8037c4 RS |
2418 | } |
2419 | \f | |
2420 | #ifndef UNPROTOIZE | |
2421 | ||
2422 | /* Check an individual filename for a .c suffix. If the filename has this | |
2423 | suffix, rename the file such that its suffix is changed to .C. This | |
2424 | function implements the -C option. */ | |
2425 | ||
2426 | static void | |
34e56753 RS |
2427 | rename_c_file (hp) |
2428 | const hash_table_entry *hp; | |
5f8037c4 | 2429 | { |
a2b22788 RS |
2430 | const char *filename = hp->symbol; |
2431 | int last_char_index = strlen (filename) - 1; | |
ee77eda5 MK |
2432 | char *const new_filename = (char *) alloca (strlen (filename) |
2433 | + strlen (cplus_suffix) + 1); | |
5f8037c4 RS |
2434 | |
2435 | /* Note that we don't care here if the given file was converted or not. It | |
2436 | is possible that the given file was *not* converted, simply because there | |
2437 | was nothing in it which actually required conversion. Even in this case, | |
2438 | we want to do the renaming. Note that we only rename files with the .c | |
ee77eda5 | 2439 | suffix (except for the syscalls file, which is left alone). */ |
5f8037c4 | 2440 | |
ee77eda5 MK |
2441 | if (filename[last_char_index] != 'c' || filename[last_char_index-1] != '.' |
2442 | || IS_SAME_PATH (syscalls_absolute_filename, filename)) | |
5f8037c4 RS |
2443 | return; |
2444 | ||
a2b22788 | 2445 | strcpy (new_filename, filename); |
ee77eda5 | 2446 | strcpy (&new_filename[last_char_index], cplus_suffix); |
5f8037c4 | 2447 | |
ee77eda5 | 2448 | if (rename (filename, new_filename) == -1) |
5f8037c4 | 2449 | { |
e5e809f4 | 2450 | int errno_val = errno; |
ee77eda5 | 2451 | notice ("%s: warning: can't rename file `%s' to `%s': %s\n", |
ab87f8c8 JL |
2452 | pname, shortpath (NULL, filename), |
2453 | shortpath (NULL, new_filename), xstrerror (errno_val)); | |
5f8037c4 RS |
2454 | errors++; |
2455 | return; | |
2456 | } | |
5f8037c4 RS |
2457 | } |
2458 | ||
a019653e | 2459 | #endif /* !defined (UNPROTOIZE) */ |
5f8037c4 RS |
2460 | \f |
2461 | /* Take the list of definitions and declarations attached to a particular | |
2462 | file_info node and reverse the order of the list. This should get the | |
2463 | list into an order such that the item with the lowest associated line | |
2464 | number is nearest the head of the list. When these lists are originally | |
2465 | built, they are in the opposite order. We want to traverse them in | |
2466 | normal line number order later (i.e. lowest to highest) so reverse the | |
2467 | order here. */ | |
2468 | ||
2469 | static void | |
34e56753 RS |
2470 | reverse_def_dec_list (hp) |
2471 | const hash_table_entry *hp; | |
5f8037c4 RS |
2472 | { |
2473 | file_info *file_p = hp->fip; | |
a88a8c0b | 2474 | def_dec_info *prev = NULL; |
c3583620 | 2475 | def_dec_info *current = (def_dec_info *)file_p->defs_decs; |
5f8037c4 | 2476 | |
c3583620 | 2477 | if (!current) |
5f8037c4 RS |
2478 | return; /* no list to reverse */ |
2479 | ||
2480 | prev = current; | |
c3583620 | 2481 | if (! (current = (def_dec_info *)current->next_in_file)) |
5f8037c4 RS |
2482 | return; /* can't reverse a single list element */ |
2483 | ||
a88a8c0b | 2484 | prev->next_in_file = NULL; |
5f8037c4 RS |
2485 | |
2486 | while (current) | |
2487 | { | |
c3583620 | 2488 | def_dec_info *next = (def_dec_info *)current->next_in_file; |
5f8037c4 | 2489 | |
a88a8c0b | 2490 | current->next_in_file = prev; |
5f8037c4 RS |
2491 | prev = current; |
2492 | current = next; | |
2493 | } | |
2494 | ||
2495 | file_p->defs_decs = prev; | |
2496 | } | |
2497 | ||
2498 | #ifndef UNPROTOIZE | |
2499 | ||
2500 | /* Find the (only?) extern definition for a particular function name, starting | |
2501 | from the head of the linked list of entries for the given name. If we | |
2502 | cannot find an extern definition for the given function name, issue a | |
2503 | warning and scrounge around for the next best thing, i.e. an extern | |
2504 | function declaration with a prototype attached to it. Note that we only | |
2505 | allow such substitutions for extern declarations and never for static | |
2506 | declarations. That's because the only reason we allow them at all is | |
2507 | to let un-prototyped function declarations for system-supplied library | |
2508 | functions get their prototypes from our own extra SYSCALLS.c.X file which | |
2509 | contains all of the correct prototypes for system functions. */ | |
2510 | ||
2511 | static const def_dec_info * | |
34e56753 RS |
2512 | find_extern_def (head, user) |
2513 | const def_dec_info *head; | |
2514 | const def_dec_info *user; | |
5f8037c4 RS |
2515 | { |
2516 | const def_dec_info *dd_p; | |
2517 | const def_dec_info *extern_def_p = NULL; | |
2518 | int conflict_noted = 0; | |
2519 | ||
2520 | /* Don't act too stupid here. Somebody may try to convert an entire system | |
2521 | in one swell fwoop (rather than one program at a time, as should be done) | |
2522 | and in that case, we may find that there are multiple extern definitions | |
2523 | of a given function name in the entire set of source files that we are | |
2524 | converting. If however one of these definitions resides in exactly the | |
2525 | same source file as the reference we are trying to satisfy then in that | |
2526 | case it would be stupid for us to fail to realize that this one definition | |
2527 | *must* be the precise one we are looking for. | |
2528 | ||
2529 | To make sure that we don't miss an opportunity to make this "same file" | |
2530 | leap of faith, we do a prescan of the list of records relating to the | |
2531 | given function name, and we look (on this first scan) *only* for a | |
2532 | definition of the function which is in the same file as the reference | |
2533 | we are currently trying to satisfy. */ | |
2534 | ||
2535 | for (dd_p = head; dd_p; dd_p = dd_p->next_for_func) | |
2536 | if (dd_p->is_func_def && !dd_p->is_static && dd_p->file == user->file) | |
2537 | return dd_p; | |
2538 | ||
2539 | /* Now, since we have not found a definition in the same file as the | |
2540 | reference, we scan the list again and consider all possibilities from | |
2541 | all files. Here we may get conflicts with the things listed in the | |
2542 | SYSCALLS.c.X file, but if that happens it only means that the source | |
2543 | code being converted contains its own definition of a function which | |
2544 | could have been supplied by libc.a. In such cases, we should avoid | |
2545 | issuing the normal warning, and defer to the definition given in the | |
2546 | user's own code. */ | |
2547 | ||
2548 | for (dd_p = head; dd_p; dd_p = dd_p->next_for_func) | |
2549 | if (dd_p->is_func_def && !dd_p->is_static) | |
2550 | { | |
2551 | if (!extern_def_p) /* Previous definition? */ | |
0f41302f | 2552 | extern_def_p = dd_p; /* Remember the first definition found. */ |
5f8037c4 RS |
2553 | else |
2554 | { | |
2555 | /* Ignore definition just found if it came from SYSCALLS.c.X. */ | |
2556 | ||
2557 | if (is_syscalls_file (dd_p->file)) | |
2558 | continue; | |
2559 | ||
2560 | /* Quietly replace the definition previously found with the one | |
2561 | just found if the previous one was from SYSCALLS.c.X. */ | |
2562 | ||
2563 | if (is_syscalls_file (extern_def_p->file)) | |
2564 | { | |
2565 | extern_def_p = dd_p; | |
2566 | continue; | |
2567 | } | |
2568 | ||
2569 | /* If we get here, then there is a conflict between two function | |
2570 | declarations for the same function, both of which came from the | |
2571 | user's own code. */ | |
2572 | ||
2573 | if (!conflict_noted) /* first time we noticed? */ | |
2574 | { | |
2575 | conflict_noted = 1; | |
ab87f8c8 JL |
2576 | notice ("%s: conflicting extern definitions of '%s'\n", |
2577 | pname, head->hash_entry->symbol); | |
5f8037c4 RS |
2578 | if (!quiet_flag) |
2579 | { | |
ab87f8c8 JL |
2580 | notice ("%s: declarations of '%s' will not be converted\n", |
2581 | pname, head->hash_entry->symbol); | |
2582 | notice ("%s: conflict list for '%s' follows:\n", | |
2583 | pname, head->hash_entry->symbol); | |
5f8037c4 | 2584 | fprintf (stderr, "%s: %s(%d): %s\n", |
a2b22788 RS |
2585 | pname, |
2586 | shortpath (NULL, extern_def_p->file->hash_entry->symbol), | |
2587 | extern_def_p->line, extern_def_p->ansi_decl); | |
5f8037c4 RS |
2588 | } |
2589 | } | |
2590 | if (!quiet_flag) | |
2591 | fprintf (stderr, "%s: %s(%d): %s\n", | |
a2b22788 RS |
2592 | pname, |
2593 | shortpath (NULL, dd_p->file->hash_entry->symbol), | |
2594 | dd_p->line, dd_p->ansi_decl); | |
5f8037c4 RS |
2595 | } |
2596 | } | |
2597 | ||
2598 | /* We want to err on the side of caution, so if we found multiple conflicting | |
2599 | definitions for the same function, treat this as being that same as if we | |
2600 | had found no definitions (i.e. return NULL). */ | |
2601 | ||
2602 | if (conflict_noted) | |
2603 | return NULL; | |
2604 | ||
2605 | if (!extern_def_p) | |
2606 | { | |
2607 | /* We have no definitions for this function so do the next best thing. | |
2608 | Search for an extern declaration already in prototype form. */ | |
2609 | ||
2610 | for (dd_p = head; dd_p; dd_p = dd_p->next_for_func) | |
2611 | if (!dd_p->is_func_def && !dd_p->is_static && dd_p->prototyped) | |
2612 | { | |
2613 | extern_def_p = dd_p; /* save a pointer to the definition */ | |
2614 | if (!quiet_flag) | |
ab87f8c8 JL |
2615 | notice ("%s: warning: using formals list from %s(%d) for function `%s'\n", |
2616 | pname, | |
2617 | shortpath (NULL, dd_p->file->hash_entry->symbol), | |
2618 | dd_p->line, dd_p->hash_entry->symbol); | |
5f8037c4 RS |
2619 | break; |
2620 | } | |
2621 | ||
2622 | /* Gripe about unprototyped function declarations that we found no | |
2623 | corresponding definition (or other source of prototype information) | |
2624 | for. | |
2625 | ||
2626 | Gripe even if the unprototyped declaration we are worried about | |
2627 | exists in a file in one of the "system" include directories. We | |
2628 | can gripe about these because we should have at least found a | |
2629 | corresponding (pseudo) definition in the SYSCALLS.c.X file. If we | |
2630 | didn't, then that means that the SYSCALLS.c.X file is missing some | |
2631 | needed prototypes for this particular system. That is worth telling | |
2632 | the user about! */ | |
2633 | ||
2634 | if (!extern_def_p) | |
2635 | { | |
2636 | const char *file = user->file->hash_entry->symbol; | |
2637 | ||
2638 | if (!quiet_flag) | |
2639 | if (in_system_include_dir (file)) | |
2640 | { | |
2641 | /* Why copy this string into `needed' at all? | |
2642 | Why not just use user->ansi_decl without copying? */ | |
34e56753 | 2643 | char *needed = (char *) alloca (strlen (user->ansi_decl) + 1); |
5f8037c4 RS |
2644 | char *p; |
2645 | ||
2646 | strcpy (needed, user->ansi_decl); | |
2647 | p = (NONCONST char *) substr (needed, user->hash_entry->symbol) | |
2648 | + strlen (user->hash_entry->symbol) + 2; | |
a609bfc6 RS |
2649 | /* Avoid having ??? in the string. */ |
2650 | *p++ = '?'; | |
2651 | *p++ = '?'; | |
2652 | *p++ = '?'; | |
2653 | strcpy (p, ");"); | |
5f8037c4 | 2654 | |
ab87f8c8 JL |
2655 | notice ("%s: %d: `%s' used but missing from SYSCALLS\n", |
2656 | shortpath (NULL, file), user->line, | |
2657 | needed+7); /* Don't print "extern " */ | |
5f8037c4 | 2658 | } |
8241a41f | 2659 | #if 0 |
5f8037c4 | 2660 | else |
ab87f8c8 JL |
2661 | notice ("%s: %d: warning: no extern definition for `%s'\n", |
2662 | shortpath (NULL, file), user->line, | |
2663 | user->hash_entry->symbol); | |
8241a41f | 2664 | #endif |
5f8037c4 RS |
2665 | } |
2666 | } | |
2667 | return extern_def_p; | |
2668 | } | |
2669 | \f | |
2670 | /* Find the (only?) static definition for a particular function name in a | |
2671 | given file. Here we get the function-name and the file info indirectly | |
0f41302f | 2672 | from the def_dec_info record pointer which is passed in. */ |
5f8037c4 RS |
2673 | |
2674 | static const def_dec_info * | |
34e56753 RS |
2675 | find_static_definition (user) |
2676 | const def_dec_info *user; | |
5f8037c4 RS |
2677 | { |
2678 | const def_dec_info *head = user->hash_entry->ddip; | |
2679 | const def_dec_info *dd_p; | |
2680 | int num_static_defs = 0; | |
2681 | const def_dec_info *static_def_p = NULL; | |
2682 | ||
2683 | for (dd_p = head; dd_p; dd_p = dd_p->next_for_func) | |
2684 | if (dd_p->is_func_def && dd_p->is_static && (dd_p->file == user->file)) | |
2685 | { | |
2686 | static_def_p = dd_p; /* save a pointer to the definition */ | |
2687 | num_static_defs++; | |
2688 | } | |
2689 | if (num_static_defs == 0) | |
2690 | { | |
2691 | if (!quiet_flag) | |
ab87f8c8 JL |
2692 | notice ("%s: warning: no static definition for `%s' in file `%s'\n", |
2693 | pname, head->hash_entry->symbol, | |
2694 | shortpath (NULL, user->file->hash_entry->symbol)); | |
5f8037c4 RS |
2695 | } |
2696 | else if (num_static_defs > 1) | |
2697 | { | |
ab87f8c8 JL |
2698 | notice ("%s: multiple static defs of `%s' in file `%s'\n", |
2699 | pname, head->hash_entry->symbol, | |
2700 | shortpath (NULL, user->file->hash_entry->symbol)); | |
5f8037c4 RS |
2701 | return NULL; |
2702 | } | |
2703 | return static_def_p; | |
2704 | } | |
2705 | ||
2706 | /* Find good prototype style formal argument lists for all of the function | |
2707 | declarations which didn't have them before now. | |
2708 | ||
2709 | To do this we consider each function name one at a time. For each function | |
2710 | name, we look at the items on the linked list of def_dec_info records for | |
2711 | that particular name. | |
2712 | ||
2713 | Somewhere on this list we should find one (and only one) def_dec_info | |
2714 | record which represents the actual function definition, and this record | |
2715 | should have a nice formal argument list already associated with it. | |
2716 | ||
2717 | Thus, all we have to do is to connect up all of the other def_dec_info | |
2718 | records for this particular function name to the special one which has | |
2719 | the full-blown formals list. | |
2720 | ||
2721 | Of course it is a little more complicated than just that. See below for | |
2722 | more details. */ | |
2723 | ||
2724 | static void | |
34e56753 RS |
2725 | connect_defs_and_decs (hp) |
2726 | const hash_table_entry *hp; | |
5f8037c4 RS |
2727 | { |
2728 | const def_dec_info *dd_p; | |
2729 | const def_dec_info *extern_def_p = NULL; | |
2730 | int first_extern_reference = 1; | |
2731 | ||
2732 | /* Traverse the list of definitions and declarations for this particular | |
2733 | function name. For each item on the list, if it is a function | |
2734 | definition (either old style or new style) then GCC has already been | |
2735 | kind enough to produce a prototype for us, and it is associated with | |
2736 | the item already, so declare the item as its own associated "definition". | |
2737 | ||
2738 | Also, for each item which is only a function declaration, but which | |
2739 | nonetheless has its own prototype already (obviously supplied by the user) | |
9ec36da5 | 2740 | declare the item as its own definition. |
5f8037c4 RS |
2741 | |
2742 | Note that when/if there are multiple user-supplied prototypes already | |
2743 | present for multiple declarations of any given function, these multiple | |
2744 | prototypes *should* all match exactly with one another and with the | |
2745 | prototype for the actual function definition. We don't check for this | |
2746 | here however, since we assume that the compiler must have already done | |
d45cf215 | 2747 | this consistency checking when it was creating the .X files. */ |
5f8037c4 RS |
2748 | |
2749 | for (dd_p = hp->ddip; dd_p; dd_p = dd_p->next_for_func) | |
2750 | if (dd_p->prototyped) | |
2751 | ((NONCONST def_dec_info *) dd_p)->definition = dd_p; | |
2752 | ||
2753 | /* Traverse the list of definitions and declarations for this particular | |
2754 | function name. For each item on the list, if it is an extern function | |
2755 | declaration and if it has no associated definition yet, go try to find | |
2756 | the matching extern definition for the declaration. | |
2757 | ||
2758 | When looking for the matching function definition, warn the user if we | |
2759 | fail to find one. | |
2760 | ||
2761 | If we find more that one function definition also issue a warning. | |
2762 | ||
2763 | Do the search for the matching definition only once per unique function | |
2764 | name (and only when absolutely needed) so that we can avoid putting out | |
2765 | redundant warning messages, and so that we will only put out warning | |
2766 | messages when there is actually a reference (i.e. a declaration) for | |
2767 | which we need to find a matching definition. */ | |
2768 | ||
2769 | for (dd_p = hp->ddip; dd_p; dd_p = dd_p->next_for_func) | |
2770 | if (!dd_p->is_func_def && !dd_p->is_static && !dd_p->definition) | |
2771 | { | |
2772 | if (first_extern_reference) | |
2773 | { | |
2774 | extern_def_p = find_extern_def (hp->ddip, dd_p); | |
2775 | first_extern_reference = 0; | |
2776 | } | |
2777 | ((NONCONST def_dec_info *) dd_p)->definition = extern_def_p; | |
2778 | } | |
2779 | ||
2780 | /* Traverse the list of definitions and declarations for this particular | |
2781 | function name. For each item on the list, if it is a static function | |
2782 | declaration and if it has no associated definition yet, go try to find | |
2783 | the matching static definition for the declaration within the same file. | |
2784 | ||
2785 | When looking for the matching function definition, warn the user if we | |
2786 | fail to find one in the same file with the declaration, and refuse to | |
2787 | convert this kind of cross-file static function declaration. After all, | |
2788 | this is stupid practice and should be discouraged. | |
2789 | ||
2790 | We don't have to worry about the possibility that there is more than one | |
2791 | matching function definition in the given file because that would have | |
2792 | been flagged as an error by the compiler. | |
2793 | ||
2794 | Do the search for the matching definition only once per unique | |
2795 | function-name/source-file pair (and only when absolutely needed) so that | |
2796 | we can avoid putting out redundant warning messages, and so that we will | |
2797 | only put out warning messages when there is actually a reference (i.e. a | |
2798 | declaration) for which we actually need to find a matching definition. */ | |
2799 | ||
2800 | for (dd_p = hp->ddip; dd_p; dd_p = dd_p->next_for_func) | |
2801 | if (!dd_p->is_func_def && dd_p->is_static && !dd_p->definition) | |
2802 | { | |
2803 | const def_dec_info *dd_p2; | |
2804 | const def_dec_info *static_def; | |
2805 | ||
2806 | /* We have now found a single static declaration for which we need to | |
2807 | find a matching definition. We want to minimize the work (and the | |
2808 | number of warnings), so we will find an appropriate (matching) | |
2809 | static definition for this declaration, and then distribute it | |
2810 | (as the definition for) any and all other static declarations | |
2811 | for this function name which occur within the same file, and which | |
2812 | do not already have definitions. | |
2813 | ||
2814 | Note that a trick is used here to prevent subsequent attempts to | |
a019653e | 2815 | call find_static_definition for a given function-name & file |
5f8037c4 RS |
2816 | if the first such call returns NULL. Essentially, we convert |
2817 | these NULL return values to -1, and put the -1 into the definition | |
2818 | field for each other static declaration from the same file which | |
2819 | does not already have an associated definition. | |
2820 | This makes these other static declarations look like they are | |
2821 | actually defined already when the outer loop here revisits them | |
2822 | later on. Thus, the outer loop will skip over them. Later, we | |
2823 | turn the -1's back to NULL's. */ | |
2824 | ||
2825 | ((NONCONST def_dec_info *) dd_p)->definition = | |
2826 | (static_def = find_static_definition (dd_p)) | |
2827 | ? static_def | |
2828 | : (const def_dec_info *) -1; | |
2829 | ||
2830 | for (dd_p2 = dd_p->next_for_func; dd_p2; dd_p2 = dd_p2->next_for_func) | |
2831 | if (!dd_p2->is_func_def && dd_p2->is_static | |
2832 | && !dd_p2->definition && (dd_p2->file == dd_p->file)) | |
2833 | ((NONCONST def_dec_info *)dd_p2)->definition = dd_p->definition; | |
2834 | } | |
2835 | ||
2836 | /* Convert any dummy (-1) definitions we created in the step above back to | |
2837 | NULL's (as they should be). */ | |
2838 | ||
2839 | for (dd_p = hp->ddip; dd_p; dd_p = dd_p->next_for_func) | |
2840 | if (dd_p->definition == (def_dec_info *) -1) | |
2841 | ((NONCONST def_dec_info *) dd_p)->definition = NULL; | |
2842 | } | |
2843 | ||
a019653e | 2844 | #endif /* !defined (UNPROTOIZE) */ |
5f8037c4 RS |
2845 | |
2846 | /* Give a pointer into the clean text buffer, return a number which is the | |
2847 | original source line number that the given pointer points into. */ | |
2848 | ||
2849 | static int | |
34e56753 RS |
2850 | identify_lineno (clean_p) |
2851 | const char *clean_p; | |
5f8037c4 RS |
2852 | { |
2853 | int line_num = 1; | |
2854 | const char *scan_p; | |
2855 | ||
2856 | for (scan_p = clean_text_base; scan_p <= clean_p; scan_p++) | |
2857 | if (*scan_p == '\n') | |
2858 | line_num++; | |
2859 | return line_num; | |
2860 | } | |
2861 | ||
2862 | /* Issue an error message and give up on doing this particular edit. */ | |
2863 | ||
2864 | static void | |
34e56753 RS |
2865 | declare_source_confusing (clean_p) |
2866 | const char *clean_p; | |
5f8037c4 RS |
2867 | { |
2868 | if (!quiet_flag) | |
2869 | { | |
2870 | if (clean_p == 0) | |
ab87f8c8 JL |
2871 | notice ("%s: %d: warning: source too confusing\n", |
2872 | shortpath (NULL, convert_filename), last_known_line_number); | |
5f8037c4 | 2873 | else |
ab87f8c8 JL |
2874 | notice ("%s: %d: warning: source too confusing\n", |
2875 | shortpath (NULL, convert_filename), | |
2876 | identify_lineno (clean_p)); | |
5f8037c4 RS |
2877 | } |
2878 | longjmp (source_confusion_recovery, 1); | |
2879 | } | |
2880 | ||
2881 | /* Check that a condition which is expected to be true in the original source | |
2882 | code is in fact true. If not, issue an error message and give up on | |
2883 | converting this particular source file. */ | |
2884 | ||
34e56753 RS |
2885 | static void |
2886 | check_source (cond, clean_p) | |
2887 | int cond; | |
2888 | const char *clean_p; | |
5f8037c4 RS |
2889 | { |
2890 | if (!cond) | |
2891 | declare_source_confusing (clean_p); | |
2892 | } | |
2893 | ||
2894 | /* If we think of the in-core cleaned text buffer as a memory mapped | |
2895 | file (with the variable last_known_line_start acting as sort of a | |
2896 | file pointer) then we can imagine doing "seeks" on the buffer. The | |
2897 | following routine implements a kind of "seek" operation for the in-core | |
2898 | (cleaned) copy of the source file. When finished, it returns a pointer to | |
2899 | the start of a given (numbered) line in the cleaned text buffer. | |
2900 | ||
2901 | Note that protoize only has to "seek" in the forward direction on the | |
2902 | in-core cleaned text file buffers, and it never needs to back up. | |
2903 | ||
2904 | This routine is made a little bit faster by remembering the line number | |
2905 | (and pointer value) supplied (and returned) from the previous "seek". | |
2906 | This prevents us from always having to start all over back at the top | |
2907 | of the in-core cleaned buffer again. */ | |
2908 | ||
2909 | static const char * | |
34e56753 RS |
2910 | seek_to_line (n) |
2911 | int n; | |
5f8037c4 RS |
2912 | { |
2913 | if (n < last_known_line_number) | |
2914 | abort (); | |
2915 | ||
2916 | while (n > last_known_line_number) | |
2917 | { | |
2918 | while (*last_known_line_start != '\n') | |
2919 | check_source (++last_known_line_start < clean_text_limit, 0); | |
2920 | last_known_line_start++; | |
2921 | last_known_line_number++; | |
2922 | } | |
2923 | return last_known_line_start; | |
2924 | } | |
2925 | ||
2926 | /* Given a pointer to a character in the cleaned text buffer, return a pointer | |
abc95ed3 | 2927 | to the next non-whitespace character which follows it. */ |
5f8037c4 RS |
2928 | |
2929 | static const char * | |
34e56753 RS |
2930 | forward_to_next_token_char (ptr) |
2931 | const char *ptr; | |
5f8037c4 | 2932 | { |
e51712db KG |
2933 | for (++ptr; ISSPACE ((const unsigned char)*ptr); |
2934 | check_source (++ptr < clean_text_limit, 0)) | |
5f8037c4 RS |
2935 | continue; |
2936 | return ptr; | |
2937 | } | |
2938 | ||
2939 | /* Copy a chunk of text of length `len' and starting at `str' to the current | |
2940 | output buffer. Note that all attempts to add stuff to the current output | |
2941 | buffer ultimately go through here. */ | |
2942 | ||
2943 | static void | |
34e56753 RS |
2944 | output_bytes (str, len) |
2945 | const char *str; | |
2946 | size_t len; | |
5f8037c4 RS |
2947 | { |
2948 | if ((repl_write_ptr + 1) + len >= repl_text_limit) | |
2949 | { | |
2950 | size_t new_size = (repl_text_limit - repl_text_base) << 1; | |
2951 | char *new_buf = (char *) xrealloc (repl_text_base, new_size); | |
2952 | ||
2953 | repl_write_ptr = new_buf + (repl_write_ptr - repl_text_base); | |
2954 | repl_text_base = new_buf; | |
2955 | repl_text_limit = new_buf + new_size; | |
2956 | } | |
2957 | memcpy (repl_write_ptr + 1, str, len); | |
2958 | repl_write_ptr += len; | |
2959 | } | |
2960 | ||
2961 | /* Copy all bytes (except the trailing null) of a null terminated string to | |
2962 | the current output buffer. */ | |
2963 | ||
2964 | static void | |
34e56753 RS |
2965 | output_string (str) |
2966 | const char *str; | |
5f8037c4 RS |
2967 | { |
2968 | output_bytes (str, strlen (str)); | |
2969 | } | |
2970 | ||
2971 | /* Copy some characters from the original text buffer to the current output | |
2972 | buffer. | |
2973 | ||
2974 | This routine takes a pointer argument `p' which is assumed to be a pointer | |
2975 | into the cleaned text buffer. The bytes which are copied are the `original' | |
2976 | equivalents for the set of bytes between the last value of `clean_read_ptr' | |
2977 | and the argument value `p'. | |
2978 | ||
2979 | The set of bytes copied however, comes *not* from the cleaned text buffer, | |
2980 | but rather from the direct counterparts of these bytes within the original | |
2981 | text buffer. | |
2982 | ||
2983 | Thus, when this function is called, some bytes from the original text | |
2984 | buffer (which may include original comments and preprocessing directives) | |
2985 | will be copied into the output buffer. | |
2986 | ||
9faa82d8 | 2987 | Note that the request implied when this routine is called includes the |
5f8037c4 RS |
2988 | byte pointed to by the argument pointer `p'. */ |
2989 | ||
2990 | static void | |
34e56753 RS |
2991 | output_up_to (p) |
2992 | const char *p; | |
5f8037c4 RS |
2993 | { |
2994 | size_t copy_length = (size_t) (p - clean_read_ptr); | |
2995 | const char *copy_start = orig_text_base+(clean_read_ptr-clean_text_base)+1; | |
2996 | ||
2997 | if (copy_length == 0) | |
2998 | return; | |
2999 | ||
3000 | output_bytes (copy_start, copy_length); | |
3001 | clean_read_ptr = p; | |
3002 | } | |
3003 | ||
3004 | /* Given a pointer to a def_dec_info record which represents some form of | |
3005 | definition of a function (perhaps a real definition, or in lieu of that | |
3006 | perhaps just a declaration with a full prototype) return true if this | |
3007 | function is one which we should avoid converting. Return false | |
3008 | otherwise. */ | |
3009 | ||
3010 | static int | |
34e56753 RS |
3011 | other_variable_style_function (ansi_header) |
3012 | const char *ansi_header; | |
5f8037c4 RS |
3013 | { |
3014 | #ifdef UNPROTOIZE | |
3015 | ||
3016 | /* See if we have a stdarg function, or a function which has stdarg style | |
3017 | parameters or a stdarg style return type. */ | |
3018 | ||
87d34878 | 3019 | return substr (ansi_header, "...") != 0; |
5f8037c4 | 3020 | |
a019653e | 3021 | #else /* !defined (UNPROTOIZE) */ |
5f8037c4 RS |
3022 | |
3023 | /* See if we have a varargs function, or a function which has varargs style | |
3024 | parameters or a varargs style return type. */ | |
3025 | ||
3026 | const char *p; | |
3027 | int len = strlen (varargs_style_indicator); | |
3028 | ||
3029 | for (p = ansi_header; p; ) | |
3030 | { | |
3031 | const char *candidate; | |
3032 | ||
3033 | if ((candidate = substr (p, varargs_style_indicator)) == 0) | |
3034 | return 0; | |
3035 | else | |
3036 | if (!is_id_char (candidate[-1]) && !is_id_char (candidate[len])) | |
3037 | return 1; | |
3038 | else | |
3039 | p = candidate + 1; | |
3040 | } | |
3041 | return 0; | |
a019653e | 3042 | #endif /* !defined (UNPROTOIZE) */ |
5f8037c4 RS |
3043 | } |
3044 | ||
3045 | /* Do the editing operation specifically for a function "declaration". Note | |
3046 | that editing for function "definitions" are handled in a separate routine | |
3047 | below. */ | |
3048 | ||
3049 | static void | |
34e56753 RS |
3050 | edit_fn_declaration (def_dec_p, clean_text_p) |
3051 | const def_dec_info *def_dec_p; | |
8241a41f | 3052 | const char *volatile clean_text_p; |
5f8037c4 RS |
3053 | { |
3054 | const char *start_formals; | |
3055 | const char *end_formals; | |
3056 | const char *function_to_edit = def_dec_p->hash_entry->symbol; | |
3057 | size_t func_name_len = strlen (function_to_edit); | |
3058 | const char *end_of_fn_name; | |
3059 | ||
3060 | #ifndef UNPROTOIZE | |
3061 | ||
3062 | const f_list_chain_item *this_f_list_chain_item; | |
3063 | const def_dec_info *definition = def_dec_p->definition; | |
3064 | ||
3065 | /* If we are protoizing, and if we found no corresponding definition for | |
3066 | this particular function declaration, then just leave this declaration | |
3067 | exactly as it is. */ | |
3068 | ||
3069 | if (!definition) | |
3070 | return; | |
3071 | ||
3072 | /* If we are protoizing, and if the corresponding definition that we found | |
3073 | for this particular function declaration defined an old style varargs | |
3074 | function, then we want to issue a warning and just leave this function | |
3075 | declaration unconverted. */ | |
3076 | ||
3077 | if (other_variable_style_function (definition->ansi_decl)) | |
3078 | { | |
3079 | if (!quiet_flag) | |
ab87f8c8 JL |
3080 | notice ("%s: %d: warning: varargs function declaration not converted\n", |
3081 | shortpath (NULL, def_dec_p->file->hash_entry->symbol), | |
3082 | def_dec_p->line); | |
5f8037c4 RS |
3083 | return; |
3084 | } | |
3085 | ||
a019653e | 3086 | #endif /* !defined (UNPROTOIZE) */ |
5f8037c4 RS |
3087 | |
3088 | /* Setup here to recover from confusing source code detected during this | |
3089 | particular "edit". */ | |
3090 | ||
3091 | save_pointers (); | |
3092 | if (setjmp (source_confusion_recovery)) | |
3093 | { | |
3094 | restore_pointers (); | |
ab87f8c8 JL |
3095 | notice ("%s: declaration of function `%s' not converted\n", |
3096 | pname, function_to_edit); | |
5f8037c4 RS |
3097 | return; |
3098 | } | |
3099 | ||
3100 | /* We are editing a function declaration. The line number we did a seek to | |
3101 | contains the comma or semicolon which follows the declaration. Our job | |
3102 | now is to scan backwards looking for the function name. This name *must* | |
3103 | be followed by open paren (ignoring whitespace, of course). We need to | |
3104 | replace everything between that open paren and the corresponding closing | |
3105 | paren. If we are protoizing, we need to insert the prototype-style | |
3106 | formals lists. If we are unprotoizing, we need to just delete everything | |
3107 | between the pairs of opening and closing parens. */ | |
3108 | ||
3109 | /* First move up to the end of the line. */ | |
3110 | ||
3111 | while (*clean_text_p != '\n') | |
3112 | check_source (++clean_text_p < clean_text_limit, 0); | |
3113 | clean_text_p--; /* Point to just before the newline character. */ | |
3114 | ||
3115 | /* Now we can scan backwards for the function name. */ | |
3116 | ||
3117 | do | |
3118 | { | |
3119 | for (;;) | |
3120 | { | |
3121 | /* Scan leftwards until we find some character which can be | |
3122 | part of an identifier. */ | |
3123 | ||
3124 | while (!is_id_char (*clean_text_p)) | |
3125 | check_source (--clean_text_p > clean_read_ptr, 0); | |
3126 | ||
3127 | /* Scan backwards until we find a char that cannot be part of an | |
3128 | identifier. */ | |
3129 | ||
3130 | while (is_id_char (*clean_text_p)) | |
3131 | check_source (--clean_text_p > clean_read_ptr, 0); | |
3132 | ||
3133 | /* Having found an "id break", see if the following id is the one | |
3134 | that we are looking for. If so, then exit from this loop. */ | |
3135 | ||
3136 | if (!strncmp (clean_text_p+1, function_to_edit, func_name_len)) | |
3137 | { | |
3138 | char ch = *(clean_text_p + 1 + func_name_len); | |
3139 | ||
3140 | /* Must also check to see that the name in the source text | |
3141 | ends where it should (in order to prevent bogus matches | |
3142 | on similar but longer identifiers. */ | |
3143 | ||
3144 | if (! is_id_char (ch)) | |
3145 | break; /* exit from loop */ | |
3146 | } | |
3147 | } | |
3148 | ||
3149 | /* We have now found the first perfect match for the function name in | |
3150 | our backward search. This may or may not be the actual function | |
3151 | name at the start of the actual function declaration (i.e. we could | |
3152 | have easily been mislead). We will try to avoid getting fooled too | |
3153 | often by looking forward for the open paren which should follow the | |
3154 | identifier we just found. We ignore whitespace while hunting. If | |
3155 | the next non-whitespace byte we see is *not* an open left paren, | |
3156 | then we must assume that we have been fooled and we start over | |
6dc42e49 | 3157 | again accordingly. Note that there is no guarantee, that even if |
5f8037c4 RS |
3158 | we do see the open paren, that we are in the right place. |
3159 | Programmers do the strangest things sometimes! */ | |
3160 | ||
3161 | end_of_fn_name = clean_text_p + strlen (def_dec_p->hash_entry->symbol); | |
3162 | start_formals = forward_to_next_token_char (end_of_fn_name); | |
3163 | } | |
3164 | while (*start_formals != '('); | |
3165 | ||
3166 | /* start_of_formals now points to the opening left paren which immediately | |
3167 | follows the name of the function. */ | |
3168 | ||
3169 | /* Note that there may be several formals lists which need to be modified | |
3170 | due to the possibility that the return type of this function is a | |
3171 | pointer-to-function type. If there are several formals lists, we | |
3172 | convert them in left-to-right order here. */ | |
3173 | ||
3174 | #ifndef UNPROTOIZE | |
3175 | this_f_list_chain_item = definition->f_list_chain; | |
a019653e | 3176 | #endif /* !defined (UNPROTOIZE) */ |
5f8037c4 RS |
3177 | |
3178 | for (;;) | |
3179 | { | |
3180 | { | |
3181 | int depth; | |
3182 | ||
3183 | end_formals = start_formals + 1; | |
3184 | depth = 1; | |
3185 | for (; depth; check_source (++end_formals < clean_text_limit, 0)) | |
3186 | { | |
3187 | switch (*end_formals) | |
3188 | { | |
3189 | case '(': | |
3190 | depth++; | |
3191 | break; | |
3192 | case ')': | |
3193 | depth--; | |
3194 | break; | |
3195 | } | |
3196 | } | |
3197 | end_formals--; | |
3198 | } | |
3199 | ||
3200 | /* end_formals now points to the closing right paren of the formals | |
3201 | list whose left paren is pointed to by start_formals. */ | |
3202 | ||
3203 | /* Now, if we are protoizing, we insert the new ANSI-style formals list | |
3204 | attached to the associated definition of this function. If however | |
3205 | we are unprotoizing, then we simply delete any formals list which | |
3206 | may be present. */ | |
3207 | ||
3208 | output_up_to (start_formals); | |
3209 | #ifndef UNPROTOIZE | |
3210 | if (this_f_list_chain_item) | |
3211 | { | |
3212 | output_string (this_f_list_chain_item->formals_list); | |
3213 | this_f_list_chain_item = this_f_list_chain_item->chain_next; | |
3214 | } | |
3215 | else | |
3216 | { | |
3217 | if (!quiet_flag) | |
ab87f8c8 JL |
3218 | notice ("%s: warning: too many parameter lists in declaration of `%s'\n", |
3219 | pname, def_dec_p->hash_entry->symbol); | |
5f8037c4 RS |
3220 | check_source (0, end_formals); /* leave the declaration intact */ |
3221 | } | |
a019653e | 3222 | #endif /* !defined (UNPROTOIZE) */ |
5f8037c4 RS |
3223 | clean_read_ptr = end_formals - 1; |
3224 | ||
3225 | /* Now see if it looks like there may be another formals list associated | |
3226 | with the function declaration that we are converting (following the | |
3227 | formals list that we just converted. */ | |
3228 | ||
3229 | { | |
3230 | const char *another_r_paren = forward_to_next_token_char (end_formals); | |
3231 | ||
3232 | if ((*another_r_paren != ')') | |
3233 | || (*(start_formals = forward_to_next_token_char (another_r_paren)) != '(')) | |
3234 | { | |
3235 | #ifndef UNPROTOIZE | |
3236 | if (this_f_list_chain_item) | |
3237 | { | |
3238 | if (!quiet_flag) | |
ab87f8c8 JL |
3239 | notice ("\n%s: warning: too few parameter lists in declaration of `%s'\n", |
3240 | pname, def_dec_p->hash_entry->symbol); | |
5f8037c4 RS |
3241 | check_source (0, start_formals); /* leave the decl intact */ |
3242 | } | |
a019653e | 3243 | #endif /* !defined (UNPROTOIZE) */ |
5f8037c4 RS |
3244 | break; |
3245 | ||
3246 | } | |
3247 | } | |
3248 | ||
3249 | /* There does appear to be yet another formals list, so loop around | |
3250 | again, and convert it also. */ | |
3251 | } | |
3252 | } | |
3253 | ||
3254 | /* Edit a whole group of formals lists, starting with the rightmost one | |
3255 | from some set of formals lists. This routine is called once (from the | |
3256 | outside) for each function declaration which is converted. It is | |
3257 | recursive however, and it calls itself once for each remaining formal | |
3258 | list that lies to the left of the one it was originally called to work | |
3259 | on. Thus, a whole set gets done in right-to-left order. | |
3260 | ||
3261 | This routine returns non-zero if it thinks that it should not be trying | |
3262 | to convert this particular function definition (because the name of the | |
3263 | function doesn't match the one expected). */ | |
3264 | ||
3265 | static int | |
34e56753 RS |
3266 | edit_formals_lists (end_formals, f_list_count, def_dec_p) |
3267 | const char *end_formals; | |
3268 | unsigned int f_list_count; | |
3269 | const def_dec_info *def_dec_p; | |
5f8037c4 RS |
3270 | { |
3271 | const char *start_formals; | |
3272 | int depth; | |
3273 | ||
3274 | start_formals = end_formals - 1; | |
3275 | depth = 1; | |
3276 | for (; depth; check_source (--start_formals > clean_read_ptr, 0)) | |
3277 | { | |
3278 | switch (*start_formals) | |
3279 | { | |
3280 | case '(': | |
3281 | depth--; | |
3282 | break; | |
3283 | case ')': | |
3284 | depth++; | |
3285 | break; | |
3286 | } | |
3287 | } | |
3288 | start_formals++; | |
3289 | ||
3290 | /* start_formals now points to the opening left paren of the formals list. */ | |
3291 | ||
3292 | f_list_count--; | |
3293 | ||
3294 | if (f_list_count) | |
3295 | { | |
3296 | const char *next_end; | |
3297 | ||
3298 | /* There should be more formal lists to the left of here. */ | |
3299 | ||
3300 | next_end = start_formals - 1; | |
3301 | check_source (next_end > clean_read_ptr, 0); | |
e51712db | 3302 | while (ISSPACE ((const unsigned char)*next_end)) |
5f8037c4 RS |
3303 | check_source (--next_end > clean_read_ptr, 0); |
3304 | check_source (*next_end == ')', next_end); | |
3305 | check_source (--next_end > clean_read_ptr, 0); | |
3306 | check_source (*next_end == ')', next_end); | |
3307 | if (edit_formals_lists (next_end, f_list_count, def_dec_p)) | |
3308 | return 1; | |
3309 | } | |
3310 | ||
3311 | /* Check that the function name in the header we are working on is the same | |
3312 | as the one we would expect to find. If not, issue a warning and return | |
3313 | non-zero. */ | |
3314 | ||
3315 | if (f_list_count == 0) | |
3316 | { | |
3317 | const char *expected = def_dec_p->hash_entry->symbol; | |
3318 | const char *func_name_start; | |
3319 | const char *func_name_limit; | |
3320 | size_t func_name_len; | |
3321 | ||
e51712db KG |
3322 | for (func_name_limit = start_formals-1; |
3323 | ISSPACE ((const unsigned char)*func_name_limit); ) | |
5f8037c4 RS |
3324 | check_source (--func_name_limit > clean_read_ptr, 0); |
3325 | ||
3326 | for (func_name_start = func_name_limit++; | |
3327 | is_id_char (*func_name_start); | |
3328 | func_name_start--) | |
3329 | check_source (func_name_start > clean_read_ptr, 0); | |
3330 | func_name_start++; | |
3331 | func_name_len = func_name_limit - func_name_start; | |
3332 | if (func_name_len == 0) | |
3333 | check_source (0, func_name_start); | |
3334 | if (func_name_len != strlen (expected) | |
a2b22788 | 3335 | || strncmp (func_name_start, expected, func_name_len)) |
5f8037c4 | 3336 | { |
ab87f8c8 JL |
3337 | notice ("%s: %d: warning: found `%s' but expected `%s'\n", |
3338 | shortpath (NULL, def_dec_p->file->hash_entry->symbol), | |
3339 | identify_lineno (func_name_start), | |
3340 | dupnstr (func_name_start, func_name_len), | |
3341 | expected); | |
5f8037c4 RS |
3342 | return 1; |
3343 | } | |
3344 | } | |
3345 | ||
3346 | output_up_to (start_formals); | |
3347 | ||
3348 | #ifdef UNPROTOIZE | |
3349 | if (f_list_count == 0) | |
3350 | output_string (def_dec_p->formal_names); | |
a019653e | 3351 | #else /* !defined (UNPROTOIZE) */ |
5f8037c4 RS |
3352 | { |
3353 | unsigned f_list_depth; | |
3354 | const f_list_chain_item *flci_p = def_dec_p->f_list_chain; | |
3355 | ||
3356 | /* At this point, the current value of f_list count says how many | |
3357 | links we have to follow through the f_list_chain to get to the | |
3358 | particular formals list that we need to output next. */ | |
3359 | ||
3360 | for (f_list_depth = 0; f_list_depth < f_list_count; f_list_depth++) | |
3361 | flci_p = flci_p->chain_next; | |
3362 | output_string (flci_p->formals_list); | |
3363 | } | |
a019653e | 3364 | #endif /* !defined (UNPROTOIZE) */ |
5f8037c4 RS |
3365 | |
3366 | clean_read_ptr = end_formals - 1; | |
3367 | return 0; | |
3368 | } | |
3369 | ||
0f41302f MS |
3370 | /* Given a pointer to a byte in the clean text buffer which points to |
3371 | the beginning of a line that contains a "follower" token for a | |
3372 | function definition header, do whatever is necessary to find the | |
3373 | right closing paren for the rightmost formals list of the function | |
3374 | definition header. */ | |
5f8037c4 RS |
3375 | |
3376 | static const char * | |
34e56753 RS |
3377 | find_rightmost_formals_list (clean_text_p) |
3378 | const char *clean_text_p; | |
5f8037c4 RS |
3379 | { |
3380 | const char *end_formals; | |
3381 | ||
3382 | /* We are editing a function definition. The line number we did a seek | |
3383 | to contains the first token which immediately follows the entire set of | |
3384 | formals lists which are part of this particular function definition | |
3385 | header. | |
3386 | ||
3387 | Our job now is to scan leftwards in the clean text looking for the | |
3388 | right-paren which is at the end of the function header's rightmost | |
3389 | formals list. | |
3390 | ||
3391 | If we ignore whitespace, this right paren should be the first one we | |
3392 | see which is (ignoring whitespace) immediately followed either by the | |
3393 | open curly-brace beginning the function body or by an alphabetic | |
3394 | character (in the case where the function definition is in old (K&R) | |
3395 | style and there are some declarations of formal parameters). */ | |
3396 | ||
3397 | /* It is possible that the right paren we are looking for is on the | |
3398 | current line (together with its following token). Just in case that | |
3399 | might be true, we start out here by skipping down to the right end of | |
3400 | the current line before starting our scan. */ | |
3401 | ||
3402 | for (end_formals = clean_text_p; *end_formals != '\n'; end_formals++) | |
3403 | continue; | |
3404 | end_formals--; | |
3405 | ||
34e56753 RS |
3406 | #ifdef UNPROTOIZE |
3407 | ||
5f8037c4 RS |
3408 | /* Now scan backwards while looking for the right end of the rightmost |
3409 | formals list associated with this function definition. */ | |
3410 | ||
34e56753 RS |
3411 | { |
3412 | char ch; | |
3413 | const char *l_brace_p; | |
3414 | ||
3415 | /* Look leftward and try to find a right-paren. */ | |
3416 | ||
3417 | while (*end_formals != ')') | |
3418 | { | |
79c9824e KG |
3419 | if (ISSPACE ((unsigned char)*end_formals)) |
3420 | while (ISSPACE ((unsigned char)*end_formals)) | |
34e56753 RS |
3421 | check_source (--end_formals > clean_read_ptr, 0); |
3422 | else | |
3423 | check_source (--end_formals > clean_read_ptr, 0); | |
3424 | } | |
3425 | ||
3426 | ch = *(l_brace_p = forward_to_next_token_char (end_formals)); | |
3427 | /* Since we are unprotoizing an ANSI-style (prototyped) function | |
3428 | definition, there had better not be anything (except whitespace) | |
3429 | between the end of the ANSI formals list and the beginning of the | |
3430 | function body (i.e. the '{'). */ | |
3431 | ||
3432 | check_source (ch == '{', l_brace_p); | |
3433 | } | |
3434 | ||
a019653e | 3435 | #else /* !defined (UNPROTOIZE) */ |
34e56753 RS |
3436 | |
3437 | /* Now scan backwards while looking for the right end of the rightmost | |
3438 | formals list associated with this function definition. */ | |
3439 | ||
3440 | while (1) | |
5f8037c4 RS |
3441 | { |
3442 | char ch; | |
3443 | const char *l_brace_p; | |
3444 | ||
3445 | /* Look leftward and try to find a right-paren. */ | |
3446 | ||
3447 | while (*end_formals != ')') | |
3448 | { | |
e51712db KG |
3449 | if (ISSPACE ((const unsigned char)*end_formals)) |
3450 | while (ISSPACE ((const unsigned char)*end_formals)) | |
5f8037c4 RS |
3451 | check_source (--end_formals > clean_read_ptr, 0); |
3452 | else | |
3453 | check_source (--end_formals > clean_read_ptr, 0); | |
3454 | } | |
3455 | ||
3456 | ch = *(l_brace_p = forward_to_next_token_char (end_formals)); | |
3457 | ||
5f8037c4 RS |
3458 | /* Since it is possible that we found a right paren before the starting |
3459 | '{' of the body which IS NOT the one at the end of the real K&R | |
3460 | formals list (say for instance, we found one embedded inside one of | |
3461 | the old K&R formal parameter declarations) we have to check to be | |
3462 | sure that this is in fact the right paren that we were looking for. | |
3463 | ||
3464 | The one we were looking for *must* be followed by either a '{' or | |
3826a3da | 3465 | by an alphabetic character, while others *cannot* validly be followed |
5f8037c4 RS |
3466 | by such characters. */ |
3467 | ||
e51712db | 3468 | if ((ch == '{') || ISALPHA ((unsigned char)ch)) |
5f8037c4 RS |
3469 | break; |
3470 | ||
3471 | /* At this point, we have found a right paren, but we know that it is | |
3472 | not the one we were looking for, so backup one character and keep | |
3473 | looking. */ | |
3474 | ||
3475 | check_source (--end_formals > clean_read_ptr, 0); | |
34e56753 | 3476 | } |
5f8037c4 | 3477 | |
a019653e | 3478 | #endif /* !defined (UNPROTOIZE) */ |
5f8037c4 | 3479 | |
5f8037c4 RS |
3480 | return end_formals; |
3481 | } | |
3482 | ||
3483 | #ifndef UNPROTOIZE | |
3484 | ||
3485 | /* Insert into the output file a totally new declaration for a function | |
3486 | which (up until now) was being called from within the current block | |
3487 | without having been declared at any point such that the declaration | |
3488 | was visible (i.e. in scope) at the point of the call. | |
3489 | ||
3490 | We need to add in explicit declarations for all such function calls | |
3491 | in order to get the full benefit of prototype-based function call | |
3492 | parameter type checking. */ | |
3493 | ||
3494 | static void | |
34e56753 RS |
3495 | add_local_decl (def_dec_p, clean_text_p) |
3496 | const def_dec_info *def_dec_p; | |
3497 | const char *clean_text_p; | |
5f8037c4 RS |
3498 | { |
3499 | const char *start_of_block; | |
3500 | const char *function_to_edit = def_dec_p->hash_entry->symbol; | |
3501 | ||
3502 | /* Don't insert new local explicit declarations unless explicitly requested | |
3503 | to do so. */ | |
3504 | ||
3505 | if (!local_flag) | |
3506 | return; | |
3507 | ||
3508 | /* Setup here to recover from confusing source code detected during this | |
3509 | particular "edit". */ | |
3510 | ||
3511 | save_pointers (); | |
3512 | if (setjmp (source_confusion_recovery)) | |
3513 | { | |
3514 | restore_pointers (); | |
ab87f8c8 JL |
3515 | notice ("%s: local declaration for function `%s' not inserted\n", |
3516 | pname, function_to_edit); | |
5f8037c4 RS |
3517 | return; |
3518 | } | |
3519 | ||
3520 | /* We have already done a seek to the start of the line which should | |
3521 | contain *the* open curly brace which begins the block in which we need | |
3522 | to insert an explicit function declaration (to replace the implicit one). | |
3523 | ||
3524 | Now we scan that line, starting from the left, until we find the | |
3525 | open curly brace we are looking for. Note that there may actually be | |
3526 | multiple open curly braces on the given line, but we will be happy | |
3527 | with the leftmost one no matter what. */ | |
3528 | ||
3529 | start_of_block = clean_text_p; | |
3530 | while (*start_of_block != '{' && *start_of_block != '\n') | |
3531 | check_source (++start_of_block < clean_text_limit, 0); | |
3532 | ||
3533 | /* Note that the line from the original source could possibly | |
3534 | contain *no* open curly braces! This happens if the line contains | |
3535 | a macro call which expands into a chunk of text which includes a | |
3536 | block (and that block's associated open and close curly braces). | |
3537 | In cases like this, we give up, issue a warning, and do nothing. */ | |
3538 | ||
3539 | if (*start_of_block != '{') | |
3540 | { | |
3541 | if (!quiet_flag) | |
ab87f8c8 | 3542 | notice ("\n%s: %d: warning: can't add declaration of `%s' into macro call\n", |
a2b22788 RS |
3543 | def_dec_p->file->hash_entry->symbol, def_dec_p->line, |
3544 | def_dec_p->hash_entry->symbol); | |
5f8037c4 RS |
3545 | return; |
3546 | } | |
3547 | ||
3548 | /* Figure out what a nice (pretty) indentation would be for the new | |
3549 | declaration we are adding. In order to do this, we must scan forward | |
3550 | from the '{' until we find the first line which starts with some | |
3551 | non-whitespace characters (i.e. real "token" material). */ | |
3552 | ||
3553 | { | |
3554 | const char *ep = forward_to_next_token_char (start_of_block) - 1; | |
3555 | const char *sp; | |
3556 | ||
3557 | /* Now we have ep pointing at the rightmost byte of some existing indent | |
3558 | stuff. At least that is the hope. | |
3559 | ||
3560 | We can now just scan backwards and find the left end of the existing | |
3561 | indentation string, and then copy it to the output buffer. */ | |
3562 | ||
e51712db | 3563 | for (sp = ep; ISSPACE ((const unsigned char)*sp) && *sp != '\n'; sp--) |
5f8037c4 RS |
3564 | continue; |
3565 | ||
3566 | /* Now write out the open { which began this block, and any following | |
3567 | trash up to and including the last byte of the existing indent that | |
3568 | we just found. */ | |
3569 | ||
3570 | output_up_to (ep); | |
3571 | ||
3572 | /* Now we go ahead and insert the new declaration at this point. | |
3573 | ||
3574 | If the definition of the given function is in the same file that we | |
3575 | are currently editing, and if its full ANSI declaration normally | |
3576 | would start with the keyword `extern', suppress the `extern'. */ | |
3577 | ||
3578 | { | |
3579 | const char *decl = def_dec_p->definition->ansi_decl; | |
3580 | ||
3581 | if ((*decl == 'e') && (def_dec_p->file == def_dec_p->definition->file)) | |
3582 | decl += 7; | |
3583 | output_string (decl); | |
3584 | } | |
3585 | ||
d45cf215 | 3586 | /* Finally, write out a new indent string, just like the preceding one |
5f8037c4 RS |
3587 | that we found. This will typically include a newline as the first |
3588 | character of the indent string. */ | |
3589 | ||
3590 | output_bytes (sp, (size_t) (ep - sp) + 1); | |
3591 | } | |
3592 | } | |
3593 | ||
3594 | /* Given a pointer to a file_info record, and a pointer to the beginning | |
3595 | of a line (in the clean text buffer) which is assumed to contain the | |
3596 | first "follower" token for the first function definition header in the | |
3597 | given file, find a good place to insert some new global function | |
3598 | declarations (which will replace scattered and imprecise implicit ones) | |
3599 | and then insert the new explicit declaration at that point in the file. */ | |
3600 | ||
3601 | static void | |
34e56753 RS |
3602 | add_global_decls (file_p, clean_text_p) |
3603 | const file_info *file_p; | |
3604 | const char *clean_text_p; | |
5f8037c4 RS |
3605 | { |
3606 | const def_dec_info *dd_p; | |
3607 | const char *scan_p; | |
3608 | ||
3609 | /* Setup here to recover from confusing source code detected during this | |
3610 | particular "edit". */ | |
3611 | ||
3612 | save_pointers (); | |
3613 | if (setjmp (source_confusion_recovery)) | |
3614 | { | |
3615 | restore_pointers (); | |
ab87f8c8 JL |
3616 | notice ("%s: global declarations for file `%s' not inserted\n", |
3617 | pname, shortpath (NULL, file_p->hash_entry->symbol)); | |
5f8037c4 RS |
3618 | return; |
3619 | } | |
3620 | ||
3621 | /* Start by finding a good location for adding the new explicit function | |
3622 | declarations. To do this, we scan backwards, ignoring whitespace | |
3623 | and comments and other junk until we find either a semicolon, or until | |
3624 | we hit the beginning of the file. */ | |
3625 | ||
3626 | scan_p = find_rightmost_formals_list (clean_text_p); | |
3627 | for (;; --scan_p) | |
3628 | { | |
3629 | if (scan_p < clean_text_base) | |
3630 | break; | |
3631 | check_source (scan_p > clean_read_ptr, 0); | |
3632 | if (*scan_p == ';') | |
3633 | break; | |
3634 | } | |
3635 | ||
3636 | /* scan_p now points either to a semicolon, or to just before the start | |
3637 | of the whole file. */ | |
3638 | ||
3639 | /* Now scan forward for the first non-whitespace character. In theory, | |
3640 | this should be the first character of the following function definition | |
0f41302f | 3641 | header. We will put in the added declarations just prior to that. */ |
5f8037c4 RS |
3642 | |
3643 | scan_p++; | |
e51712db | 3644 | while (ISSPACE ((const unsigned char)*scan_p)) |
5f8037c4 RS |
3645 | scan_p++; |
3646 | scan_p--; | |
3647 | ||
3648 | output_up_to (scan_p); | |
3649 | ||
3650 | /* Now write out full prototypes for all of the things that had been | |
3651 | implicitly declared in this file (but only those for which we were | |
3652 | actually able to find unique matching definitions). Avoid duplicates | |
3653 | by marking things that we write out as we go. */ | |
3654 | ||
3655 | { | |
3656 | int some_decls_added = 0; | |
3657 | ||
3658 | for (dd_p = file_p->defs_decs; dd_p; dd_p = dd_p->next_in_file) | |
3659 | if (dd_p->is_implicit && dd_p->definition && !dd_p->definition->written) | |
3660 | { | |
3661 | const char *decl = dd_p->definition->ansi_decl; | |
3662 | ||
3663 | /* If the function for which we are inserting a declaration is | |
3664 | actually defined later in the same file, then suppress the | |
3665 | leading `extern' keyword (if there is one). */ | |
3666 | ||
3667 | if (*decl == 'e' && (dd_p->file == dd_p->definition->file)) | |
3668 | decl += 7; | |
3669 | ||
3670 | output_string ("\n"); | |
3671 | output_string (decl); | |
3672 | some_decls_added = 1; | |
3673 | ((NONCONST def_dec_info *) dd_p->definition)->written = 1; | |
3674 | } | |
3675 | if (some_decls_added) | |
3676 | output_string ("\n\n"); | |
3677 | } | |
3678 | ||
3679 | /* Unmark all of the definitions that we just marked. */ | |
3680 | ||
3681 | for (dd_p = file_p->defs_decs; dd_p; dd_p = dd_p->next_in_file) | |
3682 | if (dd_p->definition) | |
3683 | ((NONCONST def_dec_info *) dd_p->definition)->written = 0; | |
3684 | } | |
3685 | ||
a019653e | 3686 | #endif /* !defined (UNPROTOIZE) */ |
5f8037c4 RS |
3687 | |
3688 | /* Do the editing operation specifically for a function "definition". Note | |
3689 | that editing operations for function "declarations" are handled by a | |
3690 | separate routine above. */ | |
3691 | ||
3692 | static void | |
34e56753 RS |
3693 | edit_fn_definition (def_dec_p, clean_text_p) |
3694 | const def_dec_info *def_dec_p; | |
3695 | const char *clean_text_p; | |
5f8037c4 RS |
3696 | { |
3697 | const char *end_formals; | |
3698 | const char *function_to_edit = def_dec_p->hash_entry->symbol; | |
3699 | ||
3700 | /* Setup here to recover from confusing source code detected during this | |
3701 | particular "edit". */ | |
3702 | ||
3703 | save_pointers (); | |
3704 | if (setjmp (source_confusion_recovery)) | |
3705 | { | |
3706 | restore_pointers (); | |
ab87f8c8 JL |
3707 | notice ("%s: definition of function `%s' not converted\n", |
3708 | pname, function_to_edit); | |
5f8037c4 RS |
3709 | return; |
3710 | } | |
3711 | ||
3712 | end_formals = find_rightmost_formals_list (clean_text_p); | |
3713 | ||
3714 | /* end_of_formals now points to the closing right paren of the rightmost | |
3715 | formals list which is actually part of the `header' of the function | |
3716 | definition that we are converting. */ | |
3717 | ||
3718 | /* If the header of this function definition looks like it declares a | |
3719 | function with a variable number of arguments, and if the way it does | |
3720 | that is different from that way we would like it (i.e. varargs vs. | |
3721 | stdarg) then issue a warning and leave the header unconverted. */ | |
3722 | ||
3723 | if (other_variable_style_function (def_dec_p->ansi_decl)) | |
3724 | { | |
3725 | if (!quiet_flag) | |
ab87f8c8 JL |
3726 | notice ("%s: %d: warning: definition of %s not converted\n", |
3727 | shortpath (NULL, def_dec_p->file->hash_entry->symbol), | |
3728 | identify_lineno (end_formals), | |
3729 | other_var_style); | |
5f8037c4 RS |
3730 | output_up_to (end_formals); |
3731 | return; | |
3732 | } | |
3733 | ||
3734 | if (edit_formals_lists (end_formals, def_dec_p->f_list_count, def_dec_p)) | |
3735 | { | |
3736 | restore_pointers (); | |
ab87f8c8 JL |
3737 | notice ("%s: definition of function `%s' not converted\n", |
3738 | pname, function_to_edit); | |
5f8037c4 RS |
3739 | return; |
3740 | } | |
3741 | ||
3742 | /* Have to output the last right paren because this never gets flushed by | |
3743 | edit_formals_list. */ | |
3744 | ||
3745 | output_up_to (end_formals); | |
3746 | ||
3747 | #ifdef UNPROTOIZE | |
3748 | { | |
3749 | const char *decl_p; | |
3750 | const char *semicolon_p; | |
3751 | const char *limit_p; | |
3752 | const char *scan_p; | |
3753 | int had_newlines = 0; | |
3754 | ||
3755 | /* Now write out the K&R style formal declarations, one per line. */ | |
3756 | ||
3757 | decl_p = def_dec_p->formal_decls; | |
3758 | limit_p = decl_p + strlen (decl_p); | |
3759 | for (;decl_p < limit_p; decl_p = semicolon_p + 2) | |
3760 | { | |
3761 | for (semicolon_p = decl_p; *semicolon_p != ';'; semicolon_p++) | |
3762 | continue; | |
3763 | output_string ("\n"); | |
3764 | output_string (indent_string); | |
3765 | output_bytes (decl_p, (size_t) ((semicolon_p + 1) - decl_p)); | |
3766 | } | |
3767 | ||
3768 | /* If there are no newlines between the end of the formals list and the | |
3769 | start of the body, we should insert one now. */ | |
3770 | ||
3771 | for (scan_p = end_formals+1; *scan_p != '{'; ) | |
3772 | { | |
3773 | if (*scan_p == '\n') | |
3774 | { | |
3775 | had_newlines = 1; | |
3776 | break; | |
3777 | } | |
3778 | check_source (++scan_p < clean_text_limit, 0); | |
3779 | } | |
3780 | if (!had_newlines) | |
3781 | output_string ("\n"); | |
3782 | } | |
a019653e | 3783 | #else /* !defined (UNPROTOIZE) */ |
9faa82d8 | 3784 | /* If we are protoizing, there may be some flotsam & jetsam (like comments |
5f8037c4 RS |
3785 | and preprocessing directives) after the old formals list but before |
3786 | the following { and we would like to preserve that stuff while effectively | |
3787 | deleting the existing K&R formal parameter declarations. We do so here | |
3788 | in a rather tricky way. Basically, we white out any stuff *except* | |
3789 | the comments/pp-directives in the original text buffer, then, if there | |
3790 | is anything in this area *other* than whitespace, we output it. */ | |
3791 | { | |
3792 | const char *end_formals_orig; | |
3793 | const char *start_body; | |
3794 | const char *start_body_orig; | |
3795 | const char *scan; | |
3796 | const char *scan_orig; | |
f79e449b | 3797 | int have_flotsam = 0; |
5f8037c4 RS |
3798 | int have_newlines = 0; |
3799 | ||
3800 | for (start_body = end_formals + 1; *start_body != '{';) | |
3801 | check_source (++start_body < clean_text_limit, 0); | |
3802 | ||
3803 | end_formals_orig = orig_text_base + (end_formals - clean_text_base); | |
3804 | start_body_orig = orig_text_base + (start_body - clean_text_base); | |
3805 | scan = end_formals + 1; | |
3806 | scan_orig = end_formals_orig + 1; | |
3807 | for (; scan < start_body; scan++, scan_orig++) | |
3808 | { | |
3809 | if (*scan == *scan_orig) | |
3810 | { | |
3811 | have_newlines |= (*scan_orig == '\n'); | |
3812 | /* Leave identical whitespace alone. */ | |
e51712db | 3813 | if (!ISSPACE ((const unsigned char)*scan_orig)) |
5f8037c4 RS |
3814 | *((NONCONST char *)scan_orig) = ' '; /* identical - so whiteout */ |
3815 | } | |
3816 | else | |
f79e449b | 3817 | have_flotsam = 1; |
5f8037c4 | 3818 | } |
f79e449b | 3819 | if (have_flotsam) |
5f8037c4 RS |
3820 | output_bytes (end_formals_orig + 1, |
3821 | (size_t) (start_body_orig - end_formals_orig) - 1); | |
3822 | else | |
3823 | if (have_newlines) | |
3824 | output_string ("\n"); | |
3825 | else | |
3826 | output_string (" "); | |
3827 | clean_read_ptr = start_body - 1; | |
3828 | } | |
a019653e | 3829 | #endif /* !defined (UNPROTOIZE) */ |
5f8037c4 RS |
3830 | } |
3831 | ||
3832 | /* Clean up the clean text buffer. Do this by converting comments and | |
d17e26f7 | 3833 | preprocessing directives into spaces. Also convert line continuations |
5f8037c4 RS |
3834 | into whitespace. Also, whiteout string and character literals. */ |
3835 | ||
3836 | static void | |
34e56753 RS |
3837 | do_cleaning (new_clean_text_base, new_clean_text_limit) |
3838 | char *new_clean_text_base; | |
ffb9f2f1 | 3839 | const char *new_clean_text_limit; |
5f8037c4 RS |
3840 | { |
3841 | char *scan_p; | |
3842 | int non_whitespace_since_newline = 0; | |
3843 | ||
3844 | for (scan_p = new_clean_text_base; scan_p < new_clean_text_limit; scan_p++) | |
3845 | { | |
3846 | switch (*scan_p) | |
3847 | { | |
3848 | case '/': /* Handle comments. */ | |
3849 | if (scan_p[1] != '*') | |
3850 | goto regular; | |
3851 | non_whitespace_since_newline = 1; | |
3852 | scan_p[0] = ' '; | |
3853 | scan_p[1] = ' '; | |
3854 | scan_p += 2; | |
3855 | while (scan_p[1] != '/' || scan_p[0] != '*') | |
3856 | { | |
e51712db | 3857 | if (!ISSPACE ((const unsigned char)*scan_p)) |
5f8037c4 RS |
3858 | *scan_p = ' '; |
3859 | if (++scan_p >= new_clean_text_limit) | |
3860 | abort (); | |
3861 | } | |
3862 | *scan_p++ = ' '; | |
3863 | *scan_p = ' '; | |
3864 | break; | |
3865 | ||
3866 | case '#': /* Handle pp directives. */ | |
3867 | if (non_whitespace_since_newline) | |
3868 | goto regular; | |
3869 | *scan_p = ' '; | |
3870 | while (scan_p[1] != '\n' || scan_p[0] == '\\') | |
3871 | { | |
e51712db | 3872 | if (!ISSPACE ((const unsigned char)*scan_p)) |
5f8037c4 RS |
3873 | *scan_p = ' '; |
3874 | if (++scan_p >= new_clean_text_limit) | |
3875 | abort (); | |
3876 | } | |
3877 | *scan_p++ = ' '; | |
3878 | break; | |
3879 | ||
3880 | case '\'': /* Handle character literals. */ | |
3881 | non_whitespace_since_newline = 1; | |
3882 | while (scan_p[1] != '\'' || scan_p[0] == '\\') | |
3883 | { | |
e51712db KG |
3884 | if (scan_p[0] == '\\' |
3885 | && !ISSPACE ((const unsigned char)scan_p[1])) | |
5f8037c4 | 3886 | scan_p[1] = ' '; |
e51712db | 3887 | if (!ISSPACE ((const unsigned char)*scan_p)) |
5f8037c4 RS |
3888 | *scan_p = ' '; |
3889 | if (++scan_p >= new_clean_text_limit) | |
3890 | abort (); | |
3891 | } | |
3892 | *scan_p++ = ' '; | |
3893 | break; | |
3894 | ||
3895 | case '"': /* Handle string literals. */ | |
3896 | non_whitespace_since_newline = 1; | |
3897 | while (scan_p[1] != '"' || scan_p[0] == '\\') | |
3898 | { | |
e51712db KG |
3899 | if (scan_p[0] == '\\' |
3900 | && !ISSPACE ((const unsigned char)scan_p[1])) | |
5f8037c4 | 3901 | scan_p[1] = ' '; |
e51712db | 3902 | if (!ISSPACE ((const unsigned char)*scan_p)) |
5f8037c4 RS |
3903 | *scan_p = ' '; |
3904 | if (++scan_p >= new_clean_text_limit) | |
3905 | abort (); | |
3906 | } | |
e51712db | 3907 | if (!ISSPACE ((const unsigned char)*scan_p)) |
741d3d8a RK |
3908 | *scan_p = ' '; |
3909 | scan_p++; | |
5f8037c4 RS |
3910 | break; |
3911 | ||
3912 | case '\\': /* Handle line continuations. */ | |
3913 | if (scan_p[1] != '\n') | |
3914 | goto regular; | |
3915 | *scan_p = ' '; | |
3916 | break; | |
3917 | ||
3918 | case '\n': | |
3919 | non_whitespace_since_newline = 0; /* Reset. */ | |
3920 | break; | |
3921 | ||
3922 | case ' ': | |
3923 | case '\v': | |
3924 | case '\t': | |
3925 | case '\r': | |
3926 | case '\f': | |
3927 | case '\b': | |
3928 | break; /* Whitespace characters. */ | |
3929 | ||
3930 | default: | |
3931 | regular: | |
3932 | non_whitespace_since_newline = 1; | |
3933 | break; | |
3934 | } | |
3935 | } | |
3936 | } | |
3937 | ||
3938 | /* Given a pointer to the closing right parenthesis for a particular formals | |
3939 | list (in the clean text buffer) find the corresponding left parenthesis | |
3940 | and return a pointer to it. */ | |
3941 | ||
3942 | static const char * | |
34e56753 RS |
3943 | careful_find_l_paren (p) |
3944 | const char *p; | |
5f8037c4 RS |
3945 | { |
3946 | const char *q; | |
3947 | int paren_depth; | |
3948 | ||
3949 | for (paren_depth = 1, q = p-1; paren_depth; check_source (--q >= clean_text_base, 0)) | |
3950 | { | |
3951 | switch (*q) | |
3952 | { | |
3953 | case ')': | |
3954 | paren_depth++; | |
3955 | break; | |
3956 | case '(': | |
3957 | paren_depth--; | |
3958 | break; | |
3959 | } | |
3960 | } | |
3961 | return ++q; | |
3962 | } | |
3963 | ||
3964 | /* Scan the clean text buffer for cases of function definitions that we | |
3965 | don't really know about because they were preprocessed out when the | |
3966 | aux info files were created. | |
3967 | ||
3968 | In this version of protoize/unprotoize we just give a warning for each | |
3969 | one found. A later version may be able to at least unprotoize such | |
3970 | missed items. | |
3971 | ||
3972 | Note that we may easily find all function definitions simply by | |
3973 | looking for places where there is a left paren which is (ignoring | |
3974 | whitespace) immediately followed by either a left-brace or by an | |
3975 | upper or lower case letter. Whenever we find this combination, we | |
3976 | have also found a function definition header. | |
3977 | ||
3978 | Finding function *declarations* using syntactic clues is much harder. | |
3979 | I will probably try to do this in a later version though. */ | |
3980 | ||
3981 | static void | |
34e56753 RS |
3982 | scan_for_missed_items (file_p) |
3983 | const file_info *file_p; | |
5f8037c4 RS |
3984 | { |
3985 | static const char *scan_p; | |
3986 | const char *limit = clean_text_limit - 3; | |
3987 | static const char *backup_limit; | |
3988 | ||
3989 | backup_limit = clean_text_base - 1; | |
3990 | ||
3991 | for (scan_p = clean_text_base; scan_p < limit; scan_p++) | |
3992 | { | |
3993 | if (*scan_p == ')') | |
3994 | { | |
3995 | static const char *last_r_paren; | |
3996 | const char *ahead_p; | |
3997 | ||
3998 | last_r_paren = scan_p; | |
3999 | ||
e51712db | 4000 | for (ahead_p = scan_p + 1; ISSPACE ((const unsigned char)*ahead_p); ) |
5f8037c4 RS |
4001 | check_source (++ahead_p < limit, limit); |
4002 | ||
4003 | scan_p = ahead_p - 1; | |
4004 | ||
e51712db | 4005 | if (ISALPHA ((const unsigned char)*ahead_p) || *ahead_p == '{') |
5f8037c4 RS |
4006 | { |
4007 | const char *last_l_paren; | |
4008 | const int lineno = identify_lineno (ahead_p); | |
4009 | ||
4010 | if (setjmp (source_confusion_recovery)) | |
4011 | continue; | |
4012 | ||
4013 | /* We know we have a function definition header. Now skip | |
4014 | leftwards over all of its associated formals lists. */ | |
4015 | ||
4016 | do | |
4017 | { | |
4018 | last_l_paren = careful_find_l_paren (last_r_paren); | |
e51712db KG |
4019 | for (last_r_paren = last_l_paren-1; |
4020 | ISSPACE ((const unsigned char)*last_r_paren); ) | |
5f8037c4 RS |
4021 | check_source (--last_r_paren >= backup_limit, backup_limit); |
4022 | } | |
4023 | while (*last_r_paren == ')'); | |
4024 | ||
4025 | if (is_id_char (*last_r_paren)) | |
4026 | { | |
4027 | const char *id_limit = last_r_paren + 1; | |
4028 | const char *id_start; | |
4029 | size_t id_length; | |
4030 | const def_dec_info *dd_p; | |
4031 | ||
4032 | for (id_start = id_limit-1; is_id_char (*id_start); ) | |
4033 | check_source (--id_start >= backup_limit, backup_limit); | |
4034 | id_start++; | |
4035 | backup_limit = id_start; | |
4036 | if ((id_length = (size_t) (id_limit - id_start)) == 0) | |
4037 | goto not_missed; | |
4038 | ||
4039 | { | |
34e56753 | 4040 | char *func_name = (char *) alloca (id_length + 1); |
5f8037c4 | 4041 | static const char * const stmt_keywords[] |
c9d28865 | 4042 | = { "if", "else", "do", "while", "for", "switch", "case", "return", 0 }; |
5f8037c4 RS |
4043 | const char * const *stmt_keyword; |
4044 | ||
4045 | strncpy (func_name, id_start, id_length); | |
4046 | func_name[id_length] = '\0'; | |
4047 | ||
4048 | /* We must check here to see if we are actually looking at | |
4049 | a statement rather than an actual function call. */ | |
4050 | ||
4051 | for (stmt_keyword = stmt_keywords; *stmt_keyword; stmt_keyword++) | |
4052 | if (!strcmp (func_name, *stmt_keyword)) | |
4053 | goto not_missed; | |
4054 | ||
4055 | #if 0 | |
ab87f8c8 JL |
4056 | notice ("%s: found definition of `%s' at %s(%d)\n", |
4057 | pname, | |
4058 | func_name, | |
4059 | shortpath (NULL, file_p->hash_entry->symbol), | |
4060 | identify_lineno (id_start)); | |
5f8037c4 RS |
4061 | #endif /* 0 */ |
4062 | /* We really should check for a match of the function name | |
4063 | here also, but why bother. */ | |
4064 | ||
4065 | for (dd_p = file_p->defs_decs; dd_p; dd_p = dd_p->next_in_file) | |
4066 | if (dd_p->is_func_def && dd_p->line == lineno) | |
4067 | goto not_missed; | |
4068 | ||
4069 | /* If we make it here, then we did not know about this | |
4070 | function definition. */ | |
4071 | ||
ab87f8c8 JL |
4072 | notice ("%s: %d: warning: `%s' excluded by preprocessing\n", |
4073 | shortpath (NULL, file_p->hash_entry->symbol), | |
4074 | identify_lineno (id_start), func_name); | |
4075 | notice ("%s: function definition not converted\n", | |
4076 | pname); | |
5f8037c4 RS |
4077 | } |
4078 | not_missed: ; | |
4079 | } | |
4080 | } | |
4081 | } | |
4082 | } | |
4083 | } | |
4084 | ||
4085 | /* Do all editing operations for a single source file (either a "base" file | |
4086 | or an "include" file). To do this we read the file into memory, keep a | |
4087 | virgin copy there, make another cleaned in-core copy of the original file | |
d17e26f7 | 4088 | (i.e. one in which all of the comments and preprocessing directives have |
5f8037c4 RS |
4089 | been replaced with whitespace), then use these two in-core copies of the |
4090 | file to make a new edited in-core copy of the file. Finally, rename the | |
4091 | original file (as a way of saving it), and then write the edited version | |
4092 | of the file from core to a disk file of the same name as the original. | |
4093 | ||
4094 | Note that the trick of making a copy of the original sans comments & | |
d17e26f7 | 4095 | preprocessing directives make the editing a whole lot easier. */ |
5f8037c4 RS |
4096 | |
4097 | static void | |
34e56753 RS |
4098 | edit_file (hp) |
4099 | const hash_table_entry *hp; | |
5f8037c4 RS |
4100 | { |
4101 | struct stat stat_buf; | |
4102 | const file_info *file_p = hp->fip; | |
4103 | char *new_orig_text_base; | |
4104 | char *new_orig_text_limit; | |
4105 | char *new_clean_text_base; | |
4106 | char *new_clean_text_limit; | |
4107 | size_t orig_size; | |
4108 | size_t repl_size; | |
4109 | int first_definition_in_file; | |
4110 | ||
4111 | /* If we are not supposed to be converting this file, or if there is | |
4112 | nothing in there which needs converting, just skip this file. */ | |
4113 | ||
4114 | if (!needs_to_be_converted (file_p)) | |
4115 | return; | |
4116 | ||
a2b22788 | 4117 | convert_filename = file_p->hash_entry->symbol; |
5f8037c4 RS |
4118 | |
4119 | /* Convert a file if it is in a directory where we want conversion | |
4120 | and the file is not excluded. */ | |
4121 | ||
a2b22788 RS |
4122 | if (!directory_specified_p (convert_filename) |
4123 | || file_excluded_p (convert_filename)) | |
5f8037c4 RS |
4124 | { |
4125 | if (!quiet_flag | |
4126 | #ifdef UNPROTOIZE | |
4127 | /* Don't even mention "system" include files unless we are | |
4128 | protoizing. If we are protoizing, we mention these as a | |
1a2ef701 | 4129 | gentle way of prodding the user to convert his "system" |
5f8037c4 | 4130 | include files to prototype format. */ |
a2b22788 | 4131 | && !in_system_include_dir (convert_filename) |
a019653e | 4132 | #endif /* defined (UNPROTOIZE) */ |
5f8037c4 | 4133 | ) |
ab87f8c8 JL |
4134 | notice ("%s: `%s' not converted\n", |
4135 | pname, shortpath (NULL, convert_filename)); | |
5f8037c4 RS |
4136 | return; |
4137 | } | |
4138 | ||
4139 | /* Let the user know what we are up to. */ | |
4140 | ||
4141 | if (nochange_flag) | |
ab87f8c8 JL |
4142 | notice ("%s: would convert file `%s'\n", |
4143 | pname, shortpath (NULL, convert_filename)); | |
5f8037c4 | 4144 | else |
ab87f8c8 JL |
4145 | notice ("%s: converting file `%s'\n", |
4146 | pname, shortpath (NULL, convert_filename)); | |
34e56753 | 4147 | fflush (stderr); |
5f8037c4 RS |
4148 | |
4149 | /* Find out the size (in bytes) of the original file. */ | |
4150 | ||
a2b22788 | 4151 | /* The cast avoids an erroneous warning on AIX. */ |
ffb9f2f1 | 4152 | if (stat (convert_filename, &stat_buf) == -1) |
5f8037c4 | 4153 | { |
e5e809f4 | 4154 | int errno_val = errno; |
ab87f8c8 JL |
4155 | notice ("%s: can't get status for file `%s': %s\n", |
4156 | pname, shortpath (NULL, convert_filename), | |
4157 | xstrerror (errno_val)); | |
5f8037c4 RS |
4158 | return; |
4159 | } | |
4160 | orig_size = stat_buf.st_size; | |
4161 | ||
4162 | /* Allocate a buffer to hold the original text. */ | |
4163 | ||
4164 | orig_text_base = new_orig_text_base = (char *) xmalloc (orig_size + 2); | |
4165 | orig_text_limit = new_orig_text_limit = new_orig_text_base + orig_size; | |
4166 | ||
4167 | /* Allocate a buffer to hold the cleaned-up version of the original text. */ | |
4168 | ||
4169 | clean_text_base = new_clean_text_base = (char *) xmalloc (orig_size + 2); | |
4170 | clean_text_limit = new_clean_text_limit = new_clean_text_base + orig_size; | |
4171 | clean_read_ptr = clean_text_base - 1; | |
4172 | ||
4173 | /* Allocate a buffer that will hopefully be large enough to hold the entire | |
4174 | converted output text. As an initial guess for the maximum size of the | |
4175 | output buffer, use 125% of the size of the original + some extra. This | |
4176 | buffer can be expanded later as needed. */ | |
4177 | ||
4178 | repl_size = orig_size + (orig_size >> 2) + 4096; | |
4179 | repl_text_base = (char *) xmalloc (repl_size + 2); | |
4180 | repl_text_limit = repl_text_base + repl_size - 1; | |
4181 | repl_write_ptr = repl_text_base - 1; | |
4182 | ||
4183 | { | |
4184 | int input_file; | |
ee77eda5 | 4185 | int fd_flags; |
5f8037c4 RS |
4186 | |
4187 | /* Open the file to be converted in READ ONLY mode. */ | |
4188 | ||
ee77eda5 MK |
4189 | fd_flags = O_RDONLY; |
4190 | #ifdef O_BINARY | |
4191 | /* Use binary mode to avoid having to deal with different EOL characters. */ | |
4192 | fd_flags |= O_BINARY; | |
4193 | #endif | |
4194 | if ((input_file = open (convert_filename, fd_flags, 0444)) == -1) | |
5f8037c4 | 4195 | { |
e5e809f4 | 4196 | int errno_val = errno; |
ab87f8c8 JL |
4197 | notice ("%s: can't open file `%s' for reading: %s\n", |
4198 | pname, shortpath (NULL, convert_filename), | |
4199 | xstrerror (errno_val)); | |
5f8037c4 RS |
4200 | return; |
4201 | } | |
4202 | ||
4203 | /* Read the entire original source text file into the original text buffer | |
4204 | in one swell fwoop. Then figure out where the end of the text is and | |
4205 | make sure that it ends with a newline followed by a null. */ | |
4206 | ||
e51712db KG |
4207 | if (safe_read (input_file, new_orig_text_base, orig_size) != |
4208 | (int) orig_size) | |
5f8037c4 | 4209 | { |
e5e809f4 | 4210 | int errno_val = errno; |
5f8037c4 | 4211 | close (input_file); |
ab87f8c8 JL |
4212 | notice ("\n%s: error reading input file `%s': %s\n", |
4213 | pname, shortpath (NULL, convert_filename), | |
4214 | xstrerror (errno_val)); | |
5f8037c4 RS |
4215 | return; |
4216 | } | |
4217 | ||
4218 | close (input_file); | |
4219 | } | |
4220 | ||
4221 | if (orig_size == 0 || orig_text_limit[-1] != '\n') | |
4222 | { | |
4223 | *new_orig_text_limit++ = '\n'; | |
4224 | orig_text_limit++; | |
4225 | } | |
4226 | ||
4227 | /* Create the cleaned up copy of the original text. */ | |
4228 | ||
4229 | memcpy (new_clean_text_base, orig_text_base, | |
4230 | (size_t) (orig_text_limit - orig_text_base)); | |
4231 | do_cleaning (new_clean_text_base, new_clean_text_limit); | |
4232 | ||
4233 | #if 0 | |
4234 | { | |
4235 | int clean_file; | |
4236 | size_t clean_size = orig_text_limit - orig_text_base; | |
a2b22788 | 4237 | char *const clean_filename = (char *) alloca (strlen (convert_filename) + 6 + 1); |
5f8037c4 RS |
4238 | |
4239 | /* Open (and create) the clean file. */ | |
4240 | ||
a2b22788 RS |
4241 | strcpy (clean_filename, convert_filename); |
4242 | strcat (clean_filename, ".clean"); | |
4243 | if ((clean_file = creat (clean_filename, 0666)) == -1) | |
5f8037c4 | 4244 | { |
e5e809f4 | 4245 | int errno_val = errno; |
ab87f8c8 JL |
4246 | notice ("%s: can't create/open clean file `%s': %s\n", |
4247 | pname, shortpath (NULL, clean_filename), | |
4248 | xstrerror (errno_val)); | |
5f8037c4 RS |
4249 | return; |
4250 | } | |
4251 | ||
4252 | /* Write the clean file. */ | |
4253 | ||
fa0cd3ff | 4254 | safe_write (clean_file, new_clean_text_base, clean_size, clean_filename); |
5f8037c4 RS |
4255 | |
4256 | close (clean_file); | |
4257 | } | |
4258 | #endif /* 0 */ | |
4259 | ||
4260 | /* Do a simplified scan of the input looking for things that were not | |
4261 | mentioned in the aux info files because of the fact that they were | |
4262 | in a region of the source which was preprocessed-out (via #if or | |
4263 | via #ifdef). */ | |
4264 | ||
4265 | scan_for_missed_items (file_p); | |
4266 | ||
4267 | /* Setup to do line-oriented forward seeking in the clean text buffer. */ | |
4268 | ||
4269 | last_known_line_number = 1; | |
4270 | last_known_line_start = clean_text_base; | |
4271 | ||
4272 | /* Now get down to business and make all of the necessary edits. */ | |
4273 | ||
4274 | { | |
4275 | const def_dec_info *def_dec_p; | |
4276 | ||
4277 | first_definition_in_file = 1; | |
4278 | def_dec_p = file_p->defs_decs; | |
4279 | for (; def_dec_p; def_dec_p = def_dec_p->next_in_file) | |
4280 | { | |
4281 | const char *clean_text_p = seek_to_line (def_dec_p->line); | |
4282 | ||
4283 | /* clean_text_p now points to the first character of the line which | |
4284 | contains the `terminator' for the declaration or definition that | |
4285 | we are about to process. */ | |
4286 | ||
4287 | #ifndef UNPROTOIZE | |
4288 | ||
4289 | if (global_flag && def_dec_p->is_func_def && first_definition_in_file) | |
4290 | { | |
4291 | add_global_decls (def_dec_p->file, clean_text_p); | |
4292 | first_definition_in_file = 0; | |
4293 | } | |
4294 | ||
4295 | /* Don't edit this item if it is already in prototype format or if it | |
4296 | is a function declaration and we have found no corresponding | |
4297 | definition. */ | |
4298 | ||
4299 | if (def_dec_p->prototyped | |
4300 | || (!def_dec_p->is_func_def && !def_dec_p->definition)) | |
4301 | continue; | |
4302 | ||
a019653e | 4303 | #endif /* !defined (UNPROTOIZE) */ |
5f8037c4 RS |
4304 | |
4305 | if (def_dec_p->is_func_def) | |
4306 | edit_fn_definition (def_dec_p, clean_text_p); | |
4307 | else | |
4308 | #ifndef UNPROTOIZE | |
4309 | if (def_dec_p->is_implicit) | |
4310 | add_local_decl (def_dec_p, clean_text_p); | |
4311 | else | |
a019653e | 4312 | #endif /* !defined (UNPROTOIZE) */ |
5f8037c4 RS |
4313 | edit_fn_declaration (def_dec_p, clean_text_p); |
4314 | } | |
4315 | } | |
4316 | ||
4317 | /* Finalize things. Output the last trailing part of the original text. */ | |
4318 | ||
4319 | output_up_to (clean_text_limit - 1); | |
4320 | ||
4321 | /* If this is just a test run, stop now and just deallocate the buffers. */ | |
4322 | ||
4323 | if (nochange_flag) | |
4324 | { | |
4325 | free (new_orig_text_base); | |
4326 | free (new_clean_text_base); | |
4327 | free (repl_text_base); | |
4328 | return; | |
4329 | } | |
4330 | ||
4331 | /* Change the name of the original input file. This is just a quick way of | |
4332 | saving the original file. */ | |
4333 | ||
4334 | if (!nosave_flag) | |
4335 | { | |
db3cf6fb MS |
4336 | char *new_filename |
4337 | = (char *) xmalloc (strlen (convert_filename) + strlen (save_suffix) + 2); | |
5f8037c4 | 4338 | |
a2b22788 | 4339 | strcpy (new_filename, convert_filename); |
a7db8bbb MK |
4340 | #ifdef __MSDOS__ |
4341 | /* MSDOS filenames are restricted to 8.3 format, so we save `foo.c' | |
4342 | as `foo.<save_suffix>'. */ | |
4343 | new_filename[(strlen (convert_filename) - 1] = '\0'; | |
4344 | #endif | |
a2b22788 | 4345 | strcat (new_filename, save_suffix); |
ee77eda5 MK |
4346 | |
4347 | /* Don't overwrite existing file. */ | |
4348 | if (access (new_filename, F_OK) == 0) | |
4349 | { | |
4350 | if (!quiet_flag) | |
4351 | notice ("%s: warning: file `%s' already saved in `%s'\n", | |
4352 | pname, | |
4353 | shortpath (NULL, convert_filename), | |
4354 | shortpath (NULL, new_filename)); | |
4355 | } | |
4356 | else if (rename (convert_filename, new_filename) == -1) | |
5f8037c4 | 4357 | { |
e5e809f4 | 4358 | int errno_val = errno; |
ee77eda5 MK |
4359 | notice ("%s: can't link file `%s' to `%s': %s\n", |
4360 | pname, | |
4361 | shortpath (NULL, convert_filename), | |
4362 | shortpath (NULL, new_filename), | |
4363 | xstrerror (errno_val)); | |
4364 | return; | |
5f8037c4 RS |
4365 | } |
4366 | } | |
4367 | ||
ffb9f2f1 | 4368 | if (unlink (convert_filename) == -1) |
5f8037c4 | 4369 | { |
e5e809f4 | 4370 | int errno_val = errno; |
ee77eda5 MK |
4371 | /* The file may have already been renamed. */ |
4372 | if (errno_val != ENOENT) | |
4373 | { | |
4374 | notice ("%s: can't delete file `%s': %s\n", | |
4375 | pname, shortpath (NULL, convert_filename), | |
4376 | xstrerror (errno_val)); | |
4377 | return; | |
4378 | } | |
5f8037c4 RS |
4379 | } |
4380 | ||
4381 | { | |
4382 | int output_file; | |
4383 | ||
4384 | /* Open (and create) the output file. */ | |
4385 | ||
a2b22788 | 4386 | if ((output_file = creat (convert_filename, 0666)) == -1) |
5f8037c4 | 4387 | { |
e5e809f4 | 4388 | int errno_val = errno; |
ab87f8c8 JL |
4389 | notice ("%s: can't create/open output file `%s': %s\n", |
4390 | pname, shortpath (NULL, convert_filename), | |
4391 | xstrerror (errno_val)); | |
5f8037c4 RS |
4392 | return; |
4393 | } | |
ee77eda5 MK |
4394 | #ifdef O_BINARY |
4395 | /* Use binary mode to avoid changing the existing EOL character. */ | |
4396 | setmode (output_file, O_BINARY); | |
4397 | #endif | |
5f8037c4 RS |
4398 | |
4399 | /* Write the output file. */ | |
4400 | ||
4401 | { | |
4402 | unsigned int out_size = (repl_write_ptr + 1) - repl_text_base; | |
4403 | ||
fa0cd3ff | 4404 | safe_write (output_file, repl_text_base, out_size, convert_filename); |
5f8037c4 RS |
4405 | } |
4406 | ||
4407 | close (output_file); | |
4408 | } | |
4409 | ||
4410 | /* Deallocate the conversion buffers. */ | |
4411 | ||
4412 | free (new_orig_text_base); | |
4413 | free (new_clean_text_base); | |
4414 | free (repl_text_base); | |
4415 | ||
4416 | /* Change the mode of the output file to match the original file. */ | |
4417 | ||
a2b22788 | 4418 | /* The cast avoids an erroneous warning on AIX. */ |
ffb9f2f1 | 4419 | if (chmod (convert_filename, stat_buf.st_mode) == -1) |
e5e809f4 JL |
4420 | { |
4421 | int errno_val = errno; | |
ab87f8c8 JL |
4422 | notice ("%s: can't change mode of file `%s': %s\n", |
4423 | pname, shortpath (NULL, convert_filename), | |
4424 | xstrerror (errno_val)); | |
e5e809f4 | 4425 | } |
5f8037c4 RS |
4426 | |
4427 | /* Note: We would try to change the owner and group of the output file | |
4428 | to match those of the input file here, except that may not be a good | |
4429 | thing to do because it might be misleading. Also, it might not even | |
4430 | be possible to do that (on BSD systems with quotas for instance). */ | |
4431 | } | |
4432 | ||
4433 | /* Do all of the individual steps needed to do the protoization (or | |
4434 | unprotoization) of the files referenced in the aux_info files given | |
4435 | in the command line. */ | |
4436 | ||
4437 | static void | |
34e56753 | 4438 | do_processing () |
5f8037c4 RS |
4439 | { |
4440 | const char * const *base_pp; | |
a2b22788 RS |
4441 | const char * const * const end_pps |
4442 | = &base_source_filenames[n_base_source_files]; | |
5f8037c4 RS |
4443 | |
4444 | #ifndef UNPROTOIZE | |
4445 | int syscalls_len; | |
a019653e | 4446 | #endif /* !defined (UNPROTOIZE) */ |
5f8037c4 RS |
4447 | |
4448 | /* One-by-one, check (and create if necessary), open, and read all of the | |
4449 | stuff in each aux_info file. After reading each aux_info file, the | |
4450 | aux_info_file just read will be automatically deleted unless the | |
4451 | keep_flag is set. */ | |
4452 | ||
a2b22788 | 4453 | for (base_pp = base_source_filenames; base_pp < end_pps; base_pp++) |
5f8037c4 RS |
4454 | process_aux_info_file (*base_pp, keep_flag, 0); |
4455 | ||
4456 | #ifndef UNPROTOIZE | |
4457 | ||
4458 | /* Also open and read the special SYSCALLS.c aux_info file which gives us | |
4459 | the prototypes for all of the standard system-supplied functions. */ | |
4460 | ||
4461 | if (nondefault_syscalls_dir) | |
4462 | { | |
a2b22788 | 4463 | syscalls_absolute_filename |
d059a239 FF |
4464 | = (char *) xmalloc (strlen (nondefault_syscalls_dir) + 1 |
4465 | + sizeof (syscalls_filename)); | |
a2b22788 | 4466 | strcpy (syscalls_absolute_filename, nondefault_syscalls_dir); |
5f8037c4 RS |
4467 | } |
4468 | else | |
4469 | { | |
d059a239 FF |
4470 | GET_ENV_PATH_LIST (default_syscalls_dir, "GCC_EXEC_PREFIX"); |
4471 | if (!default_syscalls_dir) | |
4472 | { | |
4473 | default_syscalls_dir = standard_exec_prefix; | |
4474 | } | |
a2b22788 | 4475 | syscalls_absolute_filename |
d059a239 FF |
4476 | = (char *) xmalloc (strlen (default_syscalls_dir) + 0 |
4477 | + strlen (target_machine) + 1 | |
4478 | + strlen (target_version) + 1 | |
4479 | + sizeof (syscalls_filename)); | |
a2b22788 | 4480 | strcpy (syscalls_absolute_filename, default_syscalls_dir); |
d059a239 FF |
4481 | strcat (syscalls_absolute_filename, target_machine); |
4482 | strcat (syscalls_absolute_filename, "/"); | |
4483 | strcat (syscalls_absolute_filename, target_version); | |
4484 | strcat (syscalls_absolute_filename, "/"); | |
5f8037c4 RS |
4485 | } |
4486 | ||
34e56753 | 4487 | syscalls_len = strlen (syscalls_absolute_filename); |
ee77eda5 | 4488 | if (! IS_DIR_SEPARATOR (*(syscalls_absolute_filename + syscalls_len - 1))) |
5f8037c4 | 4489 | { |
ee77eda5 | 4490 | *(syscalls_absolute_filename + syscalls_len++) = DIR_SEPARATOR; |
a2b22788 | 4491 | *(syscalls_absolute_filename + syscalls_len) = '\0'; |
5f8037c4 | 4492 | } |
a2b22788 | 4493 | strcat (syscalls_absolute_filename, syscalls_filename); |
5f8037c4 RS |
4494 | |
4495 | /* Call process_aux_info_file in such a way that it does not try to | |
4496 | delete the SYSCALLS aux_info file. */ | |
4497 | ||
a2b22788 | 4498 | process_aux_info_file (syscalls_absolute_filename, 1, 1); |
5f8037c4 | 4499 | |
a019653e | 4500 | #endif /* !defined (UNPROTOIZE) */ |
5f8037c4 RS |
4501 | |
4502 | /* When we first read in all of the information from the aux_info files | |
6dc42e49 | 4503 | we saved in it descending line number order, because that was likely to |
5f8037c4 RS |
4504 | be faster. Now however, we want the chains of def & dec records to |
4505 | appear in ascending line number order as we get further away from the | |
4506 | file_info record that they hang from. The following line causes all of | |
4507 | these lists to be rearranged into ascending line number order. */ | |
4508 | ||
a2b22788 | 4509 | visit_each_hash_node (filename_primary, reverse_def_dec_list); |
5f8037c4 RS |
4510 | |
4511 | #ifndef UNPROTOIZE | |
4512 | ||
4513 | /* Now do the "real" work. The following line causes each declaration record | |
4514 | to be "visited". For each of these nodes, an attempt is made to match | |
4515 | up the function declaration with a corresponding function definition, | |
4516 | which should have a full prototype-format formals list with it. Once | |
4517 | these match-ups are made, the conversion of the function declarations | |
4518 | to prototype format can be made. */ | |
4519 | ||
4520 | visit_each_hash_node (function_name_primary, connect_defs_and_decs); | |
4521 | ||
a019653e | 4522 | #endif /* !defined (UNPROTOIZE) */ |
5f8037c4 RS |
4523 | |
4524 | /* Now convert each file that can be converted (and needs to be). */ | |
4525 | ||
a2b22788 | 4526 | visit_each_hash_node (filename_primary, edit_file); |
5f8037c4 RS |
4527 | |
4528 | #ifndef UNPROTOIZE | |
4529 | ||
4530 | /* If we are working in cplusplus mode, try to rename all .c files to .C | |
4531 | files. Don't panic if some of the renames don't work. */ | |
4532 | ||
4533 | if (cplusplus_flag && !nochange_flag) | |
a2b22788 | 4534 | visit_each_hash_node (filename_primary, rename_c_file); |
5f8037c4 | 4535 | |
a019653e | 4536 | #endif /* !defined (UNPROTOIZE) */ |
5f8037c4 RS |
4537 | } |
4538 | \f | |
4539 | static struct option longopts[] = | |
4540 | { | |
4541 | {"version", 0, 0, 'V'}, | |
a019653e | 4542 | {"file_name", 0, 0, 'p'}, |
5f8037c4 RS |
4543 | {"quiet", 0, 0, 'q'}, |
4544 | {"silent", 0, 0, 'q'}, | |
4545 | {"force", 0, 0, 'f'}, | |
4546 | {"keep", 0, 0, 'k'}, | |
4547 | {"nosave", 0, 0, 'N'}, | |
4548 | {"nochange", 0, 0, 'n'}, | |
4549 | {"compiler-options", 1, 0, 'c'}, | |
4550 | {"exclude", 1, 0, 'x'}, | |
4551 | {"directory", 1, 0, 'd'}, | |
4552 | #ifdef UNPROTOIZE | |
4553 | {"indent", 1, 0, 'i'}, | |
4554 | #else | |
4555 | {"local", 0, 0, 'l'}, | |
4556 | {"global", 0, 0, 'g'}, | |
4557 | {"c++", 0, 0, 'C'}, | |
4558 | {"syscalls-dir", 1, 0, 'B'}, | |
4559 | #endif | |
4560 | {0, 0, 0, 0} | |
4561 | }; | |
4562 | ||
ffb9f2f1 KG |
4563 | extern int main PARAMS ((int, char **const)); |
4564 | ||
5f8037c4 | 4565 | int |
34e56753 RS |
4566 | main (argc, argv) |
4567 | int argc; | |
4568 | char **const argv; | |
5f8037c4 RS |
4569 | { |
4570 | int longind; | |
4571 | int c; | |
f5188608 | 4572 | const char *params = ""; |
5f8037c4 | 4573 | |
ee77eda5 MK |
4574 | pname = strrchr (argv[0], DIR_SEPARATOR); |
4575 | #ifdef DIR_SEPARATOR_2 | |
4576 | { | |
4577 | char *slash; | |
4578 | ||
4579 | slash = strrchr (pname ? pname : argv[0], DIR_SEPARATOR_2); | |
4580 | if (slash) | |
4581 | pname = slash; | |
4582 | } | |
4583 | #endif | |
5f8037c4 RS |
4584 | pname = pname ? pname+1 : argv[0]; |
4585 | ||
d9b53430 | 4586 | #ifdef HAVE_LC_MESSAGES |
ab87f8c8 | 4587 | setlocale (LC_MESSAGES, ""); |
d9b53430 | 4588 | #endif |
735396d9 KG |
4589 | (void) bindtextdomain (PACKAGE, localedir); |
4590 | (void) textdomain (PACKAGE); | |
ab87f8c8 | 4591 | |
2e494f70 RS |
4592 | cwd_buffer = getpwd (); |
4593 | if (!cwd_buffer) | |
5f8037c4 | 4594 | { |
ab87f8c8 JL |
4595 | notice ("%s: cannot get working directory: %s\n", |
4596 | pname, xstrerror(errno)); | |
ffb9f2f1 | 4597 | return (FATAL_EXIT_CODE); |
5f8037c4 RS |
4598 | } |
4599 | ||
4600 | /* By default, convert the files in the current directory. */ | |
4601 | directory_list = string_list_cons (cwd_buffer, NULL); | |
4602 | ||
4603 | while ((c = getopt_long (argc, argv, | |
4604 | #ifdef UNPROTOIZE | |
ea8fd45e | 4605 | "c:d:i:knNp:qvVx:", |
5f8037c4 | 4606 | #else |
ea8fd45e | 4607 | "B:c:Cd:gklnNp:qvVx:", |
5f8037c4 RS |
4608 | #endif |
4609 | longopts, &longind)) != EOF) | |
4610 | { | |
0f41302f | 4611 | if (c == 0) /* Long option. */ |
5f8037c4 RS |
4612 | c = longopts[longind].val; |
4613 | switch (c) | |
4614 | { | |
ef91d7e2 | 4615 | case 'p': |
a019653e | 4616 | compiler_file_name = optarg; |
ef91d7e2 | 4617 | break; |
5f8037c4 RS |
4618 | case 'd': |
4619 | directory_list | |
4620 | = string_list_cons (abspath (NULL, optarg), directory_list); | |
4621 | break; | |
4622 | case 'x': | |
4623 | exclude_list = string_list_cons (optarg, exclude_list); | |
4624 | break; | |
4625 | ||
ea8fd45e | 4626 | case 'v': |
5f8037c4 RS |
4627 | case 'V': |
4628 | version_flag = 1; | |
4629 | break; | |
4630 | case 'q': | |
4631 | quiet_flag = 1; | |
4632 | break; | |
4633 | #if 0 | |
4634 | case 'f': | |
4635 | force_flag = 1; | |
4636 | break; | |
4637 | #endif | |
4638 | case 'n': | |
4639 | nochange_flag = 1; | |
4640 | keep_flag = 1; | |
4641 | break; | |
4642 | case 'N': | |
4643 | nosave_flag = 1; | |
4644 | break; | |
4645 | case 'k': | |
4646 | keep_flag = 1; | |
4647 | break; | |
4648 | case 'c': | |
a609bfc6 | 4649 | params = optarg; |
5f8037c4 RS |
4650 | break; |
4651 | #ifdef UNPROTOIZE | |
4652 | case 'i': | |
4653 | indent_string = optarg; | |
4654 | break; | |
a019653e | 4655 | #else /* !defined (UNPROTOIZE) */ |
5f8037c4 RS |
4656 | case 'l': |
4657 | local_flag = 1; | |
4658 | break; | |
4659 | case 'g': | |
4660 | global_flag = 1; | |
4661 | break; | |
4662 | case 'C': | |
4663 | cplusplus_flag = 1; | |
4664 | break; | |
4665 | case 'B': | |
4666 | nondefault_syscalls_dir = optarg; | |
4667 | break; | |
a019653e | 4668 | #endif /* !defined (UNPROTOIZE) */ |
5f8037c4 RS |
4669 | default: |
4670 | usage (); | |
4671 | } | |
4672 | } | |
4673 | ||
a609bfc6 RS |
4674 | /* Set up compile_params based on -p and -c options. */ |
4675 | munge_compile_params (params); | |
4676 | ||
a2b22788 | 4677 | n_base_source_files = argc - optind; |
5f8037c4 | 4678 | |
a2b22788 | 4679 | /* Now actually make a list of the base source filenames. */ |
5f8037c4 | 4680 | |
db3cf6fb MS |
4681 | base_source_filenames |
4682 | = (const char **) xmalloc ((n_base_source_files + 1) * sizeof (char *)); | |
a2b22788 | 4683 | n_base_source_files = 0; |
5f8037c4 RS |
4684 | for (; optind < argc; optind++) |
4685 | { | |
4686 | const char *path = abspath (NULL, argv[optind]); | |
4687 | int len = strlen (path); | |
4688 | ||
4689 | if (path[len-1] == 'c' && path[len-2] == '.') | |
a2b22788 | 4690 | base_source_filenames[n_base_source_files++] = path; |
5f8037c4 RS |
4691 | else |
4692 | { | |
ab87f8c8 JL |
4693 | notice ("%s: input file names must have .c suffixes: %s\n", |
4694 | pname, shortpath (NULL, path)); | |
5f8037c4 RS |
4695 | errors++; |
4696 | } | |
4697 | } | |
4698 | ||
4699 | #ifndef UNPROTOIZE | |
4700 | /* We are only interested in the very first identifier token in the | |
4701 | definition of `va_list', so if there is more junk after that first | |
4702 | identifier token, delete it from the `varargs_style_indicator'. */ | |
4703 | { | |
4704 | const char *cp; | |
4705 | ||
e51712db KG |
4706 | for (cp = varargs_style_indicator; |
4707 | ISALNUM ((const unsigned char)*cp) || *cp == '_'; cp++) | |
5f8037c4 RS |
4708 | continue; |
4709 | if (*cp != 0) | |
4710 | varargs_style_indicator = savestring (varargs_style_indicator, | |
4711 | cp - varargs_style_indicator); | |
4712 | } | |
a019653e | 4713 | #endif /* !defined (UNPROTOIZE) */ |
5f8037c4 RS |
4714 | |
4715 | if (errors) | |
4716 | usage (); | |
4717 | else | |
4718 | { | |
4719 | if (version_flag) | |
4720 | fprintf (stderr, "%s: %s\n", pname, version_string); | |
4721 | do_processing (); | |
4722 | } | |
2e3f9f3d | 4723 | |
ffb9f2f1 | 4724 | return (errors ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE); |
5f8037c4 | 4725 | } |