clang 24.0.0git
TextEncoding.cpp
Go to the documentation of this file.
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
12#include "llvm/Support/TextEncoding.h"
13
14using namespace clang;
15
16llvm::TextEncodingConverter *
18 switch (Action) {
20 return ToLiteralEncodingConverter.get();
22 // return null for non-z/OS targets
23 return ToIBM1047Converter.get();
24 default:
25 return nullptr;
26 }
27}
28
29std::error_code
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(UTF8, TE.LiteralEncoding);
44 if (ErrorOrLiteralConverter)
45 TE.ToLiteralEncodingConverter = std::make_unique<TextEncodingConverter>(
46 std::move(*ErrorOrLiteralConverter));
47 else
48 return ErrorOrLiteralConverter.getError();
49
50 ErrorOrLiteralConverter =
51 llvm::TextEncodingConverter::create(TE.LiteralEncoding, UTF8);
52 if (ErrorOrLiteralConverter)
53 TE.FromLiteralEncodingConverter = std::make_unique<TextEncodingConverter>(
54 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(UTF8, IBM1047);
69 assert(ErrorOrConverter &&
70 "Failed to create TextEncodingConverter from UTF-8 to IBM-1047");
71 TE.ToIBM1047Converter =
72 std::make_unique<TextEncodingConverter>(std::move(*ErrorOrConverter));
73
74 ErrorOrConverter = llvm::TextEncodingConverter::create(IBM1047, UTF8);
75 assert(ErrorOrConverter &&
76 "Failed to create TextEncodingConverter from IBM-1047 to UTF-8");
77 TE.FromIBM1047Converter =
78 std::make_unique<TextEncodingConverter>(std::move(*ErrorOrConverter));
79
80 return std::error_code();
81}
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
std::string LiteralEncoding
Name of the literal encoding to convert the internal encoding to.
Exposes information about the current target.
Definition TargetInfo.h:228
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
static std::error_code setConvertersFromOptions(TextEncoding &TE, const clang::LangOptions &Opts, TargetInfo &TInfo)
llvm::TextEncodingConverter * getConverter(ConversionAction Action) const
Defines the clang::TargetInfo interface.
Top level wrappers for InstallAPI frontend operations.
ConversionAction
@ CA_ToLiteralEncodingForAsm
@ CA_ToLiteralEncoding
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30