Print this page
11506 smatch resync


 124                 return NULL;
 125 
 126         type = get_type(expr);
 127         if (type && type->type == SYM_PTR)
 128                 type = get_real_base_type(type);
 129         if (!type || type->type != SYM_FN)
 130                 return NULL;
 131 
 132         name = expr_to_var(expr->fn);
 133         if (!name)
 134                 return NULL;
 135         snprintf(buf, sizeof(buf), "r %s()", name);
 136         free_string(name);
 137         return alloc_string(buf);
 138 }
 139 
 140 char *get_fnptr_name(struct expression *expr)
 141 {
 142         char *name;
 143 



 144         expr = strip_expr(expr);
 145 
 146         /* (*ptrs[0])(a, b, c) is the same as ptrs[0](a, b, c); */
 147         if (expr->type == EXPR_PREOP && expr->op == '*')
 148                 expr = strip_expr(expr->unop);
 149 
 150         name = get_from__symbol_get(expr);
 151         if (name)
 152                 return name;
 153 
 154         name = get_array_ptr(expr);
 155         if (name)
 156                 return name;
 157 
 158         name = get_returned_ptr(expr);
 159         if (name)
 160                 return name;
 161 
 162         name = get_member_name(expr);
 163         if (name)


 328         type = get_real_base_type(cur_func_sym);
 329         if (!type || type->type != SYM_FN)
 330                 return;
 331         type = get_real_base_type(type);
 332         if (!type || type->type != SYM_PTR)
 333                 return;
 334         type = get_real_base_type(type);
 335         if (!type || type->type != SYM_FN)
 336                 return;
 337 
 338         if (expr->type == EXPR_PREOP && expr->op == '&')
 339                 expr = strip_expr(expr->unop);
 340 
 341         fn_name = get_fnptr_name(expr);
 342         if (!fn_name)
 343                 return;
 344         snprintf(ptr_name, sizeof(ptr_name), "r %s()", get_function());
 345         sql_insert_function_ptr(fn_name, ptr_name);
 346 }
 347 



















































 348 void register_function_ptrs(int id)
 349 {
 350         my_id = id;
 351 
 352         if (!option_info)
 353                 return;
 354 


 355         add_hook(&match_passes_function_pointer, FUNCTION_CALL_HOOK);
 356         add_hook(&match_returns_function_pointer, RETURN_HOOK);
 357         add_hook(&match_function_assign, ASSIGNMENT_HOOK);
 358         add_hook(&match_function_assign, GLOBAL_ASSIGNMENT_HOOK);
 359 }


 124                 return NULL;
 125 
 126         type = get_type(expr);
 127         if (type && type->type == SYM_PTR)
 128                 type = get_real_base_type(type);
 129         if (!type || type->type != SYM_FN)
 130                 return NULL;
 131 
 132         name = expr_to_var(expr->fn);
 133         if (!name)
 134                 return NULL;
 135         snprintf(buf, sizeof(buf), "r %s()", name);
 136         free_string(name);
 137         return alloc_string(buf);
 138 }
 139 
 140 char *get_fnptr_name(struct expression *expr)
 141 {
 142         char *name;
 143 
 144         if (is_zero(expr))
 145                 return NULL;
 146 
 147         expr = strip_expr(expr);
 148 
 149         /* (*ptrs[0])(a, b, c) is the same as ptrs[0](a, b, c); */
 150         if (expr->type == EXPR_PREOP && expr->op == '*')
 151                 expr = strip_expr(expr->unop);
 152 
 153         name = get_from__symbol_get(expr);
 154         if (name)
 155                 return name;
 156 
 157         name = get_array_ptr(expr);
 158         if (name)
 159                 return name;
 160 
 161         name = get_returned_ptr(expr);
 162         if (name)
 163                 return name;
 164 
 165         name = get_member_name(expr);
 166         if (name)


 331         type = get_real_base_type(cur_func_sym);
 332         if (!type || type->type != SYM_FN)
 333                 return;
 334         type = get_real_base_type(type);
 335         if (!type || type->type != SYM_PTR)
 336                 return;
 337         type = get_real_base_type(type);
 338         if (!type || type->type != SYM_FN)
 339                 return;
 340 
 341         if (expr->type == EXPR_PREOP && expr->op == '&')
 342                 expr = strip_expr(expr->unop);
 343 
 344         fn_name = get_fnptr_name(expr);
 345         if (!fn_name)
 346                 return;
 347         snprintf(ptr_name, sizeof(ptr_name), "r %s()", get_function());
 348         sql_insert_function_ptr(fn_name, ptr_name);
 349 }
 350 
 351 static void print_initializer_list(struct expression_list *expr_list,
 352                 struct symbol *struct_type)
 353 {
 354         struct expression *expr;
 355         struct symbol *base_type;
 356         char struct_name[256];
 357 
 358         FOR_EACH_PTR(expr_list, expr) {
 359                 if (expr->type == EXPR_INDEX && expr->idx_expression && expr->idx_expression->type == EXPR_INITIALIZER) {
 360                         print_initializer_list(expr->idx_expression->expr_list, struct_type);
 361                         continue;
 362                 }
 363                 if (expr->type != EXPR_IDENTIFIER)
 364                         continue;
 365                 if (!expr->expr_ident)
 366                         continue;
 367                 if (!expr->ident_expression ||
 368                     expr->ident_expression->type != EXPR_SYMBOL ||
 369                     !expr->ident_expression->symbol_name)
 370                         continue;
 371                 base_type = get_type(expr->ident_expression);
 372                 if (!base_type || base_type->type != SYM_FN)
 373                         continue;
 374                 snprintf(struct_name, sizeof(struct_name), "(struct %s)->%s",
 375                          struct_type->ident->name, expr->expr_ident->name);
 376                 sql_insert_function_ptr(expr->ident_expression->symbol_name->name,
 377                                         struct_name);
 378         } END_FOR_EACH_PTR(expr);
 379 }
 380 
 381 static void global_variable(struct symbol *sym)
 382 {
 383         struct symbol *struct_type;
 384 
 385         if (!sym->ident)
 386                 return;
 387         if (!sym->initializer || sym->initializer->type != EXPR_INITIALIZER)
 388                 return;
 389         struct_type = get_base_type(sym);
 390         if (!struct_type)
 391                 return;
 392         if (struct_type->type == SYM_ARRAY) {
 393                 struct_type = get_base_type(struct_type);
 394                 if (!struct_type)
 395                         return;
 396         }
 397         if (struct_type->type != SYM_STRUCT || !struct_type->ident)
 398                 return;
 399         print_initializer_list(sym->initializer->expr_list, struct_type);
 400 }
 401 
 402 void register_function_ptrs(int id)
 403 {
 404         my_id = id;
 405 
 406         if (!option_info)
 407                 return;
 408 
 409         add_hook(&global_variable, BASE_HOOK);
 410         add_hook(&global_variable, DECLARATION_HOOK);
 411         add_hook(&match_passes_function_pointer, FUNCTION_CALL_HOOK);
 412         add_hook(&match_returns_function_pointer, RETURN_HOOK);
 413         add_hook(&match_function_assign, ASSIGNMENT_HOOK);
 414         add_hook(&match_function_assign, GLOBAL_ASSIGNMENT_HOOK);
 415 }