File: cparse.c

package info (click to toggle)
pmccabe 2.8-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 876 kB
  • sloc: ansic: 3,084; cpp: 1,080; sh: 401; makefile: 42
file content (719 lines) | stat: -rw-r--r-- 13,619 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
/* Copyright (c) 2002 Hewlett-Packard under GPL version 2 or later */
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
#include "pmccabe.h"
#include "dmain.h"

/* $Id: cparse.c,v 1.24 2001/01/26 23:00:30 bame Exp $ */

// handles C++ function/identifier names, destructors etc
// also silently discards 'class' if it is preceeded by 'enum'
int
fancygettoken(char *buf, int classflag, int *line, int *nLine)
{
    int c, c2;
    char tmpbuf[2048];

    if ((c = gettoken(buf, line, nLine)) == T_IDENT)
    {
	if ((c = gettoken(tmpbuf, NULL, NULL)) == ':')
	{
	    if ((c = ncss_Getchar()) == ':')
	    {
		buf += strlen(buf);
		*buf++ = ':';
		*buf++ = ':';
		*buf = '\0';
		switch (c = gettoken(tmpbuf, NULL, NULL))
		{
		case T_OPERATOR:
		    strcat(buf, tmpbuf);
		    buf += strlen(buf);
		    getoverloadedop(buf);
		    break;
		case T_IDENT:
		    /* ident::ident - could be recursive */
		    ncss_Ungets(tmpbuf);
		    if ((c = fancygettoken(buf, 1, NULL, NULL)) != T_IDENT)
			ncss_Ungetc(c);
		    break;
		case '~':	/* destructor, collect the identifier */
		    *buf++ = c;
		    gettoken(buf, NULL, NULL);
		    break;
		default:
		    ncss_Ungetc(c);
		    *buf++ = '\0';
		    break;
		}
	    }
	    else
	    {
		/* only got ':', who knows what this is */
		ncss_Ungets("::");
	    }
	}
	else
	{
	    ungettoken(c, tmpbuf);
	}

	c = T_IDENT;
    }
    else if (c == T_ENUM)
    {
	if ((c2 = gettoken(tmpbuf, NULL, NULL)) != T_CLASS)
	{
	    ungettoken(c2, tmpbuf);
	}
    }
    else if (classflag && c == T_OPERATOR)
    {
	/* strcat(buf, tmpbuf); */
	buf += strlen(buf);
	getoverloadedop(buf);
    }
    else if (classflag && c == '~')
    {
	*buf++ = c;
	c = gettoken(buf, NULL, NULL);
	if (c != T_IDENT)
	{
	    fprintf(stderr, "fatal error file %s line %d\n",
		    __FILE__, __LINE__);
	    exit(3);
	}
    }

    return c;
}

int
toplevelstatement(stats_t * stats)
/*
 *	At the top level of a C file, the statements are blocks of
 *	tokens ending in either ; or are function definitions which
 *	end in }.  Interesting types of statements include class
 *	and struct definitions - because they may contain inline functions,
 *	and function definitions.  All others are merely counted.
 */
{
    bool endofstatement = false;
    int c;
    char buf[1024];
    int functionFirstLine = -1;
    int functionFirstNLine = -1;
    int functionDefLine;
    int line, nLine;
    bool maybenamespace(void);


    buf[0] = '\0';

    c = skipws();
    ncss_Ungetc(c);

    /* gettoken eats whitespace */
    while (!endofstatement &&
	   (c =
	    fancygettoken(buf, stats->type == STATS_CLASS, &line,
			  &nLine)) != EOF)
    {
	if (functionFirstLine == -1)
	{
	    functionFirstLine = line;
	    functionFirstNLine = nLine;
	}
	switch (c)
	{
	case T_CLASS:
	case T_STRUCT:
	case T_UNION:
	    if (maybeclass())
	    {
		stats->nsemicolons--;
		endofstatement = true;
	    }
	    break;

	case T_NAMESPACE:
	    if (maybenamespace())
	    {
		/* no trailing semicolon for namespaces */
		endofstatement = true;
	    }
	    break;

	case '(':
	    /* possible start of function */
	    functionDefLine = Line;
	    possiblefn(stats, buf, functionFirstLine, functionDefLine,
		       functionFirstNLine);
	    endofstatement = true;
	    break;

	case '}':
	case ')':
	    Exit = 2;
	    {
		char _buf[100];

		sprintf(_buf, "too many %c's", c);
		fileerror(_buf);
	    }
	    break;

	case '{':
	    c = matchcurly();
	    break;

	case ':':		/* This should catch C++ "class foo { public: } */
	case ';':
	    /* end of statement */
	    endofstatement = true;
	    break;

	default:
	    break;
	}
    }

    return c;
}

