1 #include "dissect.h"
2 
3 static unsigned dotc_stream;
4 
storage(struct symbol * sym)5 static inline char storage(struct symbol *sym)
6 {
7 	int t = sym->type;
8 	unsigned m = sym->ctype.modifiers;
9 
10 	if (m & MOD_INLINE || t == SYM_STRUCT || t == SYM_UNION /*|| t == SYM_ENUM*/)
11 		return sym->pos.stream == dotc_stream ? 's' : 'g';
12 
13 	return (m & MOD_STATIC) ? 's' : (m & MOD_NONLOCAL) ? 'g' : 'l';
14 }
15 
show_mode(unsigned mode)16 static inline const char *show_mode(unsigned mode)
17 {
18 	static char str[3];
19 
20 	if (mode == -1)
21 		return "def";
22 
23 #define	U(u_r)	"-rwm"[(mode / u_r) & 3]
24 	str[0] = U(U_R_AOF);
25 	str[1] = U(U_R_VAL);
26 	str[2] = U(U_R_PTR);
27 #undef	U
28 
29 	return str;
30 }
31 
print_usage(struct position * pos,struct symbol * sym,unsigned mode)32 static void print_usage(struct position *pos, struct symbol *sym, unsigned mode)
33 {
34 	static unsigned curr_stream = -1;
35 
36 	if (curr_stream != pos->stream) {
37 		curr_stream = pos->stream;
38 		printf("\nFILE: %s\n\n", stream_name(curr_stream));
39 	}
40 
41 	printf("%4d:%-3d %c %-5.3s",
42 		pos->line, pos->pos, storage(sym), show_mode(mode));
43 }
44 
r_symbol(unsigned mode,struct position * pos,struct symbol * sym)45 static void r_symbol(unsigned mode, struct position *pos, struct symbol *sym)
46 {
47 	print_usage(pos, sym, mode);
48 
49 	if (!sym->ident)
50 		sym->ident = built_in_ident("__asm__");
51 
52 	printf("%-32.*s %s\n",
53 		sym->ident->len, sym->ident->name,
54 		show_typename(sym->ctype.base_type));
55 }
56 
r_member(unsigned mode,struct position * pos,struct symbol * sym,struct symbol * mem)57 static void r_member(unsigned mode, struct position *pos, struct symbol *sym, struct symbol *mem)
58 {
59 	struct ident *ni, *si, *mi;
60 
61 	print_usage(pos, sym, mode);
62 
63 	ni = built_in_ident("?");
64 	si = sym->ident ?: ni;
65 	/* mem == NULL means entire struct accessed */
66 	mi = mem ? (mem->ident ?: ni) : built_in_ident("*");
67 
68 	printf("%.*s.%-*.*s %s\n",
69 		si->len, si->name,
70 		32-1 - si->len, mi->len, mi->name,
71 		show_typename(mem ? mem->ctype.base_type : sym));
72 }
73 
r_symdef(struct symbol * sym)74 static void r_symdef(struct symbol *sym)
75 {
76 	r_symbol(-1, &sym->pos, sym);
77 }
78 
main(int argc,char ** argv)79 int main(int argc, char **argv)
80 {
81 	static struct reporter reporter = {
82 		.r_symdef = r_symdef,
83 		.r_symbol = r_symbol,
84 		.r_member = r_member,
85 	};
86 	struct string_list *filelist = NULL;
87 	char *file;
88 
89 	sparse_initialize(argc, argv, &filelist);
90 
91 	FOR_EACH_PTR(filelist, file) {
92 		dotc_stream = input_stream_nr;
93 		dissect(__sparse(file), &reporter);
94 	} END_FOR_EACH_PTR(file);
95 
96 	return 0;
97 }
98