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. */
23 #include "coretypes.h"
28 static void skip_to_closing_brace
PARAMS ((cpp_reader
*));
29 static const cpp_token
*get_a_token
PARAMS ((cpp_reader
*));
31 int brace_nesting
= 0;
33 /* The first extern_C_braces_length elements of extern_C_braces
34 indicate the (brace nesting levels of) left braces that were
35 prefixed by extern "C". */
36 int extern_C_braces_length
= 0;
37 char extern_C_braces
[20];
38 #define in_extern_C_brace (extern_C_braces_length>0)
40 /* True if the function declaration currently being scanned is
41 prefixed by extern "C". */
42 int current_extern_C
= 0;
44 /* Get a token but skip padding. */
45 static const cpp_token
*
51 const cpp_token
*result
= cpp_get_token (pfile
);
52 if (result
->type
!= CPP_PADDING
)
58 skip_to_closing_brace (pfile
)
64 enum cpp_ttype token
= get_a_token (pfile
)->type
;
68 if (token
== CPP_OPEN_BRACE
)
70 if (token
== CPP_CLOSE_BRACE
&& --nesting
== 0)
75 /* This function scans a C source file (actually, the output of cpp),
76 reading from FP. It looks for function declarations, and
77 external variable declarations.
79 The following grammar (as well as some extra stuff) is recognized:
82 (decl-specifier)* declarator ("," declarator)* ";"
88 (ptr-operator)* dname [ "(" argument-declaration-list ")" ]
90 ("*" | "&") ("const" | "volatile")*
94 Here dname is the actual name being declared.
98 scan_decls (pfile
, argc
, argv
)
100 int argc ATTRIBUTE_UNUSED
;
101 char **argv ATTRIBUTE_UNUSED
;
103 int saw_extern
, saw_inline
;
105 const cpp_token
*token
;
108 token
= get_a_token (pfile
);
111 current_extern_C
= 0;
114 if (token
->type
== CPP_OPEN_BRACE
)
116 /* Pop an 'extern "C"' nesting level, if appropriate. */
117 if (extern_C_braces_length
118 && extern_C_braces
[extern_C_braces_length
- 1] == brace_nesting
)
119 extern_C_braces_length
--;
123 if (token
->type
== CPP_OPEN_BRACE
)
129 if (token
->type
== CPP_EOF
)
132 if (token
->type
== CPP_SEMICOLON
)
134 if (token
->type
!= CPP_NAME
)
137 prev_id
.type
= CPP_EOF
;
143 goto handle_statement
;
151 if (prev_id
.type
!= CPP_EOF
&& saw_extern
)
153 recognized_extern (&prev_id
);
155 if (token
->type
== CPP_COMMA
)
157 /* ... fall through ... */
158 case CPP_OPEN_BRACE
: case CPP_CLOSE_BRACE
:
165 /* Looks like this is the start of a formal parameter list. */
166 if (prev_id
.type
!= CPP_EOF
)
169 int have_arg_list
= 0;
172 token
= get_a_token (pfile
);
173 if (token
->type
== CPP_OPEN_PAREN
)
175 else if (token
->type
== CPP_CLOSE_PAREN
)
181 else if (token
->type
== CPP_EOF
)
183 else if (token
->type
== CPP_NAME
184 || token
->type
== CPP_ELLIPSIS
)
187 recognized_function (&prev_id
, token
->line
,
189 : in_extern_C_brace
|| current_extern_C
190 ? 'F' : 'f'), have_arg_list
);
191 token
= get_a_token (pfile
);
192 if (token
->type
== CPP_OPEN_BRACE
)
194 /* skip body of (normally) inline function */
195 skip_to_closing_brace (pfile
);
199 /* skip a possible __attribute__ or throw expression after the
201 while (token
->type
!= CPP_SEMICOLON
&& token
->type
!= CPP_EOF
)
202 token
= get_a_token (pfile
);
207 /* "inline" and "extern" are recognized but skipped */
208 if (cpp_ideq (token
, "inline"))
212 else if (cpp_ideq (token
, "extern"))
215 token
= get_a_token (pfile
);
216 if (token
->type
== CPP_STRING
217 && token
->val
.str
.len
== 1
218 && token
->val
.str
.text
[0] == 'C')
220 current_extern_C
= 1;
221 token
= get_a_token (pfile
);
222 if (token
->type
== CPP_OPEN_BRACE
)
225 extern_C_braces
[extern_C_braces_length
++]
234 /* This may be the name of a variable or function. */
238 token
= get_a_token (pfile
);