[BACK]Return to C.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ctags

Annotation of src/usr.bin/ctags/C.c, Revision 1.11

1.11    ! avsm        1: /*     $OpenBSD: C.c,v 1.10 2003/06/12 20:58:09 deraadt Exp $  */
1.1       deraadt     2: /*     $NetBSD: C.c,v 1.3 1995/03/26 20:14:02 glass Exp $      */
                      3:
                      4: /*
                      5:  * Copyright (c) 1987, 1993, 1994
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
1.9       millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
                     33: #ifndef lint
                     34: #if 0
                     35: static char sccsid[] = "@(#)C.c        8.4 (Berkeley) 4/2/94";
                     36: #else
1.11    ! avsm       37: static char rcsid[] = "$OpenBSD: C.c,v 1.10 2003/06/12 20:58:09 deraadt Exp $";
1.1       deraadt    38: #endif
                     39: #endif /* not lint */
                     40:
                     41: #include <limits.h>
                     42: #include <stdio.h>
                     43: #include <string.h>
                     44:
                     45: #include "ctags.h"
                     46:
1.7       millert    47: static int     func_entry(void);
                     48: static void    hash_entry(void);
                     49: static void    skip_string(int);
                     50: static int     str_entry(int);
1.1       deraadt    51:
                     52: /*
                     53:  * c_entries --
                     54:  *     read .c and .h files and call appropriate routines
                     55:  */
                     56: void
