git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@4566 4a8a32a2...
[fw/sdcc] / support / cpp2 / pretty-print.c
1 /* Various declarations for language-independent pretty-print subroutines.
2    Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
3    Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA.  */
21
22 #include "config.h"
23 #undef FLOAT /* This is for hpux. They should change hpux.  */
24 #undef FFS  /* Some systems define this in param.h.  */
25 #include "system.h"
26 #include "intl.h"
27 #include "pretty-print.h"
28
29 #define obstack_chunk_alloc xmalloc
30 #define obstack_chunk_free  free
31
32 /* A pointer to the formatted diagnostic message.  */
33 #define pp_formatted_text_data(PP) \
34    ((const char *) obstack_base (pp_base (PP)->buffer->obstack))
35
36 /* Format an integer given by va_arg (ARG, type-specifier T) where
37    type-specifier is a precision modifier as indicated by PREC.  F is
38    a string used to construct the appropriate format-specifier.  */
39 #define pp_integer_with_precision(PP, ARG, PREC, T, F)       \
40   do                                                         \
41     switch (PREC)                                            \
42       {                                                      \
43       case 0:                                                \
44         pp_scalar (PP, "%" F, va_arg (ARG, T));              \
45         break;                                               \
46                                                              \
47       case 1:                                                \
48         pp_scalar (PP, "%l" F, va_arg (ARG, long T));        \
49         break;                                               \
50                                                              \
51       case 2:                                                \
52         pp_scalar (PP, "%" PRINTF_INT64_MODIFIER F, va_arg (ARG, long_long)); \
53         break;                                               \
54                                                              \
55       default:                                               \
56         break;                                               \
57       }                                                      \
58   while (0)
59
60
61 /* Subroutine of pp_set_maximum_length.  Set up PRETTY-PRINTER's
62    internal maximum characters per line.  */
63 static void
64 pp_set_real_maximum_length (pretty_printer *pp)
65 {
66   /* If we're told not to wrap lines then do the obvious thing.  In case
67      we'll emit prefix only once per message, it is appropriate
68      not to increase unnecessarily the line-length cut-off.  */
69   if (!pp_is_wrapping_line (pp)
70       || pp_prefixing_rule (pp) == DIAGNOSTICS_SHOW_PREFIX_ONCE
71       || pp_prefixing_rule (pp) == DIAGNOSTICS_SHOW_PREFIX_NEVER)
72     pp->maximum_length = pp_line_cutoff (pp);
73   else
74     {
75       int prefix_length = pp->prefix ? strlen (pp->prefix) : 0;
76       /* If the prefix is ridiculously too long, output at least
77          32 characters.  */
78       if (pp_line_cutoff (pp) - prefix_length < 32)
79         pp->maximum_length = pp_line_cutoff (pp) + 32;
80       else
81         pp->maximum_length = pp_line_cutoff (pp);
82     }
83 }
84
85 /* Clear PRETTY-PRINTER's output state.  */
86 static inline void
87 pp_clear_state (pretty_printer *pp)
88 {
89   pp->emitted_prefix = false;
90   pp_indentation (pp) = 0;
91 }
92
93 /* Flush the formatted text of PRETTY-PRINTER onto the attached stream.  */
94 void
95 pp_write_text_to_stream (pretty_printer *pp)
96 {
97   const char *text = pp_formatted_text (pp);
98   fputs (text, pp->buffer->stream);
99   pp_clear_output_area (pp);
100 }
101
102 /* Wrap a text delimited by START and END into PRETTY-PRINTER.  */
103 static void
104 pp_wrap_text (pretty_printer *pp, const char *start, const char *end)
105 {
106   bool wrapping_line = pp_is_wrapping_line (pp);
107
108   while (start != end)
109     {
110       /* Dump anything bordered by whitespaces.  */
111       {
112         const char *p = start;
113         while (p != end && !ISBLANK (*p) && *p != '\n')
114           ++p;
115         if (wrapping_line
116             && p - start >= pp_remaining_character_count_for_line (pp))
117           pp_newline (pp);
118         pp_append_text (pp, start, p);
119         start = p;
120       }
121
122       if (start != end && ISBLANK (*start))
123         {
124           pp_space (pp);
125           ++start;
126         }
127       if (start != end && *start == '\n')
128         {
129           pp_newline (pp);
130           ++start;
131         }
132     }
133 }
134
135 /* Same as pp_wrap_text but wrap text only when in line-wrapping mode.  */
136 static inline void
137 pp_maybe_wrap_text (pretty_printer *pp, const char *start, const char *end)
138 {
139   if (pp_is_wrapping_line (pp))
140     pp_wrap_text (pp, start, end);
141   else
142     pp_append_text (pp, start, end);
143 }
144
145 /* Append to the output area of PRETTY-PRINTER a string specified by its
146    STARTing character and LENGTH.  */
147 static inline void
148 pp_append_r (pretty_printer *pp, const char *start, int length)
149 {
150   obstack_grow (pp->buffer->obstack, start, length);
151   pp->buffer->line_length += length;
152 }
153
154 /* Insert enough spaces into the output area of PRETTY-PRINTER to bring
155    the column position to the current indentation level, assuming that a
156    newline has just been written to the buffer.  */
157 void
158 pp_base_indent (pretty_printer *pp)
159 {
160   int n = pp_indentation (pp);
161   int i;
162
163   for (i = 0; i < n; ++i)
164     pp_space (pp);
165 }
166
167 /* The following format specifiers are recognized as being client independent:
168    %d, %i: (signed) integer in base ten.
169    %u: unsigned integer in base ten.
170    %o: unsigned integer in base eight.
171    %x: unsigned integer in base sixteen.
172    %ld, %li, %lo, %lu, %lx: long versions of the above.
173    %lld, %lli, %llo, %llu, %llx: long long versions.
174    %wd, %wi, %wo, %wu, %wx: HOST_WIDE_INT versions.
175    %c: character.
176    %s: string.
177    %p: pointer.
178    %m: strerror(text->err_no) - does not consume a value from args_ptr.
179    %%: '%'.
180    %<: opening quote.
181    %>: closing quote.
182    %': apostrophe (should only be used in untranslated messages;
183        translations should use appropriate punctuation directly).
184    %.*s: a substring the length of which is specified by an argument
185          integer.
186    %Ns: likewise, but length specified as constant in the format string.
187    %H: location_t.
188    %J: a decl tree, from which DECL_SOURCE_LOCATION will be recorded.
189    Flag 'q': quote formatted text (must come immediately after '%').
190
191    Arguments can be used sequentially, or through %N$ resp. *N$
192    notation Nth argument after the format string.  If %N$ / *N$
193    notation is used, it must be used for all arguments, except %m, %%,
194    %<, %> and %', which may not have a number, as they do not consume
195    an argument.  When %M$.*N$s is used, M must be N + 1.  (This may
196    also be written %M$.*s, provided N is not otherwise used.)  The
197    format string must have conversion specifiers with argument numbers
198    1 up to highest argument; each argument may only be used once.
199    A format string can have at most 30 arguments.  */
200
201 /* Formatting phases 1 and 2: render TEXT->format_spec plus
202    TEXT->args_ptr into a series of chunks in PP->buffer->args[].
203    Phase 3 is in pp_base_format_text.  */
204
205 void
206 pp_base_format (pretty_printer *pp, text_info *text)
207 {
208   output_buffer *buffer = pp->buffer;
209   const char *p;
210   const char **args;
211   struct chunk_info *new_chunk_array;
212
213   unsigned int curarg = 0, chunk = 0, argno;
214   pp_wrapping_mode_t old_wrapping_mode;
215   bool any_unnumbered = false, any_numbered = false;
216   const char **formatters[PP_NL_ARGMAX];
217
218   /* Allocate a new chunk structure.  */
219   new_chunk_array = XOBNEW (&buffer->chunk_obstack, struct chunk_info);
220   new_chunk_array->prev = buffer->cur_chunk_array;
221   buffer->cur_chunk_array = new_chunk_array;
222   args = new_chunk_array->args;
223
224   /* Formatting phase 1: split up TEXT->format_spec into chunks in
225      PP->buffer->args[].  Even-numbered chunks are to be output
226      verbatim, odd-numbered chunks are format specifiers.
227      %m, %%, %<, %>, and %' are replaced with the appropriate text at
228      this point.  */
229
230   memset (formatters, 0, sizeof formatters);
231
232   for (p = text->format_spec; *p; )
233     {
234       while (*p != '\0' && *p != '%')
235         {
236           obstack_1grow (&buffer->chunk_obstack, *p);
237           p++;
238         }
239
240       if (*p == '\0')
241         break;
242
243       switch (*++p)
244         {
245         case '\0':
246           gcc_unreachable ();
247
248         case '%':
249           obstack_1grow (&buffer->chunk_obstack, '%');
250           p++;
251           continue;
252
253         case '<':
254           obstack_grow (&buffer->chunk_obstack,
255                         open_quote, strlen (open_quote));
256           p++;
257           continue;
258
259         case '>':
260         case '\'':
261           obstack_grow (&buffer->chunk_obstack,
262                         close_quote, strlen (close_quote));
263           p++;
264           continue;
265
266         case 'm':
267           {
268             const char *errstr = xstrerror (text->err_no);
269             obstack_grow (&buffer->chunk_obstack, errstr, strlen (errstr));
270           }
271           p++;
272           continue;
273
274         default:
275           /* Handled in phase 2.  Terminate the plain chunk here.  */
276           obstack_1grow (&buffer->chunk_obstack, '\0');
277           gcc_assert (chunk < PP_NL_ARGMAX * 2);
278           args[chunk++] = XOBFINISH (&buffer->chunk_obstack, const char *);
279           break;
280         }
281
282       if (ISDIGIT (*p))
283         {
284           char *end;
285           argno = strtoul (p, &end, 10) - 1;
286           p = end;
287           gcc_assert (*p == '$');
288           p++;
289
290           any_numbered = true;
291           gcc_assert (!any_unnumbered);
292         }
293       else
294         {
295           argno = curarg++;
296           any_unnumbered = true;
297           gcc_assert (!any_numbered);
298         }
299       gcc_assert (argno < PP_NL_ARGMAX);
300       gcc_assert (!formatters[argno]);
301       formatters[argno] = &args[chunk];
302       do
303         {
304           obstack_1grow (&buffer->chunk_obstack, *p);
305           p++;
306         }
307       while (strchr ("qwl+#", p[-1]));
308
309       if (p[-1] == '.')
310         {
311           /* We handle '%.Ns' and '%.*s' or '%M$.*N$s'
312              (where M == N + 1).  */
313           if (ISDIGIT (*p))
314             {
315               do
316                 {
317                   obstack_1grow (&buffer->chunk_obstack, *p);
318                   p++;
319                 }
320               while (ISDIGIT (p[-1]));
321               gcc_assert (p[-1] == 's');
322             }
323           else
324             {
325               gcc_assert (*p == '*');
326               obstack_1grow (&buffer->chunk_obstack, '*');
327               p++;
328
329               if (ISDIGIT (*p))
330                 {
331                   char *end;
332                   unsigned int argno2 = strtoul (p, &end, 10) - 1;
333                   p = end;
334                   gcc_assert (argno2 == argno - 1);
335                   gcc_assert (!any_unnumbered);
336                   gcc_assert (*p == '$');
337
338                   p++;
339                   formatters[argno2] = formatters[argno];
340                 }
341               else
342                 {
343                   gcc_assert (!any_numbered);
344                   formatters[argno+1] = formatters[argno];
345                   curarg++;
346                 }
347               gcc_assert (*p == 's');
348               obstack_1grow (&buffer->chunk_obstack, 's');
349               p++;
350             }
351         }
352       if (*p == '\0')
353         break;
354
355       obstack_1grow (&buffer->chunk_obstack, '\0');
356       gcc_assert (chunk < PP_NL_ARGMAX * 2);
357       args[chunk++] = XOBFINISH (&buffer->chunk_obstack, const char *);
358     }
359
360   obstack_1grow (&buffer->chunk_obstack, '\0');
361   gcc_assert (chunk < PP_NL_ARGMAX * 2);
362   args[chunk++] = XOBFINISH (&buffer->chunk_obstack, const char *);
363   args[chunk] = 0;
364
365   /* Set output to the argument obstack, and switch line-wrapping and
366      prefixing off.  */
367   buffer->obstack = &buffer->chunk_obstack;
368   old_wrapping_mode = pp_set_verbatim_wrapping (pp);
369
370   /* Second phase.  Replace each formatter with the formatted text it
371      corresponds to.  */
372
373   for (argno = 0; formatters[argno]; argno++)
374     {
375       int precision = 0;
376       bool wide = false;
377       bool plus = false;
378       bool hash = false;
379       bool quote = false;
380
381       /* We do not attempt to enforce any ordering on the modifier
382          characters.  */
383
384       for (p = *formatters[argno];; p++)
385         {
386           switch (*p)
387             {
388             case 'q':
389               gcc_assert (!quote);
390               quote = true;
391               continue;
392
393             case '+':
394               gcc_assert (!plus);
395               plus = true;
396               continue;
397
398             case '#':
399               gcc_assert (!hash);
400               hash = true;
401               continue;
402
403             case 'w':
404               gcc_assert (!wide);
405               wide = true;
406               continue;
407
408             case 'l':
409               /* We don't support precision beyond that of "long long".  */
410               gcc_assert (precision < 2);
411               precision++;
412               continue;
413             }
414           break;
415         }
416
417       gcc_assert (!wide || precision == 0);
418
419       if (quote)
420         pp_string (pp, open_quote);
421
422       switch (*p)
423         {
424         case 'c':
425           pp_character (pp, va_arg (*text->args_ptr, int));
426           break;
427
428         case 'd':
429         case 'i':
430           if (wide)
431             pp_wide_integer (pp, va_arg (*text->args_ptr, HOST_WIDE_INT));
432           else
433             pp_integer_with_precision
434               (pp, *text->args_ptr, precision, int, "d");
435           break;
436
437         case 'o':
438           if (wide)
439             pp_scalar (pp, "%" HOST_WIDE_INT_PRINT "o",
440                        va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
441           else
442             pp_integer_with_precision
443               (pp, *text->args_ptr, precision, unsigned, "o");
444           break;
445
446         case 's':
447           pp_string (pp, va_arg (*text->args_ptr, const char *));
448           break;
449
450         case 'p':
451           pp_pointer (pp, va_arg (*text->args_ptr, void *));
452           break;
453
454         case 'u':
455           if (wide)
456             pp_scalar (pp, HOST_WIDE_INT_PRINT_UNSIGNED,
457                        va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
458           else
459             pp_integer_with_precision
460               (pp, *text->args_ptr, precision, unsigned, "u");
461           break;
462
463         case 'x':
464           if (wide)
465             pp_scalar (pp, HOST_WIDE_INT_PRINT_HEX,
466                        va_arg (*text->args_ptr, unsigned HOST_WIDE_INT));
467           else
468             pp_integer_with_precision
469               (pp, *text->args_ptr, precision, unsigned, "x");
470           break;
471
472         case 'H':
473           {
474             location_t *locus = va_arg (*text->args_ptr, location_t *);
475             gcc_assert (text->locus != NULL);
476             *text->locus = *locus;
477           }
478           break;
479
480         case 'J':
481           {
482             tree t = va_arg (*text->args_ptr, tree);
483             gcc_assert (text->locus != NULL);
484             *text->locus = DECL_SOURCE_LOCATION (t);
485           }
486           break;
487
488         case '.':
489           {
490             int n;
491             const char *s;
492
493             /* We handle '%.Ns' and '%.*s' or '%M$.*N$s'
494                (where M == N + 1).  The format string should be verified
495                already from the first phase.  */
496             p++;
497             if (ISDIGIT (*p))
498               {
499                 char *end;
500                 n = strtoul (p, &end, 10);
501                 p = end;
502                 gcc_assert (*p == 's');
503               }
504             else
505               {
506                 gcc_assert (*p == '*');
507                 p++;
508                 gcc_assert (*p == 's');
509                 n = va_arg (*text->args_ptr, int);
510
511                 /* This consumes a second entry in the formatters array.  */
512                 gcc_assert (formatters[argno] == formatters[argno+1]);
513                 argno++;
514               }
515
516             s = va_arg (*text->args_ptr, const char *);
517             pp_append_text (pp, s, s + n);
518           }
519           break;
520
521         default:
522           {
523             bool ok;
524
525             gcc_assert (pp_format_decoder (pp));
526             ok = pp_format_decoder (pp) (pp, text, p,
527                                          precision, wide, plus, hash);
528             gcc_assert (ok);
529           }
530         }
531
532       if (quote)
533         pp_string (pp, close_quote);
534
535       obstack_1grow (&buffer->chunk_obstack, '\0');
536       *formatters[argno] = XOBFINISH (&buffer->chunk_obstack, const char *);
537     }
538
539 #ifdef ENABLE_CHECKING
540   for (; argno < PP_NL_ARGMAX; argno++)
541     gcc_assert (!formatters[argno]);
542 #endif
543
544   /* Revert to normal obstack and wrapping mode.  */
545   buffer->obstack = &buffer->formatted_obstack;
546   buffer->line_length = 0;
547   pp_wrapping_mode (pp) = old_wrapping_mode;
548   pp_clear_state (pp);
549 }
550
551 /* Format of a message pointed to by TEXT.  */
552 void
553 pp_base_output_formatted_text (pretty_printer *pp)
554 {
555   unsigned int chunk;
556   output_buffer *buffer = pp_buffer (pp);
557   struct chunk_info *chunk_array = buffer->cur_chunk_array;
558   const char **args = chunk_array->args;
559
560   gcc_assert (buffer->obstack == &buffer->formatted_obstack);
561   gcc_assert (buffer->line_length == 0);
562
563   /* This is a third phase, first 2 phases done in pp_base_format_args.
564      Now we actually print it.  */
565   for (chunk = 0; args[chunk]; chunk++)
566     pp_string (pp, args[chunk]);
567
568   /* Deallocate the chunk structure and everything after it (i.e. the
569      associated series of formatted strings).  */
570   buffer->cur_chunk_array = chunk_array->prev;
571   obstack_free (&buffer->chunk_obstack, chunk_array);
572 }
573
574 /* Helper subroutine of output_verbatim and verbatim. Do the appropriate
575    settings needed by BUFFER for a verbatim formatting.  */
576 void
577 pp_base_format_verbatim (pretty_printer *pp, text_info *text)
578 {
579   /* Set verbatim mode.  */
580   pp_wrapping_mode_t oldmode = pp_set_verbatim_wrapping (pp);
581
582   /* Do the actual formatting.  */
583   pp_format (pp, text);
584   pp_output_formatted_text (pp);
585
586   /* Restore previous settings.  */
587   pp_wrapping_mode (pp) = oldmode;
588 }
589
590 /* Flush the content of BUFFER onto the attached stream.  */
591 void
592 pp_base_flush (pretty_printer *pp)
593 {
594   pp_write_text_to_stream (pp);
595   pp_clear_state (pp);
596   fputc ('\n', pp->buffer->stream);
597   fflush (pp->buffer->stream);
598   pp_needs_newline (pp) = false;
599 }
600
601 /* Sets the number of maximum characters per line PRETTY-PRINTER can
602    output in line-wrapping mode.  A LENGTH value 0 suppresses
603    line-wrapping.  */
604 void
605 pp_base_set_line_maximum_length (pretty_printer *pp, int length)
606 {
607   pp_line_cutoff (pp) = length;
608   pp_set_real_maximum_length (pp);
609 }
610
611 /* Clear PRETTY-PRINTER output area text info.  */
612 void
613 pp_base_clear_output_area (pretty_printer *pp)
614 {
615   obstack_free (pp->buffer->obstack, obstack_base (pp->buffer->obstack));
616   pp->buffer->line_length = 0;
617 }
618
619 /* Set PREFIX for PRETTY-PRINTER.  */
620 void
621 pp_base_set_prefix (pretty_printer *pp, const char *prefix)
622 {
623   pp->prefix = prefix;
624   pp_set_real_maximum_length (pp);
625   pp->emitted_prefix = false;
626   pp_indentation (pp) = 0;
627 }
628
629 /* Free PRETTY-PRINTER's prefix, a previously malloc()'d string.  */
630 void
631 pp_base_destroy_prefix (pretty_printer *pp)
632 {
633   if (pp->prefix != NULL)
634     {
635       free ((char *) pp->prefix);
636       pp->prefix = NULL;
637     }
638 }
639
640 /* Write out PRETTY-PRINTER's prefix.  */
641 void
642 pp_base_emit_prefix (pretty_printer *pp)
643 {
644   if (pp->prefix != NULL)
645     {
646       switch (pp_prefixing_rule (pp))
647         {
648         default:
649         case DIAGNOSTICS_SHOW_PREFIX_NEVER:
650           break;
651
652         case DIAGNOSTICS_SHOW_PREFIX_ONCE:
653           if (pp->emitted_prefix)
654             {
655               pp_base_indent (pp);
656               break;
657             }
658           pp_indentation (pp) += 3;
659           /* Fall through.  */
660
661         case DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE:
662           {
663             int prefix_length = strlen (pp->prefix);
664             pp_append_r (pp, pp->prefix, prefix_length);
665             pp->emitted_prefix = true;
666           }
667           break;
668         }
669     }
670 }
671
672 /* Construct a PRETTY-PRINTER with PREFIX and of MAXIMUM_LENGTH
673    characters per line.  */
674 void
675 pp_construct (pretty_printer *pp, const char *prefix, int maximum_length)
676 {
677   memset (pp, 0, sizeof (pretty_printer));
678   pp->buffer = xcalloc (1, sizeof (output_buffer));
679   obstack_init (&pp->buffer->chunk_obstack);
680   obstack_init (&pp->buffer->formatted_obstack);
681   pp->buffer->obstack = &pp->buffer->formatted_obstack;
682   pp->buffer->stream = stderr;
683   pp_line_cutoff (pp) = maximum_length;
684   pp_prefixing_rule (pp) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
685   pp_set_prefix (pp, prefix);
686 }
687
688 /* Append a string delimited by START and END to the output area of
689    PRETTY-PRINTER.  No line wrapping is done.  However, if beginning a
690    new line then emit PRETTY-PRINTER's prefix and skip any leading
691    whitespace if appropriate.  The caller must ensure that it is
692    safe to do so.  */
693 void
694 pp_base_append_text (pretty_printer *pp, const char *start, const char *end)
695 {
696   /* Emit prefix and skip whitespace if we're starting a new line.  */
697   if (pp->buffer->line_length == 0)
698     {
699       pp_emit_prefix (pp);
700       if (pp_is_wrapping_line (pp))
701         while (start != end && *start == ' ')
702           ++start;
703     }
704   pp_append_r (pp, start, end - start);
705 }
706
707 /* Finishes constructing a NULL-terminated character string representing
708    the PRETTY-PRINTED text.  */
709 const char *
710 pp_base_formatted_text (pretty_printer *pp)
711 {
712   obstack_1grow (pp->buffer->obstack, '\0');
713   return pp_formatted_text_data (pp);
714 }
715
716 /*  Return a pointer to the last character emitted in PRETTY-PRINTER's
717     output area.  A NULL pointer means no character available.  */
718 const char *
719 pp_base_last_position_in_text (const pretty_printer *pp)
720 {
721   const char *p = NULL;
722   struct obstack *text = pp->buffer->obstack;
723
724   if (obstack_base (text) != obstack_next_free (text))
725     p = ((const char *) obstack_next_free (text)) - 1;
726   return p;
727 }
728
729 /* Return the amount of characters PRETTY-PRINTER can accept to
730    make a full line.  Meaningful only in line-wrapping mode.  */
731 int
732 pp_base_remaining_character_count_for_line (pretty_printer *pp)
733 {
734   return pp->maximum_length - pp->buffer->line_length;
735 }
736
737
738 /* Format a message into BUFFER a la printf.  */
739 void
740 pp_printf (pretty_printer *pp, const char *msg, ...)
741 {
742   text_info text;
743   va_list ap;
744
745   va_start (ap, msg);
746   text.err_no = errno;
747   text.args_ptr = &ap;
748   text.format_spec = msg;
749   text.locus = NULL;
750   pp_format (pp, &text);
751   pp_output_formatted_text (pp);
752   va_end (ap);
753 }
754
755
756 /* Output MESSAGE verbatim into BUFFER.  */
757 void
758 pp_verbatim (pretty_printer *pp, const char *msg, ...)
759 {
760   text_info text;
761   va_list ap;
762
763   va_start (ap, msg);
764   text.err_no = errno;
765   text.args_ptr = &ap;
766   text.format_spec = msg;
767   text.locus = NULL;
768   pp_format_verbatim (pp, &text);
769   va_end (ap);
770 }
771
772
773
774 /* Have PRETTY-PRINTER start a new line.  */
775 void
776 pp_base_newline (pretty_printer *pp)
777 {
778   obstack_1grow (pp->buffer->obstack, '\n');
779   pp->buffer->line_length = 0;
780 }
781
782 /* Have PRETTY-PRINTER add a CHARACTER.  */
783 void
784 pp_base_character (pretty_printer *pp, int c)
785 {
786   if (pp_is_wrapping_line (pp)
787       && pp_remaining_character_count_for_line (pp) <= 0)
788     {
789       pp_newline (pp);
790       if (ISSPACE (c))
791         return;
792     }
793   obstack_1grow (pp->buffer->obstack, c);
794   ++pp->buffer->line_length;
795 }
796
797 /* Append a STRING to the output area of PRETTY-PRINTER; the STRING may
798    be line-wrapped if in appropriate mode.  */
799 void
800 pp_base_string (pretty_printer *pp, const char *str)
801 {
802   pp_maybe_wrap_text (pp, str, str + (str ? strlen (str) : 0));
803 }
804
805 /* Maybe print out a whitespace if needed.  */
806
807 void
808 pp_base_maybe_space (pretty_printer *pp)
809 {
810   if (pp_base (pp)->padding != pp_none)
811     {
812       pp_space (pp);
813       pp_base (pp)->padding = pp_none;
814     }
815 }