*new* input_free_device: don't call this after input_unregister_device()
[smatch.git] / ast-inspect.c
blob293334e991db7f97ca3d24483def4090b5fc6049
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;
69 default:
70 break;
75 void inspect_statement_list(AstNode *node)
77 inspect_ptr_list(node, "statement_list", inspect_statement);
81 static const char *symbol_type_name(enum type type)
83 static const char *type_name[] = {
84 [SYM_UNINITIALIZED] = "SYM_UNINITIALIZED",
85 [SYM_PREPROCESSOR] = "SYM_PREPROCESSOR",
86 [SYM_BASETYPE] = "SYM_BASETYPE",
87 [SYM_NODE] = "SYM_NODE",
88 [SYM_PTR] = "SYM_PTR",
89 [SYM_FN] = "SYM_FN",
90 [SYM_ARRAY] = "SYM_ARRAY",
91 [SYM_STRUCT] = "SYM_STRUCT",
92 [SYM_UNION] = "SYM_UNION",
93 [SYM_ENUM] = "SYM_ENUM",
94 [SYM_TYPEDEF] = "SYM_TYPEDEF",
95 [SYM_TYPEOF] = "SYM_TYPEOF",
96 [SYM_MEMBER] = "SYM_MEMBER",
97 [SYM_BITFIELD] = "SYM_BITFIELD",
98 [SYM_LABEL] = "SYM_LABEL",
99 [SYM_RESTRICT] = "SYM_RESTRICT",
100 [SYM_FOULED] = "SYM_FOULED",
101 [SYM_KEYWORD] = "SYM_KEYWORD",
102 [SYM_BAD] = "SYM_BAD",
104 return type_name[type] ?: "UNKNOWN_TYPE";
108 void inspect_symbol(AstNode *node)
110 struct symbol *sym = node->ptr;
111 node->text = g_strdup_printf("%s %s: %s", node->text, symbol_type_name(sym->type),
112 show_ident(sym->ident));
113 ast_append_child(node, "ctype.base_type:", sym->ctype.base_type,inspect_symbol);
115 switch (sym->type) {
116 case SYM_FN:
117 ast_append_child(node, "stmt:", sym->stmt, inspect_statement);
118 break;
119 default:
120 break;
125 void inspect_symbol_list(AstNode *node)
127 inspect_ptr_list(node, "symbol_list", inspect_symbol);
131 static const char *expression_type_name(enum expression_type type)
133 static const char *expression_type_name[] = {
134 [EXPR_VALUE] = "EXPR_VALUE",
135 [EXPR_STRING] = "EXPR_STRING",
136 [EXPR_SYMBOL] = "EXPR_SYMBOL",
137 [EXPR_TYPE] = "EXPR_TYPE",
138 [EXPR_BINOP] = "EXPR_BINOP",
139 [EXPR_ASSIGNMENT] = "EXPR_ASSIGNMENT",
140 [EXPR_LOGICAL] = "EXPR_LOGICAL",
141 [EXPR_DEREF] = "EXPR_DEREF",
142 [EXPR_PREOP] = "EXPR_PREOP",
143 [EXPR_POSTOP] = "EXPR_POSTOP",
144 [EXPR_CAST] = "EXPR_CAST",
145 [EXPR_FORCE_CAST] = "EXPR_FORCE_CAST",
146 [EXPR_IMPLIED_CAST] = "EXPR_IMPLIED_CAST",
147 [EXPR_SIZEOF] = "EXPR_SIZEOF",
148 [EXPR_ALIGNOF] = "EXPR_ALIGNOF",
149 [EXPR_PTRSIZEOF] = "EXPR_PTRSIZEOF",
150 [EXPR_CONDITIONAL] = "EXPR_CONDITIONAL",
151 [EXPR_SELECT] = "EXPR_SELECT",
152 [EXPR_STATEMENT] = "EXPR_STATEMENT",
153 [EXPR_CALL] = "EXPR_CALL",
154 [EXPR_COMMA] = "EXPR_COMMA",
155 [EXPR_COMPARE] = "EXPR_COMPARE",
156 [EXPR_LABEL] = "EXPR_LABEL",
157 [EXPR_INITIALIZER] = "EXPR_INITIALIZER",
158 [EXPR_IDENTIFIER] = "EXPR_IDENTIFIER",
159 [EXPR_INDEX] = "EXPR_INDEX",
160 [EXPR_POS] = "EXPR_POS",
161 [EXPR_FVALUE] = "EXPR_FVALUE",
162 [EXPR_SLICE] = "EXPR_SLICE",
163 [EXPR_OFFSETOF] = "EXPR_OFFSETOF",
165 return expression_type_name[type] ?: "UNKNOWN_EXPRESSION_TYPE";
168 void inspect_expression(AstNode *node)
170 struct expression *expr = node->ptr;
171 node->text = g_strdup_printf("%s %s", node->text, expression_type_name(expr->type));
172 switch (expr->type) {
173 case EXPR_STATEMENT:
174 ast_append_child(node, "statement:", expr->statement, inspect_statement);
175 break;
176 case EXPR_BINOP:
177 case EXPR_COMMA:
178 case EXPR_COMPARE:
179 case EXPR_LOGICAL:
180 case EXPR_ASSIGNMENT:
181 ast_append_child(node, "left:", expr->left, inspect_expression);
182 ast_append_child(node, "right:", expr->right, inspect_expression);
183 break;
184 default:
185 break;