1.10      deraadt    57: c_entries(void)
1.1       deraadt    58: {
                     59:        int     c;                      /* current character */
                     60:        int     level;                  /* brace level */
                     61:        int     token;                  /* if reading a token */
                     62:        int     t_def;                  /* if reading a typedef */
                     63:        int     t_level;                /* typedef's brace level */
                     64:        char    *sp;                    /* buffer pointer */
                     65:        char    tok[MAXTOKEN];          /* token buffer */
                     66:
                     67:        lineftell = ftell(inf);
                     68:        sp = tok; token = t_def = NO; t_level = -1; level = 0; lineno = 1;
                     69:        while (GETC(!=, EOF)) {
                     70:                switch (c) {
                     71:                /*
                     72:                 * Here's where it DOESN'T handle: {
                     73:                 *      foo(a)
                     74:                 *      {
                     75:                 *      #ifdef notdef
                     76:                 *              }
                     77:                 *      #endif
                     78:                 *              if (a)
                     79:                 *                      puts("hello, world");
                     80:                 *      }
                     81:                 */
                     82:                case '{':
                     83:                        ++level;
                     84:                        goto endtok;
                     85:                case '}':
                     86:                        /*
                     87:                         * if level goes below zero, try and fix
                     88:                         * it, even though we've already messed up
                     89:                         */
                     90:                        if (--level < 0)
                     91:                                level = 0;
                     92:                        goto endtok;
                     93:
                     94:                case '\n':
                     95:                        SETLINE;
                     96:                        /*
                     97:                         * the above 3 cases are similar in that they
                     98:                         * are special characters that also end tokens.
                     99:                         */
1.5       deraadt   100: endtok:                        if (sp > tok) {
1.1       deraadt   101:                                *sp = EOS;
                    102:                                token = YES;
                    103:                                sp = tok;
                    104:                        }
                    105:                        else
                    106:                                token = NO;
                    107:                        continue;
                    108:
                    109:                /*
                    110:                 * We ignore quoted strings and character constants
                    111:                 * completely.
                    112:                 */
                    113:                case '"':
                    114:                case '\'':
                    115:                        (void)skip_string(c);
                    116:                        break;
                    117:
                    118:                /*
                    119:                 * comments can be fun; note the state is unchanged after
                    120:                 * return, in case we found:
                    121:                 *      "foo() XX comment XX { int bar; }"
                    122:                 */
                    123:                case '/':
                    124:                        if (GETC(==, '*')) {
1.8       pjanzen   125:                                skip_comment(c);
                    126:                                continue;
                    127:                        } else if (c == '/') {
                    128:                                skip_comment(c);
1.1       deraadt   129:                                continue;
                    130:                        }
                    131:                        (void)ungetc(c, inf);
                    132:                        c = '/';
                    133:                        goto storec;
                    134:
                    135:                /* hash marks flag #define's. */
                    136:                case '#':
                    137:                        if (sp == tok) {
                    138:                                hash_entry();
                    139:                                break;
                    140:                        }
                    141:                        goto storec;
                    142:
                    143:                /*
                    144:                 * if we have a current token, parenthesis on
                    145:                 * level zero indicates a function.
                    146:                 */
                    147:                case '(':
1.8       pjanzen   148:                        do {
                    149:                                c = getc(inf);
                    150:                        } while (iswhite(c));
                    151:                        if (c == '*')
                    152:                                break;
                    153:                        else
                    154:                                ungetc(c, inf);
1.1       deraadt   155:                        if (!level && token) {
                    156:                                int     curline;
                    157:
                    158:                                if (sp != tok)
                    159:                                        *sp = EOS;
                    160:                                /*
                    161:                                 * grab the line immediately, we may
                    162:                                 * already be wrong, for example,
                    163:                                 *      foo\n
                    164:                                 *      (arg1,
                    165:                                 */
                    166:                                getline();
                    167:                                curline = lineno;
                    168:                                if (func_entry()) {
                    169:                                        ++level;
                    170:                                        pfnote(tok, curline);
                    171:                                }
                    172:                                break;
                    173:                        }
                    174:                        goto storec;
                    175:
                    176:                /*
                    177:                 * semi-colons indicate the end of a typedef; if we find a
                    178:                 * typedef we search for the next semi-colon of the same
                    179:                 * level as the typedef.  Ignoring "structs", they are
                    180:                 * tricky, since you can find:
                    181:                 *
1.3       deraadt   182:                 *      "typedef int time_t;"
1.1       deraadt   183:                 *      "typedef unsigned int u_int;"
                    184:                 *      "typedef unsigned int u_int [10];"
                    185:                 *
                    186:                 * If looking at a typedef, we save a copy of the last token
                    187:                 * found.  Then, when we find the ';' we take the current
                    188:                 * token if it starts with a valid token name, else we take
                    189:                 * the one we saved.  There's probably some reasonable
                    190:                 * alternative to this...
                    191:                 */
                    192:                case ';':
                    193:                        if (t_def && level == t_level) {
                    194:                                t_def = NO;
                    195:                                getline();
                    196:                                if (sp != tok)
                    197:                                        *sp = EOS;
                    198:                                pfnote(tok, lineno);
                    199:                                break;
                    200:                        }
                    201:                        goto storec;
                    202:
                    203:                /*
                    204:                 * store characters until one that can't be part of a token
                    205:                 * comes along; check the current token against certain
                    206:                 * reserved words.
                    207:                 */
                    208:                default:
1.4       deraadt   209:                        /*
                    210:                         * to treat following function.
                    211:                         * func      (arg) {
                    212:                         * ....
                    213:                         * }
                    214:                         */
                    215:                        if (c == ' ' || c == '\t') {
                    216:                                int save = c;
                    217:                                while (GETC(!=, EOF) && (c == ' ' || c == '\t'))
                    218:                                        ;
                    219:                                if (c == EOF)
                    220:                                        return;
                    221:                                (void)ungetc(c, inf);
                    222:                                c = save;
                    223:                        }
1.1       deraadt   224:        storec:         if (!intoken(c)) {
                    225:                                if (sp == tok)
                    226:                                        break;
                    227:                                *sp = EOS;
                    228:                                if (tflag) {
                    229:                                        /* no typedefs inside typedefs */
                    230:                                        if (!t_def &&
                    231:                                                   !memcmp(tok, "typedef",8)) {
                    232:                                                t_def = YES;
                    233:                                                t_level = level;
                    234:                                                break;
                    235:                                        }
                    236:                                        /* catch "typedef struct" */
                    237:                                        if ((!t_def || t_level < level)
                    238:                                            && (!memcmp(tok, "struct", 7)
                    239:                                            || !memcmp(tok, "union", 6)
                    240:                                            || !memcmp(tok, "enum", 5))) {
                    241:                                                /*
                    242:                                                 * get line immediately;
                    243:                                                 * may change before '{'
                    244:                                                 */
                    245:                                                getline();
                    246:                                                if (str_entry(c))
                    247:                                                        ++level;
                    248:                                                break;
                    249:                                                /* } */
                    250:                                        }
                    251:                                }
                    252:                                sp = tok;
                    253:                        }
                    254:                        else if (sp != tok || begtoken(c)) {
1.6       espie     255:                                /* hell... truncate it */
                    256:                                if (sp == tok + sizeof tok - 1)
                    257:                                        *sp = EOS;
                    258:                                else
1.5       deraadt   259:                                        *sp++ = c;
1.1       deraadt   260:                                token = YES;
                    261:                        }
                    262:                        continue;
                    263:                }
                    264:
                    265:                sp = tok;
                    266:                token = NO;
                    267:        }
                    268: }
                    269:
                    270: /*
                    271:  * func_entry --
                    272:  *     handle a function reference
                    273:  */
                    274: static int
