Coverage Report

Created: 2026-01-09 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
14.3k
{
161
14.3k
  std::string retval;
162
14.3k
  retval += kNumberWrapperName;
163
14.3k
  retval += "(";
164
14.3k
  retval += ExpressionToString(expr);
165
14.3k
  retval += ")";
166
167
14.3k
  return retval;
168
14.3k
}
169
170
std::string
171
AllowedIndexExpressionToString(const Expression &expr)
172
5.29k
{
173
5.29k
  std::string retval;
174
5.29k
  retval += kNotNaNAndNilWrapperName;
175
5.29k
  retval += "(";
176
5.29k
  retval += ExpressionToString(expr);
177
5.29k
  retval += ")";
178
5.29k
  return retval;
179
5.29k
}
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
137k
  {
196
137k
    return id_++;
197
137k
  }
198
199
  /**
200
   * Cleans history. Should be used to make fuzzer starts
201
   * independent.
202
   */
203
  void clean()
204
3.53k
  {
205
3.53k
    id_ = 0;
206
3.53k
  }
207
208
private:
209
  std::size_t id_ = 0;
210
};
211
212
/** A singleton for counter id provider. */
213
CounterIdProvider&
214
GetCounterIdProvider()
215
282k
{
216
282k
  static CounterIdProvider provider;
217
282k
  return provider;
218
282k
}
219
220
std::string
221
GetCounterName(std::size_t id)
222
275k
{
223
275k
  return kCounterNamePrefix + std::to_string(id);
224
275k
}
225
226
/** Returns `<counter_name> = <counter_name> + 1`. */
227
std::string
228
GetCounterIncrement(const std::string &counter_name)
229
137k
{
230
137k
  std::string retval = counter_name;
231
137k
  retval += " = ";
232
137k
  retval += counter_name;
233
137k
  retval += " + 1;\n";
234
137k
  return retval;
235
137k
}
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
137k
{
244
137k
  std::string retval = "if ";
245
137k
  retval += counter_name;
246
137k
  retval += " > ";
247
137k
  retval += std::to_string(kMaxCounterValue);
248
137k
  retval += " then ";
249
137k
  retval += then_block;
250
137k
  retval += " end\n";
251
137k
  return retval;
252
137k
}
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
137k
  {
269
137k
    block_stack_.push(type);
270
137k
    if (block_type_is_returnable_(type)) {
271
51.0k
      returnable_stack_.push(type);
272
51.0k
    }
273
137k
  }
274
275
  void step_out()
276
137k
  {
277
137k
    assert(!block_stack_.empty());
278
137k
    if (block_type_is_returnable_(block_stack_.top())) {
279
51.0k
      assert(!returnable_stack_.empty());
280
51.0k
      returnable_stack_.pop();
281
51.0k
    }
282
137k
    block_stack_.pop();
283
137k
  }
284
285
  std::string get_next_block_setup()
286
137k
  {
287
137k
    std::size_t id = GetCounterIdProvider().next();
288
137k
    std::string counter_name = GetCounterName(id);
289
290
137k
    return GetCondition(counter_name, get_exit_statement_()) +
291
137k
           GetCounterIncrement(counter_name);
292
137k
  }
293
294
  bool break_is_possible()
295
3.92k
  {
296
3.92k
    return !block_stack_.empty() &&
297
3.14k
           block_stack_.top() == BlockType::kBreakable;
298
3.92k
  }
299
300
  bool return_is_possible()
301
15.1k
  {
302
15.1k
    return !returnable_stack_.empty();
303
15.1k
  }
304
305
  bool vararg_is_possible()
306
6.30k
  {
307
6.30k
    return (returnable_stack_.empty() ||
308
1.59k
      (!returnable_stack_.empty() &&
309
1.59k
       returnable_stack_.top() ==
310
1.59k
        BlockType::kReturnableWithVararg));
311
6.30k
  }
312
313
private:
314
315
  bool block_type_is_returnable_(BlockType type)
316
275k
  {
317
275k
    switch (type) {
318
173k
    case BlockType::kBreakable:
319
173k
      return false;
320
97.1k
    case BlockType::kReturnable:
321
102k
    case BlockType::kReturnableWithVararg:
322
102k
      return true;
323
275k
    }
324
0
    unreachable();
325
0
  }
326
327
  std::string get_exit_statement_()
