All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alexey Gladkov <gladkov.alexey@gmail.com>
To: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Cc: linux-sparse@vger.kernel.org, Oleg Nesterov <oleg@redhat.com>,
	Alexey Gladkov <gladkov.alexey@gmail.com>
Subject: [PATCH 1/3] dissect: Allow to show all symbols
Date: Tue,  2 Nov 2021 15:06:43 +0100	[thread overview]
Message-ID: <20211102140645.83081-2-gladkov.alexey@gmail.com> (raw)
In-Reply-To: <20211102140645.83081-1-gladkov.alexey@gmail.com>

Currently dissect sees only used symbols. For indexing purposes, it is
useful to see all declared symbols.

$ nl -s\  -w2 ./z.c
 1 struct foo {
 2         int member;
 3 };
 4 #ifdef OPT
 5 static void func1(void) {
 6         struct foo *x;
 7         return 0;
 8 }
 9 #endif
10 static inline void func2(void) { return; }
11 void func(void) { return; }

$ ./test-dissect ./z.c

FILE: ./z.c

  11:6                    def   f func                             void ( ... )

$ ./test-dissect --param=dissect-show-all-symbols ./z.c

FILE: ./z.c

   1:8                    def   s foo                              struct foo
   2:13                   def   m foo.member                       int
  10:20                   def   f func2                            void ( ... )
  11:6                    def   f func                             void ( ... )

Signed-off-by: Alexey Gladkov <gladkov.alexey@gmail.com>
---
 dissect.c | 39 ++++++++++++++++++++++++++++++++++++++-
 options.c |  5 +++++
 options.h |  2 ++
 semind.c  |  1 +
 4 files changed, 46 insertions(+), 1 deletion(-)

diff --git a/dissect.c b/dissect.c
index 582e8fc3..0d6c3288 100644
--- a/dissect.c
+++ b/dissect.c
@@ -652,9 +652,46 @@ static void do_sym_list(struct symbol_list *list)
 	DO_LIST(list, sym, do_symbol(sym));
 }
 
+static inline bool valid_namespace(enum namespace ns)
+{
+	return (ns == NS_STRUCT || ns == NS_SYMBOL);
+}
+
+static void do_file(char *file)
+{
+	struct symbol_list *res = sparse_keep_tokens(file);
+
+	if (!dissect_show_all_symbols) {
+		do_sym_list(res);
+		goto end;
+	}
+
+	DO_LIST(file_scope->symbols, sym,
+		if (input_streams[sym->pos.stream].fd != -1 && valid_namespace(sym->namespace)) {
+			if (sym->type == SYM_STRUCT || sym->type == SYM_UNION) {
+				sym->ctype.base_type = sym;
+				examine_sym_node(sym, NULL);
+				continue;
+			}
+
+			do_symbol(sym);
+		}
+	);
+
+	DO_LIST(global_scope->symbols, sym,
+		if (input_streams[sym->pos.stream].fd != -1 && valid_namespace(sym->namespace)) {
+			do_symbol(sym);
+		}
+	);
+
+end:
+	/* Drop the tokens for this file after parsing */
+	clear_token_alloc();
+}
+
 void dissect(struct reporter *rep, struct string_list *filelist)
 {
 	reporter = rep;
 
-	DO_LIST(filelist, file, do_sym_list(__sparse(file)));
+	DO_LIST(filelist, file, do_file(file));
 }
diff --git a/options.c b/options.c
index 6704fc8d..b4684357 100644
--- a/options.c
+++ b/options.c
@@ -70,6 +70,8 @@ int dbg_postorder = 0;
 int dump_macro_defs = 0;
 int dump_macros_only = 0;
 
+int dissect_show_all_symbols = 0;
+
 unsigned long fdump_ir;
 int fhosted = 1;
 unsigned int fmax_errors = 100;
@@ -958,6 +960,9 @@ static char **handle_param(char *arg, char **next)
 	if (!value)
 		die("missing argument for --param option");
 
+	if (!strcmp(value, "dissect-show-all-symbols"))
+		dissect_show_all_symbols = 1;
+
 	return next;
 }
 
diff --git a/options.h b/options.h
index 0aec8764..c2a9551a 100644
--- a/options.h
+++ b/options.h
@@ -70,6 +70,8 @@ extern int dbg_postorder;
 extern int dump_macro_defs;
 extern int dump_macros_only;
 
+extern int dissect_show_all_symbols;
+
 extern unsigned long fdump_ir;
 extern int fhosted;
 extern unsigned int fmax_errors;
diff --git a/semind.c b/semind.c
index 911fc747..ad8003ba 100644
--- a/semind.c
+++ b/semind.c
@@ -329,6 +329,7 @@ done:
 	optind--;
 
 	sparse_initialize(argc - optind, argv + optind, &semind_filelist);
+	dissect_show_all_symbols = 1;
 }
 
 static void parse_cmdline_rm(int argc, char **argv)
-- 
2.33.0


  reply	other threads:[~2021-11-02 14:09 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-02 14:06 [PATCH 0/3] semind: Index more symbols Alexey Gladkov
2021-11-02 14:06 ` Alexey Gladkov [this message]
2021-11-02 14:06 ` [PATCH 2/3] dissect: Show macro definitions Alexey Gladkov
2021-11-02 14:06 ` [PATCH 3/3] dissect: Show typedefs Alexey Gladkov
2021-12-14 15:16 ` [PATCH 0/3] semind: Index more symbols Alexey Gladkov
2022-05-21  9:46 ` Luc Van Oostenryck
2022-05-23 12:45   ` Alexey Gladkov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211102140645.83081-2-gladkov.alexey@gmail.com \
    --to=gladkov.alexey@gmail.com \
    --cc=linux-sparse@vger.kernel.org \
    --cc=luc.vanoostenryck@gmail.com \
    --cc=oleg@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.