function_hooks: fix locking false positives when you have no db
[smatch.git] / ast-inspect.c
blob24d4a4a650236d54f010a0e7cd915722d37a822e
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_TYPEDEF] = "SYM_TYPEDEF",
113 [SYM_TYPEOF] = "SYM_TYPEOF",
114 [SYM_MEMBER] = "SYM_MEMBER",
115 [SYM_BITFIELD] = "SYM_BITFIELD",
116 [SYM_LABEL] = "SYM_LABEL",
117 [SYM_RESTRICT] = "SYM_RESTRICT",
118 [SYM_FOULED] = "SYM_FOULED",
119 [SYM_KEYWORD] = "SYM_KEYWORD",
120 [SYM_BAD] = "SYM_BAD",
122 return type_name[type] ?: "UNKNOWN_TYPE";
126 void inspect_symbol(AstNode *node)
128 struct symbol *sym = node->ptr;
129 node->text = g_strdup_printf("%s %s: %s", node->text, symbol_type_name(sym->type),
130 builtin_typename(sym) ?: show_ident(sym->ident));
131 ast_append_child(node, "ctype.base_type:", sym->ctype.base_type,inspect_symbol);
133 switch (sym->namespace) {
134 case NS_PREPROCESSOR:
135 break;
136 default:
137 ast_append_child(node, "arguments:", sym->arguments, inspect_symbol_list);
138 ast_append_child(node, "symbol_list:", sym->symbol_list, inspect_symbol_list);
139 ast_append_child(node, "stmt:", sym->stmt, inspect_statement);
140 break;
145 void inspect_symbol_list(AstNode *node)
147 inspect_ptr_list(node, "symbol_list", inspect_symbol);
151 static const char *expression_type_name(enum expression_type type)
153 static const char *expression_type_name[] = {
154 [EXPR_VALUE] = "EXPR_VALUE",
155 [EXPR_STRING] = "EXPR_STRING",
156 [EXPR_SYMBOL] = "EXPR_SYMBOL",
157 [EXPR_TYPE] = "EXPR_TYPE",
158 [EXPR_BINOP] = "EXPR_BINOP",
159 [EXPR_ASSIGNMENT] = "EXPR_ASSIGNMENT",
160 [EXPR_LOGICAL] = "EXPR_LOGICAL",
161 [EXPR_DEREF] = "EXPR_DEREF",
162 [EXPR_PREOP] = "EXPR_PREOP",
163 [EXPR_POSTOP] = "EXPR_POSTOP",
164 [EXPR_CAST] = "EXPR_CAST",
165 [EXPR_FORCE_CAST] = "EXPR_FORCE_CAST",
166 [EXPR_IMPLIED_CAST] = "EXPR_IMPLIED_CAST",
167 [EXPR_SIZEOF] = "EXPR_SIZEOF",
168 [EXPR_ALIGNOF] = "EXPR_ALIGNOF",
169 [EXPR_PTRSIZEOF] = "EXPR_PTRSIZEOF",
170 [EXPR_CONDITIONAL] = "EXPR_CONDITIONAL",
171 [EXPR_SELECT] = "EXPR_SELECT",
172 [EXPR_STATEMENT] = "EXPR_STATEMENT",
173 [EXPR_CALL] = "EXPR_CALL",
174 [EXPR_COMMA] = "EXPR_COMMA",
175 [EXPR_COMPARE] = "EXPR_COMPARE",
176 [EXPR_LABEL] = "EXPR_LABEL",
177 [EXPR_INITIALIZER] = "EXPR_INITIALIZER",
178 [EXPR_IDENTIFIER] = "EXPR_IDENTIFIER",
179 [EXPR_INDEX] = "EXPR_INDEX",
180 [EXPR_POS] = "EXPR_POS",
181 [EXPR_FVALUE] = "EXPR_FVALUE",
182 [EXPR_SLICE] = "EXPR_SLICE",
183 [EXPR_OFFSETOF] = "EXPR_OFFSETOF",
185 return expression_type_name[type] ?: "UNKNOWN_EXPRESSION_TYPE";
188 void inspect_expression(AstNode *node)
190 struct expression *expr = node->ptr;
191 node->text = g_strdup_printf("%s %s", node->text, expression_type_name(expr->type));
192 switch (expr->type) {
193 case EXPR_STATEMENT:
194 ast_append_child(node, "statement:", expr->statement, inspect_statement);
195 break;
196 case EXPR_BINOP:
197 case EXPR_COMMA:
198 case EXPR_COMPARE:
199 case EXPR_LOGICAL:
200 case EXPR_ASSIGNMENT:
201 ast_append_child(node, "left:", expr->left, inspect_expression);
202 ast_append_child(node, "right:", expr->right, inspect_expression);
203 break;
205 case EXPR_CAST:
206 case EXPR_FORCE_CAST:
207 case EXPR_IMPLIED_CAST:
208 ast_append_child(node, "cast_type:", expr->cast_type, inspect_symbol);
209 ast_append_child(node, "cast_expression:", expr->cast_expression, inspect_expression);
210 break;
212 case EXPR_PREOP:
213 ast_append_child(node, "unop:", expr->unop, inspect_expression);
214 break;
216 default:
217 break;