1.10      deraadt   275: func_entry(void)
1.1       deraadt   276: {
                    277:        int     c;                      /* current character */
                    278:        int     level = 0;              /* for matching '()' */
1.8       pjanzen   279:        static char attribute[] = "__attribute__";
                    280:        char maybe_attribute[sizeof attribute + 1];
                    281:        char *anext;
1.1       deraadt   282:
                    283:        /*
                    284:         * Find the end of the assumed function declaration.
                    285:         * Note that ANSI C functions can have type definitions so keep
                    286:         * track of the parentheses nesting level.
                    287:         */
                    288:        while (GETC(!=, EOF)) {
                    289:                switch (c) {
                    290:                case '\'':
                    291:                case '"':
                    292:                        /* skip strings and character constants */
                    293:                        skip_string(c);
                    294:                        break;
                    295:                case '/':
                    296:                        /* skip comments */
                    297:                        if (GETC(==, '*'))
1.8       pjanzen   298:                                skip_comment(c);
                    299:                        else if (c == '/')
                    300:                                skip_comment(c);
1.1       deraadt   301:                        break;
                    302:                case '(':
                    303:                        level++;
                    304:                        break;
                    305:                case ')':
                    306:                        if (level == 0)
                    307:                                goto fnd;
                    308:                        level--;
                    309:                        break;
                    310:                case '\n':
                    311:                        SETLINE;
                    312:                }
                    313:        }
                    314:        return (NO);
                    315: fnd:
                    316:        /*
                    317:         * we assume that the character after a function's right paren
                    318:         * is a token character if it's a function and a non-token
                    319:         * character if it's a declaration.  Comments don't count...
                    320:         */
1.8       pjanzen   321:        for (anext = maybe_attribute;;) {
1.1       deraadt   322:                while (GETC(!=, EOF) && iswhite(c))
                    323:                        if (c == '\n')
                    324:                                SETLINE;
1.8       pjanzen   325:                if (c == EOF)
                    326:                        return NO;
                    327:                /*
                    328:                 * Recognize the GNU __attribute__ extension, which would
                    329:                 * otherwise make the heuristic test DTWT
                    330:                 */
                    331:                if (anext == maybe_attribute) {
                    332:                        if (intoken(c)) {
                    333:                                *anext++ = c;
                    334:                                continue;
                    335:                        }
                    336:                } else {
                    337:                        if (intoken(c)) {
                    338:                                if (anext - maybe_attribute < (int)(sizeof attribute - 1))
                    339:                                        *anext++ = c;
                    340:                                else
                    341:                                        break;
                    342:                                continue;
                    343:                        } else {
                    344:                                *anext++ = '\0';
                    345:                                if (strcmp(maybe_attribute, attribute) == 0) {
                    346:                                        (void)ungetc(c, inf);
                    347:                                        return NO;
                    348:                                }
                    349:                                break;
                    350:                        }
                    351:                }
1.1       deraadt   352:                if (intoken(c) || c == '{')
                    353:                        break;
                    354:                if (c == '/' && GETC(==, '*'))
1.8       pjanzen   355:                        skip_comment(c);
                    356:                else if (c == '/')
                    357:                        skip_comment(c);
1.1       deraadt   358:                else {                          /* don't ever "read" '/' */
                    359:                        (void)ungetc(c, inf);
                    360:                        return (NO);
                    361:                }
                    362:        }
                    363:        if (c != '{')
                    364:                (void)skip_key('{');
                    365:        return (YES);
                    366: }
                    367:
                    368: /*
                    369:  * hash_entry --
                    370:  *     handle a line starting with a '#'
                    371:  */
                    372: static void