328
137k
  {
329
137k
    assert(!block_stack_.empty());
330
137k
    switch (block_stack_.top()) {
331
86.5k
    case BlockType::kBreakable:
332
86.5k
      return "break";
333
48.5k
    case BlockType::kReturnable:
334
51.0k
    case BlockType::kReturnableWithVararg:
335
51.0k
      return "return";
336
137k
    }
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
438k
{
358
438k
  static Context context;
359
438k
  return context;
360
438k
}
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
86.5k
{
370
86.5k
  std::string retval = GetContext().get_next_block_setup();
371
86.5k
  retval += ChunkToString(block.chunk());
372
86.5k
  return retval;
373
86.5k
}
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.0k
{
397
51.0k
  std::string body_str = "( ";
398
51.0k
  if (body.has_parlist()) {
399
8.92k
    body_str += ParListToString(body.parlist());
400
8.92k
  }
401
51.0k
  body_str += " )\n\t";
402
403
51.0k
  body_str += GetContext().get_next_block_setup();
404
405
51.0k
  body_str += BlockToString(body.block());
406
51.0k
  body_str += "end\n";
407
51.0k
  return body_str;
408
51.0k
}
409
410
bool
411
FuncBodyHasVararg(const FuncBody &body)
412
51.0k
{
413
51.0k
  if (!body.has_parlist()) {
414
42.1k
    return false;
415
42.1k
  }
416
8.92k
  const FuncBody::ParList &parlist = body.parlist();
417
8.92k
  switch (parlist.parlist_oneof_case()) {
418
1.53k
  case FuncBody::ParList::ParlistOneofCase::kNamelist:
419
1.53k
    return parlist.namelist().has_ellipsis();
420
1.52k
  case FuncBody::ParList::ParlistOneofCase::kEllipsis:
421
1.52k
    return true;
422
5.86k
  default:
423
5.86k
    return parlist.namelist().has_ellipsis();
424
8.92k
  }
425
8.92k
}
426
427
Context::BlockType
428
GetFuncBodyType(const FuncBody &body)
429
51.0k
{
430
51.0k
  return FuncBodyHasVararg(body) ?
431
2.47k
    Context::BlockType::kReturnableWithVararg :
432
51.0k
    Context::BlockType::kReturnable;
433
51.0k
}
434
435
std::string
436
ClearIdentifier(const std::string &identifier)
437
304k
{
438
304k
  std::string cleared;
439
440
304k
  bool has_first_not_digit = false;
441
304k
  for (char c : identifier) {
442
178k
    if (has_first_not_digit && (std::iswalnum(c) || c == '_')) {
443
99.8k
      cleared += c;
444
99.8k
    } else if (std::isalpha(c) || c == '_') {
445
15.5k
      has_first_not_digit = true;
446
15.5k
      cleared += c;
447
62.8k
    } else {
448
62.8k
      cleared += '_';
449
62.8k
    }
450
178k
  }
451
304k
  return cleared;
452
304k
}
453
454
/** Drop any special symbols to avoid parser errors. */
455
std::string
456
ClearString(const std::string &str)
457
202k
{
458
202k
  std::string escaped;
459
202k
  int nbackslash = 0;
460
202k
  for (char c : str) {
461
132k
    if (c == '\\') {
462
2.90k
      nbackslash++;
463
129k
    } else {
464
129k
      if (!std::isprint(c))
465
11.2k
        continue;
466
118k
      nbackslash = 0;
467
118k
      if (c == '\'' || c == '\n')
468
9.62k
        escaped += '\\';
469
118k
    }
470
121k
    escaped += c;
471
121k
  }
472
  /* Avoid escaping the ending quote. */
473
202k
  if (nbackslash & 1)
474
64
    escaped += '\\';
475
202k
  return escaped;
476
202k
}
477
478
inline std::string
479
clamp(std::string s, size_t maxSize = kMaxStrLength)
480
506k
{
481
506k
  if (s.size() > maxSize)
482
6.10k
    s.resize(maxSize);
483
506k
  return s;
484
506k
}
485
486
inline double
487
clamp(double number, double upper, double lower)
488
19.2k
{
489
19.2k
  return number <= lower ? lower :
490
19.2k
         number >= upper ? upper : number;
491
19.2k
}
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
506k
{
500
506k
  std::string str = clamp(s);
501
506k
  if (is_identifier)
502
304k
    str = ClearIdentifier(str);
503
202k
  else
504
202k
    str = ClearString(str);
505
506k
  if (is_identifier && str.empty())
506
281k
    str = std::string(kDefaultIdent);
507
506k
  return str;
508
506k
}
509
510
PROTO_TOSTRING(Block, block)
511
78.3k
{
512
78.3k
  return ChunkToString(block.chunk());
513
78.3k
}
514
515
PROTO_TOSTRING(Chunk, chunk)
516
164k
{
517
164k
  std::string chunk_str;
518
406k
  for (int i = 0; i < chunk.stat_size(); ++i)
519
241k
    chunk_str += StatementToString(chunk.stat(i)) + "\n";
520
521
164k
  if (chunk.has_laststat())
522
19.0k
    chunk_str += LastStatementToString(chunk.laststat()) + "\n";
523
524
164k
  return chunk_str;
525
164k
}
526
527
/**
528
 * LastStatement and nested types.
529
 */
530
PROTO_TOSTRING(LastStatement, laststat)
531
19.0k
{
532
19.0k
  std::string laststat_str;
533
19.0k
  using LastStatType = LastStatement::LastOneofCase;
534
19.0k
  switch (laststat.last_oneof_case()) {
535
5.96k
  case LastStatType::kExplist:
536
5.96k
    laststat_str = ReturnOptionalExpressionListToString(
537
5.96k
      laststat.explist());
538
5.96k
    break;
539
3.92k
  case LastStatType::kBreak:
540
3.92k
    if (GetContext().break_is_possible()) {
541
1.92k
      laststat_str = "break";
542
1.92k
    }
543
3.92k
    break;
544
9.21k
  default:
545
    /* Chosen as default in order to decrease number of 'break's. */
546
9.21k
    laststat_str = ReturnOptionalExpressionListToString(
547
9.21k
      laststat.explist());
548
9.21k
    break;
549
19.0k
  }
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.0k
  if (!laststat_str.empty())
560
10.5k
    laststat_str += "; ";
561
562
19.0k
  return laststat_str;
563
19.0k
}
564
565
NESTED_PROTO_TOSTRING(ReturnOptionalExpressionList, explist, LastStatement)
566
15.1k
{
567
15.1k
  if (!GetContext().return_is_possible()) {
568
6.50k
    return "";
569
6.50k
  }
570
571
8.66k
  std::string explist_str = "return";
572
8.66k
  if (explist.has_explist()) {
573
4.36k
    explist_str += " " + ExpressionListToString(explist.explist());
574
4.36k
    explist_str += " ";
575
4.36k
  }
576
8.66k
  return explist_str;
577
15.1k
}
578
579
/**
580
 * Statement and statement options.
581
 */
582
PROTO_TOSTRING(Statement, stat)
583
241k
{
584
241k
  std::string stat_str;
585
241k
  using StatType = Statement::StatOneofCase;
586
241k
  switch (stat.stat_oneof_case()) {
587
15.0k
  case StatType::kList:
588
15.0k
    stat_str = AssignmentListToString(stat.list());
589
15.0k
    break;
590
54.7k
  case StatType::kCall:
591
54.7k
    stat_str = FunctionCallToString(stat.call());
592
54.7k
    break;
593
2.73k
  case StatType::kBlock:
594
2.73k
    stat_str = DoBlockToString(stat.block());
595
2.73k
    break;
596
6.47k
  case StatType::kWhilecycle:
597
6.47k
    stat_str = WhileCycleToString(stat.whilecycle());
598
6.47k
    break;
599
55.3k
  case StatType::kRepeatcycle:
600
55.3k
    stat_str = RepeatCycleToString(stat.repeatcycle());
601
55.3k
    break;
602
10.1k
  case StatType::kIfstat:
603
10.1k
    stat_str = IfStatementToString(stat.ifstat());
604
10.1k
    break;
605
5.66k
  case StatType::kForcyclename:
606
5.66k
    stat_str = ForCycleNameToString(stat.forcyclename());
607
5.66k
    break;
608
19.0k
  case StatType::kForcyclelist:
609
19.0k
    stat_str = ForCycleListToString(stat.forcyclelist());
610
19.0k
    break;
611
6.20k
  case StatType::kFunc:
612
6.20k
    stat_str = FunctionToString(stat.func());
613
6.20k
    break;
614
10.8k
  case StatType::kLocalfunc:
615
10.8k
    stat_str = LocalFuncToString(stat.localfunc());
616
10.8k
    break;
617
11.1k
  case StatType::kLocalnames:
618
11.1k
    stat_str = LocalNamesToString(stat.localnames());
619
11.1k
    break;
620
44.0k
  default:
621
    /**
622
     * Chosen arbitrarily more for simplicity.
623
     * TODO: Choose "more interesting" defaults.
624
     */
625
44.0k
    stat_str = AssignmentListToString(stat.list());
626
44.0k
    break;
627
241k
  }
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
241k
  stat_str += "; ";
638
639
241k
  return stat_str;
640
241k
}
641
642
/**
643
 * AssignmentList and nested types.
644
 */
645
PROTO_TOSTRING(AssignmentList, assignmentlist)
646
59.0k
{
647
59.0k
  std::string list_str = VariableListToString(assignmentlist.varlist());
648
59.0k
  list_str += " = " + ExpressionListToString(assignmentlist.explist());
649
59.0k
  return list_str;
650
59.0k
}
651
652
NESTED_PROTO_TOSTRING(VariableList, varlist, AssignmentList)
653
59.0k
{
654
59.0k
  std::string varlist_str = VariableToString(varlist.var());
655
77.8k
  for (int i = 0; i < varlist.vars_size(); ++i) {
656
18.7k
    varlist_str += ", " + VariableToString(varlist.vars(i));
657
18.7k
    varlist_str += " ";
658
18.7k
  }
659
59.0k
  return varlist_str;
660
59.0k
}
661
662
/**
663
 * FunctionCall and nested types.
664
 */
665
PROTO_TOSTRING(FunctionCall, call)
666
68.5k
{
667
68.5k
  using FuncCallType = FunctionCall::CallOneofCase;
668
68.5k
  switch (call.call_oneof_case()) {
669
10.9k
  case FuncCallType::kPrefArgs:
670
10.9k
    return PrefixArgsToString(call.prefargs());
671
5.20k
  case FuncCallType::kNamedArgs:
672
5.20k
    return PrefixNamedArgsToString(call.namedargs());
673
52.4k
  default:
674
    /* Chosen for more variability of generated programs. */
675
52.4k
    return PrefixNamedArgsToString(call.namedargs());
676
68.5k
  }
677
68.5k
}
678
679
NESTED_PROTO_TOSTRING(Args, args, FunctionCall)
680
68.5k
{
681
68.5k
  using ArgsType = FunctionCall::Args::ArgsOneofCase;
682
68.5k
  switch (args.args_oneof_case()) {
683
5.12k
  case ArgsType::kExplist:
684
5.12k
    return "(" + OptionalExpressionListToString(args.explist()) +
685
5.12k
           ")";
686
1.42k
  case ArgsType::kTableconstructor:
687
1.42k
    return TableConstructorToString(args.tableconstructor());
688
2.86k
  case ArgsType::kStr:
689
2.86k
    return "'" + ConvertToStringDefault(args.str()) + "'";
690
59.1k
  default:
691
    /* For more variability. */
692
59.1k
    return TableConstructorToString(args.tableconstructor());
693
68.5k
  }
694
68.5k
}
695
696
NESTED_PROTO_TOSTRING(PrefixArgs, prefixargs, FunctionCall)
697
10.9k
{
698
10.9k
  std::string prefixargs_str = PrefixExpressionToString(
699
10.9k
    prefixargs.prefixexp());
700
10.9k
  prefixargs_str += " " + ArgsToString(prefixargs.args());
701
10.9k
  return prefixargs_str;
702
10.9k
}
703
704
NESTED_PROTO_TOSTRING(PrefixNamedArgs, prefixnamedargs, FunctionCall)
705
57.6k
{
706
57.6k
  std::string predixnamedargs_str = PrefixExpressionToString(
707
57.6k
    prefixnamedargs.prefixexp());
708
57.6k
  predixnamedargs_str += ":" + NameToString(prefixnamedargs.name());
709
57.6k
  predixnamedargs_str += " " + ArgsToString(prefixnamedargs.args());
710
57.6k
  return predixnamedargs_str;
711
57.6k
}
712
713
/**
714
 * DoBlock clause.
715
 */
716
PROTO_TOSTRING(DoBlock, block)
717
2.73k
{
718
2.73k
  return "do\n" + BlockToString(block.block()) + "end\n";
719
2.73k
}
720
721
/**
722
 * WhileCycle clause.
723
 */
724
PROTO_TOSTRING(WhileCycle, whilecycle)
725
6.47k
{
726
6.47k
  GetContext().step_in(Context::BlockType::kBreakable);
727
728
6.47k
  std::string whilecycle_str = "while ";
729
6.47k
  whilecycle_str += ExpressionToString(whilecycle.condition());
730
6.47k
  whilecycle_str += " ";
731
6.47k
  whilecycle_str += DoBlockToStringCycleProtected(whilecycle.doblock());
732
733
6.47k
  GetContext().step_out();
734
6.47k
  return whilecycle_str;
735
6.47k
}
736
737
/**
738
 * RepeatCycle clause.
739
 */
740
PROTO_TOSTRING(RepeatCycle, repeatcycle)
741
55.3k
{
742
55.3k
  GetContext().step_in(Context::BlockType::kBreakable);
743
744
55.3k
  std::string repeatcycle_str = "repeat\n";
745
55.3k
  repeatcycle_str += BlockToStringCycleProtected(repeatcycle.block());
746
55.3k
  repeatcycle_str += "until ";
747
55.3k
  repeatcycle_str += ExpressionToString(repeatcycle.condition());
748
749
55.3k
  GetContext().step_out();
750
55.3k
  return repeatcycle_str;
751
55.3k
}
752
753
/**
754
 * IfStatement and nested types.
755
 */
756
PROTO_TOSTRING(IfStatement, statement)
757
10.1k
{
758
10.1k
  std::string statement_str = "if " +
759
10.1k
    ExpressionToString(statement.condition());
760
10.1k
  statement_str += " then\n\t" + BlockToString(statement.first());
761
762
16.7k
  for (int i = 0; i < statement.clauses_size(); ++i)
763
6.58k
    statement_str += ElseIfBlockToString(statement.clauses(i));
764
765
10.1k
  if (statement.has_last())
766
4.27k
    statement_str += "else\n\t" + BlockToString(statement.last());
767
768
10.1k
  statement_str += "end\n";
769
10.1k
  return statement_str;
770
10.1k
}
771
772
NESTED_PROTO_TOSTRING(ElseIfBlock, elseifblock, IfStatement)
773
6.58k
{
774
6.58k
  std::string elseifblock_str = "elseif ";
775
6.58k
  elseifblock_str += ExpressionToString(elseifblock.condition());
776
6.58k
  elseifblock_str += " then\n\t";
777
6.58k
  elseifblock_str += BlockToString(elseifblock.block());
778
6.58k
  return elseifblock_str;
779
6.58k
}
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
5.66k
{
789
5.66k
  GetContext().step_in(Context::BlockType::kBreakable);
790
791
5.66k
  std::string forcyclename_str = "for ";
792
5.66k
  forcyclename_str += NameToString(forcyclename.name());
793
5.66k
  forcyclename_str += " = ";
794
5.66k
  forcyclename_str += NumberWrappedExpressionToString(
795
5.66k
    forcyclename.startexp());
796
5.66k
  forcyclename_str += ", ";
797
5.66k
  forcyclename_str += NumberWrappedExpressionToString(
798
5.66k
    forcyclename.stopexp());
799
800
5.66k
  if (forcyclename.has_stepexp())
801
2.98k
    forcyclename_str += ", " + NumberWrappedExpressionToString(
802
2.98k
      forcyclename.stepexp());
803
804
5.66k
  forcyclename_str += " ";
805
5.66k
  forcyclename_str += DoBlockToStringCycleProtected(
806
5.66k
    forcyclename.doblock());
807
808
5.66k
  GetContext().step_out();
809
5.66k
  return forcyclename_str;
810
5.66k
}
811
812
/**
813
 * ForCycleList clause.
814
 */
815
PROTO_TOSTRING(ForCycleList, forcyclelist)
816
19.0k
{
817
19.0k
  GetContext().step_in(Context::BlockType::kBreakable);
818
819
19.0k
  std::string forcyclelist_str = "for ";
820
19.0k
  forcyclelist_str += NameListToString(forcyclelist.names());
821
19.0k
  forcyclelist_str += " in ";
822
19.0k
  forcyclelist_str += ExpressionListToString(forcyclelist.expressions());
823
19.0k
  forcyclelist_str += " ";
824
19.0k
  forcyclelist_str += DoBlockToStringCycleProtected(
825
19.0k
    forcyclelist.doblock());
826
827
19.0k
  GetContext().step_out();
828
19.0k
  return forcyclelist_str;
829
19.0k
}
830
831
/**
832
 * Function and nested types.
833
 */
834
PROTO_TOSTRING(Function, func)
835
6.20k
{
836
6.20k
  GetContext().step_in(GetFuncBodyType(func.body()));
837
838
6.20k
  std::string func_str = "function ";
839
6.20k
  func_str += FuncNameToString(func.name());
840
6.20k
  func_str += FuncBodyToStringReqProtected(func.body());
841
842
6.20k
  GetContext().step_out();
843
6.20k
  return func_str;
844
6.20k
}
845
846
NESTED_PROTO_TOSTRING(FuncName, funcname, Function)
847
6.20k
{
848
6.20k
  std::string funcname_str = NameToString(funcname.firstname());
849
850
7.33k
  for (int i = 0; i < funcname.names_size(); ++i)
851
1.13k
    funcname_str += "." + NameToString(funcname.names(i));
852
853
6.20k
  if (funcname.has_lastname())
854
581
    funcname_str += ":" + NameToString(funcname.lastname());
855
856
6.20k
  return funcname_str;
857
6.20k
}
858
859
PROTO_TOSTRING(NameList, namelist)
860
37.6k
{
861
37.6k
  std::string namelist_str = NameToString(namelist.firstname());
862
50.9k
  for (int i = 0; i < namelist.names_size(); ++i)
863
13.3k
    namelist_str += ", " + NameToString(namelist.names(i));
864
37.6k
  return namelist_str;
865
37.6k
}
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
942
    namelist_str += ", ...";
872
7.39k
  return namelist_str;
873
7.39k
}
874
875
NESTED_PROTO_TOSTRING(ParList, parlist, FuncBody)
876
8.92k
{
877
8.92k
  using ParListType = FuncBody::ParList::ParlistOneofCase;
878
8.92k
  switch (parlist.parlist_oneof_case()) {
879
1.53k
  case ParListType::kNamelist:
880
1.53k
    return NameListWithEllipsisToString(parlist.namelist());
881
1.52k
  case ParListType::kEllipsis:
882
1.52k
    return "...";
883
5.86k
  default:
884
    /* Chosen as default in order to decrease number of ellipses. */
885
5.86k
    return NameListWithEllipsisToString(parlist.namelist());
886
8.92k
  }
887
8.92k
}
888
889
/**
890
 * LocalFunc clause.
891
 */