int
findchar(char fc)
{
    int c;

    while ((c = ncss_Getchar()) != EOF && c != fc)
    {
    }

    return c;
}

bool
maybeclass()
/*
 * We've just seen "class" at the top level in a file so
 * we may be entering a definition of same.  If so, we want to be
 * on the lookout for inline functions.  Return 1 if this is a
 * class definition else restore the function name and return 0.
 */
{
    char classname[2048], dummy[2048];
    bool isclass = false;
    int c;

    if ((c = gettoken(classname, NULL, NULL)) == T_IDENT)
    {
	/* "class name" */
	switch (c = gettoken(dummy, NULL, NULL))
	{
	case '{':		/* "class name {" */
	    break;

	case ':':
	    c = gettoken(dummy, NULL, NULL);

	    /* "struct namespace::name" ---- declaration, not definition */
	    if (c == ':')
	    {
		return 0;
	    }

	    /* "class name : ---- look for { */
	    while ((c = gettoken(dummy, NULL, NULL)) != '{')
	    {
	    }
	    break;

	default:
	    /* if we fail to get "class name [:.*] {" */
	    ungettoken(c, dummy);
	}
    }
    else if (c == '{')		/* Unnamed class */
    {
	/* "class {" */
	strcpy(classname, "unnamed");
    }
    else
    {
	/* "class BOGUS" -- perhaps this is C code using a variable "class" */
	ungettoken(c, dummy);
    }

    if (c == '{')
    {
	isclass = true;
	stats_t *class = stats_push(classname, STATS_CLASS);

	while ((c = gettoken(dummy, NULL, NULL)) != '}')
	{
	    if (c == EOF)
	    {
		fileerror("expected closing } for class scope, but got EOF");
		break;
	    }
	    else
		ungettoken(c, dummy);

	    toplevelstatement(class);
	}

	stats_pop(class);
    }

    return isclass;
}

bool
maybenamespace()
/*
 * We've just seen "namespace" at the top level in a file so
 * we may be entering a definition of same (if we next find "token {").
 * Return 1 if this is a
 * namespace definition else restore the name and return 0.
 */
{
    char nsname[2048], dummy[2048];
    bool isns = false;
    int c;

    if ((c = gettoken(nsname, NULL, NULL)) == T_IDENT)
    {
	/* "namespace name" */
	switch (c = gettoken(dummy, NULL, NULL))
	{
	case '{':		/* "namespace name {" */
	    break;

	default:
	    /* if we fail to get "namespace name {" */
	    ungettoken(c, dummy);
	}
    }
    else if (c == '{')		/* Unnamed namespace */
    {
	/* "namespace {" */
	strcpy(nsname, "anonymous_namespace");
    }
    else
    {
	/* "namespace BOGUS" -- is C code using a variable "namespace"? */
	ungettoken(c, dummy);
    }

    if (c == '{')
    {
	isns = true;
	stats_t *ns = stats_push(nsname, STATS_NAMESPACE);

	while ((c = gettoken(dummy, NULL, NULL)) != '}')
	{
	    if (c == EOF)
	    {
		fileerror("expected closing } for namespace, but got EOF");
		break;
	    }
	    else
		ungettoken(c, dummy);

	    toplevelstatement(ns);
	}

	stats_pop(ns);
    }

    return isns;
}

void
findsemicolon()
{
    int c;

    while ((c = ncss_Getchar()) != EOF && c != ';')
    {
	switch (c)
	{
	case '(':
	    c = matchparen();
	    break;
	case '{':
	    c = matchcurly();
	    break;
	}
    }

    if (c == EOF)
    {
	Exit = 5;
	fileerror("expected ';' got EOF");
    }
}

