| 1 | //===--- TextEncoding.cpp -------------------------------------------------===// |
|---|---|
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "clang/Lex/TextEncoding.h" |
| 10 | #include "clang/Basic/DiagnosticDriver.h" |
| 11 | #include "clang/Basic/TargetInfo.h" |
| 12 | #include "llvm/Support/TextEncoding.h" |
| 13 | |
| 14 | using namespace clang; |
| 15 | |
| 16 | llvm::TextEncodingConverter * |
| 17 | TextEncoding::getConverter(ConversionAction Action) const { |
| 18 | switch (Action) { |
| 19 | case CA_ToLiteralEncoding: |
| 20 | return ToLiteralEncodingConverter.get(); |
| 21 | case CA_ToLiteralEncodingForAsm: |
| 22 | // return null for non-z/OS targets |
| 23 | return ToIBM1047Converter.get(); |
| 24 | default: |
| 25 | return nullptr; |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | std::error_code |
| 30 | TextEncoding::setConvertersFromOptions(TextEncoding &TE, |
| 31 | const clang::LangOptions &Opts, |
| 32 | clang::TargetInfo &TInfo) { |
| 33 | using namespace llvm; |
| 34 | |
| 35 | const char *UTF8 = "UTF-8"; |
| 36 | TE.LiteralEncoding = |
| 37 | Opts.LiteralEncoding.empty() ? UTF8 : Opts.LiteralEncoding.c_str(); |
| 38 | |
| 39 | // Create converters between internal and literal encoding specified |
| 40 | // in fexec-charset option. |
| 41 | if (TE.LiteralEncoding != UTF8) { |
| 42 | ErrorOr<TextEncodingConverter> ErrorOrLiteralConverter = |
| 43 | llvm::TextEncodingConverter::create(From: UTF8, To: TE.LiteralEncoding); |
| 44 | if (ErrorOrLiteralConverter) |
| 45 | TE.ToLiteralEncodingConverter = std::make_unique<TextEncodingConverter>( |
| 46 | args: std::move(*ErrorOrLiteralConverter)); |
| 47 | else |
| 48 | return ErrorOrLiteralConverter.getError(); |
| 49 | |
| 50 | ErrorOrLiteralConverter = |
| 51 | llvm::TextEncodingConverter::create(From: TE.LiteralEncoding, To: UTF8); |
| 52 | if (ErrorOrLiteralConverter) |
| 53 | TE.FromLiteralEncodingConverter = std::make_unique<TextEncodingConverter>( |
| 54 | args: std::move(*ErrorOrLiteralConverter)); |
| 55 | else |
| 56 | return ErrorOrLiteralConverter.getError(); |
| 57 | } |
| 58 | |
| 59 | // The IBM-1047 converters are only needed on z/OS, where the system |
| 60 | // (assembler/linker) default encoding is IBM-1047 rather than UTF-8. |
| 61 | if (TInfo.getTriple().getOS() != llvm::Triple::ZOS) |
| 62 | return std::error_code(); |
| 63 | |
| 64 | // Create converters between UTF-8 and IBM-1047 for use when processing asm |
| 65 | // string literals on z/OS, where escape sequences are IBM-1047 code points. |
| 66 | const char *IBM1047 = "IBM-1047"; |
| 67 | ErrorOr<TextEncodingConverter> ErrorOrConverter = |
| 68 | llvm::TextEncodingConverter::create(From: UTF8, To: IBM1047); |
| 69 | assert(ErrorOrConverter && |
| 70 | "Failed to create TextEncodingConverter from UTF-8 to IBM-1047"); |
| 71 | TE.ToIBM1047Converter = |
| 72 | std::make_unique<TextEncodingConverter>(args: std::move(*ErrorOrConverter)); |
| 73 | |
| 74 | ErrorOrConverter = llvm::TextEncodingConverter::create(From: IBM1047, To: UTF8); |
| 75 | assert(ErrorOrConverter && |
| 76 | "Failed to create TextEncodingConverter from IBM-1047 to UTF-8"); |
| 77 | TE.FromIBM1047Converter = |
| 78 | std::make_unique<TextEncodingConverter>(args: std::move(*ErrorOrConverter)); |
| 79 | |
| 80 | return std::error_code(); |
| 81 | } |
| 82 |