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