Index: include/llvm/ADT/StringExtras.h =================================================================== --- include/llvm/ADT/StringExtras.h +++ include/llvm/ADT/StringExtras.h @@ -99,6 +99,15 @@ return true; } +/// Checks whether character \p C is printable. +/// +/// Locale-independent version of the C standard library isprint whose results +/// may differ on different platforms. +inline bool isPrint(char C) { + unsigned char UC = static_cast(C); + return (0x20 <= UC) && (UC <= 0x7E); +} + /// Returns the corresponding lowercase character if \p x is uppercase. inline char toLower(char x) { if (x >= 'A' && x <= 'Z') Index: include/llvm/Support/raw_ostream.h =================================================================== --- include/llvm/Support/raw_ostream.h +++ include/llvm/Support/raw_ostream.h @@ -220,7 +220,7 @@ raw_ostream &write_uuid(const uuid_t UUID); /// Output \p Str, turning '\\', '\t', '\n', '"', and anything that doesn't - /// satisfy std::isprint into an escape sequence. + /// satisfy llvm::isPrint into an escape sequence. raw_ostream &write_escaped(StringRef Str, bool UseHexEscapes = false); raw_ostream &write(unsigned char C); Index: lib/MC/MCAsmStreamer.cpp =================================================================== --- lib/MC/MCAsmStreamer.cpp +++ lib/MC/MCAsmStreamer.cpp @@ -815,7 +815,7 @@ continue; } - if (isprint((unsigned char)C)) { + if (isPrint((unsigned char)C)) { OS << (char)C; continue; } Index: lib/ProfileData/InstrProfReader.cpp =================================================================== --- lib/ProfileData/InstrProfReader.cpp +++ lib/ProfileData/InstrProfReader.cpp @@ -129,7 +129,7 @@ StringRef buffer = Buffer.getBufferStart(); return count == 0 || std::all_of(buffer.begin(), buffer.begin() + count, - [](char c) { return ::isprint(c) || ::isspace(c); }); + [](char c) { return isPrint(c) || ::isspace(c); }); } // Read the profile variant flag from the header: ":FE" means this is a FE Index: lib/Support/StringExtras.cpp =================================================================== --- lib/Support/StringExtras.cpp +++ lib/Support/StringExtras.cpp @@ -61,7 +61,7 @@ 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 (isPrint(C) && C != '\\' && C != '"') Out << C; else Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F); Index: lib/Support/raw_ostream.cpp =================================================================== --- lib/Support/raw_ostream.cpp +++ lib/Support/raw_ostream.cpp @@ -160,7 +160,7 @@ *this << '\\' << '"'; break; default: - if (std::isprint(c)) { + if (isPrint(c)) { *this << c; break; } @@ -436,7 +436,7 @@ // Print the ASCII char values for each byte on this line for (uint8_t Byte : Line) { - if (isprint(Byte)) + if (isPrint(Byte)) *this << static_cast(Byte); else *this << '.'; Index: lib/Support/regengine.inc =================================================================== --- lib/Support/regengine.inc +++ lib/Support/regengine.inc @@ -1013,7 +1013,7 @@ { static char pbuf[10]; - if (isprint(ch) || ch == ' ') + if (isPrint(ch) || ch == ' ') (void)snprintf(pbuf, sizeof pbuf, "%c", ch); else (void)snprintf(pbuf, sizeof pbuf, "\\%o", ch); Index: tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp =================================================================== --- tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp +++ tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp @@ -699,7 +699,7 @@ std::string Str; bool ArrayIsPrintable = true; for (unsigned j = i - 1, je = Record.size(); j != je; ++j) { - if (!isprint(static_cast(Record[j]))) { + if (!isPrint(static_cast(Record[j]))) { ArrayIsPrintable = false; break; } @@ -719,7 +719,7 @@ } else { bool BlobIsPrintable = true; for (unsigned i = 0, e = Blob.size(); i != e; ++i) - if (!isprint(static_cast(Blob[i]))) { + if (!isPrint(static_cast(Blob[i]))) { BlobIsPrintable = false; break; } Index: tools/llvm-objdump/llvm-objdump.cpp =================================================================== --- tools/llvm-objdump/llvm-objdump.cpp +++ tools/llvm-objdump/llvm-objdump.cpp @@ -1651,7 +1651,7 @@ } Byte = Bytes.slice(Index)[0]; outs() << format(" %02x", Byte); - AsciiData[NumBytes] = isprint(Byte) ? Byte : '.'; + AsciiData[NumBytes] = isPrint(Byte) ? Byte : '.'; uint8_t IndentOffset = 0; NumBytes++; @@ -1899,7 +1899,7 @@ // Print ascii. outs() << " "; for (std::size_t i = 0; i < 16 && addr + i < end; ++i) { - if (std::isprint(static_cast(Contents[addr + i]) & 0xFF)) + if (isPrint(static_cast(Contents[addr + i]) & 0xFF)) outs() << Contents[addr + i]; else outs() << "."; Index: tools/llvm-readobj/ObjDumper.cpp =================================================================== --- tools/llvm-readobj/ObjDumper.cpp +++ tools/llvm-readobj/ObjDumper.cpp @@ -29,7 +29,7 @@ static void printAsPrintable(raw_ostream &W, const uint8_t *Start, size_t Len) { for (size_t i = 0; i < Len; i++) - W << (isprint(Start[i]) ? static_cast(Start[i]) : '.'); + W << (isPrint(Start[i]) ? static_cast(Start[i]) : '.'); } static Expected @@ -138,7 +138,7 @@ TmpSecPtr = SecPtr; for (i = 0; TmpSecPtr + i < SecEnd && i < 16; ++i) - W.startLine() << (isprint(TmpSecPtr[i]) ? static_cast(TmpSecPtr[i]) + W.startLine() << (isPrint(TmpSecPtr[i]) ? static_cast(TmpSecPtr[i]) : '.'); W.startLine() << '\n'; Index: unittests/ADT/StringExtrasTest.cpp =================================================================== --- unittests/ADT/StringExtrasTest.cpp +++ unittests/ADT/StringExtrasTest.cpp @@ -13,6 +13,17 @@ using namespace llvm; +TEST(StringExtrasTest, isPrint) { + EXPECT_FALSE(isPrint('\0')); + EXPECT_FALSE(isPrint('\t')); + EXPECT_TRUE(isPrint('0')); + EXPECT_TRUE(isPrint('a')); + EXPECT_TRUE(isPrint('A')); + EXPECT_TRUE(isPrint(' ')); + EXPECT_TRUE(isPrint('~')); + EXPECT_TRUE(isPrint('?')); +} + TEST(StringExtrasTest, Join) { std::vector Items; EXPECT_EQ("", join(Items.begin(), Items.end(), " ")); @@ -93,6 +104,13 @@ EXPECT_EQ("abcdefg01234.,&!~`'}\"", OS.str()); } +TEST(StringExtrasTest, printEscapedString) { + std::string str; + raw_string_ostream OS(str); + printEscapedString("ABCdef123&<>\\\"'\t", OS); + EXPECT_EQ("ABCdef123&<>\\5C\\22'\\09", OS.str()); +} + TEST(StringExtrasTest, printHTMLEscaped) { std::string str; raw_string_ostream OS(str);