comparison src/cospy.c @ 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
comparison
equal deleted inserted replaced
18:882a80582a64 19:e1197013c423
5 #include "tree-pass.h" 5 #include "tree-pass.h"
6 #include "tree.h" 6 #include "tree.h"
7 #include "tree-iterator.h" 7 #include "tree-iterator.h"
8 #include "gimple.h" 8 #include "gimple.h"
9 #include "cgraph.h" 9 #include "cgraph.h"
10 #include "c-pragma.h"
11 #include "cpplib.h"
10 #include <stdio.h> 12 #include <stdio.h>
13 #include <string.h>
11 14
12 /* This is required for GCC plugin, or it is can not be loaded */ 15 /* This is required for GCC plugin, or it is can not be loaded */
13 int plugin_is_GPL_compatible; 16 int plugin_is_GPL_compatible;
17
18 /*! \brief Compile a code fragement */
19 static tree
20 compile_code_frag(const uchar *code) {
21 int code_len;
22 tree body;
23 uchar *buf;
24 static cnt = 0;
25
26 printf("frag\n");
27 if(cnt++ != 0)
28 return;
29
30 /*
31 * We need GCC to export more interface of parser to provide the
32 * capability of parsing code fragements.
33 */
34 code = "";
35 code_len = strlen((char *)code);
36 buf = (uchar *)ggc_alloc(code_len + 1);
37 strcpy(buf, code);
38 code = buf;
39 cpp_push_buffer(parse_in, code, code_len, false);
40 printf("Start parsing\n");
41 //c_parse_file();
42 printf("Parsed\n");
43 //ggc_free(buf);
44 //body = c_parser_compound_statement(the_parser); /* code between {} */
45 }
14 46
15 /*! \brief Parse a tree of type and return respective string. 47 /*! \brief Parse a tree of type and return respective string.
16 * 48 *
17 * The struncture of a pointer type 49 * The struncture of a pointer type
18 * - pointer:pointer_type 50 * - pointer:pointer_type
464 EXPR_LINENO(expr), 496 EXPR_LINENO(expr),
465 TREE_CODE(parent)); 497 TREE_CODE(parent));
466 498
467 /* Do a function call twice */ 499 /* Do a function call twice */
468 tree_insert_before(expr, expr, parent); 500 tree_insert_before(expr, expr, parent);
501
502 //compile_code_frag("void ttt(){printf(\"Hello Wolrd!!\\n\");}");
503 compile_code_frag("void ts() {ts();}\n");
469 } 504 }
470 505
471 /*! \brief Find all call expression in a code block. 506 /*! \brief Find all call expression in a code block.
472 * 507 *
473 * This function find CALL_EXPR in body of a function that represented 508 * This function find CALL_EXPR in body of a function that represented