[PATCH smatch-devel 5/5] smatch: report passed-to history for --ai

Harshit Mogalapalli <[email protected]>
Newsgroups org.kernel.vger.smatch
Message-ID <[email protected]>
Replace the generic current-state dump in --ai reports with a callback list
so modules can report focused state without recursively dumping every check.

Register the passed-to tracker as the first callback.  Walk its current
states and recursively print each function history with the tracked variable
name and argument number on the same line.

Assisted-by: Codex:5.6
Signed-off-by: Harshit Mogalapalli <[email protected]>
---
 smatch.h           |  4 +++-
 smatch_hooks.c     | 11 +++++++++++
 smatch_passed_to.c | 29 +++++++++++++++++++++++------
 3 files changed, 37 insertions(+), 7 deletions(-)

diff --git a/smatch.h b/smatch.h
index 58edc256965a..32eec7c3ccd7 100644
--- a/smatch.h
+++ b/smatch.h
@@ -186,6 +186,8 @@ DECLARE_PTR_LIST(name_sym_fn_list, name_sym_hook);
 DECLARE_PTR_LIST(string_hook_list, string_hook);
 DECLARE_PTR_LIST(stree_func_list, stree_func);
 void call_void_fns(struct void_fn_list *list);
+void register_ai_info(void_fn *fn);
+void print_ai_info(void);
 void call_expr_fns(struct expr_fn_list *list, struct expression *expr);
 void call_stmt_fns(struct stmt_fn_list *list, struct statement *stmt);
 void call_sym_fns(struct sym_fn_list *list, struct symbol *sym);
@@ -449,7 +451,7 @@ do {                                                           \
 	if (option_ai) {				       \
 		int __saved_option_ai = option_ai;	       \
 		option_ai = 0;				       \
-		__print_cur_stree();			       \
+		print_ai_info();			       \
 		option_ai = __saved_option_ai;		       \
 		sm_printf("end report: %d\n", __this_warn);    \
 	}						       \
diff --git a/smatch_hooks.c b/smatch_hooks.c
index 0933df9c6787..813c5fbfde56 100644
--- a/smatch_hooks.c
+++ b/smatch_hooks.c
@@ -135,6 +135,17 @@ void add_pre_merge_hook(int client_id, void (*hook)(struct sm_state *cur, struct
 }
 
 struct position *__hook_pos;
+static struct void_fn_list *ai_info_hooks;
+
+void register_ai_info(void_fn *fn)
+{
+	add_ptr_list(&ai_info_hooks, fn);
+}
+
+void print_ai_info(void)
+{
+	call_void_fns(ai_info_hooks);
+}
 
 static void pass_expr_to_client(expr_func *fn, void *data)
 {
diff --git a/smatch_passed_to.c b/smatch_passed_to.c
index 06b1e2a77fec..bc9231715d43 100644
--- a/smatch_passed_to.c
+++ b/smatch_passed_to.c
@@ -67,7 +67,8 @@ static void match_call(struct expression *expr)
 	free_string(fn_name);
 }
 
-static void print_passed_to_sm(struct sm_state *sm, struct state_list **printed)
+static void print_passed_to_sm(struct sm_state *sm, const char *name,
+			       struct state_list **printed)
 {
 	struct smatch_state_data *data;
 	char *fn_name;
@@ -80,17 +81,20 @@ static void print_passed_to_sm(struct sm_state *sm, struct state_list **printed)
 
 	data = sm->state->data;
 	if (!data) {
-		print_passed_to_sm(sm->left, printed);
-		print_passed_to_sm(sm->right, printed);
+		print_passed_to_sm(sm->left, name, printed);
+		print_passed_to_sm(sm->right, name, printed);
 		return;
 	}
 
-	print_passed_to_sm(data->previous, printed);
+	print_passed_to_sm(data->previous, name, printed);
 
 	fn_name = expr_to_str(data->expr->fn);
 	if (!fn_name)
 		return;
-	sm_msg("passed to %s $%d", fn_name, data->param);
+	if (name)
+		sm_msg("%s passed to %s $%d", name, fn_name, data->param);
+	else
+		sm_msg("passed to %s $%d", fn_name, data->param);
 	free_string(fn_name);
 }
 
@@ -98,14 +102,27 @@ void print_passed_to(struct expression *expr)
 {
 	struct state_list *printed = NULL;
 
-	print_passed_to_sm(get_sm_state_expr(my_id, expr), &printed);
+	print_passed_to_sm(get_sm_state_expr(my_id, expr), NULL, &printed);
 	free_slist(&printed);
 }
 
+static void print_passed_to_states(void)
+{
+	struct state_list *printed;
+	struct sm_state *sm;
+
+	FOR_EACH_MY_SM(my_id, __get_cur_stree(), sm) {
+		printed = NULL;
+		print_passed_to_sm(sm, sm->name, &printed);
+		free_slist(&printed);
+	} END_FOR_EACH_SM(sm);
+}
+
 void smatch_passed_to(int id)
 {
 	my_id = id;
 
 	set_dynamic_states(my_id);
 	add_hook(&match_call, FUNCTION_CALL_HOOK);
+	register_ai_info(&print_passed_to_states);
 }
-- 
2.52.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.