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 "llvm/Support/TextEncoding.h"
12
13using namespace clang;
14
15llvm::TextEncodingConverter *
16TextEncoding::getConverter(ConversionAction Action) const {
17 switch (Action) {
18 case CA_ToLiteralEncoding:
19 return ToLiteralEncodingConverter.get();
20 default:
21 return nullptr;
22 }
23}
24
25std::error_code
26TextEncoding::setConvertersFromOptions(TextEncoding &TE,
27 const clang::LangOptions &Opts) {
28 using namespace llvm;
29
30 const char *UTF8 = "UTF-8";
31 TE.LiteralEncoding =
32 Opts.LiteralEncoding.empty() ? UTF8 : Opts.LiteralEncoding.c_str();
33
34 // Create converter between internal and literal encoding specified
35 // in fexec-charset option.
36 if (TE.LiteralEncoding == UTF8)
37 return std::error_code();
38 ErrorOr<TextEncodingConverter> ErrorOrConverter =
39 llvm::TextEncodingConverter::create(From: UTF8, To: TE.LiteralEncoding);
40 if (ErrorOrConverter)
41 TE.ToLiteralEncodingConverter =
42 std::make_unique<TextEncodingConverter>(args: std::move(*ErrorOrConverter));
43 else
44 return ErrorOrConverter.getError();
45 return std::error_code();
46}
47