/* scan-decls.c - Extracts declarations from cpp output.
- Copyright (C) 1993, 1995 Free Software Foundation, Inc.
+ Copyright (C) 1993, 1995, 1997, 1998,
+ 1999, 2000, 2003, 2004 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
Written by Per Bothner <bothner@cygnus.com>, July 1993. */
-#include <stdio.h>
-#include <ctype.h>
-#include "hconfig.h"
+#include "bconfig.h"
+#include "system.h"
+#include "coretypes.h"
+#include "tm.h"
#include "cpplib.h"
+#include "scan.h"
+
+static void skip_to_closing_brace (cpp_reader *);
+static const cpp_token *get_a_token (cpp_reader *);
int brace_nesting = 0;
indicate the (brace nesting levels of) left braces that were
prefixed by extern "C". */
int extern_C_braces_length = 0;
-char extern_C_braces[20];
+/* 20 is not enough anymore on Solaris 9. */
+#define MAX_EXTERN_C_BRACES 200
+char extern_C_braces[MAX_EXTERN_C_BRACES];
#define in_extern_C_brace (extern_C_braces_length>0)
/* True if the function declaration currently being scanned is
prefixed by extern "C". */
int current_extern_C = 0;
+/* Get a token but skip padding. */
+static const cpp_token *
+get_a_token (cpp_reader *pfile)
+{
+ for (;;)
+ {
+ const cpp_token *result = cpp_get_token (pfile);
+ if (result->type != CPP_PADDING)
+ return result;
+ }
+}
+
static void
-skip_to_closing_brace (pfile)
- cpp_reader *pfile;
+skip_to_closing_brace (cpp_reader *pfile)
{
int nesting = 1;
for (;;)
{
- enum cpp_token token = cpp_get_token (pfile);
+ enum cpp_ttype token = get_a_token (pfile)->type;
+
if (token == CPP_EOF)
break;
- if (token == CPP_LBRACE)
+ if (token == CPP_OPEN_BRACE)
nesting++;
- if (token == CPP_RBRACE && --nesting == 0)
+ if (token == CPP_CLOSE_BRACE && --nesting == 0)
break;
}
}
/* This function scans a C source file (actually, the output of cpp),
reading from FP. It looks for function declarations, and
- external variable declarations.
+ external variable declarations.
The following grammar (as well as some extra stuff) is recognized:
*/
int
-scan_decls (pfile, argc, argv)
- cpp_reader *pfile;
- int argc;
- char **argv;
+scan_decls (cpp_reader *pfile, int argc ATTRIBUTE_UNUSED,
+ char **argv ATTRIBUTE_UNUSED)
{
int saw_extern, saw_inline;
- int old_written;
- /* If declarator_start is non-zero, it marks the start of the current
- declarator. If it is zero, we are either still parsing the
- decl-specs, or prev_id_start marks the start of the declarator. */
- int declarator_start;
- int prev_id_start, prev_id_end;
- enum cpp_token token;
+ cpp_token prev_id;
+ const cpp_token *token;
new_statement:
- CPP_SET_WRITTEN (pfile, 0);
- token = cpp_get_token (pfile);
+ token = get_a_token (pfile);
handle_statement:
current_extern_C = 0;
saw_extern = 0;
saw_inline = 0;
- if (token == CPP_RBRACE)
+ if (token->type == CPP_OPEN_BRACE)
{
/* Pop an 'extern "C"' nesting level, if appropriate. */
if (extern_C_braces_length
brace_nesting--;
goto new_statement;
}
- if (token == CPP_LBRACE)
+ if (token->type == CPP_OPEN_BRACE)
{
brace_nesting++;
goto new_statement;
}
- if (token == CPP_EOF)
+
+ if (token->type == CPP_EOF)
return 0;
- if (token == CPP_SEMICOLON)
+
+ if (token->type == CPP_SEMICOLON)
goto new_statement;
- if (token != CPP_NAME)
+ if (token->type != CPP_NAME)
goto new_statement;
- prev_id_start = 0;
- declarator_start = 0;
+ prev_id.type = CPP_EOF;
for (;;)
{
- int start_written = CPP_WRITTEN (pfile);
- token = cpp_get_token (pfile);
- handle_token:
- switch (token)
+ switch (token->type)
{
- case CPP_LPAREN:
+ default:
+ goto handle_statement;
+ case CPP_MULT:
+ case CPP_AND:
+ /* skip */
+ break;
+
+ case CPP_COMMA:
+ case CPP_SEMICOLON:
+ if (prev_id.type != CPP_EOF && saw_extern)
+ {
+ recognized_extern (&prev_id);
+ }
+ if (token->type == CPP_COMMA)
+ break;
+ /* ... fall through ... */
+ case CPP_OPEN_BRACE: case CPP_CLOSE_BRACE:
+ goto new_statement;
+
+ case CPP_EOF:
+ return 0;
+
+ case CPP_OPEN_PAREN:
/* Looks like this is the start of a formal parameter list. */
- if (prev_id_start)
+ if (prev_id.type != CPP_EOF)
{
int nesting = 1;
int have_arg_list = 0;
- cpp_buffer *fbuf = cpp_file_buffer (pfile);
- long func_lineno;
- cpp_buf_line_and_col (fbuf, &func_lineno, NULL);
+ const struct line_map *map;
+ unsigned int line;
for (;;)
{
- token = cpp_get_token (pfile);
- if (token == CPP_LPAREN)
+ token = get_a_token (pfile);
+ if (token->type == CPP_OPEN_PAREN)
nesting++;
- else if (token == CPP_RPAREN)
+ else if (token->type == CPP_CLOSE_PAREN)
{
nesting--;
if (nesting == 0)
break;
}
- else if (token == CPP_EOF)
+ else if (token->type == CPP_EOF)
break;
- else if (token == CPP_NAME || token == CPP_3DOTS)
+ else if (token->type == CPP_NAME
+ || token->type == CPP_ELLIPSIS)
have_arg_list = 1;
}
- recognized_function (pfile->token_buffer + prev_id_start,
- prev_id_end - prev_id_start,
+ map = linemap_lookup (&line_table, token->src_loc);
+ line = SOURCE_LINE (map, token->src_loc);
+ recognized_function (&prev_id, line,
(saw_inline ? 'I'
: in_extern_C_brace || current_extern_C
- ? 'F' : 'f'),
- pfile->token_buffer, prev_id_start,
- have_arg_list,
- fbuf->nominal_fname, func_lineno);
- token = cpp_get_non_space_token (pfile);
- if (token == CPP_LBRACE)
+ ? 'F' : 'f'), have_arg_list);
+ token = get_a_token (pfile);
+ if (token->type == CPP_OPEN_BRACE)
{
/* skip body of (normally) inline function */
skip_to_closing_brace (pfile);
goto new_statement;
}
- goto maybe_handle_comma;
- }
- break;
- case CPP_OTHER:
- if (CPP_WRITTEN (pfile) == start_written + 1
- && (CPP_PWRITTEN (pfile)[-1] == '*'
- || CPP_PWRITTEN (pfile)[-1] == '&'))
- declarator_start = start_written;
- else
- goto handle_statement;
- break;
- case CPP_COMMA:
- case CPP_SEMICOLON:
- if (prev_id_start && saw_extern)
- {
- recognized_extern (pfile->token_buffer + prev_id_start,
- prev_id_end - prev_id_start,
- pfile->token_buffer,
- prev_id_start);
+
+ /* skip a possible __attribute__ or throw expression after the
+ parameter list */
+ while (token->type != CPP_SEMICOLON && token->type != CPP_EOF)
+ token = get_a_token (pfile);
+ goto new_statement;
}
- /* ... fall through ... */
- maybe_handle_comma:
- if (token != CPP_COMMA)
- goto new_statement;
- handle_comma:
- /* Handle multiple declarators in a single declaration,
- as in: extern char *strcpy (), *strcat (), ... ; */
- if (declarator_start == 0)
- declarator_start = prev_id_start;
- CPP_SET_WRITTEN (pfile, declarator_start);
break;
case CPP_NAME:
/* "inline" and "extern" are recognized but skipped */
- if (strcmp (pfile->token_buffer, "inline") == 0)
+ if (cpp_ideq (token, "inline"))
{
saw_inline = 1;
- CPP_SET_WRITTEN (pfile, start_written);
}
- if (strcmp (pfile->token_buffer, "extern") == 0)
+ else if (cpp_ideq (token, "extern"))
{
saw_extern = 1;
- CPP_SET_WRITTEN (pfile, start_written);
- token = cpp_get_non_space_token (pfile);
- if (token == CPP_STRING
- && strcmp (pfile->token_buffer, "\"C\"") == 0)
+ token = get_a_token (pfile);
+ if (token->type == CPP_STRING
+ && token->val.str.len == 1
+ && token->val.str.text[0] == 'C')
{
- CPP_SET_WRITTEN (pfile, start_written);
current_extern_C = 1;
- token = cpp_get_non_space_token (pfile);
- if (token == CPP_LBRACE)
+ token = get_a_token (pfile);
+ if (token->type == CPP_OPEN_BRACE)
{
brace_nesting++;
extern_C_braces[extern_C_braces_length++]
= brace_nesting;
+ if (extern_C_braces_length >= MAX_EXTERN_C_BRACES)
+ {
+ fprintf (stderr,
+ "Internal error: out-of-bounds index\n");
+ exit (FATAL_EXIT_CODE);
+ }
goto new_statement;
}
}
else
- goto handle_token;
+ continue;
break;
}
/* This may be the name of a variable or function. */
- prev_id_start = start_written;
- prev_id_end = CPP_WRITTEN (pfile);
+ prev_id = *token;
break;
-
- case CPP_EOF:
- return; /* ??? FIXME */
-
- case CPP_LBRACE: case CPP_RBRACE: case CPP_DIRECTIVE:
- goto new_statement; /* handle_statement? */
-
- case CPP_HSPACE: case CPP_VSPACE: case CPP_COMMENT: case CPP_POP:
- /* Skip initial white space. */
- if (start_written == 0)
- CPP_SET_WRITTEN (pfile, 0);
- break;
-
- default:
- prev_id_start = 0;
}
+ token = get_a_token (pfile);
}
}