From 32cc498a2eb0b189cc90918dc28f8933b2f55278 Mon Sep 17 00:00:00 2001 From: Teddy Date: Tue, 25 Mar 2014 18:27:50 +0800 Subject: location trackdown --- ast.c | 13 ++++- ast.h | 2 + cibic.y | 188 +++++++++++++++++++++++++++++++++++++--------------------------- 3 files changed, 122 insertions(+), 81 deletions(-) diff --git a/ast.c b/ast.c index 0ca0271..e65f2c2 100644 --- a/ast.c +++ b/ast.c @@ -365,11 +365,13 @@ char *cnode_debug_type_repr(CNode *ast) { assert(type); } if (aptr == abuff) - sprintf(buffer, "%s", type); + sprintf(buffer, "%s(%d,%d)", type, + ast->loc.row, ast->loc.col); else { *aptr = '\0'; - sprintf(buffer, "%s:%s", type, abuff); + sprintf(buffer, "%s:%s(%d,%d)", type, abuff, + ast->loc.row, ast->loc.col); } return buffer; } @@ -407,3 +409,10 @@ void cnode_debug_print(CNode *ast, int fancy) { cnode_debug_print_plain(ast); puts(""); } + + +CNode *cnode_add_loc(CNode *node, YYLTYPE loc) { + node->loc.row = loc.first_line; + node->loc.col = loc.first_column; + return node; +} diff --git a/ast.h b/ast.h index f467546..eaed761 100644 --- a/ast.h +++ b/ast.h @@ -1,6 +1,7 @@ #ifndef AST_H #define AST_H #include +#include "cibic.tab.h" #define EXP_POSTFIX 1024 #define POSTFIX_ARR 1025 @@ -69,6 +70,7 @@ typedef struct CNode { } loc; } CNode; +CNode *cnode_add_loc(CNode *node, YYLTYPE loc); CNode *cnode_create_ast(CNode *wrapped); CNode *cnode_create_nop(); CNode *cnode_create_general(int type, int subtype, int pnum, va_list ap); diff --git a/cibic.y b/cibic.y index ada0306..3cfdad9 100644 --- a/cibic.y +++ b/cibic.y @@ -31,18 +31,26 @@ prog_list declaration : type_specifier ';' { - $$ = cnode_create_decl($1, cnode_list_wrap(INIT_DECLRS, cnode_create_nop())); + $$ = cnode_add_loc(cnode_create_decl( + $1, + cnode_list_wrap(INIT_DECLRS, cnode_create_nop())), @$); } | type_specifier init_declarators ';' { - $$ = cnode_create_decl($1, cnode_list_wrap(INIT_DECLRS, $2)); + $$ = cnode_add_loc(cnode_create_decl( + $1, + cnode_add_loc(cnode_list_wrap(INIT_DECLRS, $2), @2)), @$); } function_definition : type_specifier plain_declarator '(' parameters ')' compound_statement { - $$ = cnode_create_func($1, $2, cnode_list_wrap(PARAMS, $4), $6); + $$ = cnode_add_loc(cnode_create_func( + $1, $2, cnode_add_loc( + cnode_list_wrap(PARAMS, $4), @4), $6), @$); } | type_specifier plain_declarator '(' ')' compound_statement { - $$ = cnode_create_func($1, $2, cnode_list_wrap(PARAMS, cnode_create_nop()), $5); + $$ = cnode_add_loc(cnode_create_func( + $1, $2, + cnode_list_wrap(PARAMS, cnode_create_nop()), $5), @$); } parameters @@ -58,31 +66,40 @@ init_declarators | init_declarators ',' init_declarator { $$ = cnode_list_append($1, $3); } init_declarator - : declarator { $$ = cnode_create_init_declr($1, cnode_create_nop()); } - | declarator '=' initializer { $$ = cnode_create_init_declr($1, $3); } + : declarator { $$ = cnode_add_loc(cnode_create_init_declr($1, cnode_create_nop()), @$); } + | declarator '=' initializer { $$ = cnode_add_loc(cnode_create_init_declr($1, $3), @$); } initializer - : assignment_expression { $$ = cnode_create_initr(INITR_NORM, $1); } - | '{' array_initializer '}' { $$ = cnode_create_initr(INITR_ARR, $2); } + : assignment_expression { $$ = cnode_add_loc(cnode_create_initr(INITR_NORM, $1), @$); } + | '{' array_initializer '}' { $$ = cnode_add_loc(cnode_create_initr(INITR_ARR, $2), @$); } array_initializer : initializer - | array_initializer ',' initializer { $$ = cnode_list_append($1, $3); } + | array_initializer ',' initializer { + $$ = cnode_list_append(cnode_add_loc($1, @1), $3); } type_specifier - : KW_VOID { $$ = cnode_create_type_spec(KW_VOID, 0); } - | KW_CHAR { $$ = cnode_create_type_spec(KW_CHAR, 0); } - | KW_INT { $$ = cnode_create_type_spec(KW_INT, 0); } - | struct_or_union identifier '{' struct_fields '}' { $$ = cnode_create_type_spec($1, 2, $2, $4); } - | struct_or_union '{' struct_fields '}' { $$ = cnode_create_type_spec($1, 2, cnode_create_nop(), $3); } - | struct_or_union identifier { $$ = cnode_create_type_spec($1, 2, $2, cnode_create_nop()); } + : KW_VOID { $$ = cnode_add_loc(cnode_create_type_spec(KW_VOID, 0), @$); } + | KW_CHAR { $$ = cnode_add_loc(cnode_create_type_spec(KW_CHAR, 0), @$); } + | KW_INT { $$ = cnode_add_loc(cnode_create_type_spec(KW_INT, 0), @$); } + | struct_or_union identifier '{' struct_fields '}' { + $$ = cnode_add_loc(cnode_create_type_spec($1, 2, $2, cnode_add_loc($4, @4)), @$); + } + | struct_or_union '{' struct_fields '}' { + $$ = cnode_add_loc(cnode_create_type_spec($1, 2, cnode_create_nop(), cnode_add_loc($3, @3)), @$); + } + | struct_or_union identifier { + $$ = cnode_add_loc(cnode_create_type_spec($1, 2, $2, cnode_create_nop()), @$); + } struct_fields : struct_field | struct_fields struct_field { $$ = cnode_list_append($1, $2); } struct_field : type_specifier declarators ';' { - $$ = cnode_create_struct_field($1, cnode_list_wrap(DECLRS, $2)); + $$ = cnode_add_loc( + cnode_create_struct_field($1, + cnode_add_loc(cnode_list_wrap(DECLRS, $2), @2)), @$); } struct_or_union @@ -90,24 +107,31 @@ struct_or_union | KW_UNION { $$ = KW_UNION; } plain_declaration - : type_specifier declarator { $$ = cnode_create_plain_decl($1, $2); } + : type_specifier declarator { $$ = cnode_add_loc(cnode_create_plain_decl($1, $2), @$); } declarator : plain_declarator '(' ')' { - $$ = cnode_create_declr(DECLR_FUNC, 2, $1, cnode_list_wrap(PARAMS, cnode_create_nop())); + $$ = cnode_add_loc(cnode_create_declr( + DECLR_FUNC, 2, $1, + cnode_list_wrap(PARAMS, cnode_create_nop())), @$); } | plain_declarator '(' parameters ')' { - $$ = cnode_create_declr(DECLR_FUNC, 2, $1, cnode_list_wrap(PARAMS, $3)); + $$ = cnode_add_loc(cnode_create_declr( + DECLR_FUNC, 2, $1, + cnode_add_loc(cnode_list_wrap(PARAMS, $3), @3)), @$); } | declarator_array declarator_array : plain_declarator - | declarator_array '[' constant_expression ']' { $$ = cnode_create_declr(DECLR_ARR, 2, $1, $3); } + | declarator_array '[' constant_expression ']' { + $$ = cnode_add_loc(cnode_create_declr(DECLR_ARR, 2, $1, $3), @$); + } plain_declarator : identifier - | '*' plain_declarator { $$ = cnode_create_declr('*', 1, $2); } + | '*' plain_declarator { + $$ = cnode_add_loc(cnode_create_declr('*', 1, $2), @$); } statement : expression_statement @@ -117,13 +141,14 @@ statement | jump_statement expression_statement - : ';' { $$ = cnode_create_stmt(STMT_EXP, 1, cnode_create_nop()); } - | expression ';' { $$ = cnode_create_stmt(STMT_EXP, 1, $1); } + : ';' { $$ = cnode_add_loc(cnode_create_stmt(STMT_EXP, 1, cnode_create_nop()), @$); } + | expression ';' { $$ = cnode_add_loc(cnode_create_stmt(STMT_EXP, 1, $1), @$); } compound_statement : '{' comp_decls comp_stmts '}' { - $$ = cnode_create_stmt(STMT_COMP, 2, cnode_list_wrap(COMP_DECLS, $2), - cnode_list_wrap(COMP_STMTS, $3)); + $$ = cnode_add_loc( + cnode_create_stmt(STMT_COMP, 2, cnode_add_loc(cnode_list_wrap(COMP_DECLS, $2), @2), + cnode_add_loc(cnode_list_wrap(COMP_STMTS, $3), @3)), @$); } comp_decls @@ -136,18 +161,20 @@ comp_stmts selection_statement : KW_IF '(' expression ')' statement { - $$ = cnode_create_stmt(STMT_IF, 3, $3, $5, cnode_create_nop()); + $$ = cnode_add_loc( + cnode_create_stmt(STMT_IF, 3, $3, $5, cnode_create_nop()), @$); } | KW_IF '(' expression ')' statement KW_ELSE statement { - $$ = cnode_create_stmt(STMT_IF, 3, $3, $5, $7); + $$ = cnode_add_loc( + cnode_create_stmt(STMT_IF, 3, $3, $5, $7), @$); } iteration_statement : KW_WHILE '(' expression ')' statement { - $$ = cnode_create_stmt(STMT_WHILE, 2, $3, $5); + $$ = cnode_add_loc(cnode_create_stmt(STMT_WHILE, 2, $3, $5), @$); } | KW_FOR '(' optional_exp ';' optional_exp ';' optional_exp ')' statement { - $$ = cnode_create_stmt(STMT_FOR, 4, $3, $5, $7, $9); + $$ = cnode_add_loc(cnode_create_stmt(STMT_FOR, 4, $3, $5, $7, $9), @$); } optional_exp @@ -155,20 +182,21 @@ optional_exp | expression jump_statement - : KW_CONT ';' { $$ = cnode_create_stmt(STMT_CONT, 0); } - | KW_BREAK ';' { $$ = cnode_create_stmt(STMT_BREAK, 0); } - | KW_RET optional_exp ';' { $$ = cnode_create_stmt(STMT_RET, 1, $2); } + : KW_CONT ';' { $$ = cnode_add_loc(cnode_create_stmt(STMT_CONT, 0), @$); } + | KW_BREAK ';' { $$ = cnode_add_loc(cnode_create_stmt(STMT_BREAK, 0), @$); } + | KW_RET optional_exp ';' { $$ = cnode_add_loc(cnode_create_stmt(STMT_RET, 1, $2), @$); } expression : assignment_expression | expression ',' assignment_expression { - printf("%d, %d\n", @$.first_line, @$.first_column); - $$ = cnode_create_exp(',', 2, $1, $3); - } + @$ = @2; + $$ = cnode_add_loc(cnode_create_exp(',', 2, $1, $3), @2); } assignment_expression : logical_or_expression - | unary_expression assignment_operator assignment_expression { $$ = cnode_create_exp($2, 2, $1, $3); } + | unary_expression assignment_operator assignment_expression { + @$ = @2; + $$ = cnode_add_loc(cnode_create_exp($2, 2, $1, $3), @2); } assignment_operator : '=' { $$ = '='; } @@ -187,38 +215,38 @@ constant_expression: logical_or_expression logical_or_expression : logical_and_expression | logical_or_expression OPT_OR logical_and_expression { - $$ = cnode_create_exp(OPT_OR, 2, $1, $3); - } + @$ = @2; + $$ = cnode_add_loc(cnode_create_exp(OPT_OR, 2, $1, $3), @2); } logical_and_expression : inclusive_or_expression | logical_and_expression OPT_AND inclusive_or_expression { - $$ = cnode_create_exp(OPT_AND, 2, $1, $3); - } + @$ = @2; + $$ = cnode_add_loc(cnode_create_exp(OPT_AND, 2, $1, $3), @2); } inclusive_or_expression : exclusive_or_expression | inclusive_or_expression '|' exclusive_or_expression { - $$ = cnode_create_exp('|', 2, $1, $3); - } + @$ = @2; + $$ = cnode_add_loc(cnode_create_exp('|', 2, $1, $3), @2); } exclusive_or_expression : and_expression | exclusive_or_expression '^' and_expression { - $$ = cnode_create_exp('^', 2, $1, $3); - } + @$ = @2; + $$ = cnode_add_loc(cnode_create_exp('^', 2, $1, $3), @2); } and_expression : equality_expression | and_expression '&' equality_expression { - $$ = cnode_create_exp('&', 2, $1, $3); - } + @$ = @2; + $$ = cnode_add_loc(cnode_create_exp('&', 2, $1, $3), @2); } equality_expression : relational_expression | equality_expression equality_operator relational_expression { - $$ = cnode_create_exp($2, 2, $1, $3); - } + @$ = @2; + $$ = cnode_add_loc(cnode_create_exp($2, 2, $1, $3), @2); } equality_operator : OPT_EQ { $$ = OPT_EQ; } @@ -227,8 +255,8 @@ equality_operator relational_expression : shift_expression | relational_expression relational_operator shift_expression { - $$ = cnode_create_exp($2, 2, $1, $3); - } + @$ = @2; + $$ = cnode_add_loc(cnode_create_exp($2, 2, $1, $3), @2); } relational_operator : '<' { $$ = '<'; } @@ -239,8 +267,8 @@ relational_operator shift_expression : additive_expression | shift_expression shift_operator additive_expression { - $$ = cnode_create_exp($2, 2, $1, $3); - } + @$ = @2; + $$ = cnode_add_loc(cnode_create_exp($2, 2, $1, $3), @2); } shift_operator : OPT_SHL { $$ = OPT_SHL; } @@ -249,8 +277,8 @@ shift_operator additive_expression : multiplicative_expression | additive_expression additive_operator multiplicative_expression { - $$ = cnode_create_exp($2, 2, $1, $3); - } + @$ = @2; + $$ = cnode_add_loc(cnode_create_exp($2, 2, $1, $3), @2); } additive_operator : '+' { $$ = '+'; } @@ -259,8 +287,8 @@ additive_operator multiplicative_expression : cast_expression | multiplicative_expression multiplicative_operator cast_expression { - $$ = cnode_create_exp($2, 2, $1, $3); - } + @$ = @2; + $$ = cnode_add_loc(cnode_create_exp($2, 2, $1, $3), @2); } multiplicative_operator : '*' { $$ = '*'; } @@ -270,20 +298,19 @@ multiplicative_operator cast_expression : unary_expression | '(' type_name ')' cast_expression { - $$ = cnode_create_exp(EXP_CAST, 2, $2, $4); - } + $$ = cnode_add_loc(cnode_create_exp(EXP_CAST, 2, $2, $4), @$); } type_name : type_specifier - | type_name '*' { $$ = cnode_create_exp('*', 1, $1); } + | type_name '*' { $$ = cnode_add_loc(cnode_create_exp('*', 1, $1), @$); } unary_expression : postfix_expression - | OPT_INC unary_expression { $$ = cnode_create_exp(OPT_INC, 1, $2); } - | OPT_DEC unary_expression { $$ = cnode_create_exp(OPT_DEC, 1, $2); } - | unary_operator cast_expression { $$ = cnode_create_exp($1, 1, $2); } - | KW_SIZEOF unary_expression { $$ = cnode_create_exp(KW_SIZEOF, 1, $2); } - | KW_SIZEOF '(' type_name ')' { $$ = cnode_create_exp(KW_SIZEOF, 1, $3); } + | OPT_INC unary_expression { $$ = cnode_add_loc(cnode_create_exp(OPT_INC, 1, $2), @$); } + | OPT_DEC unary_expression { $$ = cnode_add_loc(cnode_create_exp(OPT_DEC, 1, $2), @$); } + | unary_operator cast_expression { $$ = cnode_add_loc(cnode_create_exp($1, 1, $2), @$); } + | KW_SIZEOF unary_expression { $$ = cnode_add_loc(cnode_create_exp(KW_SIZEOF, 1, $2), @$); } + | KW_SIZEOF '(' type_name ')' { $$ = cnode_add_loc(cnode_create_exp(KW_SIZEOF, 1, $3), @$); } unary_operator : '&' { $$ = '&'; } @@ -295,20 +322,23 @@ unary_operator postfix_expression : primary_expression - | postfix_expression postfix { $$ = cnode_create_exp(EXP_POSTFIX, 2, $1, $2); } + | postfix_expression postfix { + @$ = @2; + $$ = cnode_add_loc(cnode_create_exp(EXP_POSTFIX, 2, $1, $2), @2); } postfix - : '[' expression ']' { $$ = cnode_create_exp(POSTFIX_ARR, 1, $2); } + : '[' expression ']' { $$ = cnode_add_loc(cnode_create_exp(POSTFIX_ARR, 1, $2), @$); } | '(' arguments ')' { - $$ = cnode_create_exp(POSTFIX_CALL, 1, cnode_list_wrap(ARGS, $2)); - } + $$ = cnode_add_loc(cnode_create_exp( + POSTFIX_CALL, 1, + cnode_add_loc(cnode_list_wrap(ARGS, $2), @2)), @$); } | '(' ')' { - $$ = cnode_create_exp(POSTFIX_CALL, 1, cnode_list_wrap(ARGS, cnode_create_nop())); - } - | '.' identifier { $$ = cnode_create_exp(POSTFIX_DOT, 1, $2); } - | OPT_PTR identifier { $$ = cnode_create_exp(POSTFIX_PTR, 1, $2); } - | OPT_INC { $$ = cnode_create_exp(OPT_INC, 0); } - | OPT_DEC { $$ = cnode_create_exp(OPT_DEC, 0); } + $$ = cnode_add_loc(cnode_create_exp( + POSTFIX_CALL, 1, cnode_list_wrap(ARGS, cnode_create_nop())), @$); } + | '.' identifier { $$ = cnode_add_loc(cnode_create_exp(POSTFIX_DOT, 1, $2), @$); } + | OPT_PTR identifier { $$ = cnode_add_loc(cnode_create_exp(POSTFIX_PTR, 1, $2), @$); } + | OPT_INC { $$ = cnode_add_loc(cnode_create_exp(OPT_INC, 0), @$); } + | OPT_DEC { $$ = cnode_add_loc(cnode_create_exp(OPT_DEC, 0), @$); } arguments : assignment_expression @@ -316,11 +346,11 @@ arguments primary_expression : identifier - | INT_CONST { $$ = cnode_create_int_const($1); } - | CHAR_CONST { $$ = cnode_create_char_const($1); } - | STR_CONST { $$ = cnode_create_str_const($1); } - | '(' expression ')' { $$ = $2; } + | INT_CONST { $$ = cnode_add_loc(cnode_create_int_const($1), @$); } + | CHAR_CONST { $$ = cnode_add_loc(cnode_create_char_const($1), @$); } + | STR_CONST { $$ = cnode_add_loc(cnode_create_str_const($1), @$); } + | '(' expression ')' { $$ = cnode_add_loc($2, @$); } identifier - : IDENTIFIER { $$ = cnode_create_identifier($1); } + : IDENTIFIER { $$ = cnode_add_loc(cnode_create_identifier($1), @$); } %% -- cgit v1.2.3