1.10      deraadt   373: hash_entry(void)
1.1       deraadt   374: {
                    375:        int     c;                      /* character read */
                    376:        int     curline;                /* line started on */
                    377:        char    *sp;                    /* buffer pointer */
                    378:        char    tok[MAXTOKEN];          /* storage buffer */
1.4       deraadt   379:
                    380:        /*
                    381:         * to treat following macro.
                    382:         * #     macro(arg)        ....
                    383:         */
                    384:        while (GETC(!=, EOF) && (c == ' ' || c == '\t'))
                    385:                ;
                    386:        (void)ungetc(c, inf);
1.1       deraadt   387:
                    388:        curline = lineno;
                    389:        for (sp = tok;;) {              /* get next token */
                    390:                if (GETC(==, EOF))
                    391:                        return;
                    392:                if (iswhite(c))
                    393:                        break;
1.6       espie     394:                /* hell... truncate it */
                    395:                if (sp == tok + sizeof tok - 1)
                    396:                        *sp = EOS;
                    397:                else
1.5       deraadt   398:                        *sp++ = c;
1.1       deraadt   399:        }
                    400:        *sp = EOS;
                    401:        if (memcmp(tok, "define", 6))   /* only interested in #define's */
                    402:                goto skip;
                    403:        for (;;) {                      /* this doesn't handle "#define \n" */
                    404:                if (GETC(==, EOF))
                    405:                        return;
                    406:                if (!iswhite(c))
                    407:                        break;
                    408:        }
                    409:        for (sp = tok;;) {              /* get next token */
1.6       espie     410:                /* hell... truncate it */
                    411:                if (sp == tok + sizeof tok - 1)
                    412:                        *sp = EOS;
                    413:                else
1.5       deraadt   414:                        *sp++ = c;
1.1       deraadt   415:                if (GETC(==, EOF))
                    416:                        return;
                    417:                /*
                    418:                 * this is where it DOESN'T handle
                    419:                 * "#define \n"
                    420:                 */
                    421:                if (!intoken(c))
                    422:                        break;
                    423:        }
                    424:        *sp = EOS;
                    425:        if (dflag || c == '(') {        /* only want macros */
                    426:                getline();
                    427:                pfnote(tok, curline);
                    428:        }
                    429: skip:  if (c == '\n') {                /* get rid of rest of define */
                    430:                SETLINE
                    431:                if (*(sp - 1) != '\\')
                    432:                        return;
                    433:        }
                    434:        (void)skip_key('\n');
                    435: }
                    436:
                    437: /*
                    438:  * str_entry --
                    439:  *     handle a struct, union or enum entry
                    440:  */
                    441: static int
1.10      deraadt   442: str_entry(int c)
1.1       deraadt   443: {
                    444:        int     curline;                /* line started on */
                    445:        char    *sp;                    /* buffer pointer */
                    446:        char    tok[LINE_MAX];          /* storage buffer */
                    447:
                    448:        curline = lineno;
                    449:        while (iswhite(c))
                    450:                if (GETC(==, EOF))
                    451:                        return (NO);
                    452:        if (c == '{')           /* it was "struct {" */
                    453:                return (YES);
                    454:        for (sp = tok;;) {              /* get next token */
1.6       espie     455:                /* hell... truncate it */
                    456:                if (sp == tok + sizeof tok - 1)
                    457:                        *sp = EOS;
                    458:                else
1.5       deraadt   459:                        *sp++ = c;
1.1       deraadt   460:                if (GETC(==, EOF))
                    461:                        return (NO);
                    462:                if (!intoken(c))
                    463:                        break;
                    464:        }
                    465:        switch (c) {
                    466:                case '{':               /* it was "struct foo{" */
                    467:                        --sp;
                    468:                        break;
                    469:                case '\n':              /* it was "struct foo\n" */
                    470:                        SETLINE;
                    471:                        /*FALLTHROUGH*/
                    472:                default:                /* probably "struct foo " */
                    473:                        while (GETC(!=, EOF))
                    474:                                if (!iswhite(c))
                    475:                                        break;
                    476:                        if (c != '{') {
                    477:                                (void)ungetc(c, inf);
                    478:                                return (NO);
                    479:                        }
                    480:        }
                    481:        *sp = EOS;
                    482:        pfnote(tok, curline);
                    483:        return (YES);
                    484: }
                    485:
                    486: /*
                    487:  * skip_comment --
                    488:  *     skip over comment
                    489:  */
                    490: void
