# HG changeset patch # User Thinker K.F. Li # Date 1284187564 -28800 # Node ID e1197013c423cfe1ecba2ce56c19e2b399dace2f # Parent 882a80582a6422a121b490543dc2efd9aae0c78f A simple try on parsing code fragements diff -r 882a80582a64 -r e1197013c423 src/cospy.c --- 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 +#include /* 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.