1 /* scan-decls.c - Extracts declarations from cpp output.
2 Copyright (C) 1993, 1995 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 2, or (at your option) any
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA.
19 Written by Per Bothner <bothner@cygnus.com>, July 1993. */
26 int brace_nesting = 0;
28 /* The first extern_C_braces_length elements of extern_C_braces
29 indicate the (brace nesting levels of) left braces that were
30 prefixed by extern "C". */
31 int extern_C_braces_length = 0;
32 char extern_C_braces[20];
33 #define in_extern_C_brace (extern_C_braces_length>0)
35 /* True if the function declaration currently being scanned is
36 prefixed by extern "C". */
37 int current_extern_C = 0;
40 skip_to_closing_brace (pfile)
46 enum cpp_token token = cpp_get_token (pfile);
49 if (token == CPP_LBRACE)
51 if (token == CPP_RBRACE && --nesting == 0)
56 /* This function scans a C source file (actually, the output of cpp),
57 reading from FP. It looks for function declarations, and
58 external variable declarations.
60 The following grammar (as well as some extra stuff) is recognized:
63 (decl-specifier)* declarator ("," declarator)* ";"
69 (ptr-operator)* dname [ "(" argument-declaration-list ")" ]
71 ("*" | "&") ("const" | "volatile")*
75 Here dname is the actual name being declared.
79 scan_decls (pfile, argc, argv)
84 int saw_extern, saw_inline;
86 /* If declarator_start is non-zero, it marks the start of the current
87 declarator. If it is zero, we are either still parsing the
88 decl-specs, or prev_id_start marks the start of the declarator. */
90 int prev_id_start, prev_id_end;
94 CPP_SET_WRITTEN (pfile, 0);
95 token = cpp_get_token (pfile);
101 if (token == CPP_RBRACE)
103 /* Pop an 'extern "C"' nesting level, if appropriate. */
104 if (extern_C_braces_length
105 && extern_C_braces[extern_C_braces_length - 1] == brace_nesting)
106 extern_C_braces_length--;
110 if (token == CPP_LBRACE)
115 if (token == CPP_EOF)
117 if (token == CPP_SEMICOLON)
119 if (token != CPP_NAME)
123 declarator_start = 0;
126 int start_written = CPP_WRITTEN (pfile);
127 token = cpp_get_token (pfile);
132 /* Looks like this is the start of a formal parameter list. */
136 int have_arg_list = 0;
137 cpp_buffer *fbuf = cpp_file_buffer (pfile);
139 cpp_buf_line_and_col (fbuf, &func_lineno, NULL);
142 token = cpp_get_token (pfile);
143 if (token == CPP_LPAREN)
145 else if (token == CPP_RPAREN)
151 else if (token == CPP_EOF)
153 else if (token == CPP_NAME || token == CPP_3DOTS)
156 recognized_function (pfile->token_buffer + prev_id_start,
157 prev_id_end - prev_id_start,
159 : in_extern_C_brace || current_extern_C
161 pfile->token_buffer, prev_id_start,
163 fbuf->nominal_fname, func_lineno);
164 token = cpp_get_non_space_token (pfile);
165 if (token == CPP_LBRACE)
167 /* skip body of (normally) inline function */
168 skip_to_closing_brace (pfile);
171 goto maybe_handle_comma;
175 if (CPP_WRITTEN (pfile) == start_written + 1
176 && (CPP_PWRITTEN (pfile)[-1] == '*'
177 || CPP_PWRITTEN (pfile)[-1] == '&'))
178 declarator_start = start_written;
180 goto handle_statement;
184 if (prev_id_start && saw_extern)
186 recognized_extern (pfile->token_buffer + prev_id_start,
187 prev_id_end - prev_id_start,
191 /* ... fall through ... */
193 if (token != CPP_COMMA)
196 /* Handle multiple declarators in a single declaration,
197 as in: extern char *strcpy (), *strcat (), ... ; */
198 if (declarator_start == 0)
199 declarator_start = prev_id_start;
200 CPP_SET_WRITTEN (pfile, declarator_start);
203 /* "inline" and "extern" are recognized but skipped */
204 if (strcmp (pfile->token_buffer, "inline") == 0)
207 CPP_SET_WRITTEN (pfile, start_written);
209 if (strcmp (pfile->token_buffer, "extern") == 0)
212 CPP_SET_WRITTEN (pfile, start_written);
213 token = cpp_get_non_space_token (pfile);
214 if (token == CPP_STRING
215 && strcmp (pfile->token_buffer, "\"C\"") == 0)
217 CPP_SET_WRITTEN (pfile, start_written);
218 current_extern_C = 1;
219 token = cpp_get_non_space_token (pfile);
220 if (token == CPP_LBRACE)
223 extern_C_braces[extern_C_braces_length++]
232 /* This may be the name of a variable or function. */
233 prev_id_start = start_written;
234 prev_id_end = CPP_WRITTEN (pfile);
238 return; /* ??? FIXME */
240 case CPP_LBRACE: case CPP_RBRACE: case CPP_DIRECTIVE:
241 goto new_statement; /* handle_statement? */
243 case CPP_HSPACE: case CPP_VSPACE: case CPP_COMMENT: case CPP_POP:
244 /* Skip initial white space. */
245 if (start_written == 0)
246 CPP_SET_WRITTEN (pfile, 0);
250 prev_id_start = NULL;