int
getoverloadedop(char *buf)
/*
 * Having just read ident::operator, try to read the operator into buf.
 * If the first non-WS character is a '(', the overloaded thing is a
 * function call.  Otherwise it's some type of real operator and we
 * terminate normally on '('.  If we read ; or { we probably should
 * print a warning and bail out.
 */
{
    char tmpbuf[2048];
    int c = gettoken(tmpbuf, NULL, NULL);

    if (c == '(')
    {
	/* overloaded function call syntax */
	*buf++ = c;
	/* Match the paren */
	while (c != ')')
	{
	    if ((c = skipws()) == EOF)
		break;

	    *buf++ = c;
	}
    }
    else if (c == T_IDENT)
    {
	/* class::operator int() */
	/* Overloaded typecast */
	*buf++ = '_';
	*buf = '\0';
	strcat(buf, tmpbuf);
	buf += strlen(buf);
	*buf++ = '(';
	*buf++ = ')';
	*buf = '\0';
    }
    else if (c != EOF)
    {
	*buf++ = c;
	while ((c = ncss_Getchar()) != EOF)
	{
	    if (!ISSPACE(c))
	    {
		if (c == '(' || c == ';')
		{
		    ncss_Ungetc(c);
		    break;
		}
		else
		    *buf++ = c;
	    }
	}
    }

    *buf = '\0';

    return c;
}

bool
is_c_plus_plus(stats_t * stats, const char *name)
{
    return strchr(name, ':') != NULL ||
	stats->type == STATS_CLASS || stats->type == STATS_NAMESPACE;
}

bool
is_reserved_word(const char *word)
{
    return STREQUAL(word, "struct") ||
	STREQUAL(word, "enum") ||
	STREQUAL(word, "void") ||
	STREQUAL(word, "int") ||
	STREQUAL(word, "char") || STREQUAL(word, "const");

    return false;
}

void
possiblefn(stats_t * stats, const char *name, int line1, int defline,
	   int nLine1)
