fill_db_caller_info: calculate function_id correctly
[smatch.git] / ast-inspect.c
blob5363de3c9b62714858a3577dd5cc6975c298f067
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_RETURN:
71 ast_append_child(node, "ret_value:", stmt->ret_value, inspect_expression);
72 ast_append_child(node, "ret_target:", stmt->ret_target, inspect_symbol);
73 break;
75 default:
76 break;
81 void inspect_statement_list(AstNode *node)
83 inspect_ptr_list(node, "statement_list", inspect_statement);
87 static const char *symbol_type_name(enum type type)
89 static const char *type_name[] = {
90 [SYM_UNINITIALIZED] = "SYM_UNINITIALIZED",
91 [SYM_PREPROCESSOR] = "SYM_PREPROCESSOR",
92 [SYM_BASETYPE] = "SYM_BASETYPE",
93 [SYM_NODE] = "SYM_NODE",
94 [SYM_PTR] = "SYM_PTR",
95 [SYM_FN] = "SYM_FN",
96 [SYM_ARRAY] = "SYM_ARRAY",
97 [SYM_STRUCT] = "SYM_STRUCT",
98 [SYM_UNION] = "SYM_UNION",
99 [SYM_ENUM] = "SYM_ENUM",
100 [SYM_TYPEDEF] = "SYM_TYPEDEF",
101 [SYM_TYPEOF] = "SYM_TYPEOF",
102 [SYM_MEMBER] = "SYM_MEMBER",
103 [SYM_BITFIELD] = "SYM_BITFIELD",
104 [SYM_LABEL] = "SYM_LABEL",
105 [SYM_RESTRICT] = "SYM_RESTRICT",
106 [SYM_FOULED] = "SYM_FOULED",
107 [SYM_KEYWORD] = "SYM_KEYWORD",
108 [SYM_BAD] = "SYM_BAD",
110 return type_name[type] ?: "UNKNOWN_TYPE";
114 void inspect_symbol(AstNode *node)
116 struct symbol *sym = node->ptr;
117 node->text = g_strdup_printf("%s %s: %s", node->text, symbol_type_name(sym->type),
118 builtin_typename(sym) ?: show_ident(sym->ident));
119 ast_append_child(node, "ctype.base_type:", sym->ctype.base_type,inspect_symbol);
121 switch (sym->type) {
122 case SYM_FN:
123 ast_append_child(node, "stmt:", sym->stmt, inspect_statement);
124 break;
125 default:
126 break;
131 void inspect_symbol_list(AstNode *node)
133 inspect_ptr_list(node, "symbol_list", inspect_symbol);
137 static const char *expression_type_name(enum expression_type type)
139 static const char *expression_type_name[] = {
140 [EXPR_VALUE] = "EXPR_VALUE",
141 [EXPR_STRING] = "EXPR_STRING",
142 [EXPR_SYMBOL] = "EXPR_SYMBOL",
143 [EXPR_TYPE] = "EXPR_TYPE",
144 [EXPR_BINOP] = "EXPR_BINOP",
145 [EXPR_ASSIGNMENT] = "EXPR_ASSIGNMENT",
146 [EXPR_LOGICAL] = "EXPR_LOGICAL",
147 [EXPR_DEREF] = "EXPR_DEREF",
148 [EXPR_PREOP] = "EXPR_PREOP",
149 [EXPR_POSTOP] = "EXPR_POSTOP",
150 [EXPR_CAST] = "EXPR_CAST",
151 [EXPR_FORCE_CAST] = "EXPR_FORCE_CAST",
152 [EXPR_IMPLIED_CAST] = "EXPR_IMPLIED_CAST",
153 [EXPR_SIZEOF] = "EXPR_SIZEOF",
154 [EXPR_ALIGNOF] = "EXPR_ALIGNOF",
155 [EXPR_PTRSIZEOF] = "EXPR_PTRSIZEOF",
156 [EXPR_CONDITIONAL] = "EXPR_CONDITIONAL",
157 [EXPR_SELECT] = "EXPR_SELECT",
158 [EXPR_STATEMENT] = "EXPR_STATEMENT",
159 [EXPR_CALL] = "EXPR_CALL",
160 [EXPR_COMMA] = "EXPR_COMMA",
161 [EXPR_COMPARE] = "EXPR_COMPARE",
162 [EXPR_LABEL] = "EXPR_LABEL",
163 [EXPR_INITIALIZER] = "EXPR_INITIALIZER",
164 [EXPR_IDENTIFIER] = "EXPR_IDENTIFIER",
165 [EXPR_INDEX] = "EXPR_INDEX",
166 [EXPR_POS] = "EXPR_POS",
167 [EXPR_FVALUE] = "EXPR_FVALUE",
168 [EXPR_SLICE] = "EXPR_SLICE",
169 [EXPR_OFFSETOF] = "EXPR_OFFSETOF",
171 return expression_type_name[type] ?: "UNKNOWN_EXPRESSION_TYPE";
174 void inspect_expression(AstNode *node)
176 struct expression *expr = node->ptr;
177 node->text = g_strdup_printf("%s %s", node->text, expression_type_name(expr->type));
178 switch (expr->type) {
179 case EXPR_STATEMENT:
180 ast_append_child(node, "statement:", expr->statement, inspect_statement);
181 break;
182 case EXPR_BINOP:
183 case EXPR_COMMA:
184 case EXPR_COMPARE:
185 case EXPR_LOGICAL:
186 case EXPR_ASSIGNMENT:
187 ast_append_child(node, "left:", expr->left, inspect_expression);
188 ast_append_child(node, "right:", expr->right, inspect_expression);
189 break;
191 case EXPR_CAST:
192 case EXPR_FORCE_CAST:
193 case EXPR_IMPLIED_CAST:
194 ast_append_child(node, "cast_type:", expr->cast_type, inspect_symbol);
195 ast_append_child(node, "cast_expression:", expr->cast_expression, inspect_expression);
196 break;
198 case EXPR_PREOP:
199 ast_append_child(node, "unop:", expr->unop, inspect_expression);
200 break;
202 default:
203 break;