1 /* scan-decls.c - Extracts declarations from cpp output.
2 Copyright (C) 1993, 1995, 1997, 1998,
3 1999, 2000, 2003 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 (cpp_reader
*);
29 static const cpp_token
*get_a_token (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
*
46 get_a_token (cpp_reader
*pfile
)
50 const cpp_token
*result
= cpp_get_token (pfile
);
51 if (result
->type
!= CPP_PADDING
)
57 skip_to_closing_brace (cpp_reader
*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 (cpp_reader
*pfile
, int argc ATTRIBUTE_UNUSED
,
97 char **argv ATTRIBUTE_UNUSED
)
99 int saw_extern
, saw_inline
;
101 const cpp_token
*token
;
104 token
= get_a_token (pfile
);
107 current_extern_C
= 0;
110 if (token
->type
== CPP_OPEN_BRACE
)
112 /* Pop an 'extern "C"' nesting level, if appropriate. */
113 if (extern_C_braces_length
114 && extern_C_braces
[extern_C_braces_length
- 1] == brace_nesting
)
115 extern_C_braces_length
--;
119 if (token
->type
== CPP_OPEN_BRACE
)
125 if (token
->type
== CPP_EOF
)
128 if (token
->type
== CPP_SEMICOLON
)
130 if (token
->type
!= CPP_NAME
)
133 prev_id
.type
= CPP_EOF
;
139 goto handle_statement
;
147 if (prev_id
.type
!= CPP_EOF
&& saw_extern
)
149 recognized_extern (&prev_id
);
151 if (token
->type
== CPP_COMMA
)
153 /* ... fall through ... */
154 case CPP_OPEN_BRACE
: case CPP_CLOSE_BRACE
:
161 /* Looks like this is the start of a formal parameter list. */
162 if (prev_id
.type
!= CPP_EOF
)
165 int have_arg_list
= 0;
168 token
= get_a_token (pfile
);
169 if (token
->type
== CPP_OPEN_PAREN
)
171 else if (token
->type
== CPP_CLOSE_PAREN
)
177 else if (token
->type
== CPP_EOF
)
179 else if (token
->type
== CPP_NAME
180 || token
->type
== CPP_ELLIPSIS
)
183 recognized_function (&prev_id
, token
->line
,
185 : in_extern_C_brace
|| current_extern_C
186 ? 'F' : 'f'), have_arg_list
);
187 token
= get_a_token (pfile
);
188 if (token
->type
== CPP_OPEN_BRACE
)
190 /* skip body of (normally) inline function */
191 skip_to_closing_brace (pfile
);
195 /* skip a possible __attribute__ or throw expression after the
197 while (token
->type
!= CPP_SEMICOLON
&& token
->type
!= CPP_EOF
)
198 token
= get_a_token (pfile
);
203 /* "inline" and "extern" are recognized but skipped */
204 if (cpp_ideq (token
, "inline"))
208 else if (cpp_ideq (token
, "extern"))
211 token
= get_a_token (pfile
);
212 if (token
->type
== CPP_STRING
213 && token
->val
.str
.len
== 1
214 && token
->val
.str
.text
[0] == 'C')
216 current_extern_C
= 1;
217 token
= get_a_token (pfile
);
218 if (token
->type
== CPP_OPEN_BRACE
)
221 extern_C_braces
[extern_C_braces_length
++]
230 /* This may be the name of a variable or function. */
234 token
= get_a_token (pfile
);