892
PROTO_TOSTRING(LocalFunc, localfunc)
893
10.8k
{
894
10.8k
  GetContext().step_in(GetFuncBodyType(localfunc.funcbody()));
895
896
10.8k
  std::string localfunc_str = "local function ";
897
10.8k
  localfunc_str += NameToString(localfunc.name());
898
10.8k
  localfunc_str += " ";
899
10.8k
  localfunc_str += FuncBodyToStringReqProtected(localfunc.funcbody());
900
901
10.8k
  GetContext().step_out();
902
10.8k
  return localfunc_str;
903
10.8k
}
904
905
/**
906
 * LocalNames clause.
907
 */
908
PROTO_TOSTRING(LocalNames, localnames)
909
11.1k
{
910
11.1k
  std::string localnames_str = "local ";
911
11.1k
  localnames_str += NameListToString(localnames.namelist());
912
913
11.1k
  if (localnames.has_explist())
914
8.69k
    localnames_str += " = " + ExpressionListToString(
915
8.69k
      localnames.explist());
916
11.1k
  return localnames_str;
917
11.1k
}
918
919
/**
920
 * Expressions and variables.
921
 */
922
923
/**
924
 * Expressions clauses.
925
 */
926
PROTO_TOSTRING(ExpressionList, explist)
927
96.0k
{
928
96.0k
  std::string explist_str;
929
144k
  for (int i = 0; i < explist.expressions_size(); ++i)
930
48.8k
    explist_str += ExpressionToString(explist.expressions(i)) +
931
48.8k
        ", ";
932
96.0k
  explist_str += ExpressionToString(explist.explast()) + " ";
933
96.0k
  return explist_str;
934
96.0k
}
935
936
PROTO_TOSTRING(OptionalExpressionList, explist)
937
5.12k
{
938
5.12k
  if (explist.has_explist())
939
4.84k
    return ExpressionListToString(explist.explist());
940
283
  return "";
941
5.12k
}
942
943
PROTO_TOSTRING(PrefixExpression, prefixexp)
944
109k
{
945
109k
  using PrefExprType = PrefixExpression::PrefixOneofCase;
946
109k
  switch (prefixexp.prefix_oneof_case()) {
947
10.9k
  case PrefExprType::kVar:
948
10.9k
    return VariableToString(prefixexp.var());
949
13.8k
  case PrefExprType::kFunctioncall:
950
13.8k
    return FunctionCallToString(prefixexp.functioncall());
951
7.40k
  case PrefExprType::kExp:
952
7.40k
    return "(" + ExpressionToString(prefixexp.exp()) + ")";
953
77.7k
  default:
954
    /*
955
     * Can be generated too nested expressions with other options,
956
     * though they can be enabled for more variable fuzzing.
957
     */
958
77.7k
    return VariableToString(prefixexp.var());
959
109k
  }
960
109k
}
961
962
/**
963
 * Variable and nested types.
964
 */
