X-Git-Url: http://git.sourceforge.jp/view?a=blobdiff_plain;f=gcc%2Fscan-decls.c;h=c0e2009150fd20253a7f41179cf0216769144315;hb=c6a0599243068b33768268296b067d0fce5feec9;hp=8191045fb75aece06ca4adf4e5b9c7fe2a81ca4a;hpb=f7c4e1286f6d9e1d7841bfe92ff423b020613764;p=pf3gnuchains%2Fgcc-fork.git diff --git a/gcc/scan-decls.c b/gcc/scan-decls.c index 8191045fb75..c0e2009150f 100644 --- a/gcc/scan-decls.c +++ b/gcc/scan-decls.c @@ -1,30 +1,32 @@ /* scan-decls.c - Extracts declarations from cpp output. - Copyright (C) 1993 Free Software Foundation, Inc. + Copyright (C) 1993, 1995, 1997, 1998, + 1999, 2000, 2003, 2004, 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, 675 Mass Ave, Cambridge, MA 02139, USA. + You should have received a copy of the GNU General Public License + along with this program; see the file COPYING3. If not see + . Written by Per Bothner , July 1993. */ -#include -#include -#include "hconfig.h" +#include "bconfig.h" +#include "system.h" +#include "coretypes.h" +#include "tm.h" +#include "cpplib.h" #include "scan.h" -sstring buf; -sstring rtype; -sstring arg_list; +static void skip_to_closing_brace (cpp_reader *); +static const cpp_token *get_a_token (cpp_reader *); int brace_nesting = 0; @@ -32,48 +34,82 @@ 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 (fp) - FILE *fp; +skip_to_closing_brace (cpp_reader *pfile) { int nesting = 1; for (;;) { - int c = get_token (fp, &buf); - if (c == EOF) + enum cpp_ttype token = get_a_token (pfile)->type; + + if (token == CPP_EOF) break; - if (c == '{') + if (token == CPP_OPEN_BRACE) nesting++; - if (c == '}' && --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 certain - other interesting sequences (external variables and macros). */ + 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 (fp) - FILE *fp; +scan_decls (cpp_reader *pfile, int argc ATTRIBUTE_UNUSED, + char **argv ATTRIBUTE_UNUSED) { - int c; int saw_extern, saw_inline; + cpp_token prev_id; + const cpp_token *token; new_statement: - c = get_token (fp, &buf); + token = get_a_token (pfile); + handle_statement: current_extern_C = 0; saw_extern = 0; saw_inline = 0; - if (c == '}') + if (token->type == CPP_OPEN_BRACE) { /* Pop an 'extern "C"' nesting level, if appropriate. */ if (extern_C_braces_length @@ -82,121 +118,133 @@ scan_decls (fp) brace_nesting--; goto new_statement; } - if (c == '{') + if (token->type == CPP_OPEN_BRACE) { brace_nesting++; goto new_statement; } - if (c == EOF) + + if (token->type == CPP_EOF) return 0; - if (c == ';') + + if (token->type == CPP_SEMICOLON) goto new_statement; - if (c != IDENTIFIER_TOKEN) + if (token->type != CPP_NAME) goto new_statement; - rtype.ptr = rtype.base; - if (SSTRING_LENGTH (&buf) > 16 - && strncmp (buf.base, "__DEFINED_MACRO_", 16) == 0) - { - /* For certain interesting macro names, fixproto puts - #ifdef FOO - __DEFINED_MACRO_FOO - #endif - into the file to be pre-processed. So if we see __DEFINED_MACRO_FOO, - it means FOO was defined, which we may want to make a note of. */ - recognized_macro (buf.base+16); - goto new_statement; - } - if (strcmp (buf.base, "inline") == 0) - { - saw_inline = 1; - c = get_token (fp, &buf); - } - if (strcmp (buf.base, "extern") == 0) + + prev_id.type = CPP_EOF; + for (;;) { - saw_extern = 1; - c = get_token (fp, &buf); - if (c == STRING_TOKEN && strcmp (buf.base, "C") == 0) + switch (token->type) { - current_extern_C = 1; - c = get_token (fp, &buf); - if (c == '{') + 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) { - brace_nesting++; - extern_C_braces[extern_C_braces_length++] = brace_nesting; - goto new_statement; + recognized_extern (&prev_id); } - c = get_token (fp, &buf); - } - } - for (;;) - { - int followingc = getc (fp); /* char following token in buf */ - - MAKE_SSTRING_SPACE (&rtype, 1); - *rtype.ptr = 0; - - if (c == IDENTIFIER_TOKEN) - { - int nextc = skip_spaces (fp, followingc); - if (nextc == '(') + 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.type != CPP_EOF) { int nesting = 1; - int func_lineno = source_lineno; - char *args; - - arg_list.ptr = arg_list.base; + int have_arg_list = 0; + const struct line_map *map; + unsigned int line; for (;;) { - c = getc (fp); - if (c == '(') + token = get_a_token (pfile); + if (token->type == CPP_OPEN_PAREN) nesting++; - else if (c == ')') - if (--nesting == 0) - break; - if (c == EOF) - break; - if (c == '\n') + else if (token->type == CPP_CLOSE_PAREN) { - c = ' '; - source_lineno++; - lineno++; + nesting--; + if (nesting == 0) + break; } - SSTRING_PUT (&arg_list, c); + else if (token->type == CPP_EOF) + break; + else if (token->type == CPP_NAME + || token->type == CPP_ELLIPSIS) + have_arg_list = 1; } - SSTRING_PUT (&arg_list, '\0'); - args = arg_list.base; - while (*args == ' ') - args++; - recognized_function (buf.base, + 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'), - rtype.base, args, - source_filename.base, func_lineno); - c = get_token (fp, &buf); - if (c == '{') + ? '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 (fp); + skip_to_closing_brace (pfile); goto new_statement; } - goto handle_statement; + + /* 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); + if (token->type == CPP_EOF) + return 0; + goto new_statement; + } + break; + case CPP_NAME: + /* "inline" and "extern" are recognized but skipped */ + if (cpp_ideq (token, "inline")) + { + saw_inline = 1; } - else if (nextc == ';' && saw_extern) + else if (cpp_ideq (token, "extern")) { - recognized_extern (buf.base, rtype.base); - goto handle_statement; + saw_extern = 1; + token = get_a_token (pfile); + if (token->type == CPP_STRING + && token->val.str.len == 1 + && token->val.str.text[0] == 'C') + { + current_extern_C = 1; + 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 + continue; + break; } - else - ungetc (nextc, fp); + /* This may be the name of a variable or function. */ + prev_id = *token; + break; } - else if (followingc != EOF) - ungetc (followingc, fp); - if (c == ';' || c == '{' || c == '}' || c == EOF) - goto handle_statement; - sstring_append (&rtype, &buf); - if (followingc == ' ' || followingc == '\t' || followingc == '\n') - SSTRING_PUT (&rtype, ' '); - c = get_token (fp, &buf); + token = get_a_token (pfile); } }