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
*));
27 static const cpp_token
*get_a_token
PARAMS ((cpp_reader
*));
29 int brace_nesting
= 0;
31 /* The first extern_C_braces_length elements of extern_C_braces
32 indicate the (brace nesting levels of) left braces that were
33 prefixed by extern "C". */
34 int extern_C_braces_length
= 0;
35 char extern_C_braces
[20];
36 #define in_extern_C_brace (extern_C_braces_length>0)
38 /* True if the function declaration currently being scanned is
39 prefixed by extern "C". */
40 int current_extern_C
= 0;
42 /* Get a token but skip padding. */
43 static const cpp_token
*
49 const cpp_token
*result
= cpp_get_token (pfile
);
50 if (result
->type
!= CPP_PADDING
)
56 skip_to_closing_brace (pfile
)
62 enum cpp_ttype token
= get_a_token (pfile
)->type
;
66 if (token
== CPP_OPEN_BRACE
)
68 if (token
== CPP_CLOSE_BRACE
&& --nesting
== 0)
73 /* This function scans a C source file (actually, the output of cpp),
74 reading from FP. It looks for function declarations, and
75 external variable declarations.
77 The following grammar (as well as some extra stuff) is recognized:
80 (decl-specifier)* declarator ("," declarator)* ";"
86 (ptr-operator)* dname [ "(" argument-declaration-list ")" ]
88 ("*" | "&") ("const" | "volatile")*
92 Here dname is the actual name being declared.
96 scan_decls (pfile
, argc
, argv
)
98 int argc ATTRIBUTE_UNUSED
;
99 char **argv ATTRIBUTE_UNUSED
;
101 int saw_extern
, saw_inline
;
103 const cpp_token
*token
;
106 token
= get_a_token (pfile
);
109 current_extern_C
= 0;
112 if (token
->type
== CPP_OPEN_BRACE
)
114 /* Pop an 'extern "C"' nesting level, if appropriate. */
115 if (extern_C_braces_length
116 && extern_C_braces
[extern_C_braces_length
- 1] == brace_nesting
)
117 extern_C_braces_length
--;
121 if (token
->type
== CPP_OPEN_BRACE
)
127 if (token
->type
== CPP_EOF
)
130 if (token
->type
== CPP_SEMICOLON
)
132 if (token
->type
!= CPP_NAME
)
135 prev_id
.type
= CPP_EOF
;
141 goto handle_statement
;
149 if (prev_id
.type
!= CPP_EOF
&& saw_extern
)
151 recognized_extern (&prev_id
);
153 if (token
->type
== CPP_COMMA
)
155 /* ... fall through ... */
156 case CPP_OPEN_BRACE
: case CPP_CLOSE_BRACE
:
163 /* Looks like this is the start of a formal parameter list. */
164 if (prev_id
.type
!= CPP_EOF
)
167 int have_arg_list
= 0;
170 token
= get_a_token (pfile
);
171 if (token
->type
== CPP_OPEN_PAREN
)
173 else if (token
->type
== CPP_CLOSE_PAREN
)
179 else if (token
->type
== CPP_EOF
)
181 else if (token
->type
== CPP_NAME
182 || token
->type
== CPP_ELLIPSIS
)
185 recognized_function (&prev_id
, token
->line
,
187 : in_extern_C_brace
|| current_extern_C
188 ? 'F' : 'f'), have_arg_list
);
189 token
= get_a_token (pfile
);
190 if (token
->type
== CPP_OPEN_BRACE
)
192 /* skip body of (normally) inline function */
193 skip_to_closing_brace (pfile
);
197 /* skip a possible __attribute__ or throw expression after the
199 while (token
->type
!= CPP_SEMICOLON
&& token
->type
!= CPP_EOF
)
200 token
= get_a_token (pfile
);
205 /* "inline" and "extern" are recognized but skipped */
206 if (cpp_ideq (token
, "inline"))
210 else if (cpp_ideq (token
, "extern"))
213 token
= get_a_token (pfile
);
214 if (token
->type
== CPP_STRING
215 && token
->val
.str
.len
== 1
216 && token
->val
.str
.text
[0] == 'C')
218 current_extern_C
= 1;
219 token
= get_a_token (pfile
);
220 if (token
->type
== CPP_OPEN_BRACE
)
223 extern_C_braces
[extern_C_braces_length
++]
232 /* This may be the name of a variable or function. */
236 token
= get_a_token (pfile
);