Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/polly/lib/External/isl/isl_printer.c
Line
Count
Source (jump to first uncovered line)
1
#include <string.h>
2
#include <isl_int.h>
3
#include <isl/id.h>
4
#include <isl/id_to_id.h>
5
#include <isl_printer_private.h>
6
7
static __isl_give isl_printer *file_start_line(__isl_take isl_printer *p)
8
0
{
9
0
  fprintf(p->file, "%s%*s%s", p->indent_prefix ? p->indent_prefix : "",
10
0
            p->indent, "", p->prefix ? p->prefix : "");
11
0
  return p;
12
0
}
13
14
static __isl_give isl_printer *file_end_line(__isl_take isl_printer *p)
15
0
{
16
0
  fprintf(p->file, "%s\n", p->suffix ? p->suffix : "");
17
0
  return p;
18
0
}
19
20
static __isl_give isl_printer *file_flush(__isl_take isl_printer *p)
21
0
{
22
0
  fflush(p->file);
23
0
  return p;
24
0
}
25
26
static __isl_give isl_printer *file_print_str(__isl_take isl_printer *p,
27
  const char *s)
28
0
{
29
0
  fprintf(p->file, "%s", s);
30
0
  return p;
31
0
}
32
33
static __isl_give isl_printer *file_print_double(__isl_take isl_printer *p,
34
  double d)
35
0
{
36
0
  fprintf(p->file, "%g", d);
37
0
  return p;
38
0
}
39
40
static __isl_give isl_printer *file_print_int(__isl_take isl_printer *p, int i)
41
0
{
42
0
  fprintf(p->file, "%d", i);
43
0
  return p;
44
0
}
45
46
static __isl_give isl_printer *file_print_isl_int(__isl_take isl_printer *p, isl_int i)
47
0
{
48
0
  isl_int_print(p->file, i, p->width);
49
0
  return p;
50
0
}
51
52
static int grow_buf(__isl_keep isl_printer *p, int extra)
53
573
{
54
573
  int new_size;
55
573
  char *new_buf;
56
573
57
573
  if (p->buf_size == 0)
58
0
    return -1;
59
573
60
573
  new_size = ((p->buf_n + extra + 1) * 3) / 2;
61
573
  new_buf = isl_realloc_array(p->ctx, p->buf, char, new_size);
62
573
  if (!new_buf) {
63
0
    p->buf_size = 0;
64
0
    return -1;
65
0
  }
66
573
  p->buf = new_buf;
67
573
  p->buf_size = new_size;
68
573
69
573
  return 0;
70
573
}
71
72
static __isl_give isl_printer *str_print(__isl_take isl_printer *p,
73
  const char *s, int len)
74
239k
{
75
239k
  if (p->buf_n + len + 1 >= p->buf_size && 
grow_buf(p, len)540
)
76
0
    goto error;
77
239k
  memcpy(p->buf + p->buf_n, s, len);
78
239k
  p->buf_n += len;
79
239k
80
239k
  p->buf[p->buf_n] = '\0';
81
239k
  return p;
82
0
error:
83
0
  isl_printer_free(p);
84
0
  return NULL;
85
239k
}
86
87
static __isl_give isl_printer *str_print_indent(__isl_take isl_printer *p,
88
  int indent)
