Coverage Report

Created: 2017-04-27 19:33

/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
0
{
14
0
  fprintf(p->file, "%s\n", p->suffix ? 
p->suffix0
:
""0
);
15
0
  return p;
16
0
}
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
0
{
27
0
  fprintf(p->file, "%s", s);
28
0
  return p;
29
0
}
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
518
{
52
518
  int new_size;
53
518
  char *new_buf;
54
518
55
518
  if (p->buf_size == 0)
56
0
    return -1;
57
518
58
518
  new_size = ((p->buf_n + extra + 1) * 3) / 2;
59
518
  new_buf = isl_realloc_array(p->ctx, p->buf, char, new_size);
60
518
  if (
!new_buf518
)
{0
61
0
    p->buf_size = 0;
62
0
    return -1;
63
0
  }
64
518
  p->buf = new_buf;
65
518
  p->buf_size = new_size;
66
518
67
518
  return 0;
68
518
}
69
70
static __isl_give isl_printer *str_print(__isl_take isl_printer *p,
71
  const char *s, int len)
72
166k
{
73
166k
  if (
p->buf_n + len + 1 >= p->buf_size && 166k
grow_buf(p, len)492
)
74
0
    goto error;
75
166k
  memcpy(p->buf + p->buf_n, s, len);
76
166k
  p->buf_n += len;
77
166k
78
166k
  p->buf[p->buf_n] = '\0';
79
166k
  return p;
80
0
error:
81
0
  isl_printer_free(p);
82
0
  return NULL;
83
166k
}
84
85
static __isl_give isl_printer *str_print_indent(__isl_take isl_printer *p,
86
  int indent)
87
1.71k
{
88
1.71k
  int i;
89
1.71k
90
1.71k
  if (
p->buf_n + indent + 1 >= p->buf_size && 1.71k
grow_buf(p, indent)26
)
91
0
    goto error;
92
20.8k
  
for (i = 0; 1.71k
i < indent20.8k
;
++i19.0k
)
93
19.0k
    p->buf[p->buf_n++] = ' ';
94
1.71k
  return p;
95
0
error:
96
0
  isl_printer_free(p);
97
0
  return NULL;
98
1.71k
}
99
100
static __isl_give isl_printer *str_start_line(__isl_take isl_printer *p)
101
1.71k
{
102
1.71k
  if (p->indent_prefix)
103
0
    p = str_print(p, p->indent_prefix, strlen(p->indent_prefix));
104
1.71k
  p = str_print_indent(p, p->indent);
105
1.71k
  if (p->prefix)
106
0
    p = str_print(p, p->prefix, strlen(p->prefix));
107
1.71k
  return p;
108
1.71k
}
109
110
static __isl_give isl_printer *str_end_line(__isl_take isl_printer *p)
111
1.71k
{
112
1.71k
  if (p->suffix)
113
0
    p = str_print(p, p->suffix, strlen(p->suffix));
114
1.71k
  p = str_print(p, "\n", strlen("\n"));
115
1.71k
  return p;
116
1.71k
}
117
118
static __isl_give isl_printer *str_flush(__isl_take isl_printer *p)
119
149
{
120
149
  p->buf_n = 0;
121
149
  p->buf[p->buf_n] = '\0';
122
149
  return p;
123
149
}
124
125
static __isl_give isl_printer *str_print_str(__isl_take isl_printer *p,
126
  const char *s)
