From c444732540d5245b6219293e96d29f325daa7839 Mon Sep 17 00:00:00 2001 From: Tianjie Xu Date: Mon, 6 Mar 2017 14:44:59 -0800 Subject: Remove malloc in edify functions And switch them to std::vector & std::unique_ptr Bug: 32117870 Test: recovery tests passed on sailfish Change-Id: I5a45951c4bdf895be311d6d760e52e7a1b0798c3 --- edify/parser.yy | 71 ++++++++++++++++++++++----------------------------------- 1 file changed, 27 insertions(+), 44 deletions(-) (limited to 'edify/parser.yy') diff --git a/edify/parser.yy b/edify/parser.yy index 58a8dec65..97205fe3b 100644 --- a/edify/parser.yy +++ b/edify/parser.yy @@ -19,6 +19,10 @@ #include #include +#include +#include +#include + #include "expr.h" #include "yydefs.h" #include "parser.h" @@ -26,8 +30,8 @@ extern int gLine; extern int gColumn; -void yyerror(Expr** root, int* error_count, const char* s); -int yyparse(Expr** root, int* error_count); +void yyerror(std::unique_ptr* root, int* error_count, const char* s); +int yyparse(std::unique_ptr* root, int* error_count); struct yy_buffer_state; void yy_switch_to_buffer(struct yy_buffer_state* new_buffer); @@ -38,17 +42,11 @@ struct yy_buffer_state* yy_scan_string(const char* yystr); static Expr* Build(Function fn, YYLTYPE loc, size_t count, ...) { va_list v; va_start(v, count); - Expr* e = static_cast(malloc(sizeof(Expr))); - e->fn = fn; - e->name = "(operator)"; - e->argc = count; - e->argv = static_cast(malloc(count * sizeof(Expr*))); + Expr* e = new Expr(fn, "(operator)", loc.start, loc.end); for (size_t i = 0; i < count; ++i) { - e->argv[i] = va_arg(v, Expr*); + e->argv.emplace_back(va_arg(v, Expr*)); } va_end(v); - e->start = loc.start; - e->end = loc.end; return e; } @@ -59,10 +57,7 @@ static Expr* Build(Function fn, YYLTYPE loc, size_t count, ...) { %union { char* str; Expr* expr; - struct { - int argc; - Expr** argv; - } args; + std::vector>* args; } %token AND OR SUBSTR SUPERSTR EQ NE IF THEN ELSE ENDIF @@ -70,7 +65,10 @@ static Expr* Build(Function fn, YYLTYPE loc, size_t count, ...) { %type expr %type arglist -%parse-param {Expr** root} +%destructor { delete $$; } expr +%destructor { delete $$; } arglist + +%parse-param {std::unique_ptr* root} %parse-param {int* error_count} %error-verbose @@ -85,17 +83,11 @@ static Expr* Build(Function fn, YYLTYPE loc, size_t count, ...) { %% -input: expr { *root = $1; } +input: expr { root->reset($1); } ; expr: STRING { - $$ = static_cast(malloc(sizeof(Expr))); - $$->fn = Literal; - $$->name = $1; - $$->argc = 0; - $$->argv = NULL; - $$->start = @$.start; - $$->end = @$.end; + $$ = new Expr(Literal, $1, @$.start, @$.end); } | '(' expr ')' { $$ = $2; $$->start=@$.start; $$->end=@$.end; } | expr ';' { $$ = $1; $$->start=@1.start; $$->end=@1.end; } @@ -110,41 +102,32 @@ expr: STRING { | IF expr THEN expr ENDIF { $$ = Build(IfElseFn, @$, 2, $2, $4); } | IF expr THEN expr ELSE expr ENDIF { $$ = Build(IfElseFn, @$, 3, $2, $4, $6); } | STRING '(' arglist ')' { - $$ = static_cast(malloc(sizeof(Expr))); - $$->fn = FindFunction($1); - if ($$->fn == nullptr) { - char buffer[256]; - snprintf(buffer, sizeof(buffer), "unknown function \"%s\"", $1); - yyerror(root, error_count, buffer); + Function fn = FindFunction($1); + if (fn == nullptr) { + std::string msg = "unknown function \"" + std::string($1) + "\""; + yyerror(root, error_count, msg.c_str()); YYERROR; } - $$->name = $1; - $$->argc = $3.argc; - $$->argv = $3.argv; - $$->start = @$.start; - $$->end = @$.end; + $$ = new Expr(fn, $1, @$.start, @$.end); + $$->argv = std::move(*$3); } ; arglist: /* empty */ { - $$.argc = 0; - $$.argv = NULL; + $$ = new std::vector>; } | expr { - $$.argc = 1; - $$.argv = static_cast(malloc(sizeof(Expr*))); - $$.argv[0] = $1; + $$ = new std::vector>; + $$->emplace_back($1); } | arglist ',' expr { - $$.argc = $1.argc + 1; - $$.argv = static_cast(realloc($$.argv, $$.argc * sizeof(Expr*))); - $$.argv[$$.argc-1] = $3; + $$->push_back(std::unique_ptr($3)); } ; %% -void yyerror(Expr** root, int* error_count, const char* s) { +void yyerror(std::unique_ptr* root, int* error_count, const char* s) { if (strlen(s) == 0) { s = "syntax error"; } @@ -152,7 +135,7 @@ void yyerror(Expr** root, int* error_count, const char* s) { ++*error_count; } -int parse_string(const char* str, Expr** root, int* error_count) { +int parse_string(const char* str, std::unique_ptr* root, int* error_count) { yy_switch_to_buffer(yy_scan_string(str)); return yyparse(root, error_count); } -- cgit v1.2.3