89
2.98k
{
90
2.98k
  int i;
91
2.98k
92
2.98k
  if (p->buf_n + indent + 1 >= p->buf_size && 
grow_buf(p, indent)33
)
93
0
    goto error;
94
47.3k
  
for (i = 0; 2.98k
i < indent;
++i44.3k
)
95
44.3k
    p->buf[p->buf_n++] = ' ';
96
2.98k
  return p;
97
0
error:
98
0
  isl_printer_free(p);
99
0
  return NULL;
100
2.98k
}
101
102
static __isl_give isl_printer *str_start_line(__isl_take isl_printer *p)
103
2.98k
{
104
2.98k
  if (p->indent_prefix)
105
0
    p = str_print(p, p->indent_prefix, strlen(p->indent_prefix));
106
2.98k
  p = str_print_indent(p, p->indent);
107
2.98k
  if (p->prefix)
108
0
    p = str_print(p, p->prefix, strlen(p->prefix));
109
2.98k
  return p;
110
2.98k
}
111
112
static __isl_give isl_printer *str_end_line(__isl_take isl_printer *p)
113
2.98k
{
114
2.98k
  if (p->suffix)
115
0
    p = str_print(p, p->suffix, strlen(p->suffix));
116
2.98k
  p = str_print(p, "\n", strlen("\n"));
117
2.98k
  return p;
118
2.98k
}
119
120
static __isl_give isl_printer *str_flush(__isl_take isl_printer *p)
121
154
{
122
154
  p->buf_n = 0;
123
154
  p->buf[p->buf_n] = '\0';
124
154
  return p;
125
154
}
126
127
static __isl_give isl_printer *str_print_str(__isl_take isl_printer *p,
128
  const char *s)
129
219k
{
130
219k
  return str_print(p, s, strlen(s));
131
219k
}
132
133
static __isl_give isl_printer *str_print_double(__isl_take isl_printer *p,
134
  double d)
135
0
{
136
0
  int left = p->buf_size - p->buf_n;
137
0
  int need = snprintf(p->buf + p->buf_n, left, "%g", d);
138
0
  if (need >= left) {
139
0
    if (grow_buf(p, need))
140
0
      goto error;
141
0
    left = p->buf_size - p->buf_n;
142
0
    need = snprintf(p->buf + p->buf_n, left, "%g", d);
143
0
  }
144
0
  p->buf_n += need;
145
0
  return p;
146
0
error:
147
0
  isl_printer_free(p);
148
0
  return NULL;
149
0
}
150
151
static __isl_give isl_printer *str_print_int(__isl_take isl_printer *p, int i)
152
0
{
153
0
  int left = p->buf_size - p->buf_n;
154
0
  int need = snprintf(p->buf + p->buf_n, left, "%d", i);
155
0
  if (need >= left) {
156
0
    if (grow_buf(p, need))
157
0
      goto error;
158
0
    left = p->buf_size - p->buf_n;
159
0
    need = snprintf(p->buf + p->buf_n, left, "%d", i);
160
0
  }
161
0
  p->buf_n += need;
162
0
  return p;
163
0
error:
164
0
  isl_printer_free(p);
165
0
  return NULL;
166
0
}
167
168
static __isl_give isl_printer *str_print_isl_int(__isl_take isl_printer *p,
169
  isl_int i)
