2 * Copyright (c) 1987, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)C.c 8.4 (Berkeley) 4/2/94
30 * $FreeBSD: src/usr.bin/ctags/C.c,v 1.3.2.2 2002/07/30 00:55:07 tjr Exp $
31 * $DragonFly: src/usr.bin/ctags/C.c,v 1.2 2003/06/17 04:29:25 dillon Exp $
40 static int func_entry(void);
41 static void hash_entry(void);
42 static void skip_string(int);
43 static int str_entry(int);
47 * read .c and .h files and call appropriate routines
52 int c
; /* current character */
53 int level
; /* brace level */
54 int token
; /* if reading a token */
55 int t_def
; /* if reading a typedef */
56 int t_level
; /* typedef's brace level */
57 char *sp
; /* buffer pointer */
58 char tok
[MAXTOKEN
]; /* token buffer */
60 lineftell
= ftell(inf
);
61 sp
= tok
; token
= t_def
= NO
; t_level
= -1; level
= 0; lineno
= 1;
62 while (GETC(!=, EOF
)) {
65 * Here's where it DOESN'T handle: {
72 * puts("hello, world");
80 * if level goes below zero, try and fix
81 * it, even though we've already messed up
90 * the above 3 cases are similar in that they
91 * are special characters that also end tokens.
93 endtok
: if (sp
> tok
) {
103 * We ignore quoted strings and character constants
108 (void)skip_string(c
);
112 * comments can be fun; note the state is unchanged after
113 * return, in case we found:
114 * "foo() XX comment XX { int bar; }"
117 if (GETC(==, '*') || c
== '/') {
121 (void)ungetc(c
, inf
);
125 /* hash marks flag #define's. */
134 * if we have a current token, parenthesis on
135 * level zero indicates a function.
138 if (!level
&& token
) {
144 * grab the line immediately, we may
145 * already be wrong, for example,
153 pfnote(tok
, curline
);
160 * semi-colons indicate the end of a typedef; if we find a
161 * typedef we search for the next semi-colon of the same
162 * level as the typedef. Ignoring "structs", they are
163 * tricky, since you can find:
165 * "typedef long time_t;"
166 * "typedef unsigned int u_int;"
167 * "typedef unsigned int u_int [10];"
169 * If looking at a typedef, we save a copy of the last token
170 * found. Then, when we find the ';' we take the current
171 * token if it starts with a valid token name, else we take
172 * the one we saved. There's probably some reasonable
173 * alternative to this...
176 if (t_def
&& level
== t_level
) {
187 * store characters until one that can't be part of a token
188 * comes along; check the current token against certain
192 /* ignore whitespace */
193 if (c
== ' ' || c
== '\t') {
195 while (GETC(!=, EOF
) && (c
== ' ' || c
== '\t'))
199 (void)ungetc(c
, inf
);
202 storec
: if (!intoken(c
)) {
207 /* no typedefs inside typedefs */
209 !memcmp(tok
, "typedef",8)) {
214 /* catch "typedef struct" */
215 if ((!t_def
|| t_level
< level
)
216 && (!memcmp(tok
, "struct", 7)
217 || !memcmp(tok
, "union", 6)
218 || !memcmp(tok
, "enum", 5))) {
220 * get line immediately;
221 * may change before '{'
232 else if (sp
!= tok
|| begtoken(c
)) {
233 if (sp
== tok
+ sizeof tok
- 1)
234 /* Too long -- truncate it */
250 * handle a function reference
255 int c
; /* current character */
256 int level
= 0; /* for matching '()' */
259 * Find the end of the assumed function declaration.
260 * Note that ANSI C functions can have type definitions so keep
261 * track of the parentheses nesting level.
263 while (GETC(!=, EOF
)) {
267 /* skip strings and character constants */
272 if (GETC(==, '*') || c
== '/')
290 * we assume that the character after a function's right paren
291 * is a token character if it's a function and a non-token
292 * character if it's a declaration. Comments don't count...
295 while (GETC(!=, EOF
) && iswhite(c
))
298 if (intoken(c
) || c
== '{')
300 if (c
== '/' && (GETC(==, '*') || c
== '/'))
302 else { /* don't ever "read" '/' */
303 (void)ungetc(c
, inf
);
314 * handle a line starting with a '#'
319 int c
; /* character read */
320 int curline
; /* line started on */
321 char *sp
; /* buffer pointer */
322 char tok
[MAXTOKEN
]; /* storage buffer */
324 /* ignore leading whitespace */
325 while (GETC(!=, EOF
) && (c
== ' ' || c
== '\t'))
327 (void)ungetc(c
, inf
);
330 for (sp
= tok
;;) { /* get next token */
335 if (sp
== tok
+ sizeof tok
- 1)
336 /* Too long -- truncate it */
342 if (memcmp(tok
, "define", 6)) /* only interested in #define's */
344 for (;;) { /* this doesn't handle "#define \n" */
350 for (sp
= tok
;;) { /* get next token */
351 if (sp
== tok
+ sizeof tok
- 1)
352 /* Too long -- truncate it */
359 * this is where it DOESN'T handle
366 if (dflag
|| c
== '(') { /* only want macros */
368 pfnote(tok
, curline
);
370 skip
: if (c
== '\n') { /* get rid of rest of define */
372 if (*(sp
- 1) != '\\')
375 (void)skip_key('\n');
380 * handle a struct, union or enum entry
383 str_entry(int c
) /* c is current character */
385 int curline
; /* line started on */
386 char *sp
; /* buffer pointer */
387 char tok
[LINE_MAX
]; /* storage buffer */
393 if (c
== '{') /* it was "struct {" */
395 for (sp
= tok
;;) { /* get next token */
396 if (sp
== tok
+ sizeof tok
- 1)
397 /* Too long -- truncate it */
407 case '{': /* it was "struct foo{" */
410 case '\n': /* it was "struct foo\n" */
413 default: /* probably "struct foo " */
414 while (GETC(!=, EOF
))
418 (void)ungetc(c
, inf
);
423 pfnote(tok
, curline
);
432 skip_comment(int t
) /* t is comment character */
434 int c
; /* character read */
435 int star
; /* '*' flag */
437 for (star
= 0; GETC(!=, EOF
);)
439 /* comments don't nest, nor can they be escaped. */
444 if (star
&& t
== '*')
460 * skip to the end of a string or character constant.
468 for (skip
= NO
; GETC(!=, EOF
); )
470 case '\\': /* a backslash escapes anything */
471 skip
= !skip
; /* we toggle in case it's "\\" */
477 if (c
== key
&& !skip
)
485 * skip to next char "key"
494 for (skip
= retval
= NO
; GETC(!=, EOF
);)
496 case '\\': /* a backslash escapes anything */
497 skip
= !skip
; /* we toggle in case it's "\\" */
499 case ';': /* special case for yacc; if one */
500 case '|': /* of these chars occurs, we may */
501 retval
= YES
; /* have moved out of the rule */
502 break; /* not used by C */
505 /* skip strings and character constants */
510 if (GETC(==, '*') || c
== '/') {
514 (void)ungetc(c
, inf
);
522 if (c
== key
&& !skip
)