965
PROTO_TOSTRING(Variable, var)
966
166k
{
967
166k
  using VarType = Variable::VarOneofCase;
968
166k
  switch (var.var_oneof_case()) {
969
10.7k
  case VarType::kName:
970
10.7k
    return NameToString(var.name());
971
6.63k
  case VarType::kIndexexpr:
972
6.63k
    return IndexWithExpressionToString(var.indexexpr());
973
4.17k
  case VarType::kIndexname:
974
4.17k
    return IndexWithNameToString(var.indexname());
975
144k
  default:
976
    /*
977
     * Can be generated too nested expressions with other options,
978
     * though they can be enabled for more variable fuzzing.
979
     */
980
144k
    return NameToString(var.name());
981
166k
  }
982
166k
}
983
984
NESTED_PROTO_TOSTRING(IndexWithExpression, indexexpr, Variable)
985
6.63k
{
986
6.63k
  std::string indexexpr_str = PrefixExpressionToString(
987
6.63k
    indexexpr.prefixexp());
988
6.63k
  indexexpr_str += "[" + ExpressionToString(indexexpr.exp()) + "]";
989
6.63k
  return indexexpr_str;
990
6.63k
}
991
992
NESTED_PROTO_TOSTRING(IndexWithName, indexname, Variable)
993
4.17k
{
994
4.17k
  std::string indexname_str = PrefixExpressionToString(
995
4.17k
    indexname.prefixexp());
996
4.17k
  std::string idx_str = ConvertToStringDefault(indexname.name(), true);
997
  /* Prevent using reserved keywords as indices. */
998
4.17k
  if (KReservedLuaKeywords.find(idx_str) != KReservedLuaKeywords.end()) {
999
39
    idx_str += "_1";
1000
39
  }
1001
4.17k
  indexname_str += "." + idx_str;
1002
4.17k
  return indexname_str;
1003
4.17k
}
1004
1005
/**
1006
 * Expression and nested types.
1007
 */