170
17.7k
{
171
17.7k
  char *s;
172
17.7k
  int len;
173
17.7k
174
17.7k
  s = isl_int_get_str(i);
175
17.7k
  len = strlen(s);
176
17.7k
  if (len < p->width)
177
0
    p = str_print_indent(p, p->width - len);
178
17.7k
  p = str_print(p, s, len);
179
17.7k
  isl_int_free_str(s);
180
17.7k
  return p;
181
17.7k
}
182
183
struct isl_printer_ops {
184
  __isl_give isl_printer *(*start_line)(__isl_take isl_printer *p);
185
  __isl_give isl_printer *(*end_line)(__isl_take isl_printer *p);
186
  __isl_give isl_printer *(*print_double)(__isl_take isl_printer *p,
187
    double d);
188
  __isl_give isl_printer *(*print_int)(__isl_take isl_printer *p, int i);
189
  __isl_give isl_printer *(*print_isl_int)(__isl_take isl_printer *p,
190
            isl_int i);
191
  __isl_give isl_printer *(*print_str)(__isl_take isl_printer *p,
192
            const char *s);
193
  __isl_give isl_printer *(*flush)(__isl_take isl_printer *p);
194
};
195
196
static struct isl_printer_ops file_ops = {
197
  file_start_line,
198
  file_end_line,
199
  file_print_double,
200
  file_print_int,
201
  file_print_isl_int,
202
  file_print_str,
203
  file_flush
204
};
205
206
static struct isl_printer_ops str_ops = {
207
  str_start_line,
208
  str_end_line,
209
  str_print_double,
210
  str_print_int,
211
  str_print_isl_int,
212
  str_print_str,
213
  str_flush
214
};
215
216
__isl_give isl_printer *isl_printer_to_file(isl_ctx *ctx, FILE *file)
217
0
{
218
0
  struct isl_printer *p = isl_calloc_type(ctx, struct isl_printer);
219
0
  if (!p)
220
0
    return NULL;
221
0
  p->ctx = ctx;
222
0
  isl_ctx_ref(p->ctx);
223
0
  p->ops = &file_ops;
224
0
  p->file = file;
225
0
  p->buf = NULL;
226
0
  p->buf_n = 0;
227
0
  p->buf_size = 0;
228
0
  p->indent = 0;
229
0
  p->output_format = ISL_FORMAT_ISL;
230
0
  p->indent_prefix = NULL;
231
0
  p->prefix = NULL;
232
0
  p->suffix = NULL;
233
0
  p->width = 0;
234
0
  p->yaml_style = ISL_YAML_STYLE_FLOW;
235
0
236
0
  return p;
237
0
}
238
239
__isl_give isl_printer *isl_printer_to_str(isl_ctx *ctx)
240
8.28k
{
241
8.28k
  struct isl_printer *p = isl_calloc_type(ctx, struct isl_printer);
242
8.28k
  if (!p)
243
0
    return NULL;
244
8.28k
  p->ctx = ctx;
245
8.28k
  isl_ctx_ref(p->ctx);
246
8.28k
  p->ops = &str_ops;
247
8.28k
  p->file = NULL;
248
8.28k
  p->buf = isl_alloc_array(ctx, char, 256);
249
8.28k
  if (!p->buf)
250
0
    goto error;
251
8.28k
  p->buf_n = 0;
252
8.28k
  p->buf[0] = '\0';
253
8.28k
  p->buf_size = 256;
254
8.28k
  p->indent = 0;
255
8.28k
  p->output_format = ISL_FORMAT_ISL;
256
8.28k
  p->indent_prefix = NULL;
257
8.28k
  p->prefix = NULL;
258
8.28k
  p->suffix = NULL;
259
8.28k
  p->width = 0;
260
8.28k
  p->yaml_style = ISL_YAML_STYLE_FLOW;
261
8.28k
262
8.28k
  return p;
263
0
error:
264
0
  isl_printer_free(p);
265
0
  return NULL;
266
8.28k
}
267
268
__isl_null isl_printer *isl_printer_free(__isl_take isl_printer *p)
269
8.28k
{
270
8.28k
  if (!p)
271
0
    return NULL;
272
8.28k
  free(p->buf);
273
8.28k
  free(p->indent_prefix);
274
8.28k
  free(p->prefix);
275
8.28k
  free(p->suffix);
276
8.28k
  free(p->yaml_state);
277
8.28k
  isl_id_to_id_free(p->notes);
278
8.28k
  isl_ctx_deref(p->ctx);
279
8.28k
  free(p);
280
8.28k
281
8.28k
  return NULL;
282
8.28k
}
283
284
isl_ctx *isl_printer_get_ctx(__isl_keep isl_printer *printer)
285
12.4k
{
286
12.4k
  return printer ? printer->ctx : NULL;
287
12.4k
}
288
289
FILE *isl_printer_get_file(__isl_keep isl_printer *printer)
290
0
{
291
0
  if (!printer)
292
0
    return NULL;
293
0
  if (!printer->file)
294
0
    isl_die(isl_printer_get_ctx(printer), isl_error_invalid,
295
0
      "not a file printer", return NULL);
296
0
  return printer->file;
297
0
}
298
299
__isl_give isl_printer *isl_printer_set_isl_int_width(__isl_take isl_printer *p,
300
  int width)
301
0
{
302
0
  if (!p)
303
0
    return NULL;
304
0
305
0
  p->width = width;
306
0
307
0
  return p;
308
0
}
309
310
__isl_give isl_printer *isl_printer_set_indent(__isl_take isl_printer *p,
311
  int indent)
