Skip to content

Commit d721e8b

Browse files
committed
address some comments
1 parent 2e04f3d commit d721e8b

File tree

4 files changed

+48
-23
lines changed

4 files changed

+48
-23
lines changed

llvm/cmake/config-ix.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ if(LLVM_ENABLE_ICU)
310310
set(CMAKE_FIND_LIBRARY_SUFFIXES ${LIBRARY_SUFFIXES})
311311
endif()
312312

313-
# Check for iconv.
313+
# Check for builtin iconv to avoid licensing issues.
314314
if(LLVM_ENABLE_ICONV)
315315
if (LLVM_ENABLE_ICONV STREQUAL FORCE_ON)
316316
find_package(Iconv REQUIRED)

llvm/include/llvm/Support/CharSet.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class CharSetConverterImplBase {
3333
public:
3434
virtual ~CharSetConverterImplBase() = default;
3535

36+
/// Resets the converter to the initial state.
37+
virtual void reset() = 0;
38+
3639
/// Converts a string.
3740
/// \param[in] Source source string
3841
/// \param[out] Result container for converted string
@@ -52,8 +55,12 @@ class CharSetConverterImplBase {
5255
/// In case of an error, the result string contains the successfully converted
5356
/// part of the input string.
5457
///
55-
virtual std::error_code convert(StringRef Source,
56-
SmallVectorImpl<char> &Result) const = 0;
58+
59+
std::error_code convert(StringRef Source,
60+
SmallVectorImpl<char> &Result) const;
61+
62+
virtual std::error_code convertString(StringRef Source,
63+
SmallVectorImpl<char> &Result) = 0;
5764
};
5865
} // namespace details
5966