1008
PROTO_TOSTRING(Expression, expr)
1009
389k
{
1010
389k
  using ExprType = Expression::ExprOneofCase;
1011
389k
  switch (expr.expr_oneof_case()) {
1012
20.0k
  case ExprType::kNil:
1013
20.0k
    return "nil";
1014
5.14k
  case ExprType::kFalse:
1015
5.14k
    return "false";
1016
5.51k
  case ExprType::kTrue:
1017
5.51k
    return "true";
1018
19.2k
  case ExprType::kNumber: {
1019
    /* Clamp number between given boundaries. */
1020
19.2k
    double number = clamp(expr.number(), kMaxNumber, kMinNumber);
1021
19.2k
    return std::to_string(number);
1022
0
  }
1023
10.1k
  case ExprType::kStr:
1024
10.1k
    return "'" + ConvertToStringDefault(expr.str()) + "'";
1025
6.30k
  case ExprType::kEllipsis:
1026
6.30k
    if (GetContext().vararg_is_possible()) {
1027
5.23k
      return " ... ";
1028
5.23k
    } else {
1029
1.07k
      return " nil";
1030
1.07k
    }
1031
33.9k
  case ExprType::kFunction:
1032
33.9k
    return AnonFuncToString(expr.function());
1033
30.5k
  case ExprType::kPrefixexp:
1034
30.5k
    return PrefixExpressionToString(expr.prefixexp());
1035
10.3k
  case ExprType::kTableconstructor:
1036
10.3k
    return TableConstructorToString(expr.tableconstructor());
1037
51.5k
  case ExprType::kBinary:
1038
51.5k
    return ExpBinaryOpExpToString(expr.binary());
1039
7.59k
  case ExprType::kUnary:
1040
7.59k
    return UnaryOpExpToString(expr.unary());
1041
189k
  default:
1042
    /**
1043
     * Arbitrary choice.
1044
     * TODO: Choose "more interesting" defaults.
1045
     */
1046
189k
    return "'" + ConvertToStringDefault(expr.str()) + "'";
1047
389k
  }
1048
389k
}
1049
1050
NESTED_PROTO_TOSTRING(AnonFunc, func, Expression)
1051
33.9k
{
1052
33.9k
  GetContext().step_in(GetFuncBodyType(func.body()));
1053
1054
33.9k
  std::string retval = "function ";
1055
33.9k
  retval += FuncBodyToStringReqProtected(func.body());
1056
1057
33.9k
  GetContext().step_out();
1058
33.9k
  return retval;
1059
33.9k
}
1060
1061
NESTED_PROTO_TOSTRING(ExpBinaryOpExp, binary, Expression)
1062
51.5k
{
1063
51.5k
  std::string leftexp_str = ExpressionToString(binary.leftexp());
1064
51.5k
  std::string binop_str = BinaryOperatorToString(binary.binop());
1065
51.5k
  std::string rightexp_str = ExpressionToString(binary.rightexp());
1066
1067
51.5k
  std::string binary_str;
1068
51.5k
  if (binop_str == "<" ||
1069
49.3k
      binop_str == ">" ||
1070
48.8k
      binop_str == "<=" ||
1071
48.4k
      binop_str == ">=") {
1072
3.53k
    binary_str = kBinOpWrapperName;
1073
3.53k
    binary_str += "(" + leftexp_str;
1074
3.53k
    binary_str += ", '" + binop_str + "', ";
1075
3.53k
    binary_str += rightexp_str + ")";
1076
3.53k
    return binary_str;
1077
3.53k
  }
1078
1079
47.9k
  binary_str = leftexp_str;
1080
47.9k
  binary_str += " " + binop_str + " ";
1081
47.9k
  binary_str += rightexp_str;
1082
1083
47.9k
  return binary_str;
1084
51.5k
}
1085
1086
NESTED_PROTO_TOSTRING(UnaryOpExp, unary, Expression)
1087
7.59k
{
1088
7.59k
  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.59k
  unary_str += " " + ExpressionToString(unary.exp());
1095
7.59k
  return unary_str;
1096
7.59k
}
1097
1098
/**
1099
 * Tables and fields.
1100
 */