312
0
{
313
0
  if (!p)
314
0
    return NULL;
315
0
316
0
  p->indent = indent;
317
0
318
0
  return p;
319
0
}
320
321
__isl_give isl_printer *isl_printer_indent(__isl_take isl_printer *p,
322
  int indent)
323
1.49k
{
324
1.49k
  if (!p)
325
0
    return NULL;
326
1.49k
327
1.49k
  p->indent += indent;
328
1.49k
  if (p->indent < 0)
329
0
    p->indent = 0;
330
1.49k
331
1.49k
  return p;
332
1.49k
}
333
334
/* Replace the indent prefix of "p" by "prefix".
335
 */
336
__isl_give isl_printer *isl_printer_set_indent_prefix(__isl_take isl_printer *p,
337
  const char *prefix)
338
0
{
339
0
  if (!p)
340
0
    return NULL;
341
0
342
0
  free(p->indent_prefix);
343
0
  p->indent_prefix = prefix ? strdup(prefix) : NULL;
344
0
345
0
  return p;
346
0
}
347
348
__isl_give isl_printer *isl_printer_set_prefix(__isl_take isl_printer *p,
349
  const char *prefix)
350
0
{
351
0
  if (!p)
352
0
    return NULL;
353
0
354
0
  free(p->prefix);
355
0
  p->prefix = prefix ? strdup(prefix) : NULL;
356
0
357
0
  return p;
358
0
}
359
360
__isl_give isl_printer *isl_printer_set_suffix(__isl_take isl_printer *p,
361
  const char *suffix)
362
0
{
363
0
  if (!p)
364
0
    return NULL;
365
0
366
0
  free(p->suffix);
367
0
  p->suffix = suffix ? strdup(suffix) : NULL;
368
0
369
0
  return p;
370
0
}
371
372
__isl_give isl_printer *isl_printer_set_output_format(__isl_take isl_printer *p,
373
  int output_format)
374
157
{
375
157
  if (!p)
376
0
    return NULL;
377
157
378
157
  p->output_format = output_format;
379
157
380
157
  return p;
381
157
}
382
383
int isl_printer_get_output_format(__isl_keep isl_printer *p)
384
3.42k
{
385
3.42k
  if (!p)
386
0
    return -1;
387
3.42k
  return p->output_format;
388
3.42k
}
389
390
/* Does "p" have a note with identifier "id"?
391
 */
392
isl_bool isl_printer_has_note(__isl_keep isl_printer *p,
393
  __isl_keep isl_id *id)
394
11.9k
{
395
11.9k
  if (!p || !id)
396
0
    return isl_bool_error;
397
11.9k
  if (!p->notes)
398
11.9k
    return isl_bool_false;
399
0
  return isl_id_to_id_has(p->notes, id);
400
0
}
401
402
/* Retrieve the note identified by "id" from "p".
403
 * The note is assumed to exist.
404
 */
405
__isl_give isl_id *isl_printer_get_note(__isl_keep isl_printer *p,
406
  __isl_take isl_id *id)
407
0
{
408
0
  isl_bool has_note;
409
0
410
0
  has_note = isl_printer_has_note(p, id);
411
0
  if (has_note < 0)
412
0
    goto error;
413
0
  if (!has_note)
414
0
    isl_die(isl_printer_get_ctx(p), isl_error_invalid,
415
0
      "no such note", goto error);
416
0
417
0
  return isl_id_to_id_get(p->notes, id);
418
0
error:
419
0
  isl_id_free(id);
420
0
  return NULL;
421
0
}
422
423
/* Associate "note" to the identifier "id" in "p",
424
 * replacing the previous note associated to the identifier, if any.
425
 */
426
__isl_give isl_printer *isl_printer_set_note(__isl_take isl_printer *p,
427
  __isl_take isl_id *id, __isl_take isl_id *note)
