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, Boston, MA 02111-1307, USA.
18 Written by Per Bothner <bothner@cygnus.com>, July 1993. */
25 int brace_nesting
= 0;
27 /* The first extern_C_braces_length elements of extern_C_braces
28 indicate the (brace nesting levels of) left braces that were
29 prefixed by extern "C". */
30 int extern_C_braces_length
= 0;
31 char extern_C_braces
[20];
32 #define in_extern_C_brace (extern_C_braces_length>0)
34 /* True if the function declaration currently being scanned is
35 prefixed by extern "C". */
36 int current_extern_C
= 0;
39 skip_to_closing_brace (pfile
)
45 enum cpp_token token
= cpp_get_token (pfile
);
48 if (token
== CPP_LBRACE
)
50 if (token
== CPP_RBRACE
&& --nesting
== 0)
55 /* This function scans a C source file (actually, the output of cpp),
56 reading from FP. It looks for function declarations, and
57 external variable declarations.
59 The following grammar (as well as some extra stuff) is recognized:
62 (decl-specifier)* declarator ("," declarator)* ";"
68 (ptr-operator)* dname [ "(" argument-declaration-list ")" ]
70 ("*" | "&") ("const" | "volatile")*
74 Here dname is the actual name being declared.
78 scan_decls (pfile
, argc
, argv
)
83 int saw_extern
, saw_inline
;
85 /* If declarator_start is non-zero, it marks the start of the current
86 declarator. If it is zero, we are either still parsing the
87 decl-specs, or prev_id_start marks the start of the declarator. */
89 int prev_id_start
, prev_id_end
;
93 CPP_SET_WRITTEN (pfile
, 0);
94 token
= cpp_get_token (pfile
);
100 if (token
== CPP_RBRACE
)
102 /* Pop an 'extern "C"' nesting level, if appropriate. */
103 if (extern_C_braces_length
104 && extern_C_braces
[extern_C_braces_length
- 1] == brace_nesting
)
105 extern_C_braces_length
--;
109 if (token
== CPP_LBRACE
)
114 if (token
== CPP_EOF
)
116 if (token
== CPP_SEMICOLON
)
118 if (token
!= CPP_NAME
)
122 declarator_start
= 0;
125 int start_written
= CPP_WRITTEN (pfile
);
126 token
= cpp_get_token (pfile
);
131 /* Looks like this is the start of a formal parameter list. */
135 int have_arg_list
= 0;
136 cpp_buffer
*fbuf
= cpp_file_buffer (pfile
);
138 cpp_buf_line_and_col (fbuf
, &func_lineno
, NULL
);
141 token
= cpp_get_token (pfile
);
142 if (token
== CPP_LPAREN
)
144 else if (token
== CPP_RPAREN
)
150 else if (token
== CPP_EOF
)
152 else if (token
== CPP_NAME
|| token
== CPP_3DOTS
)
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_LBRACE
)
166 /* skip body of (normally) inline function */
167 skip_to_closing_brace (pfile
);
170 goto maybe_handle_comma
;
174 if (CPP_WRITTEN (pfile
) == 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
)
195 /* Handle multiple declarators in a single declaration,
196 as in: extern char *strcpy (), *strcat (), ... ; */
197 if (declarator_start
== 0)
198 declarator_start
= prev_id_start
;
199 CPP_SET_WRITTEN (pfile
, declarator_start
);
202 /* "inline" and "extern" are recognized but skipped */
203 if (strcmp (pfile
->token_buffer
, "inline") == 0)
206 CPP_SET_WRITTEN (pfile
, start_written
);
208 if (strcmp (pfile
->token_buffer
, "extern") == 0)
211 CPP_SET_WRITTEN (pfile
, start_written
);
212 token
= cpp_get_non_space_token (pfile
);
213 if (token
== CPP_STRING
214 && strcmp (pfile
->token_buffer
, "\"C\"") == 0)
216 CPP_SET_WRITTEN (pfile
, start_written
);
217 current_extern_C
= 1;
218 token
= cpp_get_non_space_token (pfile
);
219 if (token
== CPP_LBRACE
)
222 extern_C_braces
[extern_C_braces_length
++]
231 /* This may be the name of a variable or function. */
232 prev_id_start
= start_written
;
233 prev_id_end
= CPP_WRITTEN (pfile
);
237 return; /* ??? FIXME */
239 case CPP_LBRACE
: case CPP_RBRACE
: case CPP_DIRECTIVE
:
240 goto new_statement
; /* handle_statement? */
242 case CPP_HSPACE
: case CPP_VSPACE
: case CPP_COMMENT
: case CPP_POP
:
243 /* Skip initial white space. */
244 if (start_written
== 0)
245 CPP_SET_WRITTEN (pfile
, 0);