1101
PROTO_TOSTRING(TableConstructor, table)
1102
70.9k
{
1103
70.9k
  std::string table_str = " (setmetatable({ ";
1104
70.9k
  if (table.has_fieldlist())
1105
8.27k
    table_str += FieldListToString(table.fieldlist());
1106
70.9k
  table_str += " }, table_mt))()";
1107
70.9k
  return table_str;
1108
70.9k
}
1109
1110
PROTO_TOSTRING(FieldList, fieldlist)
1111
8.27k
{
1112
8.27k
  std::string fieldlist_str = FieldToString(fieldlist.firstfield());
1113
21.6k
  for (int i = 0; i < fieldlist.fields_size(); ++i)
1114
13.3k
    fieldlist_str += FieldWithFieldSepToString(fieldlist.fields(i));
1115
8.27k
  if (fieldlist.has_lastsep())
1116
2.01k
    fieldlist_str += FieldSepToString(fieldlist.lastsep());
1117
8.27k
  return fieldlist_str;
1118
8.27k
}
1119
1120
NESTED_PROTO_TOSTRING(FieldWithFieldSep, field, FieldList)
1121
13.3k
{
1122
13.3k
  std::string field_str = FieldSepToString(field.sep());
1123
13.3k
  field_str += " " + FieldToString(field.field());
1124
13.3k
  return field_str;
1125
13.3k
}
1126
1127
/**
1128
 * Field and nested types.
1129
 */