428
0
{
429
0
  if (!p || !id || !note)
430
0
    goto error;
431
0
  if (!p->notes) {
432
0
    p->notes = isl_id_to_id_alloc(isl_printer_get_ctx(p), 1);
433
0
    if (!p->notes)
434
0
      goto error;
435
0
  }
436
0
  p->notes = isl_id_to_id_set(p->notes, id, note);
437
0
  if (!p->notes)
438
0
    return isl_printer_free(p);
439
0
  return p;
440
0
error:
441
0
  isl_printer_free(p);
442
0
  isl_id_free(id);
443
0
  isl_id_free(note);
444
0
  return NULL;
445
0
}
446
447
/* Keep track of whether the printing to "p" is being performed from
448
 * an isl_*_dump function as specified by "dump".
449
 */
450
__isl_give isl_printer *isl_printer_set_dump(__isl_take isl_printer *p,
451
  int dump)
452
0
{
453
0
  if (!p)
454
0
    return NULL;
455
0
456
0
  p->dump = dump;
457
0
458
0
  return p;
459
0
}
460
461
/* Set the YAML style of "p" to "yaml_style" and return the updated printer.
462
 */
463
__isl_give isl_printer *isl_printer_set_yaml_style(__isl_take isl_printer *p,
464
  int yaml_style)
465
0
{
466
0
  if (!p)
467
0
    return NULL;
468
0
469
0
  p->yaml_style = yaml_style;
470
0
471
0
  return p;
472
0
}
473
474
/* Return the YAML style of "p" or -1 on error.
475
 */
476
int isl_printer_get_yaml_style(__isl_keep isl_printer *p)
477
0
{
478
0
  if (!p)
479
0
    return -1;
480
0
  return p->yaml_style;
481
0
}
482
483
/* Push "state" onto the stack of currently active YAML elements and
484
 * return the updated printer.
485
 */
486
static __isl_give isl_printer *push_state(__isl_take isl_printer *p,
487
  enum isl_yaml_state state)
488
0
{
489
0
  if (!p)
490
0
    return NULL;
491
0
492
0
  if (p->yaml_size < p->yaml_depth + 1) {
493
0
    enum isl_yaml_state *state;
494
0
    state = isl_realloc_array(p->ctx, p->yaml_state,
495
0
          enum isl_yaml_state, p->yaml_depth + 1);
496
0
    if (!state)
497
0
      return isl_printer_free(p);
498
0
    p->yaml_state = state;
499
0
    p->yaml_size = p->yaml_depth + 1;
500
0
  }
501
0
502
0
  p->yaml_state[p->yaml_depth] = state;
503
0
  p->yaml_depth++;
504
0
505
0
  return p;
506
0
}
507
508
/* Remove the innermost active YAML element from the stack and
509
 * return the updated printer.
510
 */
511
static __isl_give isl_printer *pop_state(__isl_take isl_printer *p)
512
0
{
513
0
  if (!p)
514
0
    return NULL;
515
0
  p->yaml_depth--;
516
0
  return p;
517
0
}
518
519
/* Set the state of the innermost active YAML element to "state" and
520
 * return the updated printer.
521
 */
522
static __isl_give isl_printer *update_state(__isl_take isl_printer *p,
523
  enum isl_yaml_state state)
524
0
{
525
0
  if (!p)
526
0
    return NULL;
527
0
  if (p->yaml_depth < 1)
528
0
    isl_die(isl_printer_get_ctx(p), isl_error_invalid,
529
0
      "not in YAML construct", return isl_printer_free(p));
530
0
531
0
  p->yaml_state[p->yaml_depth - 1] = state;
532
0
533
0
  return p;
534
0
}
535
536
/* Return the state of the innermost active YAML element.
537
 * Return isl_yaml_none if we are not inside any YAML element.
538
 */
539
static enum isl_yaml_state current_state(__isl_keep isl_printer *p)
540
236k
{
541
236k
  if (!p)
542
0
    return isl_yaml_none;
543
236k
  if (p->yaml_depth < 1)
544
236k
    return isl_yaml_none;
545
0
  return p->yaml_state[p->yaml_depth - 1];
546
0
}
547
548
/* If we are printing a YAML document and we are at the start of an element,
549
 * print whatever is needed before we can print the actual element and
550
 * keep track of the fact that we are now printing the element.
551
 * If "eol" is set, then whatever we print is going to be the last
552
 * thing that gets printed on this line.
553
 *
554
 * If we are about the print the first key of a mapping, then nothing
555
 * extra needs to be printed.  For any other key, however, we need
556
 * to either move to the next line (in block format) or print a comma
557
 * (in flow format).
558
 * Before printing a value in a mapping, we need to print a colon.
559
 *
560
 * For sequences, in flow format, we only need to print a comma
561
 * for each element except the first.
562
 * In block format, before the first element in the sequence,
563
 * we move to a new line, print a dash and increase the indentation.
564
 * Before any other element, we print a dash on a new line,
565
 * temporarily moving the indentation back.
566
 */