/*
 *	We've just read an open parenthesis.  If there's a legal identifier
 *	in name we may be within a function definition.
 */
{
    char dummy[2048];
    int nstatements = 0;	/* in case there's code prior to the { */
    int c;

    if (strlen(name) == 0)
    {
	/* no function name - must not be a function - return */
	findsemicolon();
	return;
    }

    if ((c = matchparen()) != EOF)
    {
	c = gettoken(dummy, NULL, NULL);

	switch (c)
	{
	case T_CONST:
	    if (is_c_plus_plus(stats, name))
	    {
		/* foo::foo() const ^ [;] { */
		/* This'll either be a ; for a declaration or a { */
		/* or a throw() and/or __attribute__() annotation */
		c = gettoken(dummy, NULL, NULL);
		break;
	    }
	    /* foo() const ^ char *a; { */
		/*** FALL THROUGH ***/

	case T_IDENT:
	    if (is_c_plus_plus(stats, name))
	    {
		/* there is no K&R C++ ;) */
		/* also, throw() and/or __attribute__ come after */
		break;
	    }
	    /* FALL THROUGH IN flat C case */
	case T_STRUCT:
	case T_UNION:
	    /* K&R function, T_IDENT is part of first parm defn */
	    /* Read up to that first '{' */
	    /* function foo(a, b, c) int a; */
	    /*                          ^   */

	    while ((c = ncss_Getchar()) != EOF && c != '{') ;

	    break;
	case '{':
	    /* open { of the function */
	    break;
	case '(':
	    /* weird possibility in C++ - what we thought was the */
	    /* parameter list was really part of an overloaded */
	    /* operator overloading of an odd typecast or something. */
	    /* The function name will be wrong but who cares :-> */

	    c = matchparen();
	    if (c != EOF)
		c = gettoken(dummy, NULL, NULL);
	    break;
	case ':':
	    /* Another C++-ism:     main(args): ident(args), ident(args) */
	    c = prefunction(&nstatements);
	    break;
	case EOF:
	    fileerror
		("expected pretty much anything after closing paren of function, but got EOF\n");
	    return;

	}

	/* we check for throw, volatile, and __attribute__ outside of the switch() above,
	 * because they are combinable in a single function declaration and might happen
	 *  IN ADDITION TO a const()
	 */
	if (c == T_VOLATILE)
	{
	    /* skip the volatile keyword */
	    c = gettoken(dummy, NULL, NULL);
	}

	if (c == T_THROW)
	{
	    /* detect void f() throw() */
	    c = gettoken(dummy, NULL, NULL);

	    if (c != '(')
	    {
		char error_message[100];

		sprintf(error_message,
			"expected opening paren for method throw declaration, but got '%c'",
			c);
		fileerror(error_message);
		return;
	    }

	    c = matchparen();

	    if (c == EOF)
	    {
		fileerror
		    ("expected closing paren for method throw declaration, but saw EOF");
		return;
	    }

	    c = gettoken(dummy, NULL, NULL);
	}

	if (c == T_IDENT)
	{
	    /* detect void f() __attribute__(...) */
	    /* and    void f() GCC_PRINTF(...) */
	    c = gettoken(dummy, NULL, NULL);

	    if (c == '(')
	    {
		c = matchparen();

		if (c == EOF)
		{
		    fileerror
			("expected closing paren for __attribute__ (or macro), but saw EOF");
		    return;
		}

	    }

	    /* if there wasn't a paren after the likely-attribute macro,
	     * the compiler-attribute arguments within the macro definition itself */
	    c = gettoken(dummy, NULL, NULL);
	}

	if (c == '{')
	{
	    if (is_reserved_word(name))
	    {
		char error_message[1000];

		snprintf(error_message, sizeof (error_message) - 1,
			 "got '%s' as a name for a function, which means a probable bug in our parser",
			 name);
		fileerror(error_message);
		return;
	    }

	    /* This really is a function */
	    stats_t *fn = stats_push(name, STATS_FUNCTION);

	    fn->nfunctions = 1;
	    fn->firstline = line1;
	    fn->defline = defline;
	    fn->nsemicolons = nstatements;

	    c = countfunction(fn);
	    fn->nLines = ncss_Line - nLine1;
	    stats->nLines -= fn->nLines;
	    if (!Totalsonly && !Filesonly)
		printstats(fn);
	    stats_pop(fn);
	}
    }
}

int
prefunction(int *nstatements)
/*
 * Handle C++ ident(args) : ident(args), ident(args).  Count each
 * ident(args) after function declaration as a statement.
 */
{
    int c;

    (*nstatements)++;

    while ((c = ncss_Getchar()) != EOF)
    {
	switch (c)
	{
	case '(':
	    c = matchparen();
	    break;
	case ',':
	    (*nstatements)++;
	    break;
	case '{':
	    return c;
	}
    }
    Exit = 9;
    fileerror("expected { got EOF");

    return c;
}

int
countfunction(stats_t * fn)
{
    int nest = 1;
    int c;
    char id[512];

    while (nest > 0 && (c = gettoken2(id, NULL, NULL)) != EOF)
    {
	switch (c)
	{
	case ';':
	    fn->nsemicolons++;
	    break;

	case '{':
	    nest++;
	    break;

	case '}':
	    nest--;
	    break;

	case '?':
	    fn->nq++;
	    break;

	case T_LOGICAL:
	    fn->nor++;
	    break;

	case T_CLASS:
	case T_UNION:
	case T_STRUCT:
	    if (maybeclass())
		fn->nsemicolons--;
	    break;

	default:
	    countword(fn, c);
	    break;
	}
    }

    fn->lastline = Line;

    if (nest > 0 /* && c == EOF */ )
    {
	Exit = 6;
	fileerror("not enough }'s");
    }

    return c;
}

void
countword(stats_t * fn, int id)
{
    switch (id)
    {
    case T_IF:
	fn->nif++;
	break;
    case T_FOR:
	fn->nfor++;
	break;
    case T_WHILE:
	fn->nwhile++;
	break;
    case T_SWITCH:
	fn->nswitch++;
	break;
    case T_CASE:
	fn->ncase++;
	break;
    }
}