Skip to content

Enable fexec-charset option #138895

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions clang/docs/LanguageExtensions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,9 @@ Builtin Macros

``__clang_literal_encoding__``
Defined to a narrow string literal that represents the current encoding of
narrow string literals, e.g., ``"hello"``. This macro typically expands to
"UTF-8" (but may change in the future if the
``-fexec-charset="Encoding-Name"`` option is implemented.)
narrow string literals, e.g., ``"hello"``. This macro expands to the text
encoding specified by ``-fexec-charset`` if any, or a system-specific default
otherwise: ``"IBM-1047"`` on z/OS and ``"UTF-8"`` on all other systems.

``__clang_wide_literal_encoding__``
Defined to a narrow string literal that represents the current encoding of
Expand Down
3 changes: 3 additions & 0 deletions clang/include/clang/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,9 @@ class LangOptions : public LangOptionsBase {
bool AtomicFineGrainedMemory = false;
bool AtomicIgnoreDenormalMode = false;

/// Name of the execution encoding to convert the internal encoding to.
std::string ExecEncoding;

LangOptions();

/// Set language defaults for the given input language and
Expand Down
7 changes: 7 additions & 0 deletions clang/include/clang/Basic/TokenKinds.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ inline bool isLiteral(TokenKind K) {
isStringLiteral(K) || K == tok::header_name || K == tok::binary_data;
}

/// Return true if this is a utf literal kind.
inline bool isUTFLiteral(TokenKind K) {
return K == tok::utf8_char_constant || K == tok::utf8_string_literal ||
K == tok::utf16_char_constant || K == tok::utf16_string_literal ||
K == tok::utf32_char_constant || K == tok::utf32_string_literal;
}

/// Return true if this is any of tok::annot_* kinds.
bool isAnnotation(TokenKind K);

Expand Down
5 changes: 5 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -7247,6 +7247,11 @@ let Visibility = [CC1Option, CC1AsOption, FC1Option] in {
def tune_cpu : Separate<["-"], "tune-cpu">,
HelpText<"Tune for a specific cpu type">,
MarshallingInfoString<TargetOpts<"TuneCPU">>;
def fexec_charset : Separate<["-"], "fexec-charset">, MetaVarName<"<charset>">,
HelpText<"Set the execution <charset> for string and character literals. "
"Supported character encodings include ISO8859-1, UTF-8, IBM-1047 "
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is too long for HelpText, which is displayed by clang --help. Also, aren't ISO8859-1, UTF-8, and IBM-1047 all covered by "those supported by the host icu or iconv library"? Do we really need to call them out separately?

Maybe something like "Use for string and character literals" could work here as help text? You can put longer information with examples of character set names and references to icu / iconv into a separate DocBrief argument.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was because ConvertEBCDIC.cpp has a table for converting between EBCDIC and UTF-8 so those conversions will always be supported even without icu/iconv. I can definitely update this to something more succinct

"and those supported by the host icu or iconv library.">,
MarshallingInfoString<LangOpts<"ExecEncoding">>;
def target_cpu : Separate<["-"], "target-cpu">,
HelpText<"Target a specific cpu type">,
MarshallingInfoString<TargetOpts<"CPU">>;
Expand Down
35 changes: 35 additions & 0 deletions clang/include/clang/Lex/LiteralConverter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//===--- clang/Lex/LiteralConverter.h - Translator for Literals -*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_LEX_LITERALCONVERTER_H
#define LLVM_CLANG_LEX_LITERALCONVERTER_H

#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/LangOptions.h"
#include "clang/Basic/TargetInfo.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/TextEncoding.h"

enum ConversionAction { NoConversion, ToSystemEncoding, ToExecEncoding };

class LiteralConverter {
llvm::StringRef InternalEncoding;
llvm::StringRef SystemEncoding;
llvm::StringRef ExecEncoding;
llvm::TextEncodingConverter *ToSystemEncodingConverter;
llvm::TextEncodingConverter *ToExecEncodingConverter;

public:
llvm::TextEncodingConverter *getConverter(ConversionAction Action);
void setConvertersFromOptions(const clang::LangOptions &Opts,
const clang::TargetInfo &TInfo,
clang::DiagnosticsEngine &Diags);
Comment on lines +30 to +32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer, for example a static fuction that returns an optional or a null pointer failure, and let the caller call deal with error

};

#endif
19 changes: 11 additions & 8 deletions clang/include/clang/Lex/LiteralSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
#include "clang/Basic/CharInfo.h"
#include "clang/Basic/LLVM.h"
#include "clang/Basic/TokenKinds.h"
#include "clang/Lex/LiteralConverter.h"
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/DataTypes.h"

#include "llvm/Support/TextEncoding.h"
namespace clang {

class DiagnosticsEngine;
Expand Down Expand Up @@ -233,6 +234,7 @@ class StringLiteralParser {
const LangOptions &Features;
const TargetInfo &Target;
DiagnosticsEngine *Diags;
LiteralConverter *LiteralConv;

unsigned MaxTokenLength;
unsigned SizeBound;
Expand All @@ -246,18 +248,19 @@ class StringLiteralParser {
StringLiteralEvalMethod EvalMethod;

public:
StringLiteralParser(ArrayRef<Token> StringToks, Preprocessor &PP,
StringLiteralEvalMethod StringMethod =
StringLiteralEvalMethod::Evaluated);
StringLiteralParser(
ArrayRef<Token> StringToks, Preprocessor &PP,
StringLiteralEvalMethod StringMethod = StringLiteralEvalMethod::Evaluated,
ConversionAction Action = ToExecEncoding);
StringLiteralParser(ArrayRef<Token> StringToks, const SourceManager &sm,
const LangOptions &features, const TargetInfo &target,
DiagnosticsEngine *diags = nullptr)
: SM(sm), Features(features), Target(target), Diags(diags),
MaxTokenLength(0), SizeBound(0), CharByteWidth(0), Kind(tok::unknown),
ResultPtr(ResultBuf.data()),
LiteralConv(nullptr), MaxTokenLength(0), SizeBound(0), CharByteWidth(0),
Kind(tok::unknown), ResultPtr(ResultBuf.data()),
EvalMethod(StringLiteralEvalMethod::Evaluated), hadError(false),
Pascal(false) {
init(StringToks);
init(StringToks, NoConversion);
}

bool hadError;
Expand Down Expand Up @@ -305,7 +308,7 @@ class StringLiteralParser {
static bool isValidUDSuffix(const LangOptions &LangOpts, StringRef Suffix);

private:
void init(ArrayRef<Token> StringToks);
void init(ArrayRef<Token> StringToks, ConversionAction Action);
bool CopyStringFragment(const Token &Tok, const char *TokBegin,
StringRef Fragment);
void DiagnoseLexingError(SourceLocation Loc);
Expand Down
3 changes: 3 additions & 0 deletions clang/include/clang/Lex/Preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "clang/Basic/TokenKinds.h"
#include "clang/Lex/HeaderSearch.h"
#include "clang/Lex/Lexer.h"
#include "clang/Lex/LiteralConverter.h"
#include "clang/Lex/MacroInfo.h"
#include "clang/Lex/ModuleLoader.h"
#include "clang/Lex/ModuleMap.h"
Expand Down Expand Up @@ -162,6 +163,7 @@ class Preprocessor {
std::unique_ptr<ScratchBuffer> ScratchBuf;
HeaderSearch &HeaderInfo;
ModuleLoader &TheModuleLoader;
LiteralConverter LiteralConv;

/// External source of macros.
ExternalPreprocessorSource *ExternalSource;
Expand Down Expand Up @@ -1224,6 +1226,7 @@ class Preprocessor {
SelectorTable &getSelectorTable() { return Selectors; }
Builtin::Context &getBuiltinInfo() { return *BuiltinInfo; }
llvm::BumpPtrAllocator &getPreprocessorAllocator() { return BP; }
LiteralConverter &getLiteralConverter() { return LiteralConv; }

void setExternalSource(ExternalPreprocessorSource *Source) {
ExternalSource = Source;
Expand Down
21 changes: 15 additions & 6 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/TextEncoding.h"
#include "llvm/Support/YAMLParser.h"
#include "llvm/TargetParser/AArch64TargetParser.h"
#include "llvm/TargetParser/ARMTargetParserCommon.h"
Expand Down Expand Up @@ -7589,12 +7590,20 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
<< value;
}

// -fexec_charset=UTF-8 is default. Reject others
if (Arg *execCharset = Args.getLastArg(options::OPT_fexec_charset_EQ)) {
StringRef value = execCharset->getValue();
if (!value.equals_insensitive("utf-8"))
D.Diag(diag::err_drv_invalid_value) << execCharset->getAsString(Args)
<< value;
// Set the default fexec-charset as the system charset.
CmdArgs.push_back("-fexec-charset");
CmdArgs.push_back(Args.MakeArgString(Triple.getDefaultTextEncoding()));
if (Arg *execEncoding = Args.getLastArg(options::OPT_fexec_charset_EQ)) {
StringRef value = execEncoding->getValue();
llvm::ErrorOr<llvm::TextEncodingConverter> ErrorOrConverter =
llvm::TextEncodingConverter::create("UTF-8", value.data());
if (ErrorOrConverter) {
CmdArgs.push_back("-fexec-charset");
CmdArgs.push_back(Args.MakeArgString(value));
} else {
D.Diag(diag::err_drv_invalid_value)
<< execEncoding->getAsString(Args) << value;
}
}

RenderDiagnosticsOptions(D, Args, CmdArgs);
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/Frontend/CompilerInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "clang/Frontend/Utils.h"
#include "clang/Frontend/VerifyDiagnosticConsumer.h"
#include "clang/Lex/HeaderSearch.h"
#include "clang/Lex/LiteralConverter.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Lex/PreprocessorOptions.h"
#include "clang/Sema/CodeCompleteConsumer.h"
Expand Down Expand Up @@ -535,6 +536,9 @@ void CompilerInstance::createPreprocessor(TranslationUnitKind TUKind) {

if (GetDependencyDirectives)
PP->setDependencyDirectivesGetter(*GetDependencyDirectives);

PP->getLiteralConverter().setConvertersFromOptions(getLangOpts(), getTarget(),
getDiagnostics());
}

std::string CompilerInstance::getSpecificModuleCachePath(StringRef ModuleHash) {
Expand Down
12 changes: 8 additions & 4 deletions clang/lib/Frontend/InitPreprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1057,10 +1057,14 @@ static void InitializePredefinedMacros(const TargetInfo &TI,
}
}

// Macros to help identify the narrow and wide character sets
// FIXME: clang currently ignores -fexec-charset=. If this changes,
// then this may need to be updated.
Builder.defineMacro("__clang_literal_encoding__", "\"UTF-8\"");
// Macros to help identify the narrow and wide character sets. This is set
// to fexec-charset. If fexec-charset is not specified, the default is the
// system charset.
if (!LangOpts.ExecEncoding.empty())
Builder.defineMacro("__clang_literal_encoding__", LangOpts.ExecEncoding);
else
Builder.defineMacro("__clang_literal_encoding__",
TI.getTriple().getDefaultTextEncoding());
if (TI.getTypeWidth(TI.getWCharType()) >= 32) {
// FIXME: 32-bit wchar_t signals UTF-32. This may change
// if -fwide-exec-charset= is ever supported.
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Lex/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ add_clang_library(clangLex
InitHeaderSearch.cpp
Lexer.cpp
LexHLSLRootSignature.cpp
LiteralConverter.cpp
LiteralSupport.cpp
MacroArgs.cpp
MacroInfo.cpp
Expand Down
53 changes: 53 additions & 0 deletions clang/lib/Lex/LiteralConverter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//===--- LiteralConverter.cpp - Translator for String Literals -----------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "clang/Lex/LiteralConverter.h"
#include "clang/Basic/DiagnosticDriver.h"

using namespace llvm;

llvm::TextEncodingConverter *
LiteralConverter::getConverter(ConversionAction Action) {
if (Action == ToSystemEncoding)
return ToSystemEncodingConverter;
else if (Action == ToExecEncoding)
return ToExecEncodingConverter;
else
return nullptr;
}

void LiteralConverter::setConvertersFromOptions(
const clang::LangOptions &Opts, const clang::TargetInfo &TInfo,
clang::DiagnosticsEngine &Diags) {
using namespace llvm;
InternalEncoding = "UTF-8";
SystemEncoding = TInfo.getTriple().getDefaultTextEncoding();
ExecEncoding =
Opts.ExecEncoding.empty() ? InternalEncoding : Opts.ExecEncoding;
// Create converter between internal and system encoding
if (InternalEncoding != SystemEncoding) {
ErrorOr<TextEncodingConverter> ErrorOrConverter =
llvm::TextEncodingConverter::create(InternalEncoding, SystemEncoding);
if (!ErrorOrConverter)
return;
ToSystemEncodingConverter =
new TextEncodingConverter(std::move(*ErrorOrConverter));
}

// Create converter between internal and exec encoding specified
// in fexec-charset option.
if (InternalEncoding == ExecEncoding)
return;
ErrorOr<TextEncodingConverter> ErrorOrConverter =
llvm::TextEncodingConverter::create(InternalEncoding, ExecEncoding);
if (!ErrorOrConverter)
Diags.Report(clang::diag::err_drv_invalid_value)
<< "-fexec-charset" << ExecEncoding;
ToExecEncodingConverter =
new TextEncodingConverter(std::move(*ErrorOrConverter));
}
Loading
Loading