1 /* scan-decls.c - Extracts declarations from cpp output.
2 Copyright (C) 1993, 1995, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 Written by Per Bothner <bothner@cygnus.com>, July 1993. */
26 static void skip_to_closing_brace PARAMS ((cpp_reader *));
28 int brace_nesting = 0;
30 /* The first extern_C_braces_length elements of extern_C_braces
31 indicate the (brace nesting levels of) left braces that were
32 prefixed by extern "C". */
33 int extern_C_braces_length = 0;
34 char extern_C_braces[20];
35 #define in_extern_C_brace (extern_C_braces_length>0)
37 /* True if the function declaration currently being scanned is
38 prefixed by extern "C". */
39 int current_extern_C = 0;
42 skip_to_closing_brace (pfile)
48 enum cpp_ttype token = cpp_get_token (pfile);
51 if (token == CPP_OPEN_BRACE)
53 if (token == CPP_CLOSE_BRACE && --nesting == 0)
58 /* This function scans a C source file (actually, the output of cpp),
59 reading from FP. It looks for function declarations, and
60 external variable declarations.
62 The following grammar (as well as some extra stuff) is recognized:
65 (decl-specifier)* declarator ("," declarator)* ";"
71 (ptr-operator)* dname [ "(" argument-declaration-list ")" ]
73 ("*" | "&") ("const" | "volatile")*
77 Here dname is the actual name being declared.
81 scan_decls (pfile, argc, argv)
83 int argc ATTRIBUTE_UNUSED;
84 char **argv ATTRIBUTE_UNUSED;
86 int saw_extern, saw_inline;
88 /* If declarator_start is non-zero, it marks the start of the current
89 declarator. If it is zero, we are either still parsing the
90 decl-specs, or prev_id_start marks the start of the declarator. */
92 int prev_id_start, prev_id_end = 0;
96 CPP_SET_WRITTEN (pfile, 0);
98 token = cpp_get_token (pfile);
101 current_extern_C = 0;
104 if (token == CPP_OPEN_BRACE)
106 /* Pop an 'extern "C"' nesting level, if appropriate. */
107 if (extern_C_braces_length
108 && extern_C_braces[extern_C_braces_length - 1] == brace_nesting)
109 extern_C_braces_length--;
113 if (token == CPP_OPEN_BRACE)
118 if (token == CPP_EOF)
120 if (token == CPP_SEMICOLON)
122 if (token != CPP_NAME)
126 declarator_start = 0;
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);
138 unsigned int func_lineno = CPP_BUF_LINE (fbuf);
141 token = cpp_get_token (pfile);
142 if (token == CPP_OPEN_PAREN)
144 else if (token == CPP_CLOSE_PAREN)
150 else if (token == CPP_EOF)
152 else if (token == CPP_NAME || token == CPP_ELLIPSIS)
155 recognized_function (pfile->token_buffer + prev_id_start,
156 prev_id_end - prev_id_start,
158 : in_extern_C_brace || current_extern_C
160 pfile->token_buffer, prev_id_start,
162 fbuf->nominal_fname, func_lineno);
163 token = cpp_get_non_space_token (pfile);
164 if (token == CPP_OPEN_BRACE)
166 /* skip body of (normally) inline function */
167 skip_to_closing_brace (pfile);
170 goto maybe_handle_comma;
174 if (CPP_WRITTEN (pfile) == (size_t) start_written + 1
175 && (CPP_PWRITTEN (pfile)[-1] == '*'
176 || CPP_PWRITTEN (pfile)[-1] == '&'))
177 declarator_start = start_written;
179 goto handle_statement;
183 if (prev_id_start && saw_extern)
185 recognized_extern (pfile->token_buffer + prev_id_start,
186 prev_id_end - prev_id_start,
190 /* ... fall through ... */
192 if (token != CPP_COMMA)
197 /* Handle multiple declarators in a single declaration,
198 as in: extern char *strcpy (), *strcat (), ... ; */
199 if (declarator_start == 0)
200 declarator_start = prev_id_start;
201 CPP_SET_WRITTEN (pfile, declarator_start);
204 /* "inline" and "extern" are recognized but skipped */
205 if (strcmp (pfile->token_buffer, "inline") == 0)
208 CPP_SET_WRITTEN (pfile, start_written);
210 if (strcmp (pfile->token_buffer, "extern") == 0)
213 CPP_SET_WRITTEN (pfile, start_written);
214 token = cpp_get_non_space_token (pfile);
215 if (token == CPP_STRING
216 && strcmp (pfile->token_buffer, "\"C\"") == 0)
218 CPP_SET_WRITTEN (pfile, start_written);
219 current_extern_C = 1;
220 token = cpp_get_non_space_token (pfile);
221 if (token == CPP_OPEN_BRACE)
224 extern_C_braces[extern_C_braces_length++]
233 /* This may be the name of a variable or function. */
234 prev_id_start = start_written;
235 prev_id_end = CPP_WRITTEN (pfile);
241 case CPP_OPEN_BRACE: case CPP_CLOSE_BRACE: case CPP_DIRECTIVE:
242 goto new_statement; /* handle_statement? */
244 case CPP_HSPACE: case CPP_VSPACE: case CPP_COMMENT: case CPP_POP:
245 /* Skip initial white space. */
246 if (start_written == 0)
247 CPP_SET_WRITTEN (pfile, 0);
254 start_written = CPP_WRITTEN (pfile);
255 token = cpp_get_token (pfile);