Skip to content

Commit 734c25a

Browse files
committed
add error checking for text_encoding function, address other comments
1 parent eeaf034 commit 734c25a

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

llvm/cmake/config-ix.cmake

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,13 +295,13 @@ if(LLVM_HAS_LOGF128)
295295
endif()
296296

297297
if (LLVM_ENABLE_ICU STREQUAL FORCE_ON AND LLVM_ENABLE_ICONV STREQUAL FORCE_ON)
298-
message(FATAL_ERROR "Both LLVM_ENABLE_ICU and LLVM_ENABLE_ICONV should not be FORCE_ON")
298+
message(FATAL_ERROR "LLVM_ENABLE_ICU and LLVM_ENABLE_ICONV should not both be FORCE_ON")
299299
endif()
300300

301-
# Check for ICU.
301+
# Check for ICU. Only allow an optional, dynamic link for ICU so we don't impact LLVM's licensing.
302302
if(LLVM_ENABLE_ICU AND NOT(LLVM_ENABLE_ICONV STREQUAL FORCE_ON))
303303
set(LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
304-
set(CMAKE_FIND_LIBRARY_SUFFIXES ".so")
304+
set(CMAKE_FIND_LIBRARY_SUFFIXES "${CMAKE_SHARED_LIBRARY_SUFFIX}")
305305
if (LLVM_ENABLE_ICU STREQUAL FORCE_ON)
306306
find_package(ICU REQUIRED COMPONENTS uc i18n)
307307
if (NOT ICU_FOUND)

llvm/include/llvm/Support/CharSet.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include "llvm/Config/config.h"
2121
#include "llvm/Support/ErrorOr.h"
2222

23-
#include <functional>
2423
#include <string>
2524
#include <system_error>
2625

@@ -89,11 +88,13 @@ class CharSetConverter {
8988

9089
public:
9190
/// Creates a CharSetConverter instance.
91+
/// Returns std::errc::invalid_argument in case the requested conversion is
92+
/// not supported.
9293
/// \param[in] CSFrom the source character encoding
9394
/// \param[in] CSTo the target character encoding
94-
/// \return a CharSetConverter instance
95-
static CharSetConverter create(text_encoding::id CSFrom,
96-
text_encoding::id CSTo);
95+
/// \return a CharSetConverter instance or an error code
96+
static ErrorOr<CharSetConverter> create(text_encoding::id CSFrom,
97+
text_encoding::id CSTo);
9798

9899
/// Creates a CharSetConverter instance.
99100
/// Returns std::errc::invalid_argument in case the requested conversion is

llvm/lib/Support/CharSet.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,9 @@ static std::optional<text_encoding::id> getKnownCharSet(StringRef CSName) {
5757
return std::nullopt;
5858
}
5959

60-
#if defined(HAVE_ICONV) || defined(HAVE_ICU)
61-
static void HandleOverflow(size_t &Capacity, char *&Output,
62-
size_t &OutputLength,
63-
SmallVectorImpl<char> &Result) {
60+
LLVM_ATTRIBUTE_UNUSED static void
61+
HandleOverflow(size_t &Capacity, char *&Output, size_t &OutputLength,
62+
SmallVectorImpl<char> &Result) {
6463
// No space left in output buffer. Double the size of the underlying
6564
// memory in the SmallVectorImpl, adjust pointer and length and continue
6665
// the conversion.
@@ -72,7 +71,6 @@ static void HandleOverflow(size_t &Capacity, char *&Output,
7271
Output = static_cast<char *>(Result.data());
7372
OutputLength = Capacity;
7473
}
75-
#endif
7674

7775
namespace {
7876
enum ConversionType {
@@ -290,8 +288,8 @@ void CharSetConverterIconv::reset() {
290288
#endif // HAVE_ICONV
291289
} // namespace
292290

293-
CharSetConverter CharSetConverter::create(text_encoding::id CPFrom,
294-
text_encoding::id CPTo) {
291+
ErrorOr<CharSetConverter> CharSetConverter::create(text_encoding::id CPFrom,
292+
text_encoding::id CPTo) {
295293

296294
assert(CPFrom != CPTo && "Text encodings should be distinct");
297295

@@ -302,19 +300,22 @@ CharSetConverter CharSetConverter::create(text_encoding::id CPFrom,
302300
CPTo == text_encoding::id::UTF8)
303301
Conversion = IBM1047ToUTF8;
304302
else
305-
llvm_unreachable("Invalid ConversionType!");
303+
return std::error_code(errno, std::generic_category());
304+
306305
std::unique_ptr<details::CharSetConverterImplBase> Converter =
307306
std::make_unique<CharSetConverterTable>(Conversion);
308-
309307
return CharSetConverter(std::move(Converter));
310308
}
311309

312310
ErrorOr<CharSetConverter> CharSetConverter::create(StringRef CSFrom,
313311
StringRef CSTo) {
314312
std::optional<text_encoding::id> From = getKnownCharSet(CSFrom);
315313
std::optional<text_encoding::id> To = getKnownCharSet(CSTo);
316-
if (From && To)
317-
return create(*From, *To);
314+
if (From && To) {
315+
ErrorOr<CharSetConverter> Converter = create(*From, *To);
316+
if (Converter)
317+
return Converter;
318+
}
318319
#ifdef HAVE_ICU
319320
UErrorCode EC = U_ZERO_ERROR;
320321
UConverterUniquePtr FromConvDesc(ucnv_open(CSFrom.str().c_str(), &EC));

0 commit comments

Comments
 (0)