@@ -111,12 +118,15 @@ class CharSetConverter {
111118
/// \return error code in case something went wrong
112119
std::error_code convert(StringRef Source,
113120
SmallVectorImpl<char> &Result) const {
114-
return Converter->convert(Source, Result);
121+
auto EC = Converter->convertString(Source, Result);
122+
Converter->reset();
123+
return EC;
115124
}
116125

117126
ErrorOr<std::string> convert(StringRef Source) const {
118127
SmallString<100> Result;
119-
auto EC = Converter->convert(Source, Result);
128+
auto EC = Converter->convertString(Source, Result);
129+
Converter->reset();
120130
if (!EC)
121131
return std::string(Result);
122132
return EC;

llvm/lib/Support/CharSet.cpp

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,15 @@ class CharSetConverterTable : public details::CharSetConverterImplBase {
9191
public:
9292
CharSetConverterTable(ConversionType ConvType) : ConvType(ConvType) {}
9393

94-
std::error_code convert(StringRef Source,
95-
SmallVectorImpl<char> &Result) const override;
94+
std::error_code convertString(StringRef Source,
95+
SmallVectorImpl<char> &Result) override;
96+
97+
void reset() override {}
9698
};
9799

98100
std::error_code
99-
CharSetConverterTable::convert(StringRef Source,
100-
SmallVectorImpl<char> &Result) const {
101+
CharSetConverterTable::convertString(StringRef Source,
102+
SmallVectorImpl<char> &Result) {
101103
if (ConvType == IBM1047ToUTF8) {
102104
ConverterEBCDIC::convertToUTF8(Source, Result);
103105
return std::error_code();
@@ -127,13 +129,15 @@ class CharSetConverterICU : public details::CharSetConverterImplBase {
127129
: FromConvDesc(std::move(FromConverter)),
128130
ToConvDesc(std::move(ToConverter)) {}
129131

130-
std::error_code convert(StringRef Source,
131-
SmallVectorImpl<char> &Result) const override;
132+
std::error_code convertString(StringRef Source,
133+
SmallVectorImpl<char> &Result) override;
134+
135+
void reset() override;
132136
};
133137

134138
std::error_code
135-
CharSetConverterICU::convert(StringRef Source,
136-
SmallVectorImpl<char> &Result) const {
139+
CharSetConverterICU::convertString(StringRef Source,
140+
SmallVectorImpl<char> &Result) {
137141
// Setup the input in case it has no backing data.
138142
size_t InputLength = Source.size();
139143
const char *In = InputLength ? const_cast<char *>(Source.data()) : "";
@@ -171,6 +175,11 @@ CharSetConverterICU::convert(StringRef Source,
171175
return std::error_code();
172176
}
173177

178+
void CharSetConverterICU::reset() {
179+
ucnv_reset(&*FromConvDesc);
180+
ucnv_reset(&*ToConvDesc);
181+
}
182+
174183
#elif defined(HAVE_ICONV)
175184
class CharSetConverterIconv : public details::CharSetConverterImplBase {
176185
class UniqueIconvT {
@@ -202,13 +211,15 @@ class CharSetConverterIconv : public details::CharSetConverterImplBase {
202211
CharSetConverterIconv(UniqueIconvT ConvDesc)
203212
: ConvDesc(std::move(ConvDesc)) {}
204213

205-
std::error_code convert(StringRef Source,
206-
SmallVectorImpl<char> &Result) const override;
214+
std::error_code convertString(StringRef Source,
215+
SmallVectorImpl<char> &Result) override;
216+
217+
void reset() override;
207218
};
208219

209220
std::error_code
210-
CharSetConverterIconv::convert(StringRef Source,
211-
SmallVectorImpl<char> &Result) const {
221+
CharSetConverterIconv::convertString(StringRef Source,
222+
SmallVectorImpl<char> &Result) {
212223
// Setup the output. We directly write into the SmallVector.
213224
size_t Capacity = Result.capacity();
214225
char *Output = static_cast<char *>(Result.data());
@@ -262,10 +273,14 @@ CharSetConverterIconv::convert(StringRef Source,
262273
} while (true);
263274

264275
// Re-adjust size to actual size.
265-
Result.resize(Capacity - OutputLength);
276+
Result.resize(Output - Result.data());
266277
return std::error_code();
267278
}
268279

280+
void CharSetConverterIconv::reset() {
281+
iconv(ConvDesc, nullptr, nullptr, nullptr, nullptr);
282+
}
283+
269284
#endif // HAVE_ICONV
270285
} // namespace
271286

@@ -281,8 +296,7 @@ CharSetConverter CharSetConverter::create(text_encoding::id CPFrom,
281296
CPTo == text_encoding::id::UTF8)
282297
Conversion = IBM1047ToUTF8;
283298
else
284-
assert(false &&
285-
"Only conversions between UTF-8 and IBM-1047 are supported");
299+
llvm_unreachable("Invalid ConversionType!");
286300
std::unique_ptr<details::CharSetConverterImplBase> Converter =
287301
std::make_unique<CharSetConverterTable>(Conversion);
288302

@@ -316,6 +330,7 @@ ErrorOr<CharSetConverter> CharSetConverter::create(StringRef CSFrom,
316330
std::unique_ptr<details::CharSetConverterImplBase> Converter =
317331
std::make_unique<CharSetConverterIconv>(ConvDesc);
318332
return CharSetConverter(std::move(Converter));
319-
#endif
333+
#else
320334
return std::make_error_code(std::errc::invalid_argument);
335+
#endif
321336
}

llvm/unittests/Support/ConvertEBCDICTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static const char AccentE[] = "\xaa\x4a\xb1\xc1\x63\x67\x9e\xc5\x74\x71\x72"
4141
// String with Cyrillic character ya.
4242
static const char CyrillicUTF[] = "\xd0\xaf";
4343

44-
TEST(CharSet, FromUTF8) {
44+
TEST(ConverterEBCDIC, convertToEBCDIC) {
4545
// Hello string.
4646
StringRef Src(HelloA);
4747
SmallString<64> Dst;
@@ -72,7 +72,7 @@ TEST(CharSet, FromUTF8) {
7272
Dst.clear();
7373
}
7474

75-
TEST(CharSet, ToUTF8) {
75+
TEST(ConverterEBCDIC, convertFromEBCDIC) {
7676
// Hello string.
7777
StringRef Src(HelloE);
7878
SmallString<64> Dst;

0 commit comments

Comments
 (0)