bfb9dc7faa
* cpplib.h (struct cpp_name): Now struct cpp_string. (CPP_INT, CPP_FLOAT, CPP_NUMBER, CPP_COMMENT, CPP_HEADER_NAME): Change to type S. (struct cpp_token): Rename 'name' field to 'str'. Add 'node' field, a cpp_hashnode *. All references to val.name updated to use val.str or val.node as appropriate. (struct cpp_reader): Add spec_nodes field. (cpp_idcmp): Now cpp_ideq; takes a token * and a char *. * cpphash.h (struct spec_nodes): New. (enum spell_type): Reorder. Only SPELL_STRING tokens use val.str. All references to 'spelling > SPELL_NONE' updated to match. (CPP_IN_SYSTEM_HEADER): Check pfile->buffer and pfile->buffer->inc are not NULL before dereferencing them. * cpplex.c (parse_name): Take a pointer to the current token, plus current position and limit as args; return the new position; don't copy the text of a name into the string buffer, instead call cpp_lookup and store the node pointer. If extending a token, copy out the text of the old into a scratch buffer, append the new, look that up and store the new node pointer. Inline. (maybe_paste_with_next): If the result of paste is a NAME, then look up the pasted text and store its node pointer. (lex_line): Adjust for new parse_name interface. Check for L"str", L'str' using spec_nodes->n_L. (spell_token): SPELL_IDENT tokens have their spelling in val.node->name. Handle SPELL_STRING tokens that don't have string delimiters. (_cpp_expand_name_space, (can_paste): Check for L ## "str" using spec_nodes->n_L. (cpp_get_token, special_symbol): No need to call cpp_lookup. (cpp_idcmp): Now cpp_ideq; take a token * and a const char *; return 1=equal 0=not, not a tristate. * cpphash.c (var_args_str): Delete. (find_param): Compare node fields directly. (is__va_args__): Use CPP_PEDANTIC. Just compare token->val.node with spec_nodes->n__VA_ARGS__. (dump_funlike_macro): Don't use var_args_str. * cpplib.c (_cpp_check_directive): Just walk through spec_nodes->dirs comparing pointers. (get_define_node, do_pragma_poison, detect_if_not_defined, parse_ifdef): The identifier has already been looked up. (do_ifdef, do_ifndef): parse_ifdef won't return a poisoned node. (do_if): Only call detect_if_not_defined at beginning of file. (_cpp_parse_assertion): Only copy string pointers for SPELL_STRING tokens. (pragma_dispatch): Take a node pointer and examine its name field. (_cpp_init_stacks): Also initialize the spec_nodes structure. * cppinit.c (cpp_reader_init): Call _cpp_init_stacks after _cpp_init_macros. (cpp_cleanup): Free pfile->spec_nodes. Call _cpp_cleanup_* in reverse order from the corresponding _cpp_init_* routines. * cppexp.c (parse_number, parse_charconst, parse_defined, lex): Check val.node->type instead of calling cpp_defined. Use spec_nodes entries where appropriate. * fix-header.c, scan-decls.c: Update for interface changes. From-SVN: r34926
229 lines
5.5 KiB
C
229 lines
5.5 KiB
C
/* scan-decls.c - Extracts declarations from cpp output.
|
|
Copyright (C) 1993, 1995, 1997, 1998,
|
|
1999, 2000 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 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.
|
|
|
|
Written by Per Bothner <bothner@cygnus.com>, July 1993. */
|
|
|
|
#include "hconfig.h"
|
|
#include "system.h"
|
|
#include "cpplib.h"
|
|
#include "scan.h"
|
|
|
|
static void skip_to_closing_brace PARAMS ((cpp_reader *));
|
|
|
|
int brace_nesting = 0;
|
|
|
|
/* The first extern_C_braces_length elements of extern_C_braces
|
|
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];
|
|
#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;
|
|
|
|
static void
|
|
skip_to_closing_brace (pfile)
|
|
cpp_reader *pfile;
|
|
{
|
|
int nesting = 1;
|
|
for (;;)
|
|
{
|
|
enum cpp_ttype token = cpp_get_token (pfile)->type;
|
|
if (token == CPP_EOF)
|
|
break;
|
|
if (token == CPP_OPEN_BRACE)
|
|
nesting++;
|
|
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.
|
|
|
|
The following grammar (as well as some extra stuff) is recognized:
|
|
|
|
declaration:
|
|
(decl-specifier)* declarator ("," declarator)* ";"
|
|
decl-specifier:
|
|
identifier
|
|
keyword
|
|
extern "C"
|
|
declarator:
|
|
(ptr-operator)* dname [ "(" argument-declaration-list ")" ]
|
|
ptr-operator:
|
|
("*" | "&") ("const" | "volatile")*
|
|
dname:
|
|
identifier
|
|
|
|
Here dname is the actual name being declared.
|
|
*/
|
|
|
|
int
|
|
scan_decls (pfile, argc, argv)
|
|
cpp_reader *pfile;
|
|
int argc ATTRIBUTE_UNUSED;
|
|
char **argv ATTRIBUTE_UNUSED;
|
|
{
|
|
int saw_extern, saw_inline;
|
|
const cpp_token *prev_id;
|
|
const cpp_token *token;
|
|
|
|
new_statement:
|
|
token = cpp_get_token (pfile);
|
|
|
|
handle_statement:
|
|
current_extern_C = 0;
|
|
saw_extern = 0;
|
|
saw_inline = 0;
|
|
if (token->type == CPP_OPEN_BRACE)
|
|
{
|
|
/* Pop an 'extern "C"' nesting level, if appropriate. */
|
|
if (extern_C_braces_length
|
|
&& extern_C_braces[extern_C_braces_length - 1] == brace_nesting)
|
|
extern_C_braces_length--;
|
|
brace_nesting--;
|
|
goto new_statement;
|
|
}
|
|
if (token->type == CPP_OPEN_BRACE)
|
|
{
|
|
brace_nesting++;
|
|
goto new_statement;
|
|
}
|
|
if (token->type == CPP_EOF)
|
|
{
|
|
cpp_pop_buffer (pfile);
|
|
if (CPP_BUFFER (pfile) == NULL)
|
|
return 0;
|
|
|
|
goto new_statement;
|
|
}
|
|
if (token->type == CPP_SEMICOLON)
|
|
goto new_statement;
|
|
if (token->type != CPP_NAME)
|
|
goto new_statement;
|
|
|
|
prev_id = 0;
|
|
for (;;)
|
|
{
|
|
switch (token->type)
|
|
{
|
|
default:
|
|
goto handle_statement;
|
|
case CPP_MULT:
|
|
case CPP_AND:
|
|
case CPP_PLACEMARKER:
|
|
/* skip */
|
|
break;
|
|
|
|
case CPP_COMMA:
|
|
case CPP_SEMICOLON:
|
|
if (prev_id && 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:
|
|
cpp_pop_buffer (pfile);
|
|
if (CPP_BUFFER (pfile) == NULL)
|
|
return 0;
|
|
break;
|
|
|
|
case CPP_OPEN_PAREN:
|
|
/* Looks like this is the start of a formal parameter list. */
|
|
if (prev_id)
|
|
{
|
|
int nesting = 1;
|
|
int have_arg_list = 0;
|
|
for (;;)
|
|
{
|
|
token = cpp_get_token (pfile);
|
|
if (token->type == CPP_OPEN_PAREN)
|
|
nesting++;
|
|
else if (token->type == CPP_CLOSE_PAREN)
|
|
{
|
|
nesting--;
|
|
if (nesting == 0)
|
|
break;
|
|
}
|
|
else if (token->type == CPP_EOF)
|
|
break;
|
|
else if (token->type == CPP_NAME
|
|
|| token->type == CPP_ELLIPSIS)
|
|
have_arg_list = 1;
|
|
}
|
|
recognized_function (prev_id,
|
|
(saw_inline ? 'I'
|
|
: in_extern_C_brace || current_extern_C
|
|
? 'F' : 'f'), have_arg_list,
|
|
CPP_BUFFER (pfile)->nominal_fname);
|
|
token = cpp_get_token (pfile);
|
|
if (token->type == CPP_OPEN_BRACE)
|
|
{
|
|
/* skip body of (normally) inline function */
|
|
skip_to_closing_brace (pfile);
|
|
goto new_statement;
|
|
}
|
|
if (token->type == CPP_SEMICOLON)
|
|
goto new_statement;
|
|
}
|
|
break;
|
|
case CPP_NAME:
|
|
/* "inline" and "extern" are recognized but skipped */
|
|
if (cpp_ideq (token, "inline"))
|
|
{
|
|
saw_inline = 1;
|
|
}
|
|
else if (cpp_ideq (token, "extern"))
|
|
{
|
|
saw_extern = 1;
|
|
token = cpp_get_token (pfile);
|
|
if (token->type == CPP_STRING
|
|
&& token->val.str.len == 1
|
|
&& token->val.str.text[0] == 'C')
|
|
{
|
|
current_extern_C = 1;
|
|
token = cpp_get_token (pfile);
|
|
if (token->type == CPP_OPEN_BRACE)
|
|
{
|
|
brace_nesting++;
|
|
extern_C_braces[extern_C_braces_length++]
|
|
= brace_nesting;
|
|
goto new_statement;
|
|
}
|
|
}
|
|
else
|
|
continue;
|
|
break;
|
|
}
|
|
/* This may be the name of a variable or function. */
|
|
prev_id = token;
|
|
break;
|
|
}
|
|
token = cpp_get_token (pfile);
|
|
}
|
|
}
|