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/unittests/ADT | |
| 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/unittests/ADT')
| -rw-r--r-- | llvm/unittests/ADT/StringExtrasTest.cpp | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/llvm/unittests/ADT/StringExtrasTest.cpp b/llvm/unittests/ADT/StringExtrasTest.cpp index f98f388f64f..1df200553a6 100644 --- a/llvm/unittests/ADT/StringExtrasTest.cpp +++ b/llvm/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<std::string> Items; EXPECT_EQ("", join(Items.begin(), Items.end(), " <sep> ")); @@ -93,6 +104,13 @@ TEST(StringExtrasTest, printLowerCase) { 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); |

