File: dump.c

package info (click to toggle)
cproto 4.7f-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 804 kB
  • ctags: 656
  • sloc: ansic: 4,017; sh: 2,987; lex: 1,056; yacc: 872; makefile: 232
file content (123 lines) | stat: -rw-r--r-- 2,694 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
/* $Id: dump.c,v 4.2 2004/03/24 23:31:46 tom Exp $
 *
 * useful dumps for cproto
 */
#include <stdio.h>
#include "cproto.h"
#include "trace.h"
#include "dump.h"

static char *whatFuncDefStyle (FuncDefStyle func_def);
static char *flagsDeclSpec(int flags);

#define PAD char pad[80]; sprintf(pad, "%-*s", level * 3, ".")

#ifndef DEBUG
#define DEBUG 0
#endif

#if DEBUG > 1
#define SHOW_CMTS(p) Trace p;
#else
#define SHOW_CMTS(p)
#endif

static char *
whatFuncDefStyle(FuncDefStyle func_def)	/* style of function definition */
{
	switch (func_def) {
	case FUNC_NONE:		return "FUNC_NONE";
	case FUNC_TRADITIONAL:	return "FUNC_TRADITIONAL";
	case FUNC_ANSI:		return "FUNC_ANSI";
	case FUNC_BOTH:		return "FUNC_BOTH";
	}
	return "?";
}

void
dump_parameter(Parameter *p, int level)
{
	dump_declarator(p->declarator, level+1);
	dump_decl_spec(&(p->decl_spec), level+1);
}

void
dump_param_list(ParameterList *p, int level)
{
	struct parameter *q;

	for (q = p->first; q != 0; q = q->next) {
		dump_parameter(q, level);
	}
}

void
dump_declarator(Declarator *d, int	level)
{
	PAD;
	Trace("%sdeclarator %p\n",		pad, d);
	Trace("%s   name /%s/\n",		pad, d->name);
	Trace("%s   text /%s/\n",		pad, d->text);
	SHOW_CMTS(("%s   begin %ld\n",		pad, d->begin))
	SHOW_CMTS(("%s   begin_comment %ld\n",	pad, d->begin_comment))
	SHOW_CMTS(("%s   end_comment %ld\n",	pad, d->end_comment))
	Trace("%s   func_def %s\n",		pad, whatFuncDefStyle(d->func_def));

#if DEBUG > 1
	if (d->func_def != FUNC_NONE) {
		Trace("%s >PARAMS of %p\n", pad, d);
		dump_param_list(&(d->params), level+1);
	}
#endif
	Trace("%s   pointer %s\n",		pad, d->pointer ? "YES" : "NO");

	if (d->head != 0 && d != d->head) {
		Trace("%s >HEAD of %p\n", pad, d);
		dump_declarator(d->head, level+1);
	}
	if (d->func_stack != 0) {
		Trace("%s >FUNC_STACK of %p\n", pad, d);
		dump_declarator(d->func_stack, level+1);
	}
	if (d->next != 0) {
		Trace("%s >NEXT of %p\n", pad,  d);
		dump_declarator(d->next, level+1);
	}
}

static char *
flagsDeclSpec(int flags)
{
	static	char	temp[100];
	static	struct	{
		int	mask;
		char	*text;
	} table [] = {
		{DS_EXTERN,	"extern"},
		{DS_STATIC,	"static"},
		{DS_CHAR, 	"char"},
		{DS_SHORT,	"short"},
		{DS_FLOAT,	"float"},
		{DS_JUNK ,	"junk"},
	};
	unsigned j;
	*temp = 0;
	for (j = 0; j < sizeof(table)/sizeof(table[0]); j++) {
		if (flags & table[j].mask) {
			if (*temp)
				(void)strcat(temp, ",");
			(void)strcat(temp, table[j].text);
		}
	}
	return temp;
}

void
dump_decl_spec(DeclSpec *d, int level)
{
	PAD;
	Trace("%sdecl_spec %p\n",	pad, d);
	Trace("%s  flags %s\n",		pad, flagsDeclSpec(d->flags));
	Trace("%s  text /%s/\n",	pad, d->text);
	SHOW_CMTS(("%s  begin %ld\n",	pad, d->begin))
}