changeset 19:e1197013c423 tip

A simple try on parsing code fragements
author Thinker K.F. Li <thinker@codemud.net>
date Sat, 11 Sep 2010 14:46:04 +0800
parents 882a80582a64
children
files src/cospy.c
diffstat 1 files changed, 35 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/cospy.c	Fri Sep 10 12:11:52 2010 +0800
+++ b/src/cospy.c	Sat Sep 11 14:46:04 2010 +0800
@@ -7,11 +7,43 @@
 #include "tree-iterator.h"
 #include "gimple.h"
 #include "cgraph.h"
+#include "c-pragma.h"
+#include "cpplib.h"
 #include <stdio.h>
+#include <string.h>
 
 /* This is required for GCC plugin, or it is can not be loaded */
 int plugin_is_GPL_compatible;
 
+/*! \brief Compile a code fragement */
+static tree
+compile_code_frag(const uchar *code) {
+    int code_len;
+    tree body;
+    uchar *buf;
+    static cnt = 0;
+
+    printf("frag\n");
+    if(cnt++ != 0)
+	return;
+
+    /*
+     * We need GCC to export more interface of parser to provide the
+     * capability of parsing code fragements.
+     */
+    code = "";
+    code_len = strlen((char *)code);
+    buf = (uchar *)ggc_alloc(code_len + 1);
+    strcpy(buf, code);
+    code = buf;
+    cpp_push_buffer(parse_in, code, code_len, false);
+    printf("Start parsing\n");
+    //c_parse_file();
+    printf("Parsed\n");
+    //ggc_free(buf);
+    //body = c_parser_compound_statement(the_parser); /* code between {} */
+}
+
 /*! \brief Parse a tree of type and return respective string.
  *
  * The struncture of a pointer type
@@ -466,6 +498,9 @@
 
     /* Do a function call twice */
     tree_insert_before(expr, expr, parent);
+
+    //compile_code_frag("void ttt(){printf(\"Hello Wolrd!!\\n\");}");
+    compile_code_frag("void ts() {ts();}\n");
 }
 
 /*! \brief Find all call expression in a code block.