diff options
author | Michael Kruse <llvm@meinersbur.de> | 2018-07-26 15:31:41 +0000 |
---|---|---|
committer | Michael Kruse <llvm@meinersbur.de> | 2018-07-26 15:31:41 +0000 |
commit | 6f1da6e345ccf89a7a0d0bc964bee7f1eb7ed9f6 (patch) | |
tree | 1465f700b8ebf02710fb2c9734ebd1d8972f0b91 /llvm/lib | |
parent | eda3c9efa2bfc2dad3d8cde62334577cee1d9d6b (diff) | |
download | bcm5719-llvm-6f1da6e345ccf89a7a0d0bc964bee7f1eb7ed9f6.tar.gz bcm5719-llvm-6f1da6e345ccf89a7a0d0bc964bee7f1eb7ed9f6.zip |
[ADT] Replace std::isprint by llvm::isPrint.
The standard library functions ::isprint/std::isprint have platform-
and locale-dependent behavior which makes LLVM's output less
predictable. In particular, regression tests my fail depending on the
implementation of these functions.
Implement llvm::isPrint in StringExtras.h with a standard behavior and
replace all uses of ::isprint/std::isprint by a call it llvm::isPrint.
The function is inlined and does not look up language settings so it
should perform better than the standard library's version.
Such a replacement has already been done for isdigit, isalpha, isxdigit
in r314883. gtest does the same in gtest-printers.cc using the following
justification:
// Returns true if c is a printable ASCII character. We test the
// value of c directly instead of calling isprint(), which is buggy on
// Windows Mobile.
inline bool IsPrintableAscii(wchar_t c) {
return 0x20 <= c && c <= 0x7E;
}
Similar issues have also been encountered by Julia:
https://github.com/JuliaLang/julia/issues/7416
I noticed the problem myself when on Windows isprint('\t') started to
evaluate to true (see https://stackoverflow.com/questions/51435249) and
thus caused several unit tests to fail. The result of isprint doesn't
seem to be well-defined even for ASCII characters. Therefore I suggest
to replace isprint by a platform-independent version.
Differential Revision: https://reviews.llvm.org/D49680
llvm-svn: 338034
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/MC/MCAsmStreamer.cpp | 2 | ||||
-rw-r--r-- | llvm/lib/ProfileData/InstrProfReader.cpp | 2 | ||||
-rw-r--r-- | llvm/lib/Support/StringExtras.cpp | 2 | ||||
-rw-r--r-- | llvm/lib/Support/raw_ostream.cpp | 4 | ||||
-rw-r--r-- | llvm/lib/Support/regengine.inc | 2 |
5 files changed, 6 insertions, 6 deletions
diff --git a/llvm/lib/MC/MCAsmStreamer.cpp b/llvm/lib/MC/MCAsmStreamer.cpp index 1429f647b61..88154bd7b25 100644 --- a/llvm/lib/MC/MCAsmStreamer.cpp +++ b/llvm/lib/MC/MCAsmStreamer.cpp @@ -815,7 +815,7 @@ static void PrintQuotedString(StringRef Data, raw_ostream &OS) { continue; } - if (isprint((unsigned char)C)) { + if (isPrint((unsigned char)C)) { OS << (char)C; continue; } diff --git a/llvm/lib/ProfileData/InstrProfReader.cpp b/llvm/lib/ProfileData/InstrProfReader.cpp index 64f98ad0051..3b704158a5c 100644 --- a/llvm/lib/ProfileData/InstrProfReader.cpp +++ b/llvm/lib/ProfileData/InstrProfReader.cpp @@ -129,7 +129,7 @@ bool TextInstrProfReader::hasFormat(const MemoryBuffer &Buffer) { 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 diff --git a/llvm/lib/Support/StringExtras.cpp b/llvm/lib/Support/StringExtras.cpp index d566b72a8d0..386d74a4798 100644 --- a/llvm/lib/Support/StringExtras.cpp +++ b/llvm/lib/Support/StringExtras.cpp @@ -61,7 +61,7 @@ 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 (isPrint(C) && C != '\\' && C != '"') Out << C; else Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F); diff --git a/llvm/lib/Support/raw_ostream.cpp b/llvm/lib/Support/raw_ostream.cpp index b2260c1865e..1dae469958f 100644 --- a/llvm/lib/Support/raw_ostream.cpp +++ b/llvm/lib/Support/raw_ostream.cpp @@ -160,7 +160,7 @@ raw_ostream &raw_ostream::write_escaped(StringRef Str, *this << '\\' << '"'; break; default: - if (std::isprint(c)) { + if (isPrint(c)) { *this << c; break; } @@ -436,7 +436,7 @@ raw_ostream &raw_ostream::operator<<(const FormattedBytes &FB) { // 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<char>(Byte); else *this << '.'; diff --git a/llvm/lib/Support/regengine.inc b/llvm/lib/Support/regengine.inc index 62d8c267f22..41787aff124 100644 --- a/llvm/lib/Support/regengine.inc +++ b/llvm/lib/Support/regengine.inc @@ -1013,7 +1013,7 @@ pchar(int ch) { 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); |