[PATCH v4 1/5] kconfig: add add another callback to the parser to view raw parse tree
From: Julian Braha
Date: Sun Jul 26 2026 - 20:17:03 EST
The final step of the Kconfig parser is to call menu_finalize(), which
will evaluate the dead code that static code analyzers like kconfirm need
to be able to view.
This patch adds another callback to the parser just before this call to
menu_finalize(), making the raw parse tree viewable as necessary to
callers of conf_set_pre_finalize_callback().
Assisted-by: Claude:claude-fable-5
Signed-off-by: Julian Braha <julianbraha@xxxxxxxxx>
---
scripts/kconfig/lkc_proto.h | 2 ++
scripts/kconfig/parser.y | 21 +++++++++++++++++++++
2 files changed, 23 insertions(+)
diff --git a/scripts/kconfig/lkc_proto.h b/scripts/kconfig/lkc_proto.h
index 8914b4e8f2a8..f03bcef7b270 100644
--- a/scripts/kconfig/lkc_proto.h
+++ b/scripts/kconfig/lkc_proto.h
@@ -6,6 +6,8 @@
/* confdata.c */
void conf_parse(const char *name);
+void conf_set_pre_finalize_callback(void (*fn)(const struct menu *root, void *data),
+ void *data);
int conf_read(const char *name);
int conf_read_simple(const char *name, int);
int conf_write_defconfig(const char *name);
diff --git a/scripts/kconfig/parser.y b/scripts/kconfig/parser.y
index 5fb6f07b6ad2..365ed450e855 100644
--- a/scripts/kconfig/parser.y
+++ b/scripts/kconfig/parser.y
@@ -28,6 +28,9 @@ static void zconf_error(const char *err, ...);
static bool zconf_endtoken(const char *tokenname,
const char *expected_tokenname);
+static void (*conf_pre_finalize_callback)(const struct menu *root, void *data);
+static void *conf_pre_finalize_callback_data;
+
struct menu *current_menu, *current_entry, *current_choice;
%}
@@ -551,6 +554,20 @@ static int choice_check_sanity(const struct menu *menu)
return ret;
}
+/*
+ * Register a callback to be invoked by conf_parse() once parsing has finished,
+ * but before menu_finalize() propagates dependencies and simplifies
+ * expressions. This hands the callback the menu tree as it was written in the
+ * Kconfig source files, which is what static analysis tools like kconfirm
+ * need. Passing NULL unregisters the callback.
+ */
+void conf_set_pre_finalize_callback(void (*fn)(const struct menu *root, void *data),
+ void *data)
+{
+ conf_pre_finalize_callback = fn;
+ conf_pre_finalize_callback_data = fn ? data : NULL;
+}
+
void conf_parse(const char *name)
{
struct menu *menu;
@@ -587,6 +604,10 @@ void conf_parse(const char *name)
menu_add_prompt(P_MENU, "Main menu", NULL);
}
+ if (conf_pre_finalize_callback)
+ conf_pre_finalize_callback(&rootmenu,
+ conf_pre_finalize_callback_data);
+
menu_finalize();
menu_for_each_entry(menu) {
--
2.54.0