1130
PROTO_TOSTRING(Field, field)
1131
21.6k
{
1132
21.6k
  using FieldType = Field::FieldOneofCase;
1133
21.6k
  switch (field.field_oneof_case()) {
1134
5.29k
  case FieldType::kExprassign:
1135
5.29k
    return ExpressionAssignmentToString(field.exprassign());
1136
723
  case FieldType::kNamedassign:
1137
723
    return NameAssignmentToString(field.namedassign());
1138
4.70k
  case FieldType::kExpression:
1139
4.70k
    return ExpressionToString(field.expression());
1140
10.9k
  default:
1141
    /* More common case of using fields. */
1142
10.9k
    return NameAssignmentToString(field.namedassign());
1143
21.6k
  }
1144
21.6k
}
1145
1146
NESTED_PROTO_TOSTRING(ExpressionAssignment, assignment, Field)
1147
5.29k
{
1148
  /* Prevent error 'table index is nil' and 'table index is NaN'. */
1149
5.29k
  std::string assignment_str = "[ " +
1150
5.29k
    AllowedIndexExpressionToString(assignment.key()) + " ]";
1151
5.29k
  assignment_str += " = " + ExpressionToString(assignment.value());
1152
5.29k
  return assignment_str;
1153
5.29k
}
1154
1155
NESTED_PROTO_TOSTRING(NameAssignment, assignment, Field)
1156
11.6k
{
1157
11.6k
  std::string assignment_str = NameToString(assignment.name());
1158
11.6k
  assignment_str += " = " + ExpressionToString(assignment.value());
1159
11.6k
  return assignment_str;
1160
11.6k
}
1161
1162
PROTO_TOSTRING(FieldSep, sep)
1163
15.3k
{
1164
15.3k
  using FieldSepType = FieldSep::SepOneofCase;
1165
15.3k
  switch (sep.sep_oneof_case()) {
1166
4.48k
  case FieldSepType::kComma:
1167
4.48k
    return ",";
1168
1.62k
  case FieldSepType::kSemicolon:
1169
1.62k
    return ";";
1170
9.26k
  default:
1171
9.26k
    return ",";
1172
15.3k
  }
1173
15.3k
}
1174
1175
/**
1176
 * Operators.
1177
 */
