aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTianjie Xu2017-03-06 16:44:59 -0600
committerTianjie Xu2017-03-22 16:20:57 -0500
commitc444732540d5245b6219293e96d29f325daa7839 (patch)
treed66338f891ad8b440a36e781988fae89cca1da7f /edify/parser.yy
parentd882b8892a1592101b901dd474de41e56c31b17b (diff)
downloadplatform-bootable-recovery-c444732540d5245b6219293e96d29f325daa7839.tar.gz
platform-bootable-recovery-c444732540d5245b6219293e96d29f325daa7839.tar.xz
platform-bootable-recovery-c444732540d5245b6219293e96d29f325daa7839.zip
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
Diffstat (limited to 'edify/parser.yy')
-rw-r--r--edify/parser.yy71
1 files changed, 27 insertions, 44 deletions
diff --git a/edify/parser.yy b/edify/parser.yy
index 58a8dec6..97205fe3 100644
--- a/edify/parser.yy
+++ b/edify/parser.yy
@@ -19,6 +19,10 @@
19#include <stdlib.h> 19#include <stdlib.h>
20#include <string.h> 20#include <string.h>
21 21
22#include <memory>
23#include <string>
24#include <vector>
25
22#include "expr.h" 26#include "expr.h"
23#include "yydefs.h" 27#include "yydefs.h"
24#include "parser.h" 28#include "parser.h"
@@ -26,8 +30,8 @@
26extern int gLine; 30extern int gLine;
27extern int gColumn; 31extern int gColumn;
28 32
29void yyerror(Expr** root, int* error_count, const char* s); 33void yyerror(std::unique_ptr<Expr>* root, int* error_count, const char* s);
30int yyparse(Expr** root, int* error_count); 34int yyparse(std::unique_ptr<Expr>* root, int* error_count);
31 35
32struct yy_buffer_state; 36struct yy_buffer_state;
33void yy_switch_to_buffer(struct yy_buffer_state* new_buffer); 37void yy_switch_to_buffer(struct yy_buffer_state* new_buffer);
@@ -38,17 +42,11 @@ struct yy_buffer_state* yy_scan_string(const char* yystr);
38static Expr* Build(Function fn, YYLTYPE loc, size_t count, ...) { 42static Expr* Build(Function fn, YYLTYPE loc, size_t count, ...) {
39 va_list v; 43 va_list v;
40 va_start(v, count); 44 va_start(v, count);
41 Expr* e = static_cast<Expr*>(malloc(sizeof(Expr))); 45 Expr* e = new Expr(fn, "(operator)", loc.start, loc.end);
42 e->fn = fn;
43 e->name = "(operator)";
44 e->argc = count;
45 e->argv = static_cast<Expr**>(malloc(count * sizeof(Expr*)));
46 for (size_t i = 0; i < count; ++i) { 46 for (size_t i = 0; i < count; ++i) {
47 e->argv[i] = va_arg(v, Expr*); 47 e->argv.emplace_back(va_arg(v, Expr*));
48 } 48 }
49 va_end(v); 49 va_end(v);
50 e->start = loc.start;
51 e->end = loc.end;
52 return e; 50 return e;
53} 51}
54 52
@@ -59,10 +57,7 @@ static Expr* Build(Function fn, YYLTYPE loc, size_t count, ...) {
59%union { 57%union {
60 char* str; 58 char* str;
61 Expr* expr; 59 Expr* expr;
62 struct { 60 std::vector<std::unique_ptr<Expr>>* args;
63 int argc;
64 Expr** argv;
65 } args;
66} 61}
67 62
68%token AND OR SUBSTR SUPERSTR EQ NE IF THEN ELSE ENDIF 63%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, ...) {
70%type <expr> expr 65%type <expr> expr
71%type <args> arglist 66%type <args> arglist
72 67
73%parse-param {Expr** root} 68%destructor { delete $$; } expr
69%destructor { delete $$; } arglist
70
71%parse-param {std::unique_ptr<Expr>* root}
74%parse-param {int* error_count} 72%parse-param {int* error_count}
75%error-verbose 73%error-verbose
76 74
@@ -85,17 +83,11 @@ static Expr* Build(Function fn, YYLTYPE loc, size_t count, ...) {
85 83
86%% 84%%
87 85
88input: expr { *root = $1; } 86input: expr { root->reset($1); }
89; 87;
90 88
91expr: STRING { 89expr: STRING {
92 $$ = static_cast<Expr*>(malloc(sizeof(Expr))); 90 $$ = new Expr(Literal, $1, @$.start, @$.end);
93 $$->fn = Literal;
94 $$->name = $1;
95 $$->argc = 0;
96 $$->argv = NULL;
97 $$->start = @$.start;
98 $$->end = @$.end;
99} 91}
100| '(' expr ')' { $$ = $2; $$->start=@$.start; $$->end=@$.end; } 92| '(' expr ')' { $$ = $2; $$->start=@$.start; $$->end=@$.end; }
101| expr ';' { $$ = $1; $$->start=@1.start; $$->end=@1.end; } 93| expr ';' { $$ = $1; $$->start=@1.start; $$->end=@1.end; }
@@ -110,41 +102,32 @@ expr: STRING {
110| IF expr THEN expr ENDIF { $$ = Build(IfElseFn, @$, 2, $2, $4); } 102| IF expr THEN expr ENDIF { $$ = Build(IfElseFn, @$, 2, $2, $4); }
111| IF expr THEN expr ELSE expr ENDIF { $$ = Build(IfElseFn, @$, 3, $2, $4, $6); } 103| IF expr THEN expr ELSE expr ENDIF { $$ = Build(IfElseFn, @$, 3, $2, $4, $6); }
112| STRING '(' arglist ')' { 104| STRING '(' arglist ')' {
113 $$ = static_cast<Expr*>(malloc(sizeof(Expr))); 105 Function fn = FindFunction($1);
114 $$->fn = FindFunction($1); 106 if (fn == nullptr) {
115 if ($$->fn == nullptr) { 107 std::string msg = "unknown function \"" + std::string($1) + "\"";
116 char buffer[256]; 108 yyerror(root, error_count, msg.c_str());
117 snprintf(buffer, sizeof(buffer), "unknown function \"%s\"", $1);
118 yyerror(root, error_count, buffer);
119 YYERROR; 109 YYERROR;
120 } 110 }
121 $$->name = $1; 111 $$ = new Expr(fn, $1, @$.start, @$.end);
122 $$->argc = $3.argc; 112 $$->argv = std::move(*$3);
123 $$->argv = $3.argv;
124 $$->start = @$.start;
125 $$->end = @$.end;
126} 113}
127; 114;
128 115
129arglist: /* empty */ { 116arglist: /* empty */ {
130 $$.argc = 0; 117 $$ = new std::vector<std::unique_ptr<Expr>>;
131 $$.argv = NULL;
132} 118}
133| expr { 119| expr {
134 $$.argc = 1; 120 $$ = new std::vector<std::unique_ptr<Expr>>;
135 $$.argv = static_cast<Expr**>(malloc(sizeof(Expr*))); 121 $$->emplace_back($1);
136 $$.argv[0] = $1;
137} 122}
138| arglist ',' expr { 123| arglist ',' expr {
139 $$.argc = $1.argc + 1; 124 $$->push_back(std::unique_ptr<Expr>($3));
140 $$.argv = static_cast<Expr**>(realloc($$.argv, $$.argc * sizeof(Expr*)));
141 $$.argv[$$.argc-1] = $3;
142} 125}
143; 126;
144 127
145%% 128%%
146 129
147void yyerror(Expr** root, int* error_count, const char* s) { 130void yyerror(std::unique_ptr<Expr>* root, int* error_count, const char* s) {
148 if (strlen(s) == 0) { 131 if (strlen(s) == 0) {
149 s = "syntax error"; 132 s = "syntax error";
150 } 133 }
@@ -152,7 +135,7 @@ void yyerror(Expr** root, int* error_count, const char* s) {
152 ++*error_count; 135 ++*error_count;
153} 136}
154 137
155int parse_string(const char* str, Expr** root, int* error_count) { 138int parse_string(const char* str, std::unique_ptr<Expr>* root, int* error_count) {
156 yy_switch_to_buffer(yy_scan_string(str)); 139 yy_switch_to_buffer(yy_scan_string(str));
157 return yyparse(root, error_count); 140 return yyparse(root, error_count);
158} 141}