127
153k
{
128
153k
  return str_print(p, s, strlen(s));
129
153k
}
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
)
{0
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
)
{0
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
11.1k
{
169
11.1k
  char *s;
170
11.1k
  int len;
171
11.1k
172
11.1k
  s = isl_int_get_str(i);
173
11.1k
  len = strlen(s);
174
11.1k
  if (len < p->width)
175
0
    p = str_print_indent(p, p->width - len);
176
11.1k
  p = str_print(p, s, len);
177
11.1k
  isl_int_free_str(s);
178
11.1k
  return p;
179
11.1k
}
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
0
{
216
0
  struct isl_printer *p = isl_calloc_type(ctx, struct isl_printer);
217
0
  if (!p)
218
0
    return NULL;
219
0
  p->ctx = ctx;
220
0
  isl_ctx_ref(p->ctx);
221
0
  p->ops = &file_ops;
222
0
  p->file = file;
223
0
  p->buf = NULL;
224
0
  p->buf_n = 0;
225
0
  p->buf_size = 0;
226
0
  p->indent = 0;
227
0
  p->output_format = ISL_FORMAT_ISL;
228
0
  p->indent_prefix = NULL;
229
0
  p->prefix = NULL;
230
0
  p->suffix = NULL;
231
0
  p->width = 0;
232
0
  p->yaml_style = ISL_YAML_STYLE_FLOW;
233
0
234
0
  return p;
235
0
}
236
237
__isl_give isl_printer *isl_printer_to_str(isl_ctx *ctx)
238
5.66k
{
239
5.66k
  struct isl_printer *p = isl_calloc_type(ctx, struct isl_printer);
240
5.66k
  if (!p)
241
0
    return NULL;
242
5.66k
  p->ctx = ctx;
243
5.66k
  isl_ctx_ref(p->ctx);
244
5.66k
  p->ops = &str_ops;
245
5.66k
  p->file = NULL;
246
5.66k
  p->buf = isl_alloc_array(ctx, char, 256);
247
5.66k
  if (!p->buf)
248
0
    goto error;
249
5.66k
  p->buf_n = 0;
250
5.66k
  p->buf[0] = '\0';
251
5.66k
  p->buf_size = 256;
252
5.66k
  p->indent = 0;
253
5.66k
  p->output_format = ISL_FORMAT_ISL;
254
5.66k
  p->indent_prefix = NULL;
255
5.66k
  p->prefix = NULL;
256
5.66k
  p->suffix = NULL;
257
5.66k
  p->width = 0;
258
5.66k
  p->yaml_style = ISL_YAML_STYLE_FLOW;
259
5.66k
260
5.66k
  return p;
261
0
error:
262
0
  isl_printer_free(p);
263
0
  return NULL;
264
5.66k
}
265
266
__isl_null isl_printer *isl_printer_free(__isl_take isl_printer *p)
267
5.66k
{
268
5.66k
  if (!p)
269
0
    return NULL;
270
5.66k
  free(p->buf);
271
5.66k
  free(p->indent_prefix);
272
5.66k
  free(p->prefix);
273
5.66k
  free(p->suffix);
274
5.66k
  free(p->yaml_state);
275
5.66k
  isl_id_to_id_free(p->notes);
276
5.66k
  isl_ctx_deref(p->ctx);
277
5.66k
  free(p);
278
5.66k
279
5.66k
  return NULL;
280
5.66k
}
281
282
isl_ctx *isl_printer_get_ctx(__isl_keep isl_printer *printer)
283
6.18k
{
284
6.18k
  return printer ? printer->ctx : NULL;
285
6.18k
}
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.10k
{
322
1.10k
  if (!p)
323
0
    return NULL;
324
1.10k
325
1.10k
  p->indent += indent;
326
1.10k
  if (p->indent < 0)
327
0
    p->indent = 0;
328
1.10k
329
1.10k
  return p;
330
1.10k
}
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
152
{
373
152
  if (!p)
374
0
    return NULL;
375
152
376
152
  p->output_format = output_format;
377
152
378
152
  return p;
379
152
}
380
381
int isl_printer_get_output_format(__isl_keep isl_printer *p)
382
2.26k
{
383
2.26k
  if (!p)
384
0
    return -1;
385
2.26k
  return p->output_format;
386
2.26k
}
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
5.81k
{
393
5.81k
  if (
!p || 5.81k
!id5.81k
)
394
0
    return isl_bool_error;
395
5.81k
  
if (5.81k
!p->notes5.81k
)
396
5.81k
    return isl_bool_false;
397
0
  return isl_id_to_id_has(p->notes, id);
398
5.81k
}
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
)
{0
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
0
{
451
0
  if (!p)
452
0
    return NULL;
453
0
454
0
  p->dump = dump;
455
0
456
0
  return p;
457
0
}
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
)
{0
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
164k
{
539
164k
  if (!p)
540
0
    return isl_yaml_none;
541
164k
  
if (164k
p->yaml_depth < 1164k
)
542
164k
    return isl_yaml_none;
543
0
  return p->yaml_state[p->yaml_depth - 1];
544
164k
}
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
164k
{
568
164k
  enum isl_yaml_state state;
569
164k
570
164k
  if (!p)
571
0
    return NULL;
572
164k
573
164k
  state = current_state(p);
574
164k
  if (
state == isl_yaml_mapping_val_start164k
)
{0
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
164k
  } else 
if (164k
state == isl_yaml_mapping_first_key_start164k
)
{0
581
0
    p = update_state(p, isl_yaml_mapping_key);
582
164k
  } else 
if (164k
state == isl_yaml_mapping_key_start164k
)
{0
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
164k
  } else 
if (164k
state == isl_yaml_sequence_first_start164k
)
{0
591
0
    if (
p->yaml_style != 0
ISL_YAML_STYLE_FLOW0
)
{0
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
164k
  } else 
if (164k
state == isl_yaml_sequence_start164k
)
{0
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
0
    p = update_state(p, isl_yaml_sequence);
609
0
  }
610
164k
611
164k
  return p;
612
164k
}
613
614
__isl_give isl_printer *isl_printer_print_str(__isl_take isl_printer *p,
615
  const char *s)
616
153k
{
617
153k
  if (!p)
618
0
    return NULL;
619
153k
  
if (153k
!s153k
)
620
0
    return isl_printer_free(p);
621
153k
  p = enter_state(p, 0);
622
153k
  if (!p)
623
0
    return NULL;
624
153k
  return p->ops->print_str(p, s);
625
153k
}
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
11.1k
{
649
11.1k
  p = enter_state(p, 0);
650
11.1k
  if (!p)
651
0
    return NULL;
652
11.1k
653
11.1k
  return p->ops->print_isl_int(p, i);
654
11.1k
}
655
656
__isl_give isl_printer *isl_printer_start_line(__isl_take isl_printer *p)
657
1.71k
{
658
1.71k
  if (!p)
659
0
    return NULL;
660
1.71k
661
1.71k
  return p->ops->start_line(p);
662
1.71k
}
663
664
__isl_give isl_printer *isl_printer_end_line(__isl_take isl_printer *p)
665
1.71k
{
666
1.71k
  if (!p)
667
0
    return NULL;
668
1.71k
669
1.71k
  return p->ops->end_line(p);
670
1.71k
}
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
5.81k
{
676
5.81k
  if (!printer)
677
0
    return NULL;
678
5.81k
  
if (5.81k
printer->ops != &str_ops5.81k
)
679
0
    isl_die(isl_printer_get_ctx(printer), isl_error_invalid,
680
5.81k
      "isl_printer_get_str can only be called on a string "
681
5.81k
      "printer", return NULL);
682
5.81k
  
if (5.81k
!printer->buf5.81k
)
683
0
    return NULL;
684
5.81k
  return strdup(printer->buf);
685
5.81k
}
686
687
__isl_give isl_printer *isl_printer_flush(__isl_take isl_printer *p)
688
149
{
689
149
  if (!p)
690
0
    return NULL;
691
149
692
149
  return p->ops->flush(p);
693
149
}
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
)
{0
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
)
{0
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
}