/src/testdir/tests/capi/luaL_loadbuffer_proto/serializer.cc
Line | Count | Source (jump to first uncovered line) |
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 | 18.2k | { |
161 | 18.2k | std::string retval; |
162 | 18.2k | retval += kNumberWrapperName; |
163 | 18.2k | retval += "("; |
164 | 18.2k | retval += ExpressionToString(expr); |
165 | 18.2k | retval += ")"; |
166 | | |
167 | 18.2k | return retval; |
168 | 18.2k | } |
169 | | |
170 | | std::string |
171 | | AllowedIndexExpressionToString(const Expression &expr) |
172 | 6.44k | { |
173 | 6.44k | std::string retval; |
174 | 6.44k | retval += kNotNaNAndNilWrapperName; |
175 | 6.44k | retval += "("; |
176 | 6.44k | retval += ExpressionToString(expr); |
177 | 6.44k | retval += ")"; |
178 | 6.44k | return retval; |
179 | 6.44k | } |
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 | 142k | { |
190 | 142k | return id_; |
191 | 142k | } |
192 | | |
193 | | /** Returns a new id that was not used after last clean(). */ |
194 | | std::size_t next() |
195 | 124k | { |
196 | 124k | return id_++; |
197 | 124k | } |
198 | | |
199 | | /** |
200 | | * Cleans history. Should be used to make fuzzer starts |
201 | | * independent. |
202 | | */ |
203 | | void clean() |
204 | 17.3k | { |
205 | 17.3k | id_ = 0; |
206 | 17.3k | } |
207 | | |
208 | | private: |
209 | | std::size_t id_ = 0; |
210 | | }; |
211 | | |
212 | | /** A singleton for counter id provider. */ |
213 | | CounterIdProvider& |
214 | | GetCounterIdProvider() |
215 | 284k | { |
216 | 284k | static CounterIdProvider provider; |
217 | 284k | return provider; |
218 | 284k | } |
219 | | |
220 | | std::string |
221 | | GetCounterName(std::size_t id) |
222 | 249k | { |
223 | 249k | return kCounterNamePrefix + std::to_string(id); |
224 | 249k | } |
225 | | |
226 | | /** Returns `<counter_name> = <counter_name> + 1`. */ |
227 | | std::string |
228 | | GetCounterIncrement(const std::string &counter_name) |
229 | 124k | { |
230 | 124k | std::string retval = counter_name; |
231 | 124k | retval += " = "; |
232 | 124k | retval += counter_name; |
233 | 124k | retval += " + 1;\n"; |
234 | 124k | return retval; |
235 | 124k | } |
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 | 124k | { |
244 | 124k | std::string retval = "if "; |
245 | 124k | retval += counter_name; |
246 | 124k | retval += " > "; |
247 | 124k | retval += std::to_string(kMaxCounterValue); |
248 | 124k | retval += " then "; |
249 | 124k | retval += then_block; |
250 | 124k | retval += " end\n"; |
251 | 124k | return retval; |
252 | 124k | } |
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 | 124k | { |
269 | 124k | block_stack_.push(type); |
270 | 124k | if (block_type_is_returnable_(type)) { |
271 | 46.6k | returnable_stack_.push(type); |
272 | 46.6k | } |
273 | 124k | } |
274 | | |
275 | | void step_out() |
276 | 124k | { |
277 | 124k | assert(!block_stack_.empty()); |
278 | 124k | if (block_type_is_returnable_(block_stack_.top())) { |
279 | 46.6k | assert(!returnable_stack_.empty()); |
280 | 46.6k | returnable_stack_.pop(); |
281 | 46.6k | } |
282 | 124k | block_stack_.pop(); |
283 | 124k | } |
284 | | |
285 | | std::string get_next_block_setup() |
286 | 124k | { |
287 | 124k | std::size_t id = GetCounterIdProvider().next(); |
288 | 124k | std::string counter_name = GetCounterName(id); |
289 | | |
290 | 124k | return GetCondition(counter_name, get_exit_statement_()) + |
291 | 124k | GetCounterIncrement(counter_name); |
292 | 124k | } |
293 | | |
294 | | bool break_is_possible() |
295 | 4.94k | { |
296 | 4.94k | return !block_stack_.empty() && |
297 | 4.94k | block_stack_.top() == BlockType::kBreakable; |
298 | 4.94k | } |
299 | | |
300 | | bool return_is_possible() |
301 | 16.3k | { |
302 | 16.3k | return !returnable_stack_.empty(); |
303 | 16.3k | } |
304 | | |
305 | | bool vararg_is_possible() |
306 | 6.42k | { |
307 | 6.42k | return (returnable_stack_.empty() || |
308 | 6.42k | (!returnable_stack_.empty() && |
309 | 1.52k | returnable_stack_.top() == |
310 | 1.52k | BlockType::kReturnableWithVararg)); |
311 | 6.42k | } |
312 | | |
313 | | private: |
314 | | |
315 | | bool block_type_is_returnable_(BlockType type) |
316 | 249k | { |
317 | 249k | switch (type) { |
318 | 156k | case BlockType::kBreakable: |
319 | 156k | return false; |
320 | 87.2k | case BlockType::kReturnable: |
321 | 93.2k | case BlockType::kReturnableWithVararg: |
322 | 93.2k | return true; |
323 | 249k | } |
324 | 0 | unreachable(); |
325 | 0 | } |
326 | | |
327 | | std::string get_exit_statement_() |
328 | 124k | { |
329 | 124k | assert(!block_stack_.empty()); |
330 | 124k | switch (block_stack_.top()) { |
331 | 78.1k | case BlockType::kBreakable: |
332 | 78.1k | return "break"; |
333 | 43.6k | case BlockType::kReturnable: |
334 | 46.6k | case BlockType::kReturnableWithVararg: |
335 | 46.6k | return "return"; |
336 | 124k | } |
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 | 402k | { |
358 | 402k | static Context context; |
359 | 402k | return context; |
360 | 402k | } |
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 | 78.1k | { |
370 | 78.1k | std::string retval = GetContext().get_next_block_setup(); |
371 | 78.1k | retval += ChunkToString(block.chunk()); |
372 | 78.1k | return retval; |
373 | 78.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.3k | { |
383 | 31.3k | std::string retval = "do\n"; |
384 | 31.3k | retval += BlockToStringCycleProtected(block.block()); |
385 | 31.3k | retval += "end\n"; |
386 | 31.3k | return retval; |
387 | 31.3k | } |
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 | 46.6k | { |
397 | 46.6k | std::string body_str = "( "; |
398 | 46.6k | if (body.has_parlist()) { |
399 | 8.96k | body_str += ParListToString(body.parlist()); |
400 | 8.96k | } |
401 | 46.6k | body_str += " )\n\t"; |
402 | | |
403 | 46.6k | body_str += GetContext().get_next_block_setup(); |
404 | | |
405 | 46.6k | body_str += BlockToString(body.block()); |
406 | 46.6k | body_str += "end\n"; |
407 | 46.6k | return body_str; |
408 | 46.6k | } |
409 | | |
410 | | bool |
411 | | FuncBodyHasVararg(const FuncBody &body) |
412 | 46.6k | { |
413 | 46.6k | if (!body.has_parlist()) { |
414 | 37.6k | return false; |
415 | 37.6k | } |
416 | 8.96k | const FuncBody::ParList &parlist = body.parlist(); |
417 | 8.96k | switch (parlist.parlist_oneof_case()) { |
418 | 1.82k | case FuncBody::ParList::ParlistOneofCase::kNamelist: |
419 | 1.82k | return parlist.namelist().has_ellipsis(); |
420 | 1.84k | case FuncBody::ParList::ParlistOneofCase::kEllipsis: |
421 | 1.84k | return true; |
422 | 5.30k | default: |
423 | 5.30k | return parlist.namelist().has_ellipsis(); |
424 | 8.96k | } |
425 | 8.96k | } |
426 | | |
427 | | Context::BlockType |
428 | | GetFuncBodyType(const FuncBody &body) |
429 | 46.6k | { |
430 | 46.6k | return FuncBodyHasVararg(body) ? |
431 | 2.99k | Context::BlockType::kReturnableWithVararg : |
432 | 46.6k | Context::BlockType::kReturnable; |
433 | 46.6k | } |
434 | | |
435 | | std::string |
436 | | ClearIdentifier(const std::string &identifier) |
437 | 340k | { |
438 | 340k | std::string cleared; |
439 | | |
440 | 340k | bool has_first_not_digit = false; |
441 | 340k | for (char c : identifier) { |
442 | 233k | if (has_first_not_digit && (std::iswalnum(c) || c == '_')) { |
443 | 134k | cleared += c; |
444 | 134k | } else if (std::isalpha(c) || c == '_') { |
445 | 22.5k | has_first_not_digit = true; |
446 | 22.5k | cleared += c; |
447 | 76.5k | } else { |
448 | 76.5k | cleared += '_'; |
449 | 76.5k | } |
450 | 233k | } |
451 | 340k | return cleared; |
452 | 340k | } |
453 | | |
454 | | inline std::string |
455 | | clamp(std::string s, size_t maxSize = kMaxStrLength) |
456 | 598k | { |
457 | 598k | if (s.size() > maxSize) |
458 | 14.4k | s.resize(maxSize); |
459 | 598k | return s; |
460 | 598k | } |
461 | | |
462 | | inline double |
463 | | clamp(double number, double upper, double lower) |
464 | 26.6k | { |
465 | 26.6k | return number <= lower ? lower : |
466 | 26.6k | number >= upper ? upper : number; |
467 | 26.6k | } |
468 | | |
469 | | inline std::string |
470 | | ConvertToStringDefault(const std::string &s, bool sanitize = false) |
471 | 598k | { |
472 | 598k | std::string ident = clamp(s); |
473 | 598k | if (sanitize) |
474 | 340k | ident = ClearIdentifier(ident); |
475 | 598k | if (ident.empty()) |
476 | 528k | ident = std::string(kDefaultIdent); |
477 | 598k | return ident; |
478 | 598k | } |
479 | | |
480 | | PROTO_TOSTRING(Block, block) |
481 | 82.4k | { |
482 | 82.4k | return ChunkToString(block.chunk()); |
483 | 82.4k | } |
484 | | |
485 | | PROTO_TOSTRING(Chunk, chunk) |
486 | 160k | { |
487 | 160k | std::string chunk_str; |
488 | 409k | for (int i = 0; i < chunk.stat_size(); ++i) |
489 | 249k | chunk_str += StatementToString(chunk.stat(i)) + "\n"; |
490 | | |
491 | 160k | if (chunk.has_laststat()) |
492 | 21.2k | chunk_str += LastStatementToString(chunk.laststat()) + "\n"; |
493 | | |
494 | 160k | return chunk_str; |
495 | 160k | } |
496 | | |
497 | | /** |
498 | | * LastStatement and nested types. |
499 | | */ |
500 | | PROTO_TOSTRING(LastStatement, laststat) |
501 | 21.2k | { |
502 | 21.2k | std::string laststat_str; |
503 | 21.2k | using LastStatType = LastStatement::LastOneofCase; |
504 | 21.2k | switch (laststat.last_oneof_case()) { |
505 | 6.32k | case LastStatType::kExplist: |
506 | 6.32k | laststat_str = ReturnOptionalExpressionListToString( |
507 | 6.32k | laststat.explist()); |
508 | 6.32k | break; |
509 | 4.94k | case LastStatType::kBreak: |
510 | 4.94k | if (GetContext().break_is_possible()) { |
511 | 2.37k | laststat_str = "break"; |
512 | 2.37k | } |
513 | 4.94k | break; |
514 | 9.98k | default: |
515 | | /* Chosen as default in order to decrease number of 'break's. */ |
516 | 9.98k | laststat_str = ReturnOptionalExpressionListToString( |
517 | 9.98k | laststat.explist()); |
518 | 9.98k | break; |
519 | 21.2k | } |
520 | | |
521 | | /* |
522 | | * Add a semicolon when last statement is not empty |
523 | | * to avoid errors like: |
524 | | * |
525 | | * <preamble.lua> |
526 | | * (nil):Name0() |
527 | | * (nil)() -- ambiguous syntax (function call x new statement) near '(' |
528 | | */ |
529 | 21.2k | if (!laststat_str.empty()) |
530 | 10.6k | laststat_str += "; "; |
531 | | |
532 | 21.2k | return laststat_str; |
533 | 21.2k | } |
534 | | |
535 | | NESTED_PROTO_TOSTRING(ReturnOptionalExpressionList, explist, LastStatement) |
536 | 16.3k | { |
537 | 16.3k | if (!GetContext().return_is_possible()) { |
538 | 7.99k | return ""; |
539 | 7.99k | } |
540 | | |
541 | 8.31k | std::string explist_str = "return"; |
542 | 8.31k | if (explist.has_explist()) { |
543 | 4.55k | explist_str += " " + ExpressionListToString(explist.explist()); |
544 | 4.55k | explist_str += " "; |
545 | 4.55k | } |
546 | 8.31k | return explist_str; |
547 | 16.3k | } |
548 | | |
549 | | /** |
550 | | * Statement and statement options. |
551 | | */ |
552 | | PROTO_TOSTRING(Statement, stat) |
553 | 249k | { |
554 | 249k | std::string stat_str; |
555 | 249k | using StatType = Statement::StatOneofCase; |
556 | 249k | switch (stat.stat_oneof_case()) { |
557 | 28.3k | case StatType::kList: |
558 | 28.3k | stat_str = AssignmentListToString(stat.list()); |
559 | 28.3k | break; |
560 | 58.5k | case StatType::kCall: |
561 | 58.5k | stat_str = FunctionCallToString(stat.call()); |
562 | 58.5k | break; |
563 | 3.07k | case StatType::kBlock: |
564 | 3.07k | stat_str = DoBlockToString(stat.block()); |
565 | 3.07k | break; |
566 | 9.40k | case StatType::kWhilecycle: |
567 | 9.40k | stat_str = WhileCycleToString(stat.whilecycle()); |
568 | 9.40k | break; |
569 | 46.8k | case StatType::kRepeatcycle: |
570 | 46.8k | stat_str = RepeatCycleToString(stat.repeatcycle()); |
571 | 46.8k | break; |
572 | 8.41k | case StatType::kIfstat: |
573 | 8.41k | stat_str = IfStatementToString(stat.ifstat()); |
574 | 8.41k | break; |
575 | 7.26k | case StatType::kForcyclename: |
576 | 7.26k | stat_str = ForCycleNameToString(stat.forcyclename()); |
577 | 7.26k | break; |
578 | 14.6k | case StatType::kForcyclelist: |
579 | 14.6k | stat_str = ForCycleListToString(stat.forcyclelist()); |
580 | 14.6k | break; |
581 | 4.66k | case StatType::kFunc: |
582 | 4.66k | stat_str = FunctionToString(stat.func()); |
583 | 4.66k | break; |
584 | 11.0k | case StatType::kLocalfunc: |
585 | 11.0k | stat_str = LocalFuncToString(stat.localfunc()); |
586 | 11.0k | break; |
587 | 8.71k | case StatType::kLocalnames: |
588 | 8.71k | stat_str = LocalNamesToString(stat.localnames()); |
589 | 8.71k | break; |
590 | 48.4k | default: |
591 | | /** |
592 | | * Chosen arbitrarily more for simplicity. |
593 | | * TODO: Choose "more interesting" defaults. |
594 | | */ |
595 | 48.4k | stat_str = AssignmentListToString(stat.list()); |
596 | 48.4k | break; |
597 | 249k | } |
598 | | |
599 | | /* |
600 | | * Always add a semicolon regardless of grammar |
601 | | * to avoid errors like: |
602 | | * |
603 | | * <preamble.lua> |
604 | | * (nil):Name0() |
605 | | * (nil)() -- ambiguous syntax (function call x new statement) near '(' |
606 | | */ |
607 | 249k | stat_str += "; "; |
608 | | |
609 | 249k | return stat_str; |
610 | 249k | } |
611 | | |
612 | | /** |
613 | | * AssignmentList and nested types. |
614 | | */ |
615 | | PROTO_TOSTRING(AssignmentList, assignmentlist) |
616 | 76.8k | { |
617 | 76.8k | std::string list_str = VariableListToString(assignmentlist.varlist()); |
618 | 76.8k | list_str += " = " + ExpressionListToString(assignmentlist.explist()); |
619 | 76.8k | return list_str; |
620 | 76.8k | } |
621 | | |
622 | | NESTED_PROTO_TOSTRING(VariableList, varlist, AssignmentList) |
623 | 76.8k | { |
624 | 76.8k | std::string varlist_str = VariableToString(varlist.var()); |
625 | 99.3k | for (int i = 0; i < varlist.vars_size(); ++i) { |
626 | 22.5k | varlist_str += ", " + VariableToString(varlist.vars(i)); |
627 | 22.5k | varlist_str += " "; |
628 | 22.5k | } |
629 | 76.8k | return varlist_str; |
630 | 76.8k | } |
631 | | |
632 | | /** |
633 | | * FunctionCall and nested types. |
634 | | */ |
635 | | PROTO_TOSTRING(FunctionCall, call) |
636 | 74.1k | { |
637 | 74.1k | using FuncCallType = FunctionCall::CallOneofCase; |
638 | 74.1k | switch (call.call_oneof_case()) { |
639 | 16.4k | case FuncCallType::kPrefArgs: |
640 | 16.4k | return PrefixArgsToString(call.prefargs()); |
641 | 5.46k | case FuncCallType::kNamedArgs: |
642 | 5.46k | return PrefixNamedArgsToString(call.namedargs()); |
643 | 52.2k | default: |
644 | | /* Chosen for more variability of generated programs. */ |
645 | 52.2k | return PrefixNamedArgsToString(call.namedargs()); |
646 | 74.1k | } |
647 | 74.1k | } |
648 | | |
649 | | NESTED_PROTO_TOSTRING(Args, args, FunctionCall) |
650 | 74.1k | { |
651 | 74.1k | using ArgsType = FunctionCall::Args::ArgsOneofCase; |
652 | 74.1k | switch (args.args_oneof_case()) { |
653 | 7.29k | case ArgsType::kExplist: |
654 | 7.29k | return "(" + OptionalExpressionListToString(args.explist()) + |
655 | 7.29k | ")"; |
656 | 1.59k | case ArgsType::kTableconstructor: |
657 | 1.59k | return TableConstructorToString(args.tableconstructor()); |
658 | 6.71k | case ArgsType::kStr: |
659 | 6.71k | return "'" + ConvertToStringDefault(args.str()) + "'"; |
660 | 58.5k | default: |
661 | | /* For more variability. */ |
662 | 58.5k | return TableConstructorToString(args.tableconstructor()); |
663 | 74.1k | } |
664 | 74.1k | } |
665 | | |
666 | | NESTED_PROTO_TOSTRING(PrefixArgs, prefixargs, FunctionCall) |
667 | 16.4k | { |
668 | 16.4k | std::string prefixargs_str = PrefixExpressionToString( |
669 | 16.4k | prefixargs.prefixexp()); |
670 | 16.4k | prefixargs_str += " " + ArgsToString(prefixargs.args()); |
671 | 16.4k | return prefixargs_str; |
672 | 16.4k | } |
673 | | |
674 | | NESTED_PROTO_TOSTRING(PrefixNamedArgs, prefixnamedargs, FunctionCall) |
675 | 57.7k | { |
676 | 57.7k | std::string predixnamedargs_str = PrefixExpressionToString( |
677 | 57.7k | prefixnamedargs.prefixexp()); |
678 | 57.7k | predixnamedargs_str += ":" + NameToString(prefixnamedargs.name()); |
679 | 57.7k | predixnamedargs_str += " " + ArgsToString(prefixnamedargs.args()); |
680 | 57.7k | return predixnamedargs_str; |
681 | 57.7k | } |
682 | | |
683 | | /** |
684 | | * DoBlock clause. |
685 | | */ |
686 | | PROTO_TOSTRING(DoBlock, block) |
687 | 3.07k | { |
688 | 3.07k | return "do\n" + BlockToString(block.block()) + "end\n"; |
689 | 3.07k | } |
690 | | |
691 | | /** |
692 | | * WhileCycle clause. |
693 | | */ |
694 | | PROTO_TOSTRING(WhileCycle, whilecycle) |
695 | 9.40k | { |
696 | 9.40k | GetContext().step_in(Context::BlockType::kBreakable); |
697 | | |
698 | 9.40k | std::string whilecycle_str = "while "; |
699 | 9.40k | whilecycle_str += ExpressionToString(whilecycle.condition()); |
700 | 9.40k | whilecycle_str += " "; |
701 | 9.40k | whilecycle_str += DoBlockToStringCycleProtected(whilecycle.doblock()); |
702 | | |
703 | 9.40k | GetContext().step_out(); |
704 | 9.40k | return whilecycle_str; |
705 | 9.40k | } |
706 | | |
707 | | /** |
708 | | * RepeatCycle clause. |
709 | | */ |
710 | | PROTO_TOSTRING(RepeatCycle, repeatcycle) |
711 | 46.8k | { |
712 | 46.8k | GetContext().step_in(Context::BlockType::kBreakable); |
713 | | |
714 | 46.8k | std::string repeatcycle_str = "repeat\n"; |
715 | 46.8k | repeatcycle_str += BlockToStringCycleProtected(repeatcycle.block()); |
716 | 46.8k | repeatcycle_str += "until "; |
717 | 46.8k | repeatcycle_str += ExpressionToString(repeatcycle.condition()); |
718 | | |
719 | 46.8k | GetContext().step_out(); |
720 | 46.8k | return repeatcycle_str; |
721 | 46.8k | } |
722 | | |
723 | | /** |
724 | | * IfStatement and nested types. |
725 | | */ |
726 | | PROTO_TOSTRING(IfStatement, statement) |
727 | 8.41k | { |
728 | 8.41k | std::string statement_str = "if " + |
729 | 8.41k | ExpressionToString(statement.condition()); |
730 | 8.41k | statement_str += " then\n\t" + BlockToString(statement.first()); |
731 | | |
732 | 12.1k | for (int i = 0; i < statement.clauses_size(); ++i) |
733 | 3.74k | statement_str += ElseIfBlockToString(statement.clauses(i)); |
734 | | |
735 | 8.41k | if (statement.has_last()) |
736 | 3.28k | statement_str += "else\n\t" + BlockToString(statement.last()); |
737 | | |
738 | 8.41k | statement_str += "end\n"; |
739 | 8.41k | return statement_str; |
740 | 8.41k | } |
741 | | |
742 | | NESTED_PROTO_TOSTRING(ElseIfBlock, elseifblock, IfStatement) |
743 | 3.74k | { |
744 | 3.74k | std::string elseifblock_str = "elseif "; |
745 | 3.74k | elseifblock_str += ExpressionToString(elseifblock.condition()); |
746 | 3.74k | elseifblock_str += " then\n\t"; |
747 | 3.74k | elseifblock_str += BlockToString(elseifblock.block()); |
748 | 3.74k | return elseifblock_str; |
749 | 3.74k | } |
750 | | |
751 | | /** |
752 | | * ForCycleName clause. |
753 | | * TODO: In 'for i = start, stop, step' construction start, stop, step |
754 | | * should be numbers. So results of the corresponding expressions |
755 | | * should be number. |
756 | | */ |
757 | | PROTO_TOSTRING(ForCycleName, forcyclename) |
758 | 7.26k | { |
759 | 7.26k | GetContext().step_in(Context::BlockType::kBreakable); |
760 | | |
761 | 7.26k | std::string forcyclename_str = "for "; |
762 | 7.26k | forcyclename_str += NameToString(forcyclename.name()); |
763 | 7.26k | forcyclename_str += " = "; |
764 | 7.26k | forcyclename_str += NumberWrappedExpressionToString( |
765 | 7.26k | forcyclename.startexp()); |
766 | 7.26k | forcyclename_str += ", "; |
767 | 7.26k | forcyclename_str += NumberWrappedExpressionToString( |
768 | 7.26k | forcyclename.stopexp()); |
769 | | |
770 | 7.26k | if (forcyclename.has_stepexp()) |
771 | 3.66k | forcyclename_str += ", " + NumberWrappedExpressionToString( |
772 | 3.66k | forcyclename.stepexp()); |
773 | | |
774 | 7.26k | forcyclename_str += " "; |
775 | 7.26k | forcyclename_str += DoBlockToStringCycleProtected( |
776 | 7.26k | forcyclename.doblock()); |
777 | | |
778 | 7.26k | GetContext().step_out(); |
779 | 7.26k | return forcyclename_str; |
780 | 7.26k | } |
781 | | |
782 | | /** |
783 | | * ForCycleList clause. |
784 | | */ |
785 | | PROTO_TOSTRING(ForCycleList, forcyclelist) |
786 | 14.6k | { |
787 | 14.6k | GetContext().step_in(Context::BlockType::kBreakable); |
788 | | |
789 | 14.6k | std::string forcyclelist_str = "for "; |
790 | 14.6k | forcyclelist_str += NameListToString(forcyclelist.names()); |
791 | 14.6k | forcyclelist_str += " in "; |
792 | 14.6k | forcyclelist_str += ExpressionListToString(forcyclelist.expressions()); |
793 | 14.6k | forcyclelist_str += " "; |
794 | 14.6k | forcyclelist_str += DoBlockToStringCycleProtected( |
795 | 14.6k | forcyclelist.doblock()); |
796 | | |
797 | 14.6k | GetContext().step_out(); |
798 | 14.6k | return forcyclelist_str; |
799 | 14.6k | } |
800 | | |
801 | | /** |
802 | | * Function and nested types. |
803 | | */ |
804 | | PROTO_TOSTRING(Function, func) |
805 | 4.66k | { |
806 | 4.66k | GetContext().step_in(GetFuncBodyType(func.body())); |
807 | | |
808 | 4.66k | std::string func_str = "function "; |
809 | 4.66k | func_str += FuncNameToString(func.name()); |
810 | 4.66k | func_str += FuncBodyToStringReqProtected(func.body()); |
811 | | |
812 | 4.66k | GetContext().step_out(); |
813 | 4.66k | return func_str; |
814 | 4.66k | } |
815 | | |
816 | | NESTED_PROTO_TOSTRING(FuncName, funcname, Function) |
817 | 4.66k | { |
818 | 4.66k | std::string funcname_str = NameToString(funcname.firstname()); |
819 | | |
820 | 6.33k | for (int i = 0; i < funcname.names_size(); ++i) |
821 | 1.67k | funcname_str += "." + NameToString(funcname.names(i)); |
822 | | |
823 | 4.66k | if (funcname.has_lastname()) |
824 | 771 | funcname_str += ":" + NameToString(funcname.lastname()); |
825 | | |
826 | 4.66k | return funcname_str; |
827 | 4.66k | } |
828 | | |
829 | | PROTO_TOSTRING(NameList, namelist) |
830 | 30.4k | { |
831 | 30.4k | std::string namelist_str = NameToString(namelist.firstname()); |
832 | 48.6k | for (int i = 0; i < namelist.names_size(); ++i) |
833 | 18.2k | namelist_str += ", " + NameToString(namelist.names(i)); |
834 | 30.4k | return namelist_str; |
835 | 30.4k | } |
836 | | |
837 | | NESTED_PROTO_TOSTRING(NameListWithEllipsis, namelist, FuncBody) |
838 | 7.12k | { |
839 | 7.12k | std::string namelist_str = NameListToString(namelist.namelist()); |
840 | 7.12k | if (namelist.has_ellipsis()) |
841 | 1.15k | namelist_str += ", ..."; |
842 | 7.12k | return namelist_str; |
843 | 7.12k | } |
844 | | |
845 | | NESTED_PROTO_TOSTRING(ParList, parlist, FuncBody) |
846 | 8.96k | { |
847 | 8.96k | using ParListType = FuncBody::ParList::ParlistOneofCase; |
848 | 8.96k | switch (parlist.parlist_oneof_case()) { |
849 | 1.82k | case ParListType::kNamelist: |
850 | 1.82k | return NameListWithEllipsisToString(parlist.namelist()); |
851 | 1.84k | case ParListType::kEllipsis: |
852 | 1.84k | return "..."; |
853 | 5.30k | default: |
854 | | /* Chosen as default in order to decrease number of ellipses. */ |
855 | 5.30k | return NameListWithEllipsisToString(parlist.namelist()); |
856 | 8.96k | } |
857 | 8.96k | } |
858 | | |
859 | | /** |
860 | | * LocalFunc clause. |
861 | | */ |
862 | | PROTO_TOSTRING(LocalFunc, localfunc) |
863 | 11.0k | { |
864 | 11.0k | GetContext().step_in(GetFuncBodyType(localfunc.funcbody())); |
865 | | |
866 | 11.0k | std::string localfunc_str = "local function "; |
867 | 11.0k | localfunc_str += NameToString(localfunc.name()); |
868 | 11.0k | localfunc_str += " "; |
869 | 11.0k | localfunc_str += FuncBodyToStringReqProtected(localfunc.funcbody()); |
870 | | |
871 | 11.0k | GetContext().step_out(); |
872 | 11.0k | return localfunc_str; |
873 | 11.0k | } |
874 | | |
875 | | /** |
876 | | * LocalNames clause. |
877 | | */ |
878 | | PROTO_TOSTRING(LocalNames, localnames) |
879 | 8.71k | { |
880 | 8.71k | std::string localnames_str = "local "; |
881 | 8.71k | localnames_str += NameListToString(localnames.namelist()); |
882 | | |
883 | 8.71k | if (localnames.has_explist()) |
884 | 5.56k | localnames_str += " = " + ExpressionListToString( |
885 | 5.56k | localnames.explist()); |
886 | 8.71k | return localnames_str; |
887 | 8.71k | } |
888 | | |
889 | | /** |
890 | | * Expressions and variables. |
891 | | */ |
892 | | |
893 | | /** |
894 | | * Expressions clauses. |
895 | | */ |
896 | | PROTO_TOSTRING(ExpressionList, explist) |
897 | 108k | { |
898 | 108k | std::string explist_str; |
899 | 176k | for (int i = 0; i < explist.expressions_size(); ++i) |
900 | 68.3k | explist_str += ExpressionToString(explist.expressions(i)) + |
901 | 68.3k | ", "; |
902 | 108k | explist_str += ExpressionToString(explist.explast()) + " "; |
903 | 108k | return explist_str; |
904 | 108k | } |
905 | | |
906 | | PROTO_TOSTRING(OptionalExpressionList, explist) |
907 | 7.29k | { |
908 | 7.29k | if (explist.has_explist()) |
909 | 6.74k | return ExpressionListToString(explist.explist()); |
910 | 554 | return ""; |
911 | 7.29k | } |
912 | | |
913 | | PROTO_TOSTRING(PrefixExpression, prefixexp) |
914 | 131k | { |
915 | 131k | using PrefExprType = PrefixExpression::PrefixOneofCase; |
916 | 131k | switch (prefixexp.prefix_oneof_case()) { |
917 | 15.7k | case PrefExprType::kVar: |
918 | 15.7k | return VariableToString(prefixexp.var()); |
919 | 15.6k | case PrefExprType::kFunctioncall: |
920 | 15.6k | return FunctionCallToString(prefixexp.functioncall()); |
921 | 8.92k | case PrefExprType::kExp: |
922 | 8.92k | return "(" + ExpressionToString(prefixexp.exp()) + ")"; |
923 | 91.6k | default: |
924 | | /* |
925 | | * Can be generated too nested expressions with other options, |
926 | | * though they can be enabled for more variable fuzzing. |
927 | | */ |
928 | 91.6k | return VariableToString(prefixexp.var()); |
929 | 131k | } |
930 | 131k | } |
931 | | |
932 | | /** |
933 | | * Variable and nested types. |
934 | | */ |
935 | | PROTO_TOSTRING(Variable, var) |
936 | 206k | { |
937 | 206k | using VarType = Variable::VarOneofCase; |
938 | 206k | switch (var.var_oneof_case()) { |
939 | 15.2k | case VarType::kName: |
940 | 15.2k | return NameToString(var.name()); |
941 | 9.16k | case VarType::kIndexexpr: |
942 | 9.16k | return IndexWithExpressionToString(var.indexexpr()); |
943 | 5.30k | case VarType::kIndexname: |
944 | 5.30k | return IndexWithNameToString(var.indexname()); |
945 | 176k | default: |
946 | | /* |
947 | | * Can be generated too nested expressions with other options, |
948 | | * though they can be enabled for more variable fuzzing. |
949 | | */ |
950 | 176k | return NameToString(var.name()); |
951 | 206k | } |
952 | 206k | } |
953 | | |
954 | | NESTED_PROTO_TOSTRING(IndexWithExpression, indexexpr, Variable) |
955 | 9.16k | { |
956 | 9.16k | std::string indexexpr_str = PrefixExpressionToString( |
957 | 9.16k | indexexpr.prefixexp()); |
958 | 9.16k | indexexpr_str += "[" + ExpressionToString(indexexpr.exp()) + "]"; |
959 | 9.16k | return indexexpr_str; |
960 | 9.16k | } |
961 | | |
962 | | NESTED_PROTO_TOSTRING(IndexWithName, indexname, Variable) |
963 | 5.30k | { |
964 | 5.30k | std::string indexname_str = PrefixExpressionToString( |
965 | 5.30k | indexname.prefixexp()); |
966 | 5.30k | std::string idx_str = ConvertToStringDefault(indexname.name(), true); |
967 | | /* Prevent using reserved keywords as indices. */ |
968 | 5.30k | if (KReservedLuaKeywords.find(idx_str) != KReservedLuaKeywords.end()) { |
969 | 39 | idx_str += "_1"; |
970 | 39 | } |
971 | 5.30k | indexname_str += "." + idx_str; |
972 | 5.30k | return indexname_str; |
973 | 5.30k | } |
974 | | |
975 | | /** |
976 | | * Expression and nested types. |
977 | | */ |
978 | | PROTO_TOSTRING(Expression, expr) |
979 | 502k | { |
980 | 502k | using ExprType = Expression::ExprOneofCase; |
981 | 502k | switch (expr.expr_oneof_case()) { |
982 | 12.2k | case ExprType::kNil: |
983 | 12.2k | return "nil"; |
984 | 7.07k | case ExprType::kFalse: |
985 | 7.07k | return "false"; |
986 | 7.25k | case ExprType::kTrue: |
987 | 7.25k | return "true"; |
988 | 26.6k | case ExprType::kNumber: { |
989 | | /* Clamp number between given boundaries. */ |
990 | 26.6k | double number = clamp(expr.number(), kMaxNumber, kMinNumber); |
991 | 26.6k | return std::to_string(number); |
992 | 0 | } |
993 | 33.2k | case ExprType::kStr: |
994 | 33.2k | return "'" + ConvertToStringDefault(expr.str()) + "'"; |
995 | 6.42k | case ExprType::kEllipsis: |
996 | 6.42k | if (GetContext().vararg_is_possible()) { |
997 | 5.44k | return " ... "; |
998 | 5.44k | } else { |
999 | 979 | return " nil"; |
1000 | 979 | } |
1001 | 30.9k | case ExprType::kFunction: |
1002 | 30.9k | return AnonFuncToString(expr.function()); |
1003 | 43.2k | case ExprType::kPrefixexp: |
1004 | 43.2k | return PrefixExpressionToString(expr.prefixexp()); |
1005 | 16.2k | case ExprType::kTableconstructor: |
1006 | 16.2k | return TableConstructorToString(expr.tableconstructor()); |
1007 | 90.1k | case ExprType::kBinary: |
1008 | 90.1k | return ExpBinaryOpExpToString(expr.binary()); |
1009 | 10.3k | case ExprType::kUnary: |
1010 | 10.3k | return UnaryOpExpToString(expr.unary()); |
1011 | 218k | default: |
1012 | | /** |
1013 | | * Arbitrary choice. |
1014 | | * TODO: Choose "more interesting" defaults. |
1015 | | */ |
1016 | 218k | return "'" + ConvertToStringDefault(expr.str()) + "'"; |
1017 | 502k | } |
1018 | 502k | } |
1019 | | |
1020 | | NESTED_PROTO_TOSTRING(AnonFunc, func, Expression) |
1021 | 30.9k | { |
1022 | 30.9k | GetContext().step_in(GetFuncBodyType(func.body())); |
1023 | | |
1024 | 30.9k | std::string retval = "function "; |
1025 | 30.9k | retval += FuncBodyToStringReqProtected(func.body()); |
1026 | | |
1027 | 30.9k | GetContext().step_out(); |
1028 | 30.9k | return retval; |
1029 | 30.9k | } |
1030 | | |
1031 | | NESTED_PROTO_TOSTRING(ExpBinaryOpExp, binary, Expression) |
1032 | 90.1k | { |
1033 | 90.1k | std::string leftexp_str = ExpressionToString(binary.leftexp()); |
1034 | 90.1k | std::string binop_str = BinaryOperatorToString(binary.binop()); |
1035 | 90.1k | std::string rightexp_str = ExpressionToString(binary.rightexp()); |
1036 | | |
1037 | 90.1k | std::string binary_str; |
1038 | 90.1k | if (binop_str == "<" || |
1039 | 90.1k | binop_str == ">" || |
1040 | 90.1k | binop_str == "<=" || |
1041 | 90.1k | binop_str == ">=") { |
1042 | 3.74k | binary_str = kBinOpWrapperName; |
1043 | 3.74k | binary_str += "(" + leftexp_str; |
1044 | 3.74k | binary_str += ", '" + binop_str + "', "; |
1045 | 3.74k | binary_str += rightexp_str + ")"; |
1046 | 3.74k | return binary_str; |
1047 | 3.74k | } |
1048 | | |
1049 | 86.4k | binary_str = leftexp_str; |
1050 | 86.4k | binary_str += " " + binop_str + " "; |
1051 | 86.4k | binary_str += rightexp_str; |
1052 | | |
1053 | 86.4k | return binary_str; |
1054 | 90.1k | } |
1055 | | |
1056 | | NESTED_PROTO_TOSTRING(UnaryOpExp, unary, Expression) |
1057 | 10.3k | { |
1058 | 10.3k | std::string unary_str = UnaryOperatorToString(unary.unop()); |
1059 | | /* |
1060 | | * Add a whitespace before an expression with unary minus, |
1061 | | * otherwise double hyphen comments the following code |
1062 | | * and it breaks generated programs syntactically. |
1063 | | */ |
1064 | 10.3k | unary_str += " " + ExpressionToString(unary.exp()); |
1065 | 10.3k | return unary_str; |
1066 | 10.3k | } |
1067 | | |
1068 | | /** |
1069 | | * Tables and fields. |
1070 | | */ |
1071 | | PROTO_TOSTRING(TableConstructor, table) |
1072 | 76.4k | { |
1073 | 76.4k | std::string table_str = " (setmetatable({ "; |
1074 | 76.4k | if (table.has_fieldlist()) |
1075 | 11.2k | table_str += FieldListToString(table.fieldlist()); |
1076 | 76.4k | table_str += " }, table_mt))()"; |
1077 | 76.4k | return table_str; |
1078 | 76.4k | } |
1079 | | |
1080 | | PROTO_TOSTRING(FieldList, fieldlist) |
1081 | 11.2k | { |
1082 | 11.2k | std::string fieldlist_str = FieldToString(fieldlist.firstfield()); |
1083 | 23.5k | for (int i = 0; i < fieldlist.fields_size(); ++i) |
1084 | 12.2k | fieldlist_str += FieldWithFieldSepToString(fieldlist.fields(i)); |
1085 | 11.2k | if (fieldlist.has_lastsep()) |
1086 | 2.79k | fieldlist_str += FieldSepToString(fieldlist.lastsep()); |
1087 | 11.2k | return fieldlist_str; |
1088 | 11.2k | } |
1089 | | |
1090 | | NESTED_PROTO_TOSTRING(FieldWithFieldSep, field, FieldList) |
1091 | 12.2k | { |
1092 | 12.2k | std::string field_str = FieldSepToString(field.sep()); |
1093 | 12.2k | field_str += " " + FieldToString(field.field()); |
1094 | 12.2k | return field_str; |
1095 | 12.2k | } |
1096 | | |
1097 | | /** |
1098 | | * Field and nested types. |
1099 | | */ |
1100 | | PROTO_TOSTRING(Field, field) |
1101 | 23.5k | { |
1102 | 23.5k | using FieldType = Field::FieldOneofCase; |
1103 | 23.5k | switch (field.field_oneof_case()) { |
1104 | 6.44k | case FieldType::kExprassign: |
1105 | 6.44k | return ExpressionAssignmentToString(field.exprassign()); |
1106 | 1.14k | case FieldType::kNamedassign: |
1107 | 1.14k | return NameAssignmentToString(field.namedassign()); |
1108 | 6.23k | case FieldType::kExpression: |
1109 | 6.23k | return ExpressionToString(field.expression()); |
1110 | 9.72k | default: |
1111 | | /* More common case of using fields. */ |
1112 | 9.72k | return NameAssignmentToString(field.namedassign()); |
1113 | 23.5k | } |
1114 | 23.5k | } |
1115 | | |
1116 | | NESTED_PROTO_TOSTRING(ExpressionAssignment, assignment, Field) |
1117 | 6.44k | { |
1118 | | /* Prevent error 'table index is nil' and 'table index is NaN'. */ |
1119 | 6.44k | std::string assignment_str = "[ " + |
1120 | 6.44k | AllowedIndexExpressionToString(assignment.key()) + " ]"; |
1121 | 6.44k | assignment_str += " = " + ExpressionToString(assignment.value()); |
1122 | 6.44k | return assignment_str; |
1123 | 6.44k | } |
1124 | | |
1125 | | NESTED_PROTO_TOSTRING(NameAssignment, assignment, Field) |
1126 | 10.8k | { |
1127 | 10.8k | std::string assignment_str = NameToString(assignment.name()); |
1128 | 10.8k | assignment_str += " = " + ExpressionToString(assignment.value()); |
1129 | 10.8k | return assignment_str; |
1130 | 10.8k | } |
1131 | | |
1132 | | PROTO_TOSTRING(FieldSep, sep) |
1133 | 15.0k | { |
1134 | 15.0k | using FieldSepType = FieldSep::SepOneofCase; |
1135 | 15.0k | switch (sep.sep_oneof_case()) { |
1136 | 3.16k | case FieldSepType::kComma: |
1137 | 3.16k | return ","; |
1138 | 2.18k | case FieldSepType::kSemicolon: |
1139 | 2.18k | return ";"; |
1140 | 9.73k | default: |
1141 | 9.73k | return ","; |
1142 | 15.0k | } |
1143 | 15.0k | } |
1144 | | |
1145 | | /** |
1146 | | * Operators. |
1147 | | */ |
1148 | | PROTO_TOSTRING(BinaryOperator, op) |
1149 | 90.1k | { |
1150 | 90.1k | using BinopType = BinaryOperator::BinaryOneofCase; |
1151 | 90.1k | switch (op.binary_oneof_case()) { |
1152 | 4.76k | case BinopType::kAdd: |
1153 | 4.76k | return "+"; |
1154 | 2.12k | case BinopType::kSub: |
1155 | 2.12k | return "-"; |
1156 | 11.2k | case BinopType::kMult: |
1157 | 11.2k | return "*"; |
1158 | 5.45k | case BinopType::kDiv: |
1159 | 5.45k | return "/"; |
1160 | 1.98k | case BinopType::kExp: |
1161 | 1.98k | return "^"; |
1162 | 2.64k | case BinopType::kMod: |
1163 | 2.64k | return "%"; |
1164 | | |
1165 | 29.8k | case BinopType::kConcat: |
1166 | 29.8k | return ".."; |
1167 | | |
1168 | 1.55k | case BinopType::kLess: |
1169 | 1.55k | return "<"; |
1170 | 899 | case BinopType::kLessEqual: |
1171 | 899 | return "<="; |
1172 | 707 | case BinopType::kGreater: |
1173 | 707 | return ">"; |
1174 | 582 | case BinopType::kGreaterEqual: |
1175 | 582 | return ">="; |
1176 | 600 | case BinopType::kEqual: |
1177 | 600 | return "=="; |
1178 | 846 | case BinopType::kNotEqual: |
1179 | 846 | return "~="; |
1180 | 1.05k | case BinopType::kAnd: |
1181 | 1.05k | return "and"; |
1182 | 2.98k | case BinopType::kOr: |
1183 | 2.98k | return "or"; |
1184 | 22.7k | default: |
1185 | | /* Works in most cases. */ |
1186 | 22.7k | return "=="; |
1187 | 90.1k | } |
1188 | 90.1k | } |
1189 | | |
1190 | | PROTO_TOSTRING(UnaryOperator, op) |
1191 | 10.3k | { |
1192 | 10.3k | using UnaryopType = UnaryOperator::UnaryOneofCase; |
1193 | 10.3k | switch (op.unary_oneof_case()) { |
1194 | 3.03k | case UnaryopType::kNegate: |
1195 | 3.03k | return "-"; |
1196 | 840 | case UnaryopType::kNot: |
1197 | 840 | return "not "; |
1198 | 2.26k | case UnaryopType::kLength: |
1199 | 2.26k | return "#"; |
1200 | 4.24k | default: |
1201 | | /* Works in most cases. */ |
1202 | 4.24k | return "not "; |
1203 | 10.3k | } |
1204 | 10.3k | } |
1205 | | |
1206 | | /** |
1207 | | * Identifier (Name). |
1208 | | */ |
1209 | | PROTO_TOSTRING(Name, name) |
1210 | 334k | { |
1211 | 334k | std::string ident = ConvertToStringDefault(name.name(), true); |
1212 | | /* Prevent using reserved keywords as identifiers. */ |
1213 | 334k | if (KReservedLuaKeywords.find(ident) != KReservedLuaKeywords.end()) { |
1214 | 80 | ident += "_1"; |
1215 | 80 | } |
1216 | | /* Identifier has default name, add an index. */ |
1217 | 334k | if (!ident.compare(kDefaultIdent)) { |
1218 | 304k | ident += std::to_string(name.num() % kMaxIdentifiers); |
1219 | 304k | } |
1220 | 334k | return ident; |
1221 | 334k | } |
1222 | | |
1223 | | } /* namespace */ |
1224 | | |
1225 | | std::string |
1226 | | MainBlockToString(const Block &block) |
1227 | 17.3k | { |
1228 | 17.3k | GetCounterIdProvider().clean(); |
1229 | | |
1230 | 17.3k | std::string block_str = BlockToString(block); |
1231 | 17.3k | std::string retval = preamble_lua; |
1232 | | |
1233 | 142k | for (size_t i = 0; i < GetCounterIdProvider().count(); ++i) { |
1234 | 124k | retval += GetCounterName(i); |
1235 | 124k | retval += " = 0\n"; |
1236 | 124k | } |
1237 | 17.3k | retval += block_str; |
1238 | | |
1239 | 17.3k | return retval; |
1240 | 17.3k | } |
1241 | | |
1242 | | } /* namespace luajit_fuzzer */ |