summaryrefslogtreecommitdiff
path: root/gcc/pretty-print.c
diff options
context:
space:
mode:
authorburnus <burnus@138bc75d-0d04-0410-961f-82ee72b054a4>2016-09-20 21:49:12 +0000
committerburnus <burnus@138bc75d-0d04-0410-961f-82ee72b054a4>2016-09-20 21:49:12 +0000
commitc9c81ef3c667aaa14c498a5449ec6d134b4b66ff (patch)
tree0ac440db6513ee01deb5e5dc6142769d1e5b7b2d /gcc/pretty-print.c
parent12cdcb9d74f55c165366ca1b1eeec013a0ce72ef (diff)
parent891196d7325e4c55d92d5ac5cfe7161c4f36c0ce (diff)
downloadgcc-fortran-dev.tar.gz
Merge from trunk (r239915 to r240230)fortran-dev
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/branches/fortran-dev@240290 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'gcc/pretty-print.c')
-rw-r--r--gcc/pretty-print.c58
1 files changed, 55 insertions, 3 deletions
diff --git a/gcc/pretty-print.c b/gcc/pretty-print.c
index 325263efd6a..a39815ea511 100644
--- a/gcc/pretty-print.c
+++ b/gcc/pretty-print.c
@@ -30,6 +30,8 @@ along with GCC; see the file COPYING3. If not see
#include <iconv.h>
#endif
+static void pp_quoted_string (pretty_printer *, const char *, size_t = -1);
+
/* Overwrite the given location/range within this text_info's rich_location.
For use e.g. when implementing "+" in client format decoders. */
@@ -555,8 +557,20 @@ pp_format (pretty_printer *pp, text_info *text)
break;
case 'c':
- pp_character (pp, va_arg (*text->args_ptr, int));
- break;
+ {
+ /* When quoting, print alphanumeric, punctuation, and the space
+ character unchanged, and all others in hexadecimal with the
+ "\x" prefix. Otherwise print them all unchanged. */
+ int chr = va_arg (*text->args_ptr, int);
+ if (ISPRINT (chr) || !quote)
+ pp_character (pp, chr);
+ else
+ {
+ const char str [2] = { chr, '\0' };
+ pp_quoted_string (pp, str, 1);
+ }
+ break;
+ }
case 'd':
case 'i':
@@ -577,7 +591,10 @@ pp_format (pretty_printer *pp, text_info *text)
break;
case 's':
- pp_string (pp, va_arg (*text->args_ptr, const char *));
+ if (quote)
+ pp_quoted_string (pp, va_arg (*text->args_ptr, const char *));
+ else
+ pp_string (pp, va_arg (*text->args_ptr, const char *));
break;
case 'p':
@@ -939,6 +956,41 @@ pp_string (pretty_printer *pp, const char *str)
pp_maybe_wrap_text (pp, str, str + strlen (str));
}
+/* Append the leading N characters of STRING to the output area of
+ PRETTY-PRINTER, quoting in hexadecimal non-printable characters.
+ Setting N = -1 is as if N were set to strlen (STRING). The STRING
+ may be line-wrapped if in appropriate mode. */
+static void
+pp_quoted_string (pretty_printer *pp, const char *str, size_t n /* = -1 */)
+{
+ gcc_checking_assert (str);
+
+ const char *last = str;
+ const char *ps;
+
+ /* Compute the length if not specified. */
+ if (n == (size_t) -1)
+ n = strlen (str);
+
+ for (ps = str; n; ++ps, --n)
+ {
+ if (ISPRINT (*ps))
+ continue;
+
+ if (last < ps)
+ pp_maybe_wrap_text (pp, last, ps - 1);
+
+ /* Append the hexadecimal value of the character. Allocate a buffer
+ that's large enough for a 32-bit char plus the hex prefix. */
+ char buf [11];
+ int n = sprintf (buf, "\\x%02x", (unsigned char)*ps);
+ pp_maybe_wrap_text (pp, buf, buf + n);
+ last = ps + 1;
+ }
+
+ pp_maybe_wrap_text (pp, last, ps);
+}
+
/* Maybe print out a whitespace if needed. */
void