File: nmain.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 (265 lines) | stat: -rw-r--r-- 5,221 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
/* Copyright (c) 2002 Hewlett-Packard under GPL version 2 or later */
#include <stdio.h>
#if defined MSC6 || defined WIN32
#define MAXPATHLEN 1024
#else
#include <sys/param.h>		/* for MAXPATHLEN */
#endif
#include <stdlib.h>
#include <assert.h>
#include "pmccabe.h"
#include "dmain.h"

/* $Id: nmain.c,v 1.23 2001/01/26 23:00:37 bame Exp $ */

int Exit = 0;

void
file(char *fname, FILE * f)
{
    stats_t *filestats;
    extern int Unbuf[], *Unptr;
    extern int Pass1;

    Input = f;

    if (Pass1)
    {
	int c;
	extern bool Cppflag;

	Cppflag = true;

	while ((c = Getchar()) != EOF)
	{
	    putc(c, stdout);
	}
	return;
    }

    Unptr = Unbuf;
    Line = 1;
    ncss_Line = 0;

    ZERO(filestats);
    filestats = stats_push(fname, STATS_FILE);
    filestats->firstline = 1;

    while (toplevelstatement(filestats) != EOF)
    {
	filestats->nsemicolons++;
    }

    filestats->lastline = Line - 1;
    /* Not 100% sure why I need to subtract 1 here */
    filestats->nLines += ncss_Line - 1;

    if (Files)
	printstats(filestats);
    stats_pop(filestats);
}

void
cycoprintstats(stats_t * fs, stats_t * fn)
{
    int basic, cycloswitch, cyclocase;

    basic = fn->nfor + fn->nwhile + fn->nif + fn->nand + fn->nor + fn->nq;

    cycloswitch = 1 + basic + fn->nswitch;
    cyclocase = 1 + basic + fn->ncase;

    printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%s\t%s\n",
	   cycloswitch,
	   cyclocase,
	   fn->nstatements,
	   fn->firstline,
	   fn->defline,
	   fn->lastline,
	   fn->lastline - fn->firstline + 1, fs->name, fn->name);
}

void
softbuildprintstats(stats_t * fs, stats_t * fn)
{
    int basic, cycloswitch, cyclocase;

    basic = fn->nfor + fn->nwhile + fn->nif + fn->nand + fn->nor + fn->nq;

    cycloswitch = 1 + basic + fn->nswitch;
    cyclocase = 1 + basic + fn->ncase;

    printf("\"%s\", line %d: %s%%\t%d\t%d\t%d\t%d\t%d\n",
	   fs->name, fn->defline,
	   fn->name,
	   cycloswitch,
	   cyclocase,
	   fn->nstatements, fn->firstline, fn->lastline - fn->firstline + 1);
}

static void
printname(stats_t * sp)
{
    if (sp != NULL)
    {
	printname(sp->prev);
	switch (sp->type)
	{
	case STATS_TOTAL:
	case STATS_FILE:
	    break;

	case STATS_FUNCTION:
	    printf("%s", sp->name);
	    break;

	case STATS_CLASS:
	    if (sp->prev != NULL && sp->prev->type == STATS_FUNCTION)
		printf("/");
	    printf("%s::", sp->name);
	    break;

	case STATS_NAMESPACE:
	    printf("%s::", sp->name);
	    break;
	}
    }
}

void
printstats(stats_t * sp)
{
    int basic, cycloswitch, cyclocase;
    int snlines;
    stats_t *fsp;

    basic = sp->nfor + sp->nwhile + sp->nif + sp->nand + sp->nor + sp->nq;

    sp->nstatements = basic - sp->nand - sp->nor + sp->nsemicolons;

    cycloswitch = sp->nfunctions + basic + sp->nswitch;
    cyclocase = sp->nfunctions + basic + sp->ncase;

    /* fix bug observed by Ahmad Jbara <ahmadjbara@gmail.com> */
    /* where #statements weren't counting switch */
    sp->nstatements += sp->nswitch;

    if (Threshold != 0 && cycloswitch < Threshold)
    {
	return;
    }

    for (fsp = sp; fsp != NULL && fsp->type != STATS_FILE; fsp = fsp->prev)
    {
    }

    if (Ncssfunction)
    {
	snlines = sp->nLines + 1;
    }
    else
    {
	snlines = sp->lastline - sp->firstline + 1;
    }

    switch (sp->type)
    {
    case STATS_TOTAL:
	if (Softbuild)
	{
	    printf("\"n/a\", line n/a: %s", sp->name);
	    printf("%%\t%d\t%d\t%d\tn/a\t%d\n",
		   cycloswitch, cyclocase, sp->nstatements, snlines);
	}
	else if (Cyco)
	{
	    printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%s\n",
		   cycloswitch,
		   cyclocase,
		   sp->nstatements,
		   sp->firstline,
		   sp->defline, sp->lastline, snlines, "Total");
	}
	else
	{
	    printf("%d\t%d\t%d\tn/a\t%d\t",
		   cycloswitch, cyclocase, sp->nstatements, snlines);

	    printf("Total\n");
	}
	break;
    case STATS_FILE:
	assert(fsp != NULL);

	if (Softbuild)
	{
	    printf("\"%s\", line 1: n/a", fsp->name);
	    printf("%%\t%d\t%d\t%d\t%d\t%d\n",
		   cycloswitch,
		   cyclocase, sp->nstatements, sp->firstline, snlines);
	}
	else if (Cyco)
	{
	    printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%s\n",
		   cycloswitch,
		   cyclocase,
		   sp->nstatements,
		   sp->firstline,
		   sp->defline, sp->lastline, snlines, fsp->name);
	}
	else
	{
	    printf("%d\t%d\t%d\t%d\t%d\t",
		   cycloswitch,
		   cyclocase, sp->nstatements, sp->firstline, snlines);

	    printf("%s\n", fsp->name);
	}
	break;
    case STATS_FUNCTION:
	assert(fsp != NULL);
	if (Softbuild)
	{
	    printf("\"%s\", line %d: ", fsp->name, sp->defline);
	    printname(sp);
	    printf("%%\t%d\t%d\t%d\t%d\t%d\n",
		   cycloswitch,
		   cyclocase, sp->nstatements, sp->firstline, snlines);
	}
	else if (Cyco)
	{
	    printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%s\t",
		   cycloswitch,
		   cyclocase,
		   sp->nstatements,
		   sp->firstline,
		   sp->defline, sp->lastline, snlines, fsp->name);
	    printname(sp);
	    putchar('\n');
	}
	else
	{
	    printf("%d\t%d\t%d\t%d\t%d\t",
		   cycloswitch,
		   cyclocase, sp->nstatements, sp->firstline, snlines);

	    printf("%s(%d): ", fsp->name, sp->defline);

	    printname(sp);
	    printf("\n");
	}
	break;
    case STATS_CLASS:
	abort();
	if (Softbuild)
	{
	}
	else if (Cyco)
	{
	}
	else
	{
	}
	break;
    }
}