567
static __isl_give isl_printer *enter_state(__isl_take isl_printer *p,
568
  int eol)
569
236k
{
570
236k
  enum isl_yaml_state state;
571
236k
572
236k
  if (!p)
573
0
    return NULL;
574
236k
575
236k
  state = current_state(p);
576
236k
  if (state == isl_yaml_mapping_val_start) {
577
0
    if (eol)
578
0
      p = p->ops->print_str(p, ":");
579
0
    else
580
0
      p = p->ops->print_str(p, ": ");
581
0
    p = update_state(p, isl_yaml_mapping_val);
582
236k
  } else if (state == isl_yaml_mapping_first_key_start) {
583
0
    p = update_state(p, isl_yaml_mapping_key);
584
236k
  } else if (state == isl_yaml_mapping_key_start) {
585
0
    if (p->yaml_style == ISL_YAML_STYLE_FLOW)
586
0
      p = p->ops->print_str(p, ", ");
587
0
    else {
588
0
      p = p->ops->end_line(p);
589
0
      p = p->ops->start_line(p);
590
0
    }
591
0
    p = update_state(p, isl_yaml_mapping_key);
592
236k
  } else if (state == isl_yaml_sequence_first_start) {
593
0
    if (p->yaml_style != ISL_YAML_STYLE_FLOW) {
594
0
      p = p->ops->end_line(p);
595
0
      p = p->ops->start_line(p);
596
0
      p = p->ops->print_str(p, "- ");
597
0
      p = isl_printer_indent(p, 2);
598
0
    }
599
0
    p = update_state(p, isl_yaml_sequence);
600
236k
  } else if (state == isl_yaml_sequence_start) {
601
0
    if (p->yaml_style == ISL_YAML_STYLE_FLOW)
602
0
      p = p->ops->print_str(p, ", ");
603
0
    else {
604
0
      p = p->ops->end_line(p);
605
0
      p = isl_printer_indent(p, -2);
606
0
      p = p->ops->start_line(p);
607
0
      p = p->ops->print_str(p, "- ");
608
0
      p = isl_printer_indent(p, 2);
609
0
    }
610
0
    p = update_state(p, isl_yaml_sequence);
611
0
  }
612
236k
613
236k
  return p;
614
236k
}
615
616
__isl_give isl_printer *isl_printer_print_str(__isl_take isl_printer *p,
617
  const char *s)
618
219k
{
619
219k
  if (!p)
620
0
    return NULL;
621
219k
  if (!s)
622
0
    return isl_printer_free(p);
623
219k
  p = enter_state(p, 0);
624
219k
  if (!p)
625
0
    return NULL;
626
219k
  return p->ops->print_str(p, s);
627
219k
}
628
629
__isl_give isl_printer *isl_printer_print_double(__isl_take isl_printer *p,
630
  double d)
631
0
{
632
0
  p = enter_state(p, 0);
633
0
  if (!p)
634
0
    return NULL;
635
0
636
0
  return p->ops->print_double(p, d);
637
0
}
638
639
__isl_give isl_printer *isl_printer_print_int(__isl_take isl_printer *p, int i)
640
0
{
641
0
  p = enter_state(p, 0);
642
0
  if (!p)
643
0
    return NULL;
644
0
645
0
  return p->ops->print_int(p, i);
646
0
}
647
648
__isl_give isl_printer *isl_printer_print_isl_int(__isl_take isl_printer *p,
649
  isl_int i)
