Coverage Report

Created: 2017-10-03 07:32

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