smatch_kernel_host_data: enable additional debug
[smatch.git] / ast-inspect.c
blobb510cd9b1d2ce191c4d6c27abfb360760625cc6e
2 #include "token.h"
3 #include "parse.h"
4 #include "symbol.h"
5 #include "ast-inspect.h"
6 #include "expression.h"
8 static inline void inspect_ptr_list(AstNode *node, const char *name, void (*inspect)(AstNode *))
10 struct ptr_list *ptrlist = node->ptr;
11 void *ptr;
12 int i = 0;
14 node->text = g_strdup_printf("%s %s:", node->text, name);
15 FOR_EACH_PTR(ptrlist, ptr) {
16 char *index = g_strdup_printf("%d: ", i++);
17 ast_append_child(node, index, ptr, inspect);
18 } END_FOR_EACH_PTR(ptr);
22 static const char *statement_type_name(enum statement_type type)
24 static const char *statement_type_name[] = {
25 [STMT_NONE] = "STMT_NONE",
26 [STMT_DECLARATION] = "STMT_DECLARATION",
27 [STMT_EXPRESSION] = "STMT_EXPRESSION",
28 [STMT_COMPOUND] = "STMT_COMPOUND",
29 [STMT_IF] = "STMT_IF",
30 [STMT_RETURN] = "STMT_RETURN",
31 [STMT_CASE] = "STMT_CASE",
32 [STMT_SWITCH] = "STMT_SWITCH",
33 [STMT_ITERATOR] = "STMT_ITERATOR",
34 [STMT_LABEL] = "STMT_LABEL",
35 [STMT_GOTO] = "STMT_GOTO",
36 [STMT_ASM] = "STMT_ASM",
37 [STMT_CONTEXT] = "STMT_CONTEXT",
38 [STMT_RANGE] = "STMT_RANGE",
40 return statement_type_name[type] ?: "UNKNOWN_STATEMENT_TYPE";
43 void inspect_statement(AstNode *node)
45 struct statement *stmt = node->ptr;
46 node->text = g_strdup_printf("%s %s:", node->text, statement_type_name(stmt->type));
47 switch (stmt->type) {
48 case STMT_COMPOUND:
49 ast_append_child(node, "stmts:", stmt->stmts, inspect_statement_list);
50 break;
51 case STMT_EXPRESSION:
52 ast_append_child(node, "expression:", stmt->expression, inspect_expression);
53 break;
54 case STMT_IF:
55 ast_append_child(node, "conditional:", stmt->if_conditional, inspect_expression);
56 ast_append_child(node, "if_true:", stmt->if_true, inspect_statement);
57 ast_append_child(node, "if_false:", stmt->if_false, inspect_statement);
58 break;
59 case STMT_ITERATOR:
60 ast_append_child(node, "break:", stmt->iterator_break, inspect_symbol);
61 ast_append_child(node, "continue:", stmt->iterator_continue, inspect_symbol);
62 ast_append_child(node, "pre_statement:", stmt->iterator_pre_statement,
63 inspect_statement);
64 ast_append_child(node, "statement:", stmt->iterator_statement,
65 inspect_statement);
66 ast_append_child(node, "post_statement:", stmt->iterator_post_statement,
67 inspect_statement);
68 break;
70 case STMT_SWITCH:
71 ast_append_child(node, "switch_expression:", stmt->switch_expression, inspect_expression);
72 ast_append_child(node, "switch_statement:", stmt->switch_statement, inspect_statement);
73 ast_append_child(node, "switch_break:", stmt->switch_break, inspect_symbol);
74 ast_append_child(node, "switch_case:", stmt->switch_case, inspect_symbol);
75 break;
76 case STMT_CASE:
77 ast_append_child(node, "case_expression:", stmt->case_expression, inspect_expression);
78 ast_append_child(node, "case_to:", stmt->case_to, inspect_expression);
79 ast_append_child(node, "case_statement:", stmt->case_statement, inspect_statement);
80 ast_append_child(node, "case_label:", stmt->case_label, inspect_symbol);
81 break;
82 case STMT_RETURN:
83 ast_append_child(node, "ret_value:", stmt->ret_value, inspect_expression);
84 ast_append_child(node, "ret_target:", stmt->ret_target, inspect_symbol);
85 break;
87 default:
88 break;
93 void inspect_statement_list(AstNode *node)
95 inspect_ptr_list(node, "statement_list", inspect_statement);
99 static const char *symbol_type_name(enum type type)
101 static const char *type_name[] = {
102 [SYM_UNINITIALIZED] = "SYM_UNINITIALIZED",
103 [SYM_PREPROCESSOR] = "SYM_PREPROCESSOR",
104 [SYM_BASETYPE] = "SYM_BASETYPE",
105 [SYM_NODE] = "SYM_NODE",
106 [SYM_PTR] = "SYM_PTR",
107 [SYM_FN] = "SYM_FN",
108 [SYM_ARRAY] = "SYM_ARRAY",
109 [SYM_STRUCT] = "SYM_STRUCT",
110 [SYM_UNION] = "SYM_UNION",
111 [SYM_ENUM] = "SYM_ENUM",
112 [SYM_TYPEOF] = "SYM_TYPEOF",
113 [SYM_BITFIELD] = "SYM_BITFIELD",
114 [SYM_LABEL] = "SYM_LABEL",
115 [SYM_RESTRICT] = "SYM_RESTRICT",
116 [SYM_FOULED] = "SYM_FOULED",
117 [SYM_KEYWORD] = "SYM_KEYWORD",
118 [SYM_BAD] = "SYM_BAD",
120 return type_name[type] ?: "UNKNOWN_TYPE";
124 void inspect_symbol(AstNode *node)
126 struct symbol *sym = node->ptr;
127 node->text = g_strdup_printf("%s %s: %s", node->text, symbol_type_name(sym->type),
128 builtin_typename(sym) ?: show_ident(sym->ident));
129 ast_append_child(node, "ctype.base_type:", sym->ctype.base_type,inspect_symbol);
131 switch (sym->namespace) {
132 case NS_PREPROCESSOR:
133 break;
134 default:
135 ast_append_child(node, "arguments:", sym->arguments, inspect_symbol_list);
136 ast_append_child(node, "symbol_list:", sym->symbol_list, inspect_symbol_list);
137 ast_append_child(node, "stmt:", sym->stmt, inspect_statement);
138 break;
143 void inspect_symbol_list(AstNode *node)
145 inspect_ptr_list(node, "symbol_list", inspect_symbol);
149 static const char *expression_type_name(enum expression_type type)
151 static const char *expression_type_name[] = {
152 [EXPR_VALUE] = "EXPR_VALUE",
153 [EXPR_STRING] = "EXPR_STRING",
154 [EXPR_SYMBOL] = "EXPR_SYMBOL",
155 [EXPR_TYPE] = "EXPR_TYPE",
156 [EXPR_BINOP] = "EXPR_BINOP",
157 [EXPR_ASSIGNMENT] = "EXPR_ASSIGNMENT",
158 [EXPR_LOGICAL] = "EXPR_LOGICAL",
159 [EXPR_DEREF] = "EXPR_DEREF",
160 [EXPR_PREOP] = "EXPR_PREOP",
161 [EXPR_POSTOP] = "EXPR_POSTOP",
162 [EXPR_CAST] = "EXPR_CAST",
163 [EXPR_FORCE_CAST] = "EXPR_FORCE_CAST",
164 [EXPR_IMPLIED_CAST] = "EXPR_IMPLIED_CAST",
165 [EXPR_SIZEOF] = "EXPR_SIZEOF",
166 [EXPR_ALIGNOF] = "EXPR_ALIGNOF",
167 [EXPR_PTRSIZEOF] = "EXPR_PTRSIZEOF",
168 [EXPR_CONDITIONAL] = "EXPR_CONDITIONAL",
169 [EXPR_SELECT] = "EXPR_SELECT",
170 [EXPR_STATEMENT] = "EXPR_STATEMENT",
171 [EXPR_CALL] = "EXPR_CALL",
172 [EXPR_COMMA] = "EXPR_COMMA",
173 [EXPR_COMPARE] = "EXPR_COMPARE",
174 [EXPR_LABEL] = "EXPR_LABEL",
175 [EXPR_INITIALIZER] = "EXPR_INITIALIZER",
176 [EXPR_IDENTIFIER] = "EXPR_IDENTIFIER",
177 [EXPR_INDEX] = "EXPR_INDEX",
178 [EXPR_POS] = "EXPR_POS",
179 [EXPR_FVALUE] = "EXPR_FVALUE",
180 [EXPR_SLICE] = "EXPR_SLICE",
181 [EXPR_OFFSETOF] = "EXPR_OFFSETOF",
183 return expression_type_name[type] ?: "UNKNOWN_EXPRESSION_TYPE";
186 void inspect_expression(AstNode *node)
188 struct expression *expr = node->ptr;
189 node->text = g_strdup_printf("%s %s", node->text, expression_type_name(expr->type));
190 switch (expr->type) {
191 case EXPR_STATEMENT:
192 ast_append_child(node, "statement:", expr->statement, inspect_statement);
193 break;
194 case EXPR_BINOP:
195 case EXPR_COMMA:
196 case EXPR_COMPARE:
197 case EXPR_LOGICAL:
198 case EXPR_ASSIGNMENT:
199 ast_append_child(node, "left:", expr->left, inspect_expression);
200 ast_append_child(node, "right:", expr->right, inspect_expression);
201 break;
203 case EXPR_CAST:
204 case EXPR_FORCE_CAST:
205 case EXPR_IMPLIED_CAST:
206 ast_append_child(node, "cast_type:", expr->cast_type, inspect_symbol);
207 ast_append_child(node, "cast_expression:", expr->cast_expression, inspect_expression);
208 break;
210 case EXPR_PREOP:
211 ast_append_child(node, "unop:", expr->unop, inspect_expression);
212 break;
214 default:
215 break;