650
17.7k
{
651
17.7k
  p = enter_state(p, 0);
652
17.7k
  if (!p)
653
0
    return NULL;
654
17.7k
655
17.7k
  return p->ops->print_isl_int(p, i);
656
17.7k
}
657
658
__isl_give isl_printer *isl_printer_start_line(__isl_take isl_printer *p)
659
2.98k
{
660
2.98k
  if (!p)
661
0
    return NULL;
662
2.98k
663
2.98k
  return p->ops->start_line(p);
664
2.98k
}
665
666
__isl_give isl_printer *isl_printer_end_line(__isl_take isl_printer *p)
667
2.98k
{
668
2.98k
  if (!p)
669
0
    return NULL;
670
2.98k
671
2.98k
  return p->ops->end_line(p);
672
2.98k
}
673
674
/* Return a copy of the string constructed by the string printer "printer".
675
 */
676
__isl_give char *isl_printer_get_str(__isl_keep isl_printer *printer)
677
8.44k
{
678
8.44k
  if (!printer)
679
0
    return NULL;
680
8.44k
  if (printer->ops != &str_ops)
681
8.44k
    
isl_die0
(isl_printer_get_ctx(printer), isl_error_invalid,
682
8.44k
      "isl_printer_get_str can only be called on a string "
683
8.44k
      "printer", return NULL);
684
8.44k
  if (!printer->buf)
685
0
    return NULL;
686
8.44k
  return strdup(printer->buf);
687
8.44k
}
688
689
__isl_give isl_printer *isl_printer_flush(__isl_take isl_printer *p)
690
154
{
691
154
  if (!p)
692
0
    return NULL;
693
154
694
154
  return p->ops->flush(p);
695
154
}
696
697
/* Start a YAML mapping and push a new state to reflect that we
698
 * are about to print the first key in a mapping.
699
 *
700
 * In flow style, print the opening brace.
701
 * In block style, move to the next line with an increased indentation,
702
 * except if this is the outer mapping or if we are inside a sequence
703
 * (in which case we have already increased the indentation and we want
704
 * to print the first key on the same line as the dash).
705
 */
706
__isl_give isl_printer *isl_printer_yaml_start_mapping(
707
  __isl_take isl_printer *p)
708
0
{
709
0
  enum isl_yaml_state state;
710
0
711
0
  if (!p)
712
0
    return NULL;
713
0
  p = enter_state(p, p->yaml_style == ISL_YAML_STYLE_BLOCK);
714
0
  if (!p)
715
0
    return NULL;
716
0
  state = current_state(p);
717
0
  if (p->yaml_style == ISL_YAML_STYLE_FLOW)
718
0
    p = p->ops->print_str(p, "{ ");
719
0
  else if (state != isl_yaml_none && state != isl_yaml_sequence) {
720
0
    p = p->ops->end_line(p);
721
0
    p = isl_printer_indent(p, 2);
722
0
    p = p->ops->start_line(p);
723
0
  }
724
0
  p = push_state(p, isl_yaml_mapping_first_key_start);
725
0
  return p;
726
0
}
727
728
/* Finish a YAML mapping and pop it from the state stack.
729
 *
730
 * In flow style, print the closing brace.
731
 *
732
 * In block style, first check if we are still in the
733
 * isl_yaml_mapping_first_key_start state.  If so, we have not printed
734
 * anything yet, so print "{}" to indicate an empty mapping.
735
 * If we increased the indentation in isl_printer_yaml_start_mapping,
736
 * then decrease it again.
737
 * If this is the outer mapping then print a newline.
738
 */
739
__isl_give isl_printer *isl_printer_yaml_end_mapping(
740
  __isl_take isl_printer *p)
