Oops. Remove empty printf that causes warnings.
[smatch.git] / show-parse.c
blobc0a7dd4aec4f129523fe1aef1e183e4c9009cd7b
1 /*
2 * sparse/show-parse.c
4 * Copyright (C) 2003 Linus Torvalds, all rights reserved.
6 * Print out results of parsing for debugging and testing.
7 */
8 #include <stdarg.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <ctype.h>
13 #include <unistd.h>
14 #include <fcntl.h>
16 #include "lib.h"
17 #include "token.h"
18 #include "parse.h"
19 #include "symbol.h"
20 #include "scope.h"
21 #include "expression.h"
24 * Symbol type printout. The type system is by far the most
25 * complicated part of C - everything else is trivial.
27 const char *modifier_string(unsigned long mod)
29 static char buffer[100];
30 char *p = buffer;
31 const char *res,**ptr, *names[] = {
32 "auto", "register", "static", "extern",
33 "const", "volatile", "signed", "unsigned",
34 "char", "short", "long", "long",
35 NULL
37 ptr = names;
38 while ((res = *ptr++) != NULL) {
39 if (mod & 1) {
40 char c;
41 *p++ = ' ';
42 while ((c = *res++) != '\0')
43 *p++ = c;
45 mod >>= 1;
47 *p++ = 0;
48 *p++ = 0;
49 return buffer+1;
52 const char *type_string(unsigned int modifiers, struct symbol *sym)
54 if (!sym)
55 return "<notype>";
57 if (sym == &int_type) {
58 if (modifiers & (SYM_CHAR | SYM_SHORT | SYM_LONG))
59 return "";
60 return "int";
62 if (sym == &fp_type)
63 return "float";
64 if (sym == &void_type)
65 return "void";
66 if (sym == &vector_type)
67 return "vector";
68 if (sym == &bad_type)
69 return "bad type";
70 if (sym->token)
71 return show_token(sym->token);
72 return "typedef";
75 static void show_one_symbol(struct symbol *sym, void *sep, int flags)
77 show_symbol(sym);
78 if (!(flags & ITERATE_LAST))
79 printf("%s", (const char *)sep);
82 void show_symbol_list(struct symbol_list *list, const char *sep)
84 symbol_iterate(list, show_one_symbol, (void *)sep);
87 void show_type_list(struct symbol *sym)
89 while (sym) {
90 show_symbol(sym);
91 sym = sym->next;
95 void show_type(struct symbol *sym)
97 struct ctype *ctype = &sym->ctype;
99 if (!sym) {
100 printf(" <typeless>");
101 return;
104 printf("%s", modifier_string(sym->ctype.modifiers));
106 switch (sym->type) {
107 case SYM_PTR:
108 show_type(sym->ctype.base_type);
109 printf(" *");
110 break;
112 case SYM_FN:
113 printf(" ");
114 show_type(sym->ctype.base_type);
115 printf(" <fn>(");
116 show_symbol_list(sym->arguments, ", ");
117 printf(")");
118 break;
120 case SYM_ARRAY:
121 printf("<array of>");
122 show_type(sym->ctype.base_type);
123 printf("[ ... ]");
124 break;
126 default:
127 printf("%s", type_string(ctype->modifiers, ctype->base_type));
128 break;
132 void show_symbol(struct symbol *sym)
134 if (!sym) {
135 printf("<anon symbol>");
136 return;
138 switch (sym->type) {
139 case SYM_FN:
140 printf("%s: ", show_token(sym->token));
141 show_type(sym);
142 printf("\n");
143 show_statement(sym->stmt);
144 break;
145 default:
146 show_type(sym);
147 printf(": %s", show_token(sym->token));
148 break;
154 * Print out a statement
156 void show_statement(struct statement *stmt)
158 if (!stmt) {
159 printf("\t<nostatement>");
160 return;
162 switch (stmt->type) {
163 case STMT_RETURN:
164 printf("\treturn ");
165 show_expression(stmt->expression);
166 break;
167 case STMT_COMPOUND:
168 printf("{\n");
169 if (stmt->syms) {
170 printf("\t");
171 show_symbol_list(stmt->syms, "\n\t");
172 printf("\n\n");
174 show_statement_list(stmt->stmts, ";\n");
175 printf("\n}\n\n");
176 break;
177 case STMT_EXPRESSION:
178 printf("\t");
179 show_expression(stmt->expression);
180 return;
181 case STMT_IF:
182 printf("\tif (");
183 show_expression(stmt->if_conditional);
184 printf(")\n");
185 show_statement(stmt->if_true);
186 if (stmt->if_false) {
187 printf("\nelse\n");
188 show_statement(stmt->if_false);
190 break;
191 case STMT_SWITCH:
192 printf("\tswitch (");
193 show_expression(stmt->switch_expression);
194 printf(")\n");
195 show_statement(stmt->switch_statement);
196 break;
198 case STMT_CASE:
199 if (!stmt->case_expression)
200 printf("default");
201 else {
202 printf("case ");
203 show_expression(stmt->case_expression);
204 if (stmt->case_to) {
205 printf(" ... ");
206 show_expression(stmt->case_to);
209 printf(":");
210 show_statement(stmt->case_statement);
211 break;
213 case STMT_BREAK:
214 printf("\tbreak");
215 break;
217 case STMT_WHILE:
218 printf("\twhile (");
219 show_expression(stmt->e1);
220 printf(")\n");
221 show_statement(stmt->iterate);
222 break;
224 case STMT_DO:
225 printf("\tdo");
226 show_statement(stmt->iterate);
227 printf("\nwhile (");
228 show_expression(stmt->e1);
229 printf(")\n");
230 break;
232 case STMT_FOR:
233 printf("\tfor (" );
234 show_expression(stmt->e1);
235 printf(" ; ");
236 show_expression(stmt->e2);
237 printf(" ; ");
238 show_expression(stmt->e3);
239 printf(")\n");
240 show_statement(stmt->iterate);
241 break;
243 default:
244 printf("WTF");
248 static void show_one_statement(struct statement *stmt, void *sep, int flags)
250 show_statement(stmt);
251 if (!(flags & ITERATE_LAST))
252 printf("%s", (const char *)sep);
255 void show_statement_list(struct statement_list *stmt, const char *sep)
257 statement_iterate(stmt, show_one_statement, (void *)sep);
261 * Print out an expression
263 void show_expression(struct expression *expr)
265 if (!expr)
266 return;
268 printf("< ");
269 switch (expr->type) {
270 case EXPR_BINOP:
271 show_expression(expr->left);
272 printf(" %s ", show_special(expr->op));
273 show_expression(expr->right);
274 break;
275 case EXPR_PREOP:
276 printf("%s<", show_special(expr->op));
277 show_expression(expr->unop);
278 printf(">");
279 break;
280 case EXPR_POSTOP:
281 show_expression(expr->unop);
282 printf(" %s ", show_special(expr->op));
283 break;
284 case EXPR_CONSTANT:
285 printf("%s", show_token(expr->token));
286 break;
287 case EXPR_SYMBOL:
288 if (!expr->symbol) {
289 warn(expr->token, "undefined symbol '%s'", show_token(expr->token));
290 printf("<nosymbol>");
291 break;
293 printf("<%s:", show_token(expr->symbol->token));
294 show_type(expr->symbol);
295 printf(">");
296 break;
297 case EXPR_DEREF:
298 show_expression(expr->deref);
299 printf("%s", show_special(expr->op));
300 printf("%s", show_token(expr->member));
301 break;
302 case EXPR_CAST:
303 printf("<cast>(");
304 show_type(expr->cast_type);
305 printf(")");
306 show_expression(expr->cast_expression);
307 break;
308 default:
309 printf("WTF");
311 printf(" >");