/src/testdir/tests/capi/luaL_loadbuffer_proto/serializer.cc
Line | Count | Source |
1 | | /* |
2 | | * SPDX-License-Identifier: BSD-2-Clause |
3 | | * |
4 | | * Copyright 2022, Tarantool AUTHORS, please see AUTHORS file. |
5 | | */ |
6 | | #include "serializer.h" |
7 | | |
8 | | #include <stack> |
9 | | #include <string> |
10 | | |
11 | | /** |
12 | | * If control flow reaches the point of the unreachable(), the program is |
13 | | * undefined. It is useful in situations where the compiler cannot deduce |
14 | | * the unreachability of the code. |
15 | | */ |
16 | | #if __has_builtin(__builtin_unreachable) || defined(__GNUC__) |
17 | 0 | # define unreachable() (assert(0), __builtin_unreachable()) |
18 | | #else |
19 | | # define unreachable() (assert(0)) |
20 | | #endif |
21 | | |
22 | | using namespace lua_grammar; |
23 | | |
24 | | extern char preamble_lua[]; |
25 | | |
26 | | #define PROTO_TOSTRING(TYPE, VAR_NAME) \ |
27 | | std::string TYPE##ToString(const TYPE & (VAR_NAME)) |
28 | | |
29 | | /* PROTO_TOSTRING version for nested (depth=2) protobuf messages. */ |
30 | | #define NESTED_PROTO_TOSTRING(TYPE, VAR_NAME, PARENT_MESSAGE) \ |
31 | | std::string TYPE##ToString \ |
32 | | (const PARENT_MESSAGE::TYPE & (VAR_NAME)) |
33 | | |
34 | | namespace luajit_fuzzer { |
35 | | namespace { |
36 | | |
37 | | /* |
38 | | * The following keywords are reserved and cannot be used as names, |
39 | | * see Lua 5.1 Reference Manual, 2.1 – Lexical Conventions. |
40 | | */ |
41 | | const std::set<std::string> KReservedLuaKeywords { |
42 | | "and", |
43 | | "break", |
44 | | "do", |
45 | | "else", |
46 | | "elseif", |
47 | | "end", |
48 | | "false", |
49 | | "for", |
50 | | "function", |
51 | | "if", |
52 | | "in", |
53 | | "local", |
54 | | "nil", |
55 | | "not", |
56 | | "or", |
57 | | "repeat", |
58 | | "return", |
59 | | "then", |
60 | | "true", |
61 | | "until", |
62 | | "while", |
63 | | }; |
64 | | |
65 | | const std::string kCounterNamePrefix = "counter_"; |
66 | | const std::string kNumberWrapperName = "always_number"; |
67 | | const std::string kBinOpWrapperName = "only_numbers_cmp"; |
68 | | const std::string kNotNaNAndNilWrapperName = "not_nan_and_nil"; |
69 | | |
70 | | PROTO_TOSTRING(Block, block); |
71 | | PROTO_TOSTRING(Chunk, chunk); |
72 | | |
73 | | PROTO_TOSTRING(Statement, stat); |
74 | | |
75 | | /** LastStatement and nested types. */ |
76 | | PROTO_TOSTRING(LastStatement, laststat); |
77 | | NESTED_PROTO_TOSTRING(ReturnOptionalExpressionList, explist, LastStatement); |
78 | | |
79 | | /** |
80 | | * Statement options. |
81 | | */ |
82 | | |
83 | | /** AssignmentList and nested types. */ |
84 | | PROTO_TOSTRING(AssignmentList, assignmentlist); |
85 | | NESTED_PROTO_TOSTRING(VariableList, varlist, AssignmentList); |
86 | | |
87 | | /** FunctionCall and nested types. */ |
88 | | PROTO_TOSTRING(FunctionCall, call); |
89 | | NESTED_PROTO_TOSTRING(Args, args, FunctionCall); |
90 | | NESTED_PROTO_TOSTRING(PrefixArgs, prefixargs, FunctionCall); |
91 | | NESTED_PROTO_TOSTRING(PrefixNamedArgs, prefixnamedargs, FunctionCall); |
92 | | |
93 | | /** DoBlock, WhileCycle and RepeatCycle clauses. */ |
94 | | PROTO_TOSTRING(DoBlock, block); |
95 | | PROTO_TOSTRING(WhileCycle, whilecycle); |
96 | | PROTO_TOSTRING(RepeatCycle, repeatcycle); |
97 | | |
98 | | /** IfStatement and nested types. */ |
99 | | PROTO_TOSTRING(IfStatement, statement); |
100 | | NESTED_PROTO_TOSTRING(ElseIfBlock, elseifblock, IfStatement); |
101 | | |
102 | | /** ForCycleName and ForCycleList clauses. */ |
103 | | PROTO_TOSTRING(ForCycleName, forcyclename); |
104 | | PROTO_TOSTRING(ForCycleList, forcyclelist); |
105 | | |
106 | | /** Function and nested types. */ |
107 | | PROTO_TOSTRING(Function, func); |
108 | | NESTED_PROTO_TOSTRING(FuncName, funcname, Function); |
109 | | |
110 | | PROTO_TOSTRING(NameList, namelist); |
111 | | NESTED_PROTO_TOSTRING(NameListWithEllipsis, namelist, FuncBody); |
112 | | NESTED_PROTO_TOSTRING(ParList, parlist, FuncBody); |
113 | | |
114 | | /** LocalFunc and LocalNames clauses. */ |
115 | | PROTO_TOSTRING(LocalFunc, localfunc); |
116 | | PROTO_TOSTRING(LocalNames, localnames); |
117 | | |
118 | | /** |
119 | | * Expressions and variables. |
120 | | */ |
121 | | |
122 | | /** Expressions clauses. */ |
123 | | PROTO_TOSTRING(ExpressionList, explist); |
124 | | PROTO_TOSTRING(OptionalExpressionList, explist); |
125 | | PROTO_TOSTRING(PrefixExpression, prefExpr); |
126 | | |
127 | | /* Variable and nested types. */ |
128 | | PROTO_TOSTRING(Variable, var); |
129 | | NESTED_PROTO_TOSTRING(IndexWithExpression, indexexpr, Variable); |
130 | | NESTED_PROTO_TOSTRING(IndexWithName, indexname, Variable); |
131 | | |
132 | | /** Expression and nested types. */ |
133 | | PROTO_TOSTRING(Expression, expr); |
134 | | NESTED_PROTO_TOSTRING(AnonFunc, function, Expression); |
135 | | NESTED_PROTO_TOSTRING(ExpBinaryOpExp, binary, Expression); |
136 | | NESTED_PROTO_TOSTRING(UnaryOpExp, unary, Expression); |
137 | | |
138 | | /** |
139 | | * Tables and fields. |
140 | | */ |
141 | | PROTO_TOSTRING(TableConstructor, table); |
142 | | PROTO_TOSTRING(FieldList, fieldlist); |
143 | | NESTED_PROTO_TOSTRING(FieldWithFieldSep, field, FieldList); |
144 | | |
145 | | /** Field and nested types. */ |
146 | | PROTO_TOSTRING(Field, field); |
147 | | NESTED_PROTO_TOSTRING(ExpressionAssignment, assignment, Field); |
148 | | NESTED_PROTO_TOSTRING(NameAssignment, assignment, Field); |
149 | | PROTO_TOSTRING(FieldSep, sep); |
150 | | |
151 | | /** Operators. */ |
152 | | PROTO_TOSTRING(BinaryOperator, op); |
153 | | PROTO_TOSTRING(UnaryOperator, op); |
154 | | |
155 | | /** Identifier (Name). */ |
156 | | PROTO_TOSTRING(Name, name); |
157 | | |
158 | | std::string |
159 | | NumberWrappedExpressionToString(const Expression &expr) |
160 | 15.4k | { |
161 | 15.4k | std::string retval; |
162 | 15.4k | retval += kNumberWrapperName; |
163 | 15.4k | retval += "("; |
164 | 15.4k | retval += ExpressionToString(expr); |
165 | 15.4k | retval += ")"; |
166 | | |
167 | 15.4k | return retval; |
168 | 15.4k | } |
169 | | |
170 | | std::string |
171 | | AllowedIndexExpressionToString(const Expression &expr) |
172 | 5.31k | { |
173 | 5.31k | std::string retval; |
174 | 5.31k | retval += kNotNaNAndNilWrapperName; |
175 | 5.31k | retval += "("; |
176 | 5.31k | retval += ExpressionToString(expr); |
177 | 5.31k | retval += ")"; |
178 | 5.31k | return retval; |
179 | 5.31k | } |
180 | | |
181 | | /** |
182 | | * Class that controls id creation for counters. Basically, a |
183 | | * variable wrapper that guarantees variable to be incremented. |
184 | | */ |
185 | | class CounterIdProvider { |
186 | | public: |
187 | | /** Returns number of id provided. */ |
188 | | std::size_t count() |
189 | 141k | { |
190 | 141k | return id_; |
191 | 141k | } |
192 | | |
193 | | /** Returns a new id that was not used after last clean(). */ |
194 | | std::size_t next() |
195 | 138k | { |
196 | 138k | return id_++; |
197 | 138k | } |
198 | | |
199 | | /** |
200 | | * Cleans history. Should be used to make fuzzer starts |
201 | | * independent. |
202 | | */ |
203 | | void clean() |
204 | 3.61k | { |
205 | 3.61k | id_ = 0; |
206 | 3.61k | } |
207 | | |
208 | | private: |
209 | | std::size_t id_ = 0; |
210 | | }; |
211 | | |
212 | | /** A singleton for counter id provider. */ |
213 | | CounterIdProvider& |
214 | | GetCounterIdProvider() |
215 | 283k | { |
216 | 283k | static CounterIdProvider provider; |
217 | 283k | return provider; |
218 | 283k | } |
219 | | |
220 | | std::string |
221 | | GetCounterName(std::size_t id) |
222 | 276k | { |
223 | 276k | return kCounterNamePrefix + std::to_string(id); |
224 | 276k | } |
225 | | |
226 | | /** Returns `<counter_name> = <counter_name> + 1`. */ |
227 | | std::string |
228 | | GetCounterIncrement(const std::string &counter_name) |
229 | 138k | { |
230 | 138k | std::string retval = counter_name; |
231 | 138k | retval += " = "; |
232 | 138k | retval += counter_name; |
233 | 138k | retval += " + 1;\n"; |
234 | 138k | return retval; |
235 | 138k | } |
236 | | |
237 | | /** |
238 | | * Returns `if <counter_name> > kMaxCounterValue then |
239 | | * <then_block> end`. |
240 | | */ |
241 | | std::string |
242 | | GetCondition(const std::string &counter_name, const std::string &then_block) |
243 | 138k | { |
244 | 138k | std::string retval = "if "; |
245 | 138k | retval += counter_name; |
246 | 138k | retval += " > "; |
247 | 138k | retval += std::to_string(kMaxCounterValue); |
248 | 138k | retval += " then "; |
249 | 138k | retval += then_block; |
250 | 138k | retval += " end\n"; |
251 | 138k | return retval; |
252 | 138k | } |
253 | | |
254 | | /** |
255 | | * Class that registers and provides context during code |
256 | | * generation. |
257 | | * Used to generate correct Lua code. |
258 | | */ |
259 | | class Context { |
260 | | public: |
261 | | enum class BlockType { |
262 | | kReturnable, |
263 | | kBreakable, |
264 | | kReturnableWithVararg, |
265 | | }; |
266 | | |
267 | | void step_in(BlockType type) |
268 | 138k | { |
269 | 138k | block_stack_.push(type); |
270 | 138k | if (block_type_is_returnable_(type)) { |
271 | 51.1k | returnable_stack_.push(type); |
272 | 51.1k | } |
273 | 138k | } |
274 | | |
275 | | void step_out() |
276 | 138k | { |
277 | 138k | assert(!block_stack_.empty()); |
278 | 138k | if (block_type_is_returnable_(block_stack_.top())) { |
279 | 51.1k | assert(!returnable_stack_.empty()); |
280 | 51.1k | returnable_stack_.pop(); |
281 | 51.1k | } |
282 | 138k | block_stack_.pop(); |
283 | 138k | } |
284 | | |
285 | | std::string get_next_block_setup() |
286 | 138k | { |
287 | 138k | std::size_t id = GetCounterIdProvider().next(); |
288 | 138k | std::string counter_name = GetCounterName(id); |
289 | | |
290 | 138k | return GetCondition(counter_name, get_exit_statement_()) + |
291 | 138k | GetCounterIncrement(counter_name); |
292 | 138k | } |
293 | | |
294 | | bool break_is_possible() |
295 | 4.02k | { |
296 | 4.02k | return !block_stack_.empty() && |
297 | 3.22k | block_stack_.top() == BlockType::kBreakable; |
298 | 4.02k | } |
299 | | |
300 | | bool return_is_possible() |
301 | 15.5k | { |
302 | 15.5k | return !returnable_stack_.empty(); |
303 | 15.5k | } |
304 | | |
305 | | bool vararg_is_possible() |
306 | 6.49k | { |
307 | 6.49k | return (returnable_stack_.empty() || |
308 | 1.80k | (!returnable_stack_.empty() && |
309 | 1.80k | returnable_stack_.top() == |
310 | 1.80k | BlockType::kReturnableWithVararg)); |
311 | 6.49k | } |
312 | | |
313 | | private: |
314 | | |
315 | | bool block_type_is_returnable_(BlockType type) |
316 | 276k | { |
317 | 276k | switch (type) { |
318 | 174k | case BlockType::kBreakable: |
319 | 174k | return false; |
320 | 97.1k | case BlockType::kReturnable: |
321 | 102k | case BlockType::kReturnableWithVararg: |
322 | 102k | return true; |
323 | 276k | } |
324 | 0 | unreachable(); |
325 | 0 | } |
326 | | |
327 | | std::string get_exit_statement_() |
328 | 138k | { |
329 | 138k | assert(!block_stack_.empty()); |
330 | 138k | switch (block_stack_.top()) { |
331 | 87.1k | case BlockType::kBreakable: |
332 | 87.1k | return "break"; |
333 | 48.5k | case BlockType::kReturnable: |
334 | 51.1k | case BlockType::kReturnableWithVararg: |
335 | 51.1k | return "return"; |
336 | 138k | } |
337 | 0 | unreachable(); |
338 | 0 | } |
339 | | |
340 | | std::stack<BlockType> block_stack_; |
341 | | /* |
342 | | * The returnable block can be exited with return from |
343 | | * the breakable block within it, but the breakable block |
344 | | * cannot be exited with break from the returnable block within |
345 | | * it. |
346 | | * Valid code: |
347 | | * `function foo() while true do return end end` |
348 | | * Erroneous code: |
349 | | * `while true do function foo() break end end` |
350 | | * This stack is used to check if `return` is possible. |
351 | | */ |
352 | | std::stack<BlockType> returnable_stack_; |
353 | | }; |
354 | | |
355 | | Context& |
356 | | GetContext() |
357 | 441k | { |
358 | 441k | static Context context; |
359 | 441k | return context; |
360 | 441k | } |
361 | | |
362 | | /** |
363 | | * Block may be placed not only in a cycle, so specially for cycles |
364 | | * there is a function that will add a break condition and a |
365 | | * counter increment. |
366 | | */ |
367 | | std::string |
368 | | BlockToStringCycleProtected(const Block &block) |
369 | 87.1k | { |
370 | 87.1k | std::string retval = GetContext().get_next_block_setup(); |
371 | 87.1k | retval += ChunkToString(block.chunk()); |
372 | 87.1k | return retval; |
373 | 87.1k | } |
374 | | |
375 | | /** |
376 | | * DoBlock may be placed not only in a cycle, so specially for |
377 | | * cycles there is a function that will call |
378 | | * BlockToStringCycleProtected(). |
379 | | */ |
380 | | std::string |
381 | | DoBlockToStringCycleProtected(const DoBlock &block) |
382 | 31.2k | { |
383 | 31.2k | std::string retval = "do\n"; |
384 | 31.2k | retval += BlockToStringCycleProtected(block.block()); |
385 | 31.2k | retval += "end\n"; |
386 | 31.2k | return retval; |
387 | 31.2k | } |
388 | | |
389 | | /** |
390 | | * FuncBody may contain recursive calls, so for all function bodies, |
391 | | * there is a function that adds a return condition and a counter |
392 | | * increment. |
393 | | */ |
394 | | std::string |
395 | | FuncBodyToStringReqProtected(const FuncBody &body) |
396 | 51.1k | { |
397 | 51.1k | std::string body_str = "( "; |
398 | 51.1k | if (body.has_parlist()) { |
399 | 9.03k | body_str += ParListToString(body.parlist()); |
400 | 9.03k | } |
401 | 51.1k | body_str += " )\n\t"; |
402 | | |
403 | 51.1k | body_str += GetContext().get_next_block_setup(); |
404 | | |
405 | 51.1k | body_str += BlockToString(body.block()); |
406 | 51.1k | body_str += "end\n"; |
407 | 51.1k | return body_str; |
408 | 51.1k | } |
409 | | |
410 | | bool |
411 | | FuncBodyHasVararg(const FuncBody &body) |
412 | 51.1k | { |
413 | 51.1k | if (!body.has_parlist()) { |
414 | 42.1k | return false; |
415 | 42.1k | } |
416 | 9.03k | const FuncBody::ParList &parlist = body.parlist(); |
417 | 9.03k | switch (parlist.parlist_oneof_case()) { |
418 | 1.55k | case FuncBody::ParList::ParlistOneofCase::kNamelist: |
419 | 1.55k | return parlist.namelist().has_ellipsis(); |
420 | 1.64k | case FuncBody::ParList::ParlistOneofCase::kEllipsis: |
421 | 1.64k | return true; |
422 | 5.83k | default: |
423 | 5.83k | return parlist.namelist().has_ellipsis(); |
424 | 9.03k | } |
425 | 9.03k | } |
426 | | |
427 | | Context::BlockType |
428 | | GetFuncBodyType(const FuncBody &body) |
429 | 51.1k | { |
430 | 51.1k | return FuncBodyHasVararg(body) ? |
431 | 2.60k | Context::BlockType::kReturnableWithVararg : |
432 | 51.1k | Context::BlockType::kReturnable; |
433 | 51.1k | } |
434 | | |
435 | | std::string |
436 | | ClearIdentifier(const std::string &identifier) |
437 | 309k | { |
438 | 309k | std::string cleared; |
439 | | |
440 | 309k | bool has_first_not_digit = false; |
441 | 309k | for (char c : identifier) { |
442 | 188k | if (has_first_not_digit && (std::iswalnum(c) || c == '_')) { |
443 | 104k | cleared += c; |
444 | 104k | } else if (std::isalpha(c) || c == '_') { |
445 | 16.2k | has_first_not_digit = true; |
446 | 16.2k | cleared += c; |
447 | 68.5k | } else { |
448 | 68.5k | cleared += '_'; |
449 | 68.5k | } |
450 | 188k | } |
451 | 309k | return cleared; |
452 | 309k | } |
453 | | |
454 | | /** Drop any special symbols to avoid parser errors. */ |
455 | | std::string |
456 | | ClearString(const std::string &str) |
457 | 204k | { |
458 | 204k | std::string escaped; |
459 | 204k | int nbackslash = 0; |
460 | 204k | for (char c : str) { |
461 | 134k | if (c == '\\') { |
462 | 2.99k | nbackslash++; |
463 | 131k | } else { |
464 | 131k | if (!std::isprint(c)) |
465 | 11.4k | continue; |
466 | 120k | nbackslash = 0; |
467 | 120k | if (c == '\'' || c == '\n') |
468 | 9.90k | escaped += '\\'; |
469 | 120k | } |
470 | 123k | escaped += c; |
471 | 123k | } |
472 | | /* Avoid escaping the ending quote. */ |
473 | 204k | if (nbackslash & 1) |
474 | 64 | escaped += '\\'; |
475 | 204k | return escaped; |
476 | 204k | } |
477 | | |
478 | | inline std::string |
479 | | clamp(std::string s, size_t maxSize = kMaxStrLength) |
480 | 514k | { |
481 | 514k | if (s.size() > maxSize) |
482 | 6.46k | s.resize(maxSize); |
483 | 514k | return s; |
484 | 514k | } |
485 | | |
486 | | inline double |
487 | | clamp(double number, double upper, double lower) |
488 | 19.6k | { |
489 | 19.6k | return number <= lower ? lower : |
490 | 19.6k | number >= upper ? upper : number; |
491 | 19.6k | } |
492 | | |
493 | | /** |
494 | | * Function to sanitize strings or identifiers. For identifiers, |
495 | | * sanitization is more stringent. |
496 | | */ |
497 | | inline std::string |
498 | | ConvertToStringDefault(const std::string &s, bool is_identifier = false) |
499 | 514k | { |
500 | 514k | std::string str = clamp(s); |
501 | 514k | if (is_identifier) |
502 | 309k | str = ClearIdentifier(str); |
503 | 204k | else |
504 | 204k | str = ClearString(str); |
505 | 514k | if (is_identifier && str.empty()) |
506 | 285k | str = std::string(kDefaultIdent); |
507 | 514k | return str; |
508 | 514k | } |
509 | | |
510 | | PROTO_TOSTRING(Block, block) |
511 | 78.9k | { |
512 | 78.9k | return ChunkToString(block.chunk()); |
513 | 78.9k | } |
514 | | |
515 | | PROTO_TOSTRING(Chunk, chunk) |
516 | 166k | { |
517 | 166k | std::string chunk_str; |
518 | 410k | for (int i = 0; i < chunk.stat_size(); ++i) |
519 | 244k | chunk_str += StatementToString(chunk.stat(i)) + "\n"; |
520 | | |
521 | 166k | if (chunk.has_laststat()) |
522 | 19.6k | chunk_str += LastStatementToString(chunk.laststat()) + "\n"; |
523 | | |
524 | 166k | return chunk_str; |
525 | 166k | } |
526 | | |
527 | | /** |
528 | | * LastStatement and nested types. |
529 | | */ |
530 | | PROTO_TOSTRING(LastStatement, laststat) |
531 | 19.6k | { |
532 | 19.6k | std::string laststat_str; |
533 | 19.6k | using LastStatType = LastStatement::LastOneofCase; |
534 | 19.6k | switch (laststat.last_oneof_case()) { |
535 | 6.01k | case LastStatType::kExplist: |
536 | 6.01k | laststat_str = ReturnOptionalExpressionListToString( |
537 | 6.01k | laststat.explist()); |
538 | 6.01k | break; |
539 | 4.02k | case LastStatType::kBreak: |
540 | 4.02k | if (GetContext().break_is_possible()) { |
541 | 1.99k | laststat_str = "break"; |
542 | 1.99k | } |
543 | 4.02k | break; |
544 | 9.58k | default: |
545 | | /* Chosen as default in order to decrease number of 'break's. */ |
546 | 9.58k | laststat_str = ReturnOptionalExpressionListToString( |
547 | 9.58k | laststat.explist()); |
548 | 9.58k | break; |
549 | 19.6k | } |
550 | | |
551 | | /* |
552 | | * Add a semicolon when last statement is not empty |
553 | | * to avoid errors like: |
554 | | * |
555 | | * <preamble.lua> |
556 | | * (nil):Name0() |
557 | | * (nil)() -- ambiguous syntax (function call x new statement) near '(' |
558 | | */ |
559 | 19.6k | if (!laststat_str.empty()) |
560 | 10.9k | laststat_str += "; "; |
561 | | |
562 | 19.6k | return laststat_str; |
563 | 19.6k | } |
564 | | |
565 | | NESTED_PROTO_TOSTRING(ReturnOptionalExpressionList, explist, LastStatement) |
566 | 15.5k | { |
567 | 15.5k | if (!GetContext().return_is_possible()) { |
568 | 6.61k | return ""; |
569 | 6.61k | } |
570 | | |
571 | 8.98k | std::string explist_str = "return"; |
572 | 8.98k | if (explist.has_explist()) { |
573 | 4.37k | explist_str += " " + ExpressionListToString(explist.explist()); |
574 | 4.37k | explist_str += " "; |
575 | 4.37k | } |
576 | 8.98k | return explist_str; |
577 | 15.5k | } |
578 | | |
579 | | /** |
580 | | * Statement and statement options. |
581 | | */ |
582 | | PROTO_TOSTRING(Statement, stat) |
583 | 244k | { |
584 | 244k | std::string stat_str; |
585 | 244k | using StatType = Statement::StatOneofCase; |
586 | 244k | switch (stat.stat_oneof_case()) { |
587 | 15.3k | case StatType::kList: |
588 | 15.3k | stat_str = AssignmentListToString(stat.list()); |
589 | 15.3k | break; |
590 | 55.4k | case StatType::kCall: |
591 | 55.4k | stat_str = FunctionCallToString(stat.call()); |
592 | 55.4k | break; |
593 | 2.81k | case StatType::kBlock: |
594 | 2.81k | stat_str = DoBlockToString(stat.block()); |
595 | 2.81k | break; |
596 | 6.53k | case StatType::kWhilecycle: |
597 | 6.53k | stat_str = WhileCycleToString(stat.whilecycle()); |
598 | 6.53k | break; |
599 | 55.9k | case StatType::kRepeatcycle: |
600 | 55.9k | stat_str = RepeatCycleToString(stat.repeatcycle()); |
601 | 55.9k | break; |
602 | 10.2k | case StatType::kIfstat: |
603 | 10.2k | stat_str = IfStatementToString(stat.ifstat()); |
604 | 10.2k | break; |
605 | 6.07k | case StatType::kForcyclename: |
606 | 6.07k | stat_str = ForCycleNameToString(stat.forcyclename()); |
607 | 6.07k | break; |
608 | 18.6k | case StatType::kForcyclelist: |
609 | 18.6k | stat_str = ForCycleListToString(stat.forcyclelist()); |
610 | 18.6k | break; |
611 | 6.31k | case StatType::kFunc: |
612 | 6.31k | stat_str = FunctionToString(stat.func()); |
613 | 6.31k | break; |
614 | 10.9k | case StatType::kLocalfunc: |
615 | 10.9k | stat_str = LocalFuncToString(stat.localfunc()); |
616 | 10.9k | break; |
617 | 10.9k | case StatType::kLocalnames: |
618 | 10.9k | stat_str = LocalNamesToString(stat.localnames()); |
619 | 10.9k | break; |
620 | 45.4k | default: |
621 | | /** |
622 | | * Chosen arbitrarily more for simplicity. |
623 | | * TODO: Choose "more interesting" defaults. |
624 | | */ |
625 | 45.4k | stat_str = AssignmentListToString(stat.list()); |
626 | 45.4k | break; |
627 | 244k | } |
628 | | |
629 | | /* |
630 | | * Always add a semicolon regardless of grammar |
631 | | * to avoid errors like: |
632 | | * |
633 | | * <preamble.lua> |
634 | | * (nil):Name0() |
635 | | * (nil)() -- ambiguous syntax (function call x new statement) near '(' |
636 | | */ |
637 | 244k | stat_str += "; "; |
638 | | |
639 | 244k | return stat_str; |
640 | 244k | } |
641 | | |
642 | | /** |
643 | | * AssignmentList and nested types. |
644 | | */ |
645 | | PROTO_TOSTRING(AssignmentList, assignmentlist) |
646 | 60.7k | { |
647 | 60.7k | std::string list_str = VariableListToString(assignmentlist.varlist()); |
648 | 60.7k | list_str += " = " + ExpressionListToString(assignmentlist.explist()); |
649 | 60.7k | return list_str; |
650 | 60.7k | } |
651 | | |
652 | | NESTED_PROTO_TOSTRING(VariableList, varlist, AssignmentList) |
653 | 60.7k | { |
654 | 60.7k | std::string varlist_str = VariableToString(varlist.var()); |
655 | 80.2k | for (int i = 0; i < varlist.vars_size(); ++i) { |
656 | 19.4k | varlist_str += ", " + VariableToString(varlist.vars(i)); |
657 | 19.4k | varlist_str += " "; |
658 | 19.4k | } |
659 | 60.7k | return varlist_str; |
660 | 60.7k | } |
661 | | |
662 | | /** |
663 | | * FunctionCall and nested types. |
664 | | */ |
665 | | PROTO_TOSTRING(FunctionCall, call) |
666 | 69.6k | { |
667 | 69.6k | using FuncCallType = FunctionCall::CallOneofCase; |
668 | 69.6k | switch (call.call_oneof_case()) { |
669 | 11.3k | case FuncCallType::kPrefArgs: |
670 | 11.3k | return PrefixArgsToString(call.prefargs()); |
671 | 5.27k | case FuncCallType::kNamedArgs: |
672 | 5.27k | return PrefixNamedArgsToString(call.namedargs()); |
673 | 52.9k | default: |
674 | | /* Chosen for more variability of generated programs. */ |
675 | 52.9k | return PrefixNamedArgsToString(call.namedargs()); |
676 | 69.6k | } |
677 | 69.6k | } |
678 | | |
679 | | NESTED_PROTO_TOSTRING(Args, args, FunctionCall) |
680 | 69.6k | { |
681 | 69.6k | using ArgsType = FunctionCall::Args::ArgsOneofCase; |
682 | 69.6k | switch (args.args_oneof_case()) { |
683 | 5.36k | case ArgsType::kExplist: |
684 | 5.36k | return "(" + OptionalExpressionListToString(args.explist()) + |
685 | 5.36k | ")"; |
686 | 1.42k | case ArgsType::kTableconstructor: |
687 | 1.42k | return TableConstructorToString(args.tableconstructor()); |
688 | 2.89k | case ArgsType::kStr: |
689 | 2.89k | return "'" + ConvertToStringDefault(args.str()) + "'"; |
690 | 59.9k | default: |
691 | | /* For more variability. */ |
692 | 59.9k | return TableConstructorToString(args.tableconstructor()); |
693 | 69.6k | } |
694 | 69.6k | } |
695 | | |
696 | | NESTED_PROTO_TOSTRING(PrefixArgs, prefixargs, FunctionCall) |
697 | 11.3k | { |
698 | 11.3k | std::string prefixargs_str = PrefixExpressionToString( |
699 | 11.3k | prefixargs.prefixexp()); |
700 | 11.3k | prefixargs_str += " " + ArgsToString(prefixargs.args()); |
701 | 11.3k | return prefixargs_str; |
702 | 11.3k | } |
703 | | |
704 | | NESTED_PROTO_TOSTRING(PrefixNamedArgs, prefixnamedargs, FunctionCall) |
705 | 58.2k | { |
706 | 58.2k | std::string predixnamedargs_str = PrefixExpressionToString( |
707 | 58.2k | prefixnamedargs.prefixexp()); |
708 | 58.2k | predixnamedargs_str += ":" + NameToString(prefixnamedargs.name()); |
709 | 58.2k | predixnamedargs_str += " " + ArgsToString(prefixnamedargs.args()); |
710 | 58.2k | return predixnamedargs_str; |
711 | 58.2k | } |
712 | | |
713 | | /** |
714 | | * DoBlock clause. |
715 | | */ |
716 | | PROTO_TOSTRING(DoBlock, block) |
717 | 2.81k | { |
718 | 2.81k | return "do\n" + BlockToString(block.block()) + "end\n"; |
719 | 2.81k | } |
720 | | |
721 | | /** |
722 | | * WhileCycle clause. |
723 | | */ |
724 | | PROTO_TOSTRING(WhileCycle, whilecycle) |
725 | 6.53k | { |
726 | 6.53k | GetContext().step_in(Context::BlockType::kBreakable); |
727 | | |
728 | 6.53k | std::string whilecycle_str = "while "; |
729 | 6.53k | whilecycle_str += ExpressionToString(whilecycle.condition()); |
730 | 6.53k | whilecycle_str += " "; |
731 | 6.53k | whilecycle_str += DoBlockToStringCycleProtected(whilecycle.doblock()); |
732 | | |
733 | 6.53k | GetContext().step_out(); |
734 | 6.53k | return whilecycle_str; |
735 | 6.53k | } |
736 | | |
737 | | /** |
738 | | * RepeatCycle clause. |
739 | | */ |
740 | | PROTO_TOSTRING(RepeatCycle, repeatcycle) |
741 | 55.9k | { |
742 | 55.9k | GetContext().step_in(Context::BlockType::kBreakable); |
743 | | |
744 | 55.9k | std::string repeatcycle_str = "repeat\n"; |
745 | 55.9k | repeatcycle_str += BlockToStringCycleProtected(repeatcycle.block()); |
746 | 55.9k | repeatcycle_str += "until "; |
747 | 55.9k | repeatcycle_str += ExpressionToString(repeatcycle.condition()); |
748 | | |
749 | 55.9k | GetContext().step_out(); |
750 | 55.9k | return repeatcycle_str; |
751 | 55.9k | } |
752 | | |
753 | | /** |
754 | | * IfStatement and nested types. |
755 | | */ |
756 | | PROTO_TOSTRING(IfStatement, statement) |
757 | 10.2k | { |
758 | 10.2k | std::string statement_str = "if " + |
759 | 10.2k | ExpressionToString(statement.condition()); |
760 | 10.2k | statement_str += " then\n\t" + BlockToString(statement.first()); |
761 | | |
762 | 16.9k | for (int i = 0; i < statement.clauses_size(); ++i) |
763 | 6.67k | statement_str += ElseIfBlockToString(statement.clauses(i)); |
764 | | |
765 | 10.2k | if (statement.has_last()) |
766 | 4.37k | statement_str += "else\n\t" + BlockToString(statement.last()); |
767 | | |
768 | 10.2k | statement_str += "end\n"; |
769 | 10.2k | return statement_str; |
770 | 10.2k | } |
771 | | |
772 | | NESTED_PROTO_TOSTRING(ElseIfBlock, elseifblock, IfStatement) |
773 | 6.67k | { |
774 | 6.67k | std::string elseifblock_str = "elseif "; |
775 | 6.67k | elseifblock_str += ExpressionToString(elseifblock.condition()); |
776 | 6.67k | elseifblock_str += " then\n\t"; |
777 | 6.67k | elseifblock_str += BlockToString(elseifblock.block()); |
778 | 6.67k | return elseifblock_str; |
779 | 6.67k | } |
780 | | |
781 | | /** |
782 | | * ForCycleName clause. |
783 | | * TODO: In 'for i = start, stop, step' construction start, stop, step |
784 | | * should be numbers. So results of the corresponding expressions |
785 | | * should be number. |
786 | | */ |
787 | | PROTO_TOSTRING(ForCycleName, forcyclename) |
788 | 6.07k | { |
789 | 6.07k | GetContext().step_in(Context::BlockType::kBreakable); |
790 | | |
791 | 6.07k | std::string forcyclename_str = "for "; |
792 | 6.07k | forcyclename_str += NameToString(forcyclename.name()); |
793 | 6.07k | forcyclename_str += " = "; |
794 | 6.07k | forcyclename_str += NumberWrappedExpressionToString( |
795 | 6.07k | forcyclename.startexp()); |
796 | 6.07k | forcyclename_str += ", "; |
797 | 6.07k | forcyclename_str += NumberWrappedExpressionToString( |
798 | 6.07k | forcyclename.stopexp()); |
799 | | |
800 | 6.07k | if (forcyclename.has_stepexp()) |
801 | 3.29k | forcyclename_str += ", " + NumberWrappedExpressionToString( |
802 | 3.29k | forcyclename.stepexp()); |
803 | | |
804 | 6.07k | forcyclename_str += " "; |
805 | 6.07k | forcyclename_str += DoBlockToStringCycleProtected( |
806 | 6.07k | forcyclename.doblock()); |
807 | | |
808 | 6.07k | GetContext().step_out(); |
809 | 6.07k | return forcyclename_str; |
810 | 6.07k | } |
811 | | |
812 | | /** |
813 | | * ForCycleList clause. |
814 | | */ |
815 | | PROTO_TOSTRING(ForCycleList, forcyclelist) |
816 | 18.6k | { |
817 | 18.6k | GetContext().step_in(Context::BlockType::kBreakable); |
818 | | |
819 | 18.6k | std::string forcyclelist_str = "for "; |
820 | 18.6k | forcyclelist_str += NameListToString(forcyclelist.names()); |
821 | 18.6k | forcyclelist_str += " in "; |
822 | 18.6k | forcyclelist_str += ExpressionListToString(forcyclelist.expressions()); |
823 | 18.6k | forcyclelist_str += " "; |
824 | 18.6k | forcyclelist_str += DoBlockToStringCycleProtected( |
825 | 18.6k | forcyclelist.doblock()); |
826 | | |
827 | 18.6k | GetContext().step_out(); |
828 | 18.6k | return forcyclelist_str; |
829 | 18.6k | } |
830 | | |
831 | | /** |
832 | | * Function and nested types. |
833 | | */ |
834 | | PROTO_TOSTRING(Function, func) |
835 | 6.31k | { |
836 | 6.31k | GetContext().step_in(GetFuncBodyType(func.body())); |
837 | | |
838 | 6.31k | std::string func_str = "function "; |
839 | 6.31k | func_str += FuncNameToString(func.name()); |
840 | 6.31k | func_str += FuncBodyToStringReqProtected(func.body()); |
841 | | |
842 | 6.31k | GetContext().step_out(); |
843 | 6.31k | return func_str; |
844 | 6.31k | } |
845 | | |
846 | | NESTED_PROTO_TOSTRING(FuncName, funcname, Function) |
847 | 6.31k | { |
848 | 6.31k | std::string funcname_str = NameToString(funcname.firstname()); |
849 | | |
850 | 7.45k | for (int i = 0; i < funcname.names_size(); ++i) |
851 | 1.14k | funcname_str += "." + NameToString(funcname.names(i)); |
852 | | |
853 | 6.31k | if (funcname.has_lastname()) |
854 | 601 | funcname_str += ":" + NameToString(funcname.lastname()); |
855 | | |
856 | 6.31k | return funcname_str; |
857 | 6.31k | } |
858 | | |
859 | | PROTO_TOSTRING(NameList, namelist) |
860 | 36.9k | { |
861 | 36.9k | std::string namelist_str = NameToString(namelist.firstname()); |
862 | 50.5k | for (int i = 0; i < namelist.names_size(); ++i) |
863 | 13.5k | namelist_str += ", " + NameToString(namelist.names(i)); |
864 | 36.9k | return namelist_str; |
865 | 36.9k | } |
866 | | |
867 | | NESTED_PROTO_TOSTRING(NameListWithEllipsis, namelist, FuncBody) |
868 | 7.39k | { |
869 | 7.39k | std::string namelist_str = NameListToString(namelist.namelist()); |
870 | 7.39k | if (namelist.has_ellipsis()) |
871 | 960 | namelist_str += ", ..."; |
872 | 7.39k | return namelist_str; |
873 | 7.39k | } |
874 | | |
875 | | NESTED_PROTO_TOSTRING(ParList, parlist, FuncBody) |
876 | 9.03k | { |
877 | 9.03k | using ParListType = FuncBody::ParList::ParlistOneofCase; |
878 | 9.03k | switch (parlist.parlist_oneof_case()) { |
879 | 1.55k | case ParListType::kNamelist: |
880 | 1.55k | return NameListWithEllipsisToString(parlist.namelist()); |
881 | 1.64k | case ParListType::kEllipsis: |
882 | 1.64k | return "..."; |
883 | 5.83k | default: |
884 | | /* Chosen as default in order to decrease number of ellipses. */ |
885 | 5.83k | return NameListWithEllipsisToString(parlist.namelist()); |
886 | 9.03k | } |
887 | 9.03k | } |
888 | | |
889 | | /** |
890 | | * LocalFunc clause. |
891 | | */ |
892 | | PROTO_TOSTRING(LocalFunc, localfunc) |
893 | 10.9k | { |
894 | 10.9k | GetContext().step_in(GetFuncBodyType(localfunc.funcbody())); |
895 | | |
896 | 10.9k | std::string localfunc_str = "local function "; |
897 | 10.9k | localfunc_str += NameToString(localfunc.name()); |
898 | 10.9k | localfunc_str += " "; |
899 | 10.9k | localfunc_str += FuncBodyToStringReqProtected(localfunc.funcbody()); |
900 | | |
901 | 10.9k | GetContext().step_out(); |
902 | 10.9k | return localfunc_str; |
903 | 10.9k | } |
904 | | |
905 | | /** |
906 | | * LocalNames clause. |
907 | | */ |
908 | | PROTO_TOSTRING(LocalNames, localnames) |
909 | 10.9k | { |
910 | 10.9k | std::string localnames_str = "local "; |
911 | 10.9k | localnames_str += NameListToString(localnames.namelist()); |
912 | | |
913 | 10.9k | if (localnames.has_explist()) |
914 | 8.50k | localnames_str += " = " + ExpressionListToString( |
915 | 8.50k | localnames.explist()); |
916 | 10.9k | return localnames_str; |
917 | 10.9k | } |
918 | | |
919 | | /** |
920 | | * Expressions and variables. |
921 | | */ |
922 | | |
923 | | /** |
924 | | * Expressions clauses. |
925 | | */ |
926 | | PROTO_TOSTRING(ExpressionList, explist) |
927 | 97.3k | { |
928 | 97.3k | std::string explist_str; |
929 | 147k | for (int i = 0; i < explist.expressions_size(); ++i) |
930 | 49.8k | explist_str += ExpressionToString(explist.expressions(i)) + |
931 | 49.8k | ", "; |
932 | 97.3k | explist_str += ExpressionToString(explist.explast()) + " "; |
933 | 97.3k | return explist_str; |
934 | 97.3k | } |
935 | | |
936 | | PROTO_TOSTRING(OptionalExpressionList, explist) |
937 | 5.36k | { |
938 | 5.36k | if (explist.has_explist()) |
939 | 5.08k | return ExpressionListToString(explist.explist()); |
940 | 286 | return ""; |
941 | 5.36k | } |
942 | | |
943 | | PROTO_TOSTRING(PrefixExpression, prefixexp) |
944 | 112k | { |
945 | 112k | using PrefExprType = PrefixExpression::PrefixOneofCase; |
946 | 112k | switch (prefixexp.prefix_oneof_case()) { |
947 | 11.4k | case PrefExprType::kVar: |
948 | 11.4k | return VariableToString(prefixexp.var()); |
949 | 14.1k | case PrefExprType::kFunctioncall: |
950 | 14.1k | return FunctionCallToString(prefixexp.functioncall()); |
951 | 7.73k | case PrefExprType::kExp: |
952 | 7.73k | return "(" + ExpressionToString(prefixexp.exp()) + ")"; |
953 | 79.3k | default: |
954 | | /* |
955 | | * Can be generated too nested expressions with other options, |
956 | | * though they can be enabled for more variable fuzzing. |
957 | | */ |
958 | 79.3k | return VariableToString(prefixexp.var()); |
959 | 112k | } |
960 | 112k | } |
961 | | |
962 | | /** |
963 | | * Variable and nested types. |
964 | | */ |
965 | | PROTO_TOSTRING(Variable, var) |
966 | 170k | { |
967 | 170k | using VarType = Variable::VarOneofCase; |
968 | 170k | switch (var.var_oneof_case()) { |
969 | 11.1k | case VarType::kName: |
970 | 11.1k | return NameToString(var.name()); |
971 | 6.78k | case VarType::kIndexexpr: |
972 | 6.78k | return IndexWithExpressionToString(var.indexexpr()); |
973 | 4.25k | case VarType::kIndexname: |
974 | 4.25k | return IndexWithNameToString(var.indexname()); |
975 | 148k | default: |
976 | | /* |
977 | | * Can be generated too nested expressions with other options, |
978 | | * though they can be enabled for more variable fuzzing. |
979 | | */ |
980 | 148k | return NameToString(var.name()); |
981 | 170k | } |
982 | 170k | } |
983 | | |
984 | | NESTED_PROTO_TOSTRING(IndexWithExpression, indexexpr, Variable) |
985 | 6.78k | { |
986 | 6.78k | std::string indexexpr_str = PrefixExpressionToString( |
987 | 6.78k | indexexpr.prefixexp()); |
988 | 6.78k | indexexpr_str += "[" + ExpressionToString(indexexpr.exp()) + "]"; |
989 | 6.78k | return indexexpr_str; |
990 | 6.78k | } |
991 | | |
992 | | NESTED_PROTO_TOSTRING(IndexWithName, indexname, Variable) |
993 | 4.25k | { |
994 | 4.25k | std::string indexname_str = PrefixExpressionToString( |
995 | 4.25k | indexname.prefixexp()); |
996 | 4.25k | std::string idx_str = ConvertToStringDefault(indexname.name(), true); |
997 | | /* Prevent using reserved keywords as indices. */ |
998 | 4.25k | if (KReservedLuaKeywords.find(idx_str) != KReservedLuaKeywords.end()) { |
999 | 40 | idx_str += "_1"; |
1000 | 40 | } |
1001 | 4.25k | indexname_str += "." + idx_str; |
1002 | 4.25k | return indexname_str; |
1003 | 4.25k | } |
1004 | | |
1005 | | /** |
1006 | | * Expression and nested types. |
1007 | | */ |
1008 | | PROTO_TOSTRING(Expression, expr) |
1009 | 398k | { |
1010 | 398k | using ExprType = Expression::ExprOneofCase; |
1011 | 398k | switch (expr.expr_oneof_case()) { |
1012 | 21.6k | case ExprType::kNil: |
1013 | 21.6k | return "nil"; |
1014 | 5.24k | case ExprType::kFalse: |
1015 | 5.24k | return "false"; |
1016 | 5.88k | case ExprType::kTrue: |
1017 | 5.88k | return "true"; |
1018 | 19.6k | case ExprType::kNumber: { |
1019 | | /* Clamp number between given boundaries. */ |
1020 | 19.6k | double number = clamp(expr.number(), kMaxNumber, kMinNumber); |
1021 | 19.6k | return std::to_string(number); |
1022 | 0 | } |
1023 | 10.3k | case ExprType::kStr: |
1024 | 10.3k | return "'" + ConvertToStringDefault(expr.str()) + "'"; |
1025 | 6.49k | case ExprType::kEllipsis: |
1026 | 6.49k | if (GetContext().vararg_is_possible()) { |
1027 | 5.34k | return " ... "; |
1028 | 5.34k | } else { |
1029 | 1.15k | return " nil"; |
1030 | 1.15k | } |
1031 | 33.8k | case ExprType::kFunction: |
1032 | 33.8k | return AnonFuncToString(expr.function()); |
1033 | 31.9k | case ExprType::kPrefixexp: |
1034 | 31.9k | return PrefixExpressionToString(expr.prefixexp()); |
1035 | 10.7k | case ExprType::kTableconstructor: |
1036 | 10.7k | return TableConstructorToString(expr.tableconstructor()); |
1037 | 53.1k | case ExprType::kBinary: |
1038 | 53.1k | return ExpBinaryOpExpToString(expr.binary()); |
1039 | 7.82k | case ExprType::kUnary: |
1040 | 7.82k | return UnaryOpExpToString(expr.unary()); |
1041 | 191k | default: |
1042 | | /** |
1043 | | * Arbitrary choice. |
1044 | | * TODO: Choose "more interesting" defaults. |
1045 | | */ |
1046 | 191k | return "'" + ConvertToStringDefault(expr.str()) + "'"; |
1047 | 398k | } |
1048 | 398k | } |
1049 | | |
1050 | | NESTED_PROTO_TOSTRING(AnonFunc, func, Expression) |
1051 | 33.8k | { |
1052 | 33.8k | GetContext().step_in(GetFuncBodyType(func.body())); |
1053 | | |
1054 | 33.8k | std::string retval = "function "; |
1055 | 33.8k | retval += FuncBodyToStringReqProtected(func.body()); |
1056 | | |
1057 | 33.8k | GetContext().step_out(); |
1058 | 33.8k | return retval; |
1059 | 33.8k | } |
1060 | | |
1061 | | NESTED_PROTO_TOSTRING(ExpBinaryOpExp, binary, Expression) |
1062 | 53.1k | { |
1063 | 53.1k | std::string leftexp_str = ExpressionToString(binary.leftexp()); |
1064 | 53.1k | std::string binop_str = BinaryOperatorToString(binary.binop()); |
1065 | 53.1k | std::string rightexp_str = ExpressionToString(binary.rightexp()); |
1066 | | |
1067 | 53.1k | std::string binary_str; |
1068 | 53.1k | if (binop_str == "<" || |
1069 | 50.7k | binop_str == ">" || |
1070 | 50.1k | binop_str == "<=" || |
1071 | 49.7k | binop_str == ">=") { |
1072 | 3.85k | binary_str = kBinOpWrapperName; |
1073 | 3.85k | binary_str += "(" + leftexp_str; |
1074 | 3.85k | binary_str += ", '" + binop_str + "', "; |
1075 | 3.85k | binary_str += rightexp_str + ")"; |
1076 | 3.85k | return binary_str; |
1077 | 3.85k | } |
1078 | | |
1079 | 49.3k | binary_str = leftexp_str; |
1080 | 49.3k | binary_str += " " + binop_str + " "; |
1081 | 49.3k | binary_str += rightexp_str; |
1082 | | |
1083 | 49.3k | return binary_str; |
1084 | 53.1k | } |
1085 | | |
1086 | | NESTED_PROTO_TOSTRING(UnaryOpExp, unary, Expression) |
1087 | 7.82k | { |
1088 | 7.82k | std::string unary_str = UnaryOperatorToString(unary.unop()); |
1089 | | /* |
1090 | | * Add a whitespace before an expression with unary minus, |
1091 | | * otherwise double hyphen comments the following code |
1092 | | * and it breaks generated programs syntactically. |
1093 | | */ |
1094 | 7.82k | unary_str += " " + ExpressionToString(unary.exp()); |
1095 | 7.82k | return unary_str; |
1096 | 7.82k | } |
1097 | | |
1098 | | /** |
1099 | | * Tables and fields. |
1100 | | */ |
1101 | | PROTO_TOSTRING(TableConstructor, table) |
1102 | 72.0k | { |
1103 | 72.0k | std::string table_str = " (setmetatable({ "; |
1104 | 72.0k | if (table.has_fieldlist()) |
1105 | 8.49k | table_str += FieldListToString(table.fieldlist()); |
1106 | 72.0k | table_str += " }, table_mt))()"; |
1107 | 72.0k | return table_str; |
1108 | 72.0k | } |
1109 | | |
1110 | | PROTO_TOSTRING(FieldList, fieldlist) |
1111 | 8.49k | { |
1112 | 8.49k | std::string fieldlist_str = FieldToString(fieldlist.firstfield()); |
1113 | 21.9k | for (int i = 0; i < fieldlist.fields_size(); ++i) |
1114 | 13.4k | fieldlist_str += FieldWithFieldSepToString(fieldlist.fields(i)); |
1115 | 8.49k | if (fieldlist.has_lastsep()) |
1116 | 2.04k | fieldlist_str += FieldSepToString(fieldlist.lastsep()); |
1117 | 8.49k | return fieldlist_str; |
1118 | 8.49k | } |
1119 | | |
1120 | | NESTED_PROTO_TOSTRING(FieldWithFieldSep, field, FieldList) |
1121 | 13.4k | { |
1122 | 13.4k | std::string field_str = FieldSepToString(field.sep()); |
1123 | 13.4k | field_str += " " + FieldToString(field.field()); |
1124 | 13.4k | return field_str; |
1125 | 13.4k | } |
1126 | | |
1127 | | /** |
1128 | | * Field and nested types. |
1129 | | */ |
1130 | | PROTO_TOSTRING(Field, field) |
1131 | 21.9k | { |
1132 | 21.9k | using FieldType = Field::FieldOneofCase; |
1133 | 21.9k | switch (field.field_oneof_case()) { |
1134 | 5.31k | case FieldType::kExprassign: |
1135 | 5.31k | return ExpressionAssignmentToString(field.exprassign()); |
1136 | 747 | case FieldType::kNamedassign: |
1137 | 747 | return NameAssignmentToString(field.namedassign()); |
1138 | 4.80k | case FieldType::kExpression: |
1139 | 4.80k | return ExpressionToString(field.expression()); |
1140 | 11.1k | default: |
1141 | | /* More common case of using fields. */ |
1142 | 11.1k | return NameAssignmentToString(field.namedassign()); |
1143 | 21.9k | } |
1144 | 21.9k | } |
1145 | | |
1146 | | NESTED_PROTO_TOSTRING(ExpressionAssignment, assignment, Field) |
1147 | 5.31k | { |
1148 | | /* Prevent error 'table index is nil' and 'table index is NaN'. */ |
1149 | 5.31k | std::string assignment_str = "[ " + |
1150 | 5.31k | AllowedIndexExpressionToString(assignment.key()) + " ]"; |
1151 | 5.31k | assignment_str += " = " + ExpressionToString(assignment.value()); |
1152 | 5.31k | return assignment_str; |
1153 | 5.31k | } |
1154 | | |
1155 | | NESTED_PROTO_TOSTRING(NameAssignment, assignment, Field) |
1156 | 11.8k | { |
1157 | 11.8k | std::string assignment_str = NameToString(assignment.name()); |
1158 | 11.8k | assignment_str += " = " + ExpressionToString(assignment.value()); |
1159 | 11.8k | return assignment_str; |
1160 | 11.8k | } |
1161 | | |
1162 | | PROTO_TOSTRING(FieldSep, sep) |
1163 | 15.5k | { |
1164 | 15.5k | using FieldSepType = FieldSep::SepOneofCase; |
1165 | 15.5k | switch (sep.sep_oneof_case()) { |
1166 | 4.53k | case FieldSepType::kComma: |
1167 | 4.53k | return ","; |
1168 | 1.67k | case FieldSepType::kSemicolon: |
1169 | 1.67k | return ";"; |
1170 | 9.31k | default: |
1171 | 9.31k | return ","; |
1172 | 15.5k | } |
1173 | 15.5k | } |
1174 | | |
1175 | | /** |
1176 | | * Operators. |
1177 | | */ |
1178 | | PROTO_TOSTRING(BinaryOperator, op) |
1179 | 53.1k | { |
1180 | 53.1k | using BinopType = BinaryOperator::BinaryOneofCase; |
1181 | 53.1k | switch (op.binary_oneof_case()) { |
1182 | 3.84k | case BinopType::kAdd: |
1183 | 3.84k | return "+"; |
1184 | 2.31k | case BinopType::kSub: |
1185 | 2.31k | return "-"; |
1186 | 5.21k | case BinopType::kMult: |
1187 | 5.21k | return "*"; |
1188 | 1.43k | case BinopType::kDiv: |
1189 | 1.43k | return "/"; |
1190 | | #if LUA_VERSION_NUM >= 503 |
1191 | | case BinopType::kIDiv: |
1192 | | return "//"; |
1193 | | #endif |
1194 | 933 | case BinopType::kExp: |
1195 | 933 | return "^"; |
1196 | 1.59k | case BinopType::kMod: |
1197 | 1.59k | return "%"; |
1198 | | |
1199 | 14.3k | case BinopType::kConcat: |
1200 | 14.3k | return ".."; |
1201 | | |
1202 | 2.42k | case BinopType::kLess: |
1203 | 2.42k | return "<"; |
1204 | 315 | case BinopType::kLessEqual: |
1205 | 315 | return "<="; |
1206 | 653 | case BinopType::kGreater: |
1207 | 653 | return ">"; |
1208 | 466 | case BinopType::kGreaterEqual: |
1209 | 466 | return ">="; |
1210 | 277 | case BinopType::kEqual: |
1211 | 277 | return "=="; |
1212 | 368 | case BinopType::kNotEqual: |
1213 | 368 | return "~="; |
1214 | 1.48k | case BinopType::kAnd: |
1215 | 1.48k | return "and"; |
1216 | 6.20k | case BinopType::kOr: |
1217 | 6.20k | return "or"; |
1218 | | |
1219 | | #if LUA_VERSION_NUM >= 503 |
1220 | | case BinopType::kBAnd: |
1221 | | return "&"; |
1222 | | case BinopType::kBOr: |
1223 | | return "|"; |
1224 | | case BinopType::kBXor: |
1225 | | return "~"; |
1226 | | case BinopType::kBShl: |
1227 | | return "<<"; |
1228 | | case BinopType::kBShr: |
1229 | | return ">>"; |
1230 | | #endif |
1231 | 11.3k | default: |
1232 | | /* Works in most cases. */ |
1233 | 11.3k | return "=="; |
1234 | 53.1k | } |
1235 | 53.1k | } |
1236 | | |
1237 | | PROTO_TOSTRING(UnaryOperator, op) |
1238 | 7.82k | { |
1239 | 7.82k | using UnaryopType = UnaryOperator::UnaryOneofCase; |
1240 | 7.82k | switch (op.unary_oneof_case()) { |
1241 | 2.56k | case UnaryopType::kNegate: |
1242 | 2.56k | return "-"; |
1243 | 627 | case UnaryopType::kNot: |
1244 | 627 | return "not "; |
1245 | 811 | case UnaryopType::kLength: |
1246 | 811 | return "#"; |
1247 | | #if LUA_VERSION_NUM >= 503 |
1248 | | case UnaryopType::kBNot: |
1249 | | return "~"; |
1250 | | #endif |
1251 | 3.82k | default: |
1252 | | /* Works in most cases. */ |
1253 | 3.82k | return "not "; |
1254 | 7.82k | } |
1255 | 7.82k | } |
1256 | | |
1257 | | /** |
1258 | | * Identifier (Name). |
1259 | | */ |
1260 | | PROTO_TOSTRING(Name, name) |
1261 | 305k | { |
1262 | 305k | std::string ident = ConvertToStringDefault(name.name(), true); |
1263 | | /* Prevent using reserved keywords as identifiers. */ |
1264 | 305k | if (KReservedLuaKeywords.find(ident) != KReservedLuaKeywords.end()) { |
1265 | 193 | ident += "_1"; |
1266 | 193 | } |
1267 | | /* Identifier has default name, add an index. */ |
1268 | 305k | if (!ident.compare(kDefaultIdent)) { |
1269 | 282k | ident += std::to_string(name.num() % kMaxIdentifiers); |
1270 | 282k | } |
1271 | 305k | return ident; |
1272 | 305k | } |
1273 | | |
1274 | | } /* namespace */ |
1275 | | |
1276 | | std::string |
1277 | | MainBlockToString(const Block &block) |
1278 | 3.61k | { |
1279 | 3.61k | GetCounterIdProvider().clean(); |
1280 | | |
1281 | 3.61k | std::string block_str = BlockToString(block); |
1282 | 3.61k | std::string retval = preamble_lua; |
1283 | | |
1284 | 141k | for (size_t i = 0; i < GetCounterIdProvider().count(); ++i) { |
1285 | 138k | retval += GetCounterName(i); |
1286 | 138k | retval += " = 0\n"; |
1287 | 138k | } |
1288 | 3.61k | retval += block_str; |
1289 | | |
1290 | 3.61k | return retval; |
1291 | 3.61k | } |
1292 | | |
1293 | | } /* namespace luajit_fuzzer */ |