741
0
{
742
0
  enum isl_yaml_state state;
743
0
744
0
  state = current_state(p);
745
0
  p = pop_state(p);
746
0
  if (!p)
747
0
    return NULL;
748
0
  if (p->yaml_style == ISL_YAML_STYLE_FLOW)
749
0
    return p->ops->print_str(p, " }");
750
0
  if (state == isl_yaml_mapping_first_key_start)
751
0
    p = p->ops->print_str(p, "{}");
752
0
  if (!p)
753
0
    return NULL;
754
0
  state = current_state(p);
755
0
  if (state != isl_yaml_none && state != isl_yaml_sequence)
756
0
    p = isl_printer_indent(p, -2);
757
0
  if (state == isl_yaml_none)
758
0
    p = p->ops->end_line(p);
759
0
  return p;
760
0
}
761
762
/* Start a YAML sequence and push a new state to reflect that we
763
 * are about to print the first element in a sequence.
764
 *
765
 * In flow style, print the opening bracket.
766
 */
767
__isl_give isl_printer *isl_printer_yaml_start_sequence(
768
  __isl_take isl_printer *p)
769
0
{
770
0
  if (!p)
771
0
    return NULL;
772
0
  p = enter_state(p, p->yaml_style == ISL_YAML_STYLE_BLOCK);
773
0
  p = push_state(p, isl_yaml_sequence_first_start);
774
0
  if (!p)
775
0
    return NULL;
776
0
  if (p->yaml_style == ISL_YAML_STYLE_FLOW)
777
0
    p = p->ops->print_str(p, "[ ");
778
0
  return p;
779
0
}
780
781
/* Finish a YAML sequence and pop it from the state stack.
782
 *
783
 * In flow style, print the closing bracket.
784
 *
785
 * In block style, check if we are still in the
786
 * isl_yaml_sequence_first_start state.  If so, we have not printed
787
 * anything yet, so print "[]" or " []" to indicate an empty sequence.
788
 * We print the extra space when we instructed enter_state not
789
 * to print a space at the end of the line.
790
 * Otherwise, undo the increase in indentation performed by
791
 * enter_state when moving away from the isl_yaml_sequence_first_start
792
 * state.
793
 * If this is the outer sequence then print a newline.
794
 */
795
__isl_give isl_printer *isl_printer_yaml_end_sequence(
796
  __isl_take isl_printer *p)
797
0
{
798
0
  enum isl_yaml_state state, up;
799
0
800
0
  state = current_state(p);
801
0
  p = pop_state(p);
802
0
  if (!p)
803
0
    return NULL;
804
0
  if (p->yaml_style == ISL_YAML_STYLE_FLOW)
805
0
    return p->ops->print_str(p, " ]");
806
0
  up = current_state(p);
807
0
  if (state == isl_yaml_sequence_first_start) {
808
0
    if (up == isl_yaml_mapping_val)
809
0
      p = p->ops->print_str(p, " []");
810
0
    else
811
0
      p = p->ops->print_str(p, "[]");
812
0
  } else {
813
0
    p = isl_printer_indent(p, -2);
814
0
  }
815
0
  if (!p)
816
0
    return NULL;
817
0
  state = current_state(p);
818
0
  if (state == isl_yaml_none)
819
0
    p = p->ops->end_line(p);
820
0
  return p;
821
0
}
822
823
/* Mark the fact that the current element is finished and that
824
 * the next output belongs to the next element.
825
 * In particular, if we are printing a key, then prepare for
826
 * printing the subsequent value.  If we are printing a value,
827
 * prepare for printing the next key.  If we are printing an
828
 * element in a sequence, prepare for printing the next element.
829
 */
830
__isl_give isl_printer *isl_printer_yaml_next(__isl_take isl_printer *p)
831
0
{
832
0
  enum isl_yaml_state state;
833
0
834
0
  if (!p)
835
0
    return NULL;
836
0
  if (p->yaml_depth < 1)
837
0
    isl_die(isl_printer_get_ctx(p), isl_error_invalid,
838
0
      "not in YAML construct", return isl_printer_free(p));
839
0
840
0
  state = current_state(p);
841
0
  if (state == isl_yaml_mapping_key)
842
0
    state = isl_yaml_mapping_val_start;
843
0
  else if (state == isl_yaml_mapping_val)
844
0
    state = isl_yaml_mapping_key_start;
845
0
  else if (state == isl_yaml_sequence)
846
0
    state = isl_yaml_sequence_start;
847
0
  p = update_state(p, state);
848
0
849
0
  return p;
850
0
}