/* fix-header.c - Make C header file suitable for C++.
- Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998,
- 1999, 2000 Free Software Foundation, Inc.
+ Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
+ 2002, 2003, 2004, 2006, 2007 Free Software Foundation, Inc.
-This program is free software; you can redistribute it and/or modify it
-under the terms of the GNU General Public License as published by the
-Free Software Foundation; either version 2, or (at your option) any
-later version.
+ This program is free software; you can redistribute it and/or modify it
+ under the terms of the GNU General Public License as published by the
+ Free Software Foundation; either version 3, or (at your option) any
+ later version.
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
+ You should have received a copy of the GNU General Public License
+ along with this program; see the file COPYING3. If not see
+ <http://www.gnu.org/licenses/>. */
/* This program massages a system include file (such as stdio.h),
into a form that is compatible with GNU C and GNU C++.
* INFILE.H is a full pathname for the input file (e.g. /usr/include/stdio.h)
* OUTFILE.H is the full pathname for where to write the output file,
if anything needs to be done. (e.g. ./include/stdio.h)
- * OPTIONS are such as you would pass to cpp.
+ * OPTIONS can be -D or -I switches as you would pass to cpp.
Written by Per Bothner <bothner@cygnus.com>, July 1993. */
-#include "hconfig.h"
+#include "bconfig.h"
#include "system.h"
+#include "coretypes.h"
+#include "tm.h"
#include "obstack.h"
#include "scan.h"
#include "cpplib.h"
+#include "incpath.h"
+#include "errors.h"
-static void v_fatal PARAMS ((const char *, va_list)) ATTRIBUTE_PRINTF (1,0) ATTRIBUTE_NORETURN;
-static void fatal PARAMS ((const char *, ...)) ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN;
+#ifdef TARGET_EXTRA_INCLUDES
+void
+TARGET_EXTRA_INCLUDES (const char *sysroot ATTRIBUTE_UNUSED,
+ const char *iprefix ATTRIBUTE_UNUSED,
+ int stdinc ATTRIBUTE_UNUSED)
+{
+}
+#endif
+
+#ifdef TARGET_EXTRA_PRE_INCLUDES
+void
+TARGET_EXTRA_PRE_INCLUDES (const char *sysroot ATTRIBUTE_UNUSED,
+ const char *iprefix ATTRIBUTE_UNUSED,
+ int stdinc ATTRIBUTE_UNUSED)
+{
+}
+#endif
+
+struct line_maps line_table;
sstring buf;
int partial_count = 0;
int warnings = 0;
-/* We no longer need to add extern "C", because cpp implicitly
- forces the standard include files to be treated as C. */
-/*#define ADD_MISSING_EXTERN_C 1 */
-
#if ADD_MISSING_EXTERN_C
int missing_extern_C_count = 0;
#endif
directory. (It might be more efficient to do directory pruning
earlier in fixproto, but this is simpler and easier to customize.) */
-static char *files_to_ignore[] = {
+static const char *const files_to_ignore[] = {
"X11/",
FIXPROTO_IGNORE_LIST
0
struct symbol_list symbol_table[SYMBOL_TABLE_SIZE];
int cur_symbol_table_size;
-static void add_symbols PARAMS ((symbol_flags, namelist));
-static struct fn_decl *lookup_std_proto PARAMS ((const char *, int));
-static void write_lbrac PARAMS ((void));
-static void recognized_macro PARAMS ((const char *));
-static void check_macro_names PARAMS ((cpp_reader *, namelist));
-static void read_scan_file PARAMS ((char *, int, char **));
-static void write_rbrac PARAMS ((void));
-static int inf_skip_spaces PARAMS ((int));
-static int inf_read_upto PARAMS ((sstring *, int));
-static int inf_scan_ident PARAMS ((sstring *, int));
-static int check_protection PARAMS ((int *, int *));
-static void cb_change_file PARAMS ((cpp_reader *, const cpp_file_change *));
+static void add_symbols (symbol_flags, namelist);
+static struct fn_decl *lookup_std_proto (const char *, int);
+static void write_lbrac (void);
+static void recognized_macro (const char *);
+static void check_macro_names (cpp_reader *, namelist);
+static void read_scan_file (char *, int, char **);
+static void write_rbrac (void);
+static int inf_skip_spaces (int);
+static int inf_read_upto (sstring *, int);
+static int inf_scan_ident (sstring *, int);
+static int check_protection (int *, int *);
+static void cb_file_change (cpp_reader *, const struct line_map *);
static void
-add_symbols (flags, names)
- symbol_flags flags;
- namelist names;
+add_symbols (symbol_flags flags, namelist names)
{
symbol_table[cur_symbol_table_size].flags = flags;
symbol_table[cur_symbol_table_size].names = names;
}
struct std_include_entry {
- const char *name;
- symbol_flags flags;
- namelist names;
+ const char *const name;
+ const symbol_flags flags;
+ const namelist names;
};
const char NONE[] = ""; /* The empty namelist. */
/* Special name to indicate a continuation line in std_include_table. */
const char CONTINUED[] = "";
-struct std_include_entry *include_entry;
+const struct std_include_entry *include_entry;
-struct std_include_entry std_include_table [] = {
+const struct std_include_entry std_include_table [] = {
{ "ctype.h", ANSI_SYMBOL,
"isalnum\0isalpha\0iscntrl\0isdigit\0isgraph\0islower\0\
isprint\0ispunct\0isspace\0isupper\0isxdigit\0tolower\0toupper\0" },
sigfillset sigismember sigpending sigprocmask sigsuspend"
because these need sigset_t or struct sigaction.
Most systems that provide them will also declare them. */
- { "signal.h", ANSI_SYMBOL, "kill\0raise\0" },
+ { "signal.h", ANSI_SYMBOL, "raise\0" },
+ { CONTINUED, POSIX1_SYMBOL, "kill\0" },
{ "stdio.h", ANSI_SYMBOL,
"clearerr\0fclose\0feof\0ferror\0fflush\0fgetc\0fgetpos\0\
Should perhaps also add NULL */
{ "stdlib.h", ANSI_SYMBOL,
"abort\0abs\0atexit\0atof\0atoi\0atol\0bsearch\0calloc\0\
-exit\0free\0getenv\0labs\0malloc\0putenv\0qsort\0rand\0realloc\0\
+exit\0free\0getenv\0labs\0malloc\0qsort\0rand\0realloc\0\
srand\0strtod\0strtol\0strtoul\0system\0" },
{ CONTINUED, ANSI_SYMBOL|MACRO_SYMBOL, "EXIT_FAILURE\0EXIT_SUCCESS\0" },
+ { CONTINUED, POSIX1_SYMBOL, "putenv\0" },
{ "string.h", ANSI_SYMBOL, "memchr\0memcmp\0memcpy\0memmove\0memset\0\
strcat\0strchr\0strcmp\0strcoll\0strcpy\0strcspn\0strerror\0\
"cfgetispeed\0cfgetospeed\0cfsetispeed\0cfsetospeed\0tcdrain\0tcflow\0tcflush\0tcgetattr\0tcsendbreak\0tcsetattr\0" },
{ "time.h", ANSI_SYMBOL,
- "asctime\0clock\0ctime\0difftime\0gmtime\0localtime\0mktime\0strftime\0time\0tzset\0" },
+ "asctime\0clock\0ctime\0difftime\0gmtime\0localtime\0mktime\0strftime\0time\0" },
+ { CONTINUED, POSIX1_SYMBOL, "tzset\0" },
{ "unistd.h", POSIX1_SYMBOL,
"_exit\0access\0alarm\0chdir\0chown\0close\0ctermid\0cuserid\0\
/* The following are only used when handling stdlib.h */
int seen_EXIT_FAILURE = 0, seen_EXIT_SUCCESS = 0;
\f
-#define obstack_chunk_alloc xmalloc
-#define obstack_chunk_free free
struct obstack scan_file_obstack;
/* NOTE: If you edit this, also edit gen-protos.c !! */
static struct fn_decl *
-lookup_std_proto (name, name_length)
- const char *name;
- int name_length;
+lookup_std_proto (const char *name, int name_length)
{
int i = hashstr (name, name_length) % HASH_SIZE;
int i0 = i;
&& strncmp (fn->fname, name, name_length) == 0)
return fn;
i = (i+1) % HASH_SIZE;
- if (i == i0)
- abort ();
+ gcc_assert (i != i0);
}
}
char *inc_filename;
int inc_filename_length;
-const char *progname = "fix-header";
FILE *outf;
sstring line;
int required_unseen_count = 0;
int required_other = 0;
-static void
-write_lbrac ()
+static void
+write_lbrac (void)
{
-
-#if ADD_MISSING_EXTERN_C
- if (missing_extern_C_count + required_unseen_count > 0)
- fprintf (outf, "#ifdef __cplusplus\nextern \"C\" {\n#endif\n");
-#endif
-
if (partial_count)
{
fprintf (outf, "#ifndef _PARAMS\n");
#define SEEN(FN) ((FN)->partial == &seen_dummy_proto)
static void
-recognized_macro (fname)
- const char *fname;
+recognized_macro (const char *fname)
{
/* The original include file defines fname as a macro. */
struct fn_decl *fn = lookup_std_proto (fname, strlen (fname));
}
void
-recognized_extern (name)
- const cpp_token *name;
+recognized_extern (const cpp_token *name)
{
switch (special_file_handling)
{
'extern "C"' braces); or 'f' for other function declarations. */
void
-recognized_function (fname, line, kind, have_arg_list)
- const cpp_token *fname;
- unsigned int line;
- int kind; /* One of 'f' 'F' or 'I' */
- int have_arg_list;
+recognized_function (const cpp_token *fname, unsigned int line, int kind,
+ int have_arg_list)
{
struct partial_proto *partial;
int i;
struct fn_decl *fn;
-#if ADD_MISSING_EXTERN_C
- if (kind == 'f')
- missing_extern_C_count++;
-#endif
- fn = lookup_std_proto ((const char *)fname->val.node->name,
- fname->val.node->length);
+ fn = lookup_std_proto ((const char *) NODE_NAME (fname->val.node),
+ NODE_LEN (fname->val.node));
/* Remove the function from the list of required function. */
if (fn)
/* If we have a full prototype, we're done. */
if (have_arg_list)
return;
-
+
if (kind == 'I') /* don't edit inline function */
return;
/* We only have a partial function declaration,
so remember that we have to add a complete prototype. */
partial_count++;
- partial = (struct partial_proto *)
- obstack_alloc (&scan_file_obstack, sizeof (struct partial_proto));
+ partial
+ = (struct partial_proto *)
+ obstack_alloc (&scan_file_obstack,
+ sizeof (struct partial_proto));
partial->line_seen = line;
partial->fn = fn;
fn->partial = partial;
call recognized_macro on it. */
static void
-check_macro_names (pfile, names)
- cpp_reader *pfile;
- namelist names;
+check_macro_names (cpp_reader *pfile, namelist names)
{
size_t len;
while (*names)
}
static void
-cb_change_file (pfile, fc)
- cpp_reader *pfile ATTRIBUTE_UNUSED;
- const cpp_file_change *fc;
+cb_file_change (cpp_reader *pfile ATTRIBUTE_UNUSED,
+ const struct line_map *map)
{
/* Just keep track of current file name. */
- cur_file = fc->to.filename;
+ cur_file = map == NULL ? NULL : map->to_file;
}
static void
-read_scan_file (in_fname, argc, argv)
- char *in_fname;
- int argc;
- char **argv;
+read_scan_file (char *in_fname, int argc, char **argv)
{
- cpp_reader* scan_in;
+ cpp_reader *scan_in;
+ cpp_callbacks *cb;
+ cpp_options *options;
struct fn_decl *fn;
- int i;
- register struct symbol_list *cur_symbols;
+ int i, strings_processed;
+ struct symbol_list *cur_symbols;
- obstack_init (&scan_file_obstack);
+ obstack_init (&scan_file_obstack);
- scan_in = cpp_create_reader (CLK_GNUC89);
- scan_in->cb.change_file = cb_change_file;
+ linemap_init (&line_table);
+ scan_in = cpp_create_reader (CLK_GNUC89, NULL, &line_table);
+ cb = cpp_get_callbacks (scan_in);
+ cb->file_change = cb_file_change;
/* We are going to be scanning a header file out of its proper context,
so ignore warnings and errors. */
- CPP_OPTION (scan_in, inhibit_warnings) = 1;
- CPP_OPTION (scan_in, inhibit_errors) = 1;
- i = cpp_handle_options (scan_in, argc, argv);
- if (i < argc && ! CPP_FATAL_ERRORS (scan_in))
- cpp_fatal (scan_in, "Invalid option `%s'", argv[i]);
- if (CPP_FATAL_ERRORS (scan_in))
+ options = cpp_get_options (scan_in);
+ options->inhibit_warnings = 1;
+ options->inhibit_errors = 1;
+ cpp_post_options (scan_in);
+
+ if (!cpp_read_main_file (scan_in, in_fname))
exit (FATAL_EXIT_CODE);
- if (! cpp_start_read (scan_in, in_fname))
+ cpp_change_file (scan_in, LC_RENAME, "<built-in>");
+ cpp_init_builtins (scan_in, true);
+ cpp_change_file (scan_in, LC_RENAME, in_fname);
+
+ /* Process switches after builtins so -D can override them. */
+ for (i = 0; i < argc; i += strings_processed)
+ {
+ strings_processed = 0;
+ if (argv[i][0] == '-')
+ {
+ if (argv[i][1] == 'I')
+ {
+ if (argv[i][2] != '\0')
+ {
+ strings_processed = 1;
+ add_path (xstrdup (argv[i] + 2), BRACKET, false, false);
+ }
+ else if (i + 1 != argc)
+ {
+ strings_processed = 2;
+ add_path (xstrdup (argv[i + 1]), BRACKET, false, false);
+ }
+ }
+ else if (argv[i][1] == 'D')
+ {
+ if (argv[i][2] != '\0')
+ strings_processed = 1, cpp_define (scan_in, argv[i] + 2);
+ else if (i + 1 != argc)
+ strings_processed = 2, cpp_define (scan_in, argv[i + 1]);
+ }
+ }
+
+ if (strings_processed == 0)
+ break;
+ }
+
+ if (i < argc)
+ cpp_error (scan_in, CPP_DL_ERROR, "invalid option `%s'", argv[i]);
+ if (cpp_errors (scan_in))
exit (FATAL_EXIT_CODE);
+ register_include_chains (scan_in, NULL /* sysroot */, NULL /* iprefix */,
+ NULL /* imultilib */, true /* stdinc */,
+ false /* cxx_stdinc */, false /* verbose */);
+
/* We are scanning a system header, so mark it as such. */
cpp_make_system_header (scan_in, 1, 0);
if (special_file_handling == stdio_h
&& (fn = lookup_std_proto ("_filbuf", 7)) != NULL)
{
- static const unsigned char getchar_call[] = "getchar();";
+ unsigned char getchar_call[] = "getchar();\n";
int seen_filbuf = 0;
- cpp_buffer *buf = CPP_BUFFER (scan_in);
- if (cpp_push_buffer (scan_in, getchar_call,
- sizeof(getchar_call) - 1) == NULL)
- return;
/* Scan the macro expansion of "getchar();". */
+ cpp_push_buffer (scan_in, getchar_call, sizeof(getchar_call) - 1,
+ /* from_stage3 */ true);
for (;;)
{
- cpp_token t;
+ const cpp_token *t = cpp_get_token (scan_in);
- cpp_get_token (scan_in, &t);
- if (t.type == CPP_EOF)
- {
- cpp_pop_buffer (scan_in);
- if (CPP_BUFFER (scan_in) == buf)
- break;
- }
- else if (cpp_ideq (&t, "_filbuf"))
+ if (t->type == CPP_EOF)
+ break;
+ else if (cpp_ideq (t, "_filbuf"))
seen_filbuf++;
}
+
if (seen_filbuf)
{
int need_filbuf = !SEEN (fn) && !REQUIRED (fn);
}
}
- if (required_unseen_count + partial_count + required_other
-#if ADD_MISSING_EXTERN_C
- + missing_extern_C_count
-#endif
- == 0)
+ if (required_unseen_count + partial_count + required_other == 0)
{
if (verbose)
fprintf (stderr, "%s: OK, nothing needs to be done.\n", inc_filename);
if (partial_count)
fprintf (stderr, "%s: %d non-prototype function declarations.\n",
inc_filename, partial_count);
-#if ADD_MISSING_EXTERN_C
- if (missing_extern_C_count)
- fprintf (stderr,
- "%s: %d declarations not protected by extern \"C\".\n",
- inc_filename, missing_extern_C_count);
-#endif
}
}
static void
-write_rbrac ()
+write_rbrac (void)
{
struct fn_decl *fn;
const char *cptr;
- register struct symbol_list *cur_symbols;
+ struct symbol_list *cur_symbols;
if (required_unseen_count)
{
break;
}
-
-#if ADD_MISSING_EXTERN_C
- if (missing_extern_C_count + required_unseen_count > 0)
- fprintf (outf, "#ifdef __cplusplus\n}\n#endif\n");
-#endif
}
/* Returns 1 iff the file is properly protected from multiple inclusion:
#define INF_UNGET(c) ((c)!=EOF && inf_ptr--)
static int
-inf_skip_spaces (c)
- int c;
+inf_skip_spaces (int c)
{
for (;;)
{
/* Read into STR from inf_buffer upto DELIM. */
static int
-inf_read_upto (str, delim)
- sstring *str;
- int delim;
+inf_read_upto (sstring *str, int delim)
{
int ch;
for (;;)
}
static int
-inf_scan_ident (s, c)
- register sstring *s;
- int c;
+inf_scan_ident (sstring *s, int c)
{
s->ptr = s->base;
- if (ISALPHA (c) || c == '_')
+ if (ISIDST (c))
{
for (;;)
{
SSTRING_PUT (s, c);
c = INF_GET ();
- if (c == EOF || !(ISALNUM (c) || c == '_'))
+ if (c == EOF || !(ISIDNUM (c)))
break;
}
}
Otherwise return 0. */
static int
-check_protection (ifndef_line, endif_line)
- int *ifndef_line, *endif_line;
+check_protection (int *ifndef_line, int *endif_line)
{
int c;
int if_nesting = 1; /* Level of nesting of #if's */
return 1;
}
-extern int main PARAMS ((int, char **));
+extern int main (int, char **);
int
-main (argc, argv)
- int argc;
- char **argv;
+main (int argc, char **argv)
{
int inf_fd;
struct stat sbuf;
int endif_line;
long to_read;
long int inf_size;
- register struct symbol_list *cur_symbols;
+ struct symbol_list *cur_symbols;
+ progname = "fix-header";
if (argv[0] && argv[0][0])
{
- register char *p;
+ char *p;
progname = 0;
for (p = argv[0]; *p; p++)
- if (*p == '/')
- progname = p;
+ if (*p == '/')
+ progname = p;
progname = progname ? progname+1 : argv[0];
}
#ifdef FIXPROTO_IGNORE_LIST
for (i = 0; files_to_ignore[i] != NULL; i++)
{
- char *ignore_name = files_to_ignore[i];
+ const char *const ignore_name = files_to_ignore[i];
int ignore_len = strlen (ignore_name);
if (strncmp (inc_filename, ignore_name, ignore_len) == 0)
{
exit (SUCCESS_EXIT_CODE);
}
}
-
+
}
#endif
if (include_entry->name != NULL)
{
- struct std_include_entry *entry;
+ const struct std_include_entry *entry;
cur_symbol_table_size = 0;
for (entry = include_entry; ;)
{
else
symbol_table[0].names = NULL;
- /* Count and mark the prototypes required for this include file. */
+ /* Count and mark the prototypes required for this include file. */
for (cur_symbols = &symbol_table[0]; cur_symbols->names; cur_symbols++)
{
int name_len;
exit (FATAL_EXIT_CODE);
}
inf_size = sbuf.st_size;
- inf_buffer = (char *) xmalloc (inf_size + 2);
- inf_buffer[inf_size] = '\n';
- inf_buffer[inf_size + 1] = '\0';
- inf_limit = inf_buffer + inf_size;
+ inf_buffer = XNEWVEC (char, inf_size + 2);
inf_ptr = inf_buffer;
to_read = inf_size;
close (inf_fd);
+ /* Inf_size may have changed if read was short (as on VMS) */
+ inf_buffer[inf_size] = '\n';
+ inf_buffer[inf_size + 1] = '\0';
+ inf_limit = inf_buffer + inf_size;
+
/* If file doesn't end with '\n', add one. */
if (inf_limit > inf_buffer && inf_limit[-1] != '\n')
inf_limit++;
c = INF_GET ();
if (c == EOF)
break;
- if (ISALPHA (c) || c == '_')
+ if (ISIDST (c))
{
c = inf_scan_ident (&buf, c);
(void) INF_UNGET (c);
return 0;
}
-\f
-
-static void
-v_fatal (str, ap)
- const char * str;
- va_list ap;
-{
- fprintf (stderr, "%s: %s: ", progname, inc_filename);
- vfprintf (stderr, str, ap);
- fprintf (stderr, "\n");
-
- exit (FATAL_EXIT_CODE);
-}
-
-static void
-fatal VPARAMS ((const char *str, ...))
-{
-#ifndef ANSI_PROTOTYPES
- const char *str;
-#endif
- va_list ap;
-
- VA_START(ap, str);
-
-#ifndef ANSI_PROTOTYPES
- str = va_arg (ap, const char *);
-#endif
-
- v_fatal(str, ap);
- va_end(ap);
-}