From 67d440b949d6fefef53deec1e585435ffb33f4f8 Mon Sep 17 00:00:00 2001 From: Reid Kleckner Date: Thu, 10 Oct 2019 18:31:57 +0000 Subject: [PATCH] Print quoted backslashes in LLVM IR as \\ instead of \5C This improves readability of Windows path string literals in LLVM IR. The LLVM assembler has supported \\ in IR strings for a long time, but the lexer doesn't tolerate escaped quotes, so they have to be printed as \22 for now. llvm-svn: 374415 --- llvm/lib/Support/StringExtras.cpp | 4 +++- llvm/test/Assembler/asm-path-writer.ll | 4 ++-- llvm/test/Assembler/source-filename-backslash.ll | 5 ++--- llvm/test/CodeGen/MIR/X86/global-value-operands.mir | 2 +- llvm/unittests/ADT/StringExtrasTest.cpp | 2 +- llvm/unittests/IR/MetadataTest.cpp | 2 +- 6 files changed, 10 insertions(+), 9 deletions(-) diff --git a/llvm/lib/Support/StringExtras.cpp b/llvm/lib/Support/StringExtras.cpp index bf28b2be5657..af8dd463e125 100644 --- a/llvm/lib/Support/StringExtras.cpp +++ b/llvm/lib/Support/StringExtras.cpp @@ -60,7 +60,9 @@ void llvm::SplitString(StringRef Source, void llvm::printEscapedString(StringRef Name, raw_ostream &Out) { for (unsigned i = 0, e = Name.size(); i != e; ++i) { unsigned char C = Name[i]; - if (isPrint(C) && C != '\\' && C != '"') + if (C == '\\') + Out << '\\' << C; + else if (isPrint(C) && C != '"') Out << C; else Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F); diff --git a/llvm/test/Assembler/asm-path-writer.ll b/llvm/test/Assembler/asm-path-writer.ll index f22639566721..a0e63ef2040f 100644 --- a/llvm/test/Assembler/asm-path-writer.ll +++ b/llvm/test/Assembler/asm-path-writer.ll @@ -1,6 +1,6 @@ ; RUN: llvm-as < %s | llvm-dis | FileCheck %s -; CHECK: ^0 = module: (path: ".\5Cf4folder\5Cabc.o", hash: (0, 0, 0, 0, 0)) +; CHECK: ^0 = module: (path: ".\\f4folder\\abc.o", hash: (0, 0, 0, 0, 0)) -^0 = module: (path: ".\5Cf4folder\5Cabc.o", hash: (0, 0, 0, 0, 0)) +^0 = module: (path: ".\5Cf4folder\\abc.o", hash: (0, 0, 0, 0, 0)) ^1 = gv: (guid: 15822663052811949562, summaries: (function: (module: ^0, flags: (linkage: external, notEligibleToImport: 0, live: 0, dsoLocal: 0), insts: 2))) diff --git a/llvm/test/Assembler/source-filename-backslash.ll b/llvm/test/Assembler/source-filename-backslash.ll index 8669401cf93d..c4b25c6053d8 100644 --- a/llvm/test/Assembler/source-filename-backslash.ll +++ b/llvm/test/Assembler/source-filename-backslash.ll @@ -1,8 +1,7 @@ - ; Make sure that llvm-as/llvm-dis properly assemble/disassemble the ; source_filename. ; RUN: llvm-as < %s | llvm-dis | FileCheck %s -; CHECK: source_filename = "C:\5Cpath\5Cwith\5Cbackslashes\5Ctest.cc" -source_filename = "C:\5Cpath\5Cwith\5Cbackslashes\5Ctest.cc" +; CHECK: source_filename = "C:\\path\\with\\backslashes\\test.cc" +source_filename = "C:\\path\\with\5Cbackslashes\\test.cc" diff --git a/llvm/test/CodeGen/MIR/X86/global-value-operands.mir b/llvm/test/CodeGen/MIR/X86/global-value-operands.mir index 6287f99e3983..7b55cb4f14ea 100644 --- a/llvm/test/CodeGen/MIR/X86/global-value-operands.mir +++ b/llvm/test/CodeGen/MIR/X86/global-value-operands.mir @@ -103,7 +103,7 @@ body: | name: test2 body: | bb.0.entry: - ; CHECK: , @"\01Hello@$%09 \5C World,", + ; CHECK: , @"\01Hello@$%09 \\ World,", $rax = MOV64rm $rip, 1, _, @"\01Hello@$%09 \\ World,", _ $eax = MOV32rm killed $rax, 1, _, 0, _ RETQ $eax diff --git a/llvm/unittests/ADT/StringExtrasTest.cpp b/llvm/unittests/ADT/StringExtrasTest.cpp index 97c91de1d3fa..921fc7d349ac 100644 --- a/llvm/unittests/ADT/StringExtrasTest.cpp +++ b/llvm/unittests/ADT/StringExtrasTest.cpp @@ -109,7 +109,7 @@ TEST(StringExtrasTest, printEscapedString) { std::string str; raw_string_ostream OS(str); printEscapedString("ABCdef123&<>\\\"'\t", OS); - EXPECT_EQ("ABCdef123&<>\\5C\\22'\\09", OS.str()); + EXPECT_EQ("ABCdef123&<>\\\\\\22'\\09", OS.str()); } TEST(StringExtrasTest, printHTMLEscaped) { diff --git a/llvm/unittests/IR/MetadataTest.cpp b/llvm/unittests/IR/MetadataTest.cpp index fa0dc61d3dfc..e6c7a5011395 100644 --- a/llvm/unittests/IR/MetadataTest.cpp +++ b/llvm/unittests/IR/MetadataTest.cpp @@ -164,7 +164,7 @@ TEST_F(MDStringTest, PrintingComplex) { std::string Str; raw_string_ostream oss(Str); s->print(oss); - EXPECT_STREQ("!\"\\00\\0A\\22\\5C\\FF\"", oss.str().c_str()); + EXPECT_STREQ("!\"\\00\\0A\\22\\\\\\FF\"", oss.str().c_str()); } typedef MetadataTest MDNodeTest;