aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>2023-07-10 13:47:37 +0200
committerCohenArthur <arthur.cohen@embecosm.com>2023-07-12 11:40:07 +0000
commit0bcba579624e6b32fae42850246e15a6087f0673 (patch)
treea89f2b1232d2c4098443b8a3288885a19cc91481 /gcc
parent4bbf156f3300590d9867b9e72ff9bb39e8a6f67a (diff)
downloadgcc-0bcba579624e6b32fae42850246e15a6087f0673.zip
gcc-0bcba579624e6b32fae42850246e15a6087f0673.tar.gz
gcc-0bcba579624e6b32fae42850246e15a6087f0673.tar.bz2
dump: Output escaped character values
Output escaped character values instead of their raw values in string. gcc/rust/ChangeLog: * lex/rust-token.cc (escape_special_chars): Add a function that escape a given string. (Token::as_string): Add call to escape function for string/char representations. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/lex/rust-token.cc46
1 files changed, 42 insertions, 4 deletions
diff --git a/gcc/rust/lex/rust-token.cc b/gcc/rust/lex/rust-token.cc
index b6be9ca..485128b 100644
--- a/gcc/rust/lex/rust-token.cc
+++ b/gcc/rust/lex/rust-token.cc
@@ -16,6 +16,7 @@
// along with GCC; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
+#include "rust-system.h"
#include "rust-token.h"
#include "rust-diagnostics.h"
@@ -172,6 +173,41 @@ Token::get_str () const
return *str;
}
+namespace {
+enum class Context
+{
+ String,
+ Char
+};
+
+const std::map<char, std::string> matches = {
+ {'\t', "\\t"}, {'\n', "\\n"}, {'\r', "\\r"},
+ {'\0', "\\0"}, {'\\', "\\\\"}, {'\v', "\\v"},
+};
+
+std::string
+escape_special_chars (const std::string &source, Context ctx)
+{
+ std::stringstream stream;
+ decltype (matches)::const_iterator result;
+ for (char c : source)
+ {
+ // FIXME: #2411 Also replace escaped unicode values and \x digits
+ if ((result = matches.find (c)) != matches.end ())
+ stream << result->second;
+ else if (c == '\'' && ctx == Context::Char)
+ stream << "\\'";
+ else if (c == '"' && ctx == Context::String)
+ stream << "\\\"";
+ else
+ stream << c;
+ }
+
+ return stream.str ();
+}
+
+} // namespace
+
std::string
Token::as_string () const
{
@@ -180,13 +216,15 @@ Token::as_string () const
switch (get_id ())
{
case STRING_LITERAL:
- return "\"" + get_str () + "\"";
+ return "\"" + escape_special_chars (get_str (), Context::String)
+ + "\"";
case BYTE_STRING_LITERAL:
- return "b\"" + get_str () + "\"";
+ return "b\"" + escape_special_chars (get_str (), Context::String)
+ + "\"";
case CHAR_LITERAL:
- return "'" + get_str () + "'";
+ return "'" + escape_special_chars (get_str (), Context::Char) + "'";
case BYTE_CHAR_LITERAL:
- return "b'" + get_str () + "'";
+ return "b'" + escape_special_chars (get_str (), Context::Char) + "'";
case LIFETIME:
return "'" + get_str ();
case INT_LITERAL: