1996-09-18 05:35:50 +00:00
|
|
|
/* scan-decls.c - Extracts declarations from cpp output.
|
2002-02-01 18:16:02 +00:00
|
|
|
Copyright (C) 1993, 1995, 1997, 1998,
|
|
|
|
1999, 2000 Free Software Foundation, Inc.
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
|
|
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"
|
1999-08-26 09:30:50 +00:00
|
|
|
#include "system.h"
|
1996-09-18 05:35:50 +00:00
|
|
|
#include "cpplib.h"
|
1999-08-26 09:30:50 +00:00
|
|
|
#include "scan.h"
|
1996-09-18 05:35:50 +00:00
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
static void skip_to_closing_brace PARAMS ((cpp_reader *));
|
|
|
|
static const cpp_token *get_a_token PARAMS ((cpp_reader *));
|
|
|
|
|
1996-09-18 05:35:50 +00:00
|
|
|
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;
|
2003-11-07 02:43:04 +00:00
|
|
|
/* 20 is not enough anymore on Solaris 9. */
|
|
|
|
#define MAX_EXTERN_C_BRACES 200
|
|
|
|
char extern_C_braces[MAX_EXTERN_C_BRACES];
|
1996-09-18 05:35:50 +00:00
|
|
|
#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;
|
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
/* Get a token but skip padding. */
|
|
|
|
static const cpp_token *
|
|
|
|
get_a_token (pfile)
|
|
|
|
cpp_reader *pfile;
|
|
|
|
{
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
const cpp_token *result = cpp_get_token (pfile);
|
|
|
|
if (result->type != CPP_PADDING)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-09-18 05:35:50 +00:00
|
|
|
static void
|
|
|
|
skip_to_closing_brace (pfile)
|
|
|
|
cpp_reader *pfile;
|
|
|
|
{
|
|
|
|
int nesting = 1;
|
|
|
|
for (;;)
|
|
|
|
{
|
2002-02-01 18:16:02 +00:00
|
|
|
enum cpp_ttype token = get_a_token (pfile)->type;
|
|
|
|
|
1996-09-18 05:35:50 +00:00
|
|
|
if (token == CPP_EOF)
|
|
|
|
break;
|
2002-02-01 18:16:02 +00:00
|
|
|
if (token == CPP_OPEN_BRACE)
|
1996-09-18 05:35:50 +00:00
|
|
|
nesting++;
|
2002-02-01 18:16:02 +00:00
|
|
|
if (token == CPP_CLOSE_BRACE && --nesting == 0)
|
1996-09-18 05:35:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This function scans a C source file (actually, the output of cpp),
|
|
|
|
reading from FP. It looks for function declarations, and
|
2003-07-11 03:40:53 +00:00
|
|
|
external variable declarations.
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
|
|
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;
|
1999-10-16 06:09:09 +00:00
|
|
|
int argc ATTRIBUTE_UNUSED;
|
|
|
|
char **argv ATTRIBUTE_UNUSED;
|
1996-09-18 05:35:50 +00:00
|
|
|
{
|
|
|
|
int saw_extern, saw_inline;
|
2002-02-01 18:16:02 +00:00
|
|
|
cpp_token prev_id;
|
|
|
|
const cpp_token *token;
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
|
|
new_statement:
|
2002-02-01 18:16:02 +00:00
|
|
|
token = get_a_token (pfile);
|
1996-09-18 05:35:50 +00:00
|
|
|
|
|
|
|
handle_statement:
|
|
|
|
current_extern_C = 0;
|
|
|
|
saw_extern = 0;
|
|
|
|
saw_inline = 0;
|
2002-02-01 18:16:02 +00:00
|
|
|
if (token->type == CPP_OPEN_BRACE)
|
1996-09-18 05:35:50 +00:00
|
|
|
{
|
|
|
|
/* 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;
|
|
|
|
}
|
2002-02-01 18:16:02 +00:00
|
|
|
if (token->type == CPP_OPEN_BRACE)
|
1996-09-18 05:35:50 +00:00
|
|
|
{
|
|
|
|
brace_nesting++;
|
|
|
|
goto new_statement;
|
|
|
|
}
|
2002-02-01 18:16:02 +00:00
|
|
|
|
|
|
|
if (token->type == CPP_EOF)
|
1996-09-18 05:35:50 +00:00
|
|
|
return 0;
|
2002-02-01 18:16:02 +00:00
|
|
|
|
|
|
|
if (token->type == CPP_SEMICOLON)
|
1996-09-18 05:35:50 +00:00
|
|
|
goto new_statement;
|
2002-02-01 18:16:02 +00:00
|
|
|
if (token->type != CPP_NAME)
|
1996-09-18 05:35:50 +00:00
|
|
|
goto new_statement;
|
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
prev_id.type = CPP_EOF;
|
1996-09-18 05:35:50 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
2002-02-01 18:16:02 +00:00
|
|
|
switch (token->type)
|
1996-09-18 05:35:50 +00:00
|
|
|
{
|
2002-02-01 18:16:02 +00:00
|
|
|
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;
|
2003-07-11 03:40:53 +00:00
|
|
|
|
2002-02-01 18:16:02 +00:00
|
|
|
case CPP_EOF:
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case CPP_OPEN_PAREN:
|
1999-08-26 09:30:50 +00:00
|
|
|
/* Looks like this is the start of a formal parameter list. */
|
2002-02-01 18:16:02 +00:00
|
|
|
if (prev_id.type != CPP_EOF)
|
1996-09-18 05:35:50 +00:00
|
|
|
{
|
|
|
|
int nesting = 1;
|
|
|
|
int have_arg_list = 0;
|
|
|
|
for (;;)
|
|
|
|
{
|
2002-02-01 18:16:02 +00:00
|
|
|
token = get_a_token (pfile);
|
|
|
|
if (token->type == CPP_OPEN_PAREN)
|
1996-09-18 05:35:50 +00:00
|
|
|
nesting++;
|
2002-02-01 18:16:02 +00:00
|
|
|
else if (token->type == CPP_CLOSE_PAREN)
|
1996-09-18 05:35:50 +00:00
|
|
|
{
|
|
|
|
nesting--;
|
|
|
|
if (nesting == 0)
|
|
|
|
break;
|
|
|
|
}
|
2002-02-01 18:16:02 +00:00
|
|
|
else if (token->type == CPP_EOF)
|
1996-09-18 05:35:50 +00:00
|
|
|
break;
|
2002-02-01 18:16:02 +00:00
|
|
|
else if (token->type == CPP_NAME
|
|
|
|
|| token->type == CPP_ELLIPSIS)
|
1996-09-18 05:35:50 +00:00
|
|
|
have_arg_list = 1;
|
|
|
|
}
|
2002-02-01 18:16:02 +00:00
|
|
|
recognized_function (&prev_id, token->line,
|
1996-09-18 05:35:50 +00:00
|
|
|
(saw_inline ? 'I'
|
|
|
|
: in_extern_C_brace || current_extern_C
|
2002-02-01 18:16:02 +00:00
|
|
|
? 'F' : 'f'), have_arg_list);
|
|
|
|
token = get_a_token (pfile);
|
|
|
|
if (token->type == CPP_OPEN_BRACE)
|
1996-09-18 05:35:50 +00:00
|
|
|
{
|
|
|
|
/* skip body of (normally) inline function */
|
|
|
|
skip_to_closing_brace (pfile);
|
|
|
|
goto new_statement;
|
|
|
|
}
|
2002-02-01 18:16:02 +00:00
|
|
|
|
|
|
|
/* 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;
|
1996-09-18 05:35:50 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CPP_NAME:
|
|
|
|
/* "inline" and "extern" are recognized but skipped */
|
2002-02-01 18:16:02 +00:00
|
|
|
if (cpp_ideq (token, "inline"))
|
1996-09-18 05:35:50 +00:00
|
|
|
{
|
|
|
|
saw_inline = 1;
|
|
|
|
}
|
2002-02-01 18:16:02 +00:00
|
|
|
else if (cpp_ideq (token, "extern"))
|
1996-09-18 05:35:50 +00:00
|
|
|
{
|
|
|
|
saw_extern = 1;
|
2002-02-01 18:16:02 +00:00
|
|
|
token = get_a_token (pfile);
|
|
|
|
if (token->type == CPP_STRING
|
|
|
|
&& token->val.str.len == 1
|
|
|
|
&& token->val.str.text[0] == 'C')
|
1996-09-18 05:35:50 +00:00
|
|
|
{
|
|
|
|
current_extern_C = 1;
|
2002-02-01 18:16:02 +00:00
|
|
|
token = get_a_token (pfile);
|
|
|
|
if (token->type == CPP_OPEN_BRACE)
|
1996-09-18 05:35:50 +00:00
|
|
|
{
|
|
|
|
brace_nesting++;
|
|
|
|
extern_C_braces[extern_C_braces_length++]
|
|
|
|
= brace_nesting;
|
2003-11-07 02:43:04 +00:00
|
|
|
if (extern_C_braces_length >= MAX_EXTERN_C_BRACES)
|
|
|
|
{
|
|
|
|
fprintf (stderr,
|
|
|
|
"Internal error: out-of-bounds index\n");
|
|
|
|
exit (FATAL_EXIT_CODE);
|
|
|
|
}
|
1996-09-18 05:35:50 +00:00
|
|
|
goto new_statement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
1999-08-26 09:30:50 +00:00
|
|
|
continue;
|
1996-09-18 05:35:50 +00:00
|
|
|
break;
|
|
|
|
}
|
1999-08-26 09:30:50 +00:00
|
|
|
/* This may be the name of a variable or function. */
|
2002-02-01 18:16:02 +00:00
|
|
|
prev_id = *token;
|
1996-09-18 05:35:50 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-02-01 18:16:02 +00:00
|
|
|
token = get_a_token (pfile);
|
1996-09-18 05:35:50 +00:00
|
|
|
}
|
|
|
|
}
|