linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Christopher Li <sparse@chrisli.org>
To: Linux-Sparse <linux-sparse@vger.kernel.org>
Cc: Kamil Dudka <kdudka@redhat.com>, Josh Triplett <josh@joshtriplett.org>
Subject: [PATCH 2/4] inspect: add some example inspect for symbol and statement
Date: Fri, 2 Apr 2010 11:42:29 -0700	[thread overview]
Message-ID: <n2g70318cbf1004021142y442f3abay7c6618e89b97f881@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 246 bytes --]

This is far fro complete. It is an example how to write call back
driven inspect functions.

Inside each inspect call back function. It can add a child node
with: ast_append_child(), or add text attribute node with
ast_append_attribute()


Chris

[-- Attachment #2: 0002-inspect-add-some-example-inspect-for-symbol-and-stat.patch --]
[-- Type: application/octet-stream, Size: 4315 bytes --]

From b681073ea788d2356604327686bb1ac1acd406c8 Mon Sep 17 00:00:00 2001
From: Christopher Li <sparse@chrisli.org>
Date: Tue, 30 Mar 2010 15:50:16 -0700
Subject: [PATCH 2/4] inspect: add some example inspect for symbol and statement

This is far fro complete. It is an example how to write call back
driven inspect functions.

Inside each inspect call back function. It can add a child node
with: ast_append_child(), or add text attribute node with
ast_append_attribute()
---
 ast-inspect.c |  114 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 ast-inspect.h |   13 ++++++
 2 files changed, 127 insertions(+), 0 deletions(-)
 create mode 100644 ast-inspect.c
 create mode 100644 ast-inspect.h

diff --git a/ast-inspect.c b/ast-inspect.c
new file mode 100644
index 0000000..72f3fe0
--- /dev/null
+++ b/ast-inspect.c
@@ -0,0 +1,114 @@
+
+#include "token.h"
+#include "parse.h"
+#include "symbol.h"
+#include "ast-inspect.h"
+
+static inline void inspect_ptr_list(AstNode *node, const char *name, void (*inspect)(AstNode *))
+{
+	struct ptr_list *ptrlist = node->ptr;
+	void *ptr;
+	int i = 0;
+
+	node->text = g_strdup_printf("%s %s:", node->text, name);
+	FOR_EACH_PTR(ptrlist, ptr) {
+		char *index = g_strdup_printf("%d: ", i++);
+		ast_append_child(node, index, ptr, inspect);
+	} END_FOR_EACH_PTR(ptr);
+}
+
+
+static const char *statement_type_name(enum statement_type type)
+{
+	static const char *statement_type_name[] = {
+		[STMT_NONE] = "STMT_NONE",
+		[STMT_DECLARATION] = "STMT_DECLARATION",
+		[STMT_EXPRESSION] = "STMT_EXPRESSION",
+		[STMT_COMPOUND] = "STMT_COMPOUND",
+		[STMT_IF] = "STMT_IF",
+		[STMT_RETURN] = "STMT_RETURN",
+		[STMT_CASE] = "STMT_CASE",
+		[STMT_SWITCH] = "STMT_SWITCH",
+		[STMT_ITERATOR] = "STMT_ITERATOR",
+		[STMT_LABEL] = "STMT_LABEL",
+		[STMT_GOTO] = "STMT_GOTO",
+		[STMT_ASM] = "STMT_ASM",
+		[STMT_CONTEXT] = "STMT_CONTEXT",
+		[STMT_RANGE] = "STMT_RANGE",
+	};
+	return statement_type_name[type] ?: "UNKNOWN_STATEMENT_TYPE";
+}
+
+
+void inspect_statement(AstNode *node)
+{
+	struct statement *stmt = node->ptr;
+	node->text = g_strdup_printf("%s %s:", node->text, statement_type_name(stmt->type));
+	switch (stmt->type) {
+		case STMT_COMPOUND:
+			ast_append_child(node, "stmts:", stmt->stmts, inspect_statement_list);
+			break;
+		case STMT_IF:
+			ast_append_child(node, "if_true:", stmt->if_true, inspect_statement);
+			ast_append_child(node, "if_false:", stmt->if_false, inspect_statement);
+		default:
+			break;
+	}
+}
+
+
+void inspect_statement_list(AstNode *node)
+{
+	inspect_ptr_list(node, "statement_list", inspect_statement);
+}
+
+
+static const char *symbol_type_name(enum type type)
+{
+	static const char *type_name[] = {
+		[SYM_UNINITIALIZED] = "SYM_UNINITIALIZED",
+		[SYM_PREPROCESSOR] = "SYM_PREPROCESSOR",
+		[SYM_BASETYPE] = "SYM_BASETYPE",
+		[SYM_NODE] = "SYM_NODE",
+		[SYM_PTR] = "SYM_PTR",
+		[SYM_FN] = "SYM_FN",
+		[SYM_ARRAY] = "SYM_ARRAY",
+		[SYM_STRUCT] = "SYM_STRUCT",
+		[SYM_UNION] = "SYM_UNION",
+		[SYM_ENUM] = "SYM_ENUM",
+		[SYM_TYPEDEF] = "SYM_TYPEDEF",
+		[SYM_TYPEOF] = "SYM_TYPEOF",
+		[SYM_MEMBER] = "SYM_MEMBER",
+		[SYM_BITFIELD] = "SYM_BITFIELD",
+		[SYM_LABEL] = "SYM_LABEL",
+		[SYM_RESTRICT] = "SYM_RESTRICT",
+		[SYM_FOULED] = "SYM_FOULED",
+		[SYM_KEYWORD] = "SYM_KEYWORD",
+		[SYM_BAD] = "SYM_BAD",
+	};
+	return type_name[type] ?: "UNKNOWN_TYPE";
+}
+
+
+void inspect_symbol(AstNode *node)
+{
+	struct symbol *sym = node->ptr;
+	node->text = g_strdup_printf("%s %s: %s", node->text, symbol_type_name(sym->type),
+				      show_ident(sym->ident));
+	ast_append_child(node, "ctype.base_type:", sym->ctype.base_type,inspect_symbol);
+
+	switch (sym->type) {
+		case SYM_FN:
+			ast_append_child(node, "stmt:", sym->stmt, inspect_statement);
+			break;
+		default:
+			break;
+	}
+}
+
+
+void inspect_symbol_list(AstNode *node)
+{
+	inspect_ptr_list(node, "symbol_list", inspect_symbol);
+}
+
diff --git a/ast-inspect.h b/ast-inspect.h
new file mode 100644
index 0000000..e01d847
--- /dev/null
+++ b/ast-inspect.h
@@ -0,0 +1,13 @@
+
+#ifndef _AST_INSPECT_H_
+#define _AST_INSPECT_H_
+
+#include "ast-model.h"
+
+void inspect_symbol(AstNode *node);
+void inspect_symbol_list(AstNode *node);
+
+void inspect_statement(AstNode *node);
+void inspect_statement_list(AstNode *node);
+
+#endif
-- 
1.6.6.1


                 reply	other threads:[~2010-04-02 18:42 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=n2g70318cbf1004021142y442f3abay7c6618e89b97f881@mail.gmail.com \
    --to=sparse@chrisli.org \
    --cc=josh@joshtriplett.org \
    --cc=kdudka@redhat.com \
    --cc=linux-sparse@vger.kernel.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).