1178
PROTO_TOSTRING(BinaryOperator, op)
1179
51.5k
{
1180
51.5k
  using BinopType = BinaryOperator::BinaryOneofCase;
1181
51.5k
  switch (op.binary_oneof_case()) {
1182
3.61k
  case BinopType::kAdd:
1183
3.61k
    return "+";
1184
2.11k
  case BinopType::kSub:
1185
2.11k
    return "-";
1186
5.00k
  case BinopType::kMult:
1187
5.00k
    return "*";
1188
1.35k
  case BinopType::kDiv:
1189
1.35k
    return "/";
1190
#if LUA_VERSION_NUM >= 503
1191
  case BinopType::kIDiv:
1192
    return "//";
1193
#endif
1194
916
  case BinopType::kExp:
1195
916
    return "^";
1196
1.55k
  case BinopType::kMod:
1197
1.55k
    return "%";
1198
1199
14.2k
  case BinopType::kConcat:
1200
14.2k
    return "..";
1201
1202
2.13k
  case BinopType::kLess:
1203
2.13k
    return "<";
1204
360
  case BinopType::kLessEqual:
1205
360
    return "<=";
1206
582
  case BinopType::kGreater:
1207
582
    return ">";
1208
458
  case BinopType::kGreaterEqual:
1209
458
    return ">=";
1210
272
  case BinopType::kEqual:
1211
272
    return "==";
1212
370
  case BinopType::kNotEqual:
1213
370
    return "~=";
1214
1.33k
  case BinopType::kAnd:
1215
1.33k
    return "and";
1216
6.02k
  case BinopType::kOr:
1217
6.02k
    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.1k
  default:
1232
    /* Works in most cases. */
1233
11.1k
    return "==";
1234
51.5k
  }
1235
51.5k
}
1236
1237
PROTO_TOSTRING(UnaryOperator, op)
1238
7.59k
{
1239
7.59k
  using UnaryopType = UnaryOperator::UnaryOneofCase;
1240
7.59k
  switch (op.unary_oneof_case()) {
1241
2.40k
  case UnaryopType::kNegate:
1242
2.40k
    return "-";
1243
625
  case UnaryopType::kNot:
1244
625
    return "not ";
1245
786
  case UnaryopType::kLength:
1246
786
    return "#";
1247
#if LUA_VERSION_NUM >= 503
1248
  case UnaryopType::kBNot:
1249
    return "~";
1250
#endif
1251
3.77k
  default:
1252
    /* Works in most cases. */
1253
3.77k
    return "not ";
1254
7.59k
  }
1255
7.59k
}
1256
1257
/**
1258
 * Identifier (Name).
1259
 */
1260
PROTO_TOSTRING(Name, name)
1261
300k
{
1262
300k
  std::string ident = ConvertToStringDefault(name.name(), true);
1263
  /* Prevent using reserved keywords as identifiers. */
1264
300k
  if (KReservedLuaKeywords.find(ident) != KReservedLuaKeywords.end()) {
1265
183
    ident += "_1";
1266
183
  }
1267
  /* Identifier has default name, add an index. */
1268
300k
  if (!ident.compare(kDefaultIdent)) {
1269
278k
    ident += std::to_string(name.num() % kMaxIdentifiers);
1270
278k
  }
1271
300k
  return ident;
1272
300k
}
1273
1274
} /* namespace */
1275
1276
std::string
1277
MainBlockToString(const Block &block)
1278
3.53k
{
1279
3.53k
  GetCounterIdProvider().clean();
1280
1281
3.53k
  std::string block_str = BlockToString(block);
1282
3.53k
  std::string retval = preamble_lua;
1283
1284
141k
  for (size_t i = 0; i < GetCounterIdProvider().count(); ++i) {
1285
137k
    retval += GetCounterName(i);
1286
137k
    retval += " = 0\n";
1287
137k
  }
1288
3.53k
  retval += block_str;
1289
1290
3.53k
  return retval;
1291
3.53k
}
1292
1293
} /* namespace luajit_fuzzer */