1.8       pjanzen   491: skip_comment(int commenttype)
1.1       deraadt   492: {
                    493:        int     c;                      /* character read */
                    494:        int     star;                   /* '*' flag */
                    495:
                    496:        for (star = 0; GETC(!=, EOF);)
                    497:                switch(c) {
                    498:                /* comments don't nest, nor can they be escaped. */
                    499:                case '*':
                    500:                        star = YES;
                    501:                        break;
                    502:                case '/':
1.8       pjanzen   503:                        if (commenttype == '*' && star)
1.1       deraadt   504:                                return;
                    505:                        break;
                    506:                case '\n':
1.8       pjanzen   507:                        if (commenttype == '/') {
                    508:                                /* We don't really parse C, so sometimes it
                    509:                                 * is necessary to see the newline
                    510:                                 */
                    511:                                ungetc(c, inf);
                    512:                                return;
                    513:                        }
1.1       deraadt   514:                        SETLINE;
                    515:                        /*FALLTHROUGH*/
                    516:                default:
                    517:                        star = NO;
                    518:                        break;
                    519:                }
                    520: }
                    521:
                    522: /*
                    523:  * skip_string --
                    524:  *     skip to the end of a string or character constant.
                    525:  */
1.11    ! avsm      526: static void
1.10      deraadt   527: skip_string(int key)
1.1       deraadt   528: {
                    529:        int     c,
                    530:                skip;
                    531:
                    532:        for (skip = NO; GETC(!=, EOF); )
                    533:                switch (c) {
                    534:                case '\\':              /* a backslash escapes anything */
                    535:                        skip = !skip;   /* we toggle in case it's "\\" */
                    536:                        break;
                    537:                case '\n':
                    538:                        SETLINE;
                    539:                        /*FALLTHROUGH*/
                    540:                default:
                    541:                        if (c == key && !skip)
                    542:                                return;
                    543:                        skip = NO;
                    544:                }
                    545: }
                    546:
                    547: /*
                    548:  * skip_key --
                    549:  *     skip to next char "key"
                    550:  */
                    551: int
1.10      deraadt   552: skip_key(int key)
1.1       deraadt   553: {
                    554:        int     c,
                    555:                skip,
                    556:                retval;
                    557:
                    558:        for (skip = retval = NO; GETC(!=, EOF);)
                    559:                switch(c) {
                    560:                case '\\':              /* a backslash escapes anything */
                    561:                        skip = !skip;   /* we toggle in case it's "\\" */
                    562:                        break;
                    563:                case ';':               /* special case for yacc; if one */
                    564:                case '|':               /* of these chars occurs, we may */
                    565:                        retval = YES;   /* have moved out of the rule */
                    566:                        break;          /* not used by C */
                    567:                case '\'':
                    568:                case '"':
                    569:                        /* skip strings and character constants */
                    570:                        skip_string(c);
                    571:                        break;
                    572:                case '/':
                    573:                        /* skip comments */
                    574:                        if (GETC(==, '*')) {
1.8       pjanzen   575:                                skip_comment(c);
                    576:                                break;
                    577:                        } else if (c == '/') {
                    578:                                skip_comment(c);
1.1       deraadt   579:                                break;
                    580:                        }
                    581:                        (void)ungetc(c, inf);
                    582:                        c = '/';
                    583:                        goto norm;
                    584:                case '\n':
                    585:                        SETLINE;
                    586:                        /*FALLTHROUGH*/
                    587:                default:
                    588:                norm:
                    589:                        if (c == key && !skip)
                    590:                                return (retval);
                    591:                        skip = NO;
                    592:                }
                    593:        return (retval);
                    594: }