From ed4e2950bc31eefd1fb4b4c40b99c1e712876734 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Thu, 5 Nov 2009 20:14:16 +0000 Subject: [PATCH] Adding -fshort-wchar option. llvm-svn: 86167 --- .../clang/Basic/DiagnosticFrontendKinds.td | 3 +++ clang/include/clang/Basic/LangOptions.h | 2 ++ clang/include/clang/Basic/TargetInfo.h | 5 +++++ clang/include/clang/Driver/Options.def | 1 + clang/lib/Basic/TargetInfo.cpp | 10 ++++++++++ clang/lib/Driver/Tools.cpp | 7 +++++++ clang/lib/Frontend/PCHReader.cpp | 2 ++ clang/lib/Frontend/PCHWriter.cpp | 1 + clang/test/Driver/clang_f_opts.c | 3 ++- clang/test/Sema/wchar.c | 20 +++++++++++++------ clang/tools/clang-cc/clang-cc.cpp | 8 ++++++++ 11 files changed, 55 insertions(+), 7 deletions(-) diff --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td b/clang/include/clang/Basic/DiagnosticFrontendKinds.td index ae8b92394443..a3d790625586 100644 --- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td +++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td @@ -162,6 +162,9 @@ def warn_pch_access_control : Error< def warn_pch_char_signed : Error< "char was %select{unsigned|signed}0 in the PCH file but " "is currently %select{unsigned|signed}1">; +def warn_pch_short_wchar : Error< + "-fshort-wchar was %select{disabled|enabled}0 in the PCH file but " + "is currently %select{disabled|enabled}1">; def err_not_a_pch_file : Error< "'%0' does not appear to be a precompiled header file">, DefaultFatal; diff --git a/clang/include/clang/Basic/LangOptions.h b/clang/include/clang/Basic/LangOptions.h index d4d3fe50eba0..a63edd57fb2c 100644 --- a/clang/include/clang/Basic/LangOptions.h +++ b/clang/include/clang/Basic/LangOptions.h @@ -83,6 +83,7 @@ public: unsigned AccessControl : 1; // Whether C++ access control should // be enabled. unsigned CharIsSigned : 1; // Whether char is a signed or unsigned type + unsigned ShortWChar : 1; // Force wchar_t to be unsigned short int. unsigned OpenCL : 1; // OpenCL C99 language extensions. @@ -159,6 +160,7 @@ public: NoInline = 0; CharIsSigned = 1; + ShortWChar = 0; MainFileName = 0; } diff --git a/clang/include/clang/Basic/TargetInfo.h b/clang/include/clang/Basic/TargetInfo.h index e61ef9265cfb..23c753260ed4 100644 --- a/clang/include/clang/Basic/TargetInfo.h +++ b/clang/include/clang/Basic/TargetInfo.h @@ -346,6 +346,11 @@ public: /// options. virtual void getDefaultLangOptions(LangOptions &Opts) {} + /// setForcedLangOptions - Set forced language options. + /// Apply changes to the target information with respect to certain + /// language options which change the target configuration. + virtual void setForcedLangOptions(LangOptions &Opts); + /// getDefaultFeatures - Get the default set of target features for /// the \args CPU; this should include all legal feature strings on /// the target. diff --git a/clang/include/clang/Driver/Options.def b/clang/include/clang/Driver/Options.def index 5370114ee824..d47b0e94e237 100644 --- a/clang/include/clang/Driver/Options.def +++ b/clang/include/clang/Driver/Options.def @@ -463,6 +463,7 @@ OPTION("-fprofile-arcs", fprofile_arcs, Flag, f_Group, INVALID, "", 0, 0, 0) OPTION("-fprofile-generate", fprofile_generate, Flag, f_Group, INVALID, "", 0, 0, 0) OPTION("-framework", framework, Separate, INVALID, INVALID, "l", 0, 0, 0) OPTION("-frtti", frtti, Flag, f_Group, INVALID, "", 0, 0, 0) +OPTION("-fshort-wchar", fshort_wchar, Flag, f_Group, INVALID, "", 0, 0, 0) OPTION("-fshow-source-location", fshow_source_location, Flag, f_Group, INVALID, "", 0, 0, 0) OPTION("-fsigned-bitfields", fsigned_bitfields, Flag, f_Group, INVALID, "", 0, 0, 0) OPTION("-fsigned-char", fsigned_char, Flag, f_Group, INVALID, "", 0, 0, 0) diff --git a/clang/lib/Basic/TargetInfo.cpp b/clang/lib/Basic/TargetInfo.cpp index 12caf0c8e512..e965b9aec369 100644 --- a/clang/lib/Basic/TargetInfo.cpp +++ b/clang/lib/Basic/TargetInfo.cpp @@ -12,6 +12,7 @@ //===----------------------------------------------------------------------===// #include "clang/Basic/TargetInfo.h" +#include "clang/Basic/LangOptions.h" #include "llvm/ADT/APFloat.h" #include "llvm/ADT/STLExtras.h" #include @@ -124,6 +125,15 @@ bool TargetInfo::isTypeSigned(IntType T) const { }; } +/// setForcedLangOptions - Set forced language options. +/// Apply changes to the target information with respect to certain +/// language options which change the target configuration. +void TargetInfo::setForcedLangOptions(LangOptions &Opts) { + if (Opts.ShortWChar) { + WCharType = UnsignedShort; + WCharWidth = WCharAlign = 16; + } +} //===----------------------------------------------------------------------===// diff --git a/clang/lib/Driver/Tools.cpp b/clang/lib/Driver/Tools.cpp index 808c31c64828..c35ad7c5ea52 100644 --- a/clang/lib/Driver/Tools.cpp +++ b/clang/lib/Driver/Tools.cpp @@ -922,6 +922,13 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back("-fsigned-char=0"); } + // -fshort-wchar default varies depending on platform; only + // pass if specified. + if (Arg *A = Args.getLastArg(options::OPT_fshort_wchar)) { + if (A->getOption().matches(options::OPT_fshort_wchar)) + CmdArgs.push_back("-fshort-wchar"); + } + // -fno-pascal-strings is default, only pass non-default. If the tool chain // happened to translate to -mpascal-strings, we want to back translate here. // diff --git a/clang/lib/Frontend/PCHReader.cpp b/clang/lib/Frontend/PCHReader.cpp index 26f426ba329b..a812604e395e 100644 --- a/clang/lib/Frontend/PCHReader.cpp +++ b/clang/lib/Frontend/PCHReader.cpp @@ -104,6 +104,7 @@ PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) { PARSE_LANGOPT_IMPORTANT(NoInline, diag::warn_pch_no_inline); PARSE_LANGOPT_IMPORTANT(AccessControl, diag::warn_pch_access_control); PARSE_LANGOPT_IMPORTANT(CharIsSigned, diag::warn_pch_char_signed); + PARSE_LANGOPT_IMPORTANT(ShortWChar, diag::warn_pch_short_wchar); if ((PPLangOpts.getGCMode() != 0) != (LangOpts.getGCMode() != 0)) { Reader.Diag(diag::warn_pch_gc_mode) << LangOpts.getGCMode() << PPLangOpts.getGCMode(); @@ -1741,6 +1742,7 @@ bool PCHReader::ParseLanguageOptions( PARSE_LANGOPT(NoInline); PARSE_LANGOPT(AccessControl); PARSE_LANGOPT(CharIsSigned); + PARSE_LANGOPT(ShortWChar); LangOpts.setGCMode((LangOptions::GCMode)Record[Idx]); ++Idx; LangOpts.setVisibilityMode((LangOptions::VisibilityMode)Record[Idx]); diff --git a/clang/lib/Frontend/PCHWriter.cpp b/clang/lib/Frontend/PCHWriter.cpp index de56166125ed..436428b37552 100644 --- a/clang/lib/Frontend/PCHWriter.cpp +++ b/clang/lib/Frontend/PCHWriter.cpp @@ -765,6 +765,7 @@ void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) { // be enabled. Record.push_back(LangOpts.CharIsSigned); // Whether char is a signed or // unsigned type + Record.push_back(LangOpts.ShortWChar); // force wchar_t to be unsigned short Record.push_back(LangOpts.getGCMode()); Record.push_back(LangOpts.getVisibilityMode()); Record.push_back(LangOpts.getStackProtectorMode()); diff --git a/clang/test/Driver/clang_f_opts.c b/clang/test/Driver/clang_f_opts.c index 8266f7762398..7aecbac49c2f 100644 --- a/clang/test/Driver/clang_f_opts.c +++ b/clang/test/Driver/clang_f_opts.c @@ -2,10 +2,11 @@ // RUN: grep -F '"-fblocks"' %t && // RUN: grep -F '"--fmath-errno=1"' %t && // RUN: grep -F '"-fpascal-strings"' %t && -// RUN: clang -### -S -x c /dev/null -fblocks -fbuiltin -fmath-errno -fcommon -fpascal-strings -fno-blocks -fno-builtin -fno-math-errno -fno-common -fno-pascal-strings -fno-show-source-location %s 2> %t && +// RUN: clang -### -S -x c /dev/null -fblocks -fbuiltin -fmath-errno -fcommon -fpascal-strings -fno-blocks -fno-builtin -fno-math-errno -fno-common -fno-pascal-strings -fno-show-source-location -fshort-wchar %s 2> %t && // RUN: grep -F '"-fblocks=0"' %t && // RUN: grep -F '"-fbuiltin=0"' %t && // RUN: grep -F '"-fno-common"' %t && // RUN: grep -F '"--fmath-errno=0"' %t && // RUN: grep -F '"-fno-show-source-location"' %t && +// RUN: grep -F '"-fshort-wchar"' %t && // RUN: true diff --git a/clang/test/Sema/wchar.c b/clang/test/Sema/wchar.c index 16376009ab13..bf1996b4aa25 100644 --- a/clang/test/Sema/wchar.c +++ b/clang/test/Sema/wchar.c @@ -1,12 +1,20 @@ -// RUN: clang-cc %s -fsyntax-only -verify +// RUN: clang-cc %s -fsyntax-only -verify && +// RUN: clang-cc %s -fsyntax-only -fshort-wchar -verify -DSHORT_WCHAR + #include - + +#if defined(_WIN32) || defined(_M_IX86) || defined(__CYGWIN__) \ + || defined(_M_X64) || defined(SHORT_WCHAR) + #define WCHAR_T_TYPE unsigned short +#else + #define WCHAR_T_TYPE int +#endif + int check_wchar_size[sizeof(*L"") == sizeof(wchar_t) ? 1 : -1]; - + void foo() { - int t1[] = L"x"; + WCHAR_T_TYPE t1[] = L"x"; wchar_t tab[] = L"x"; - - int t2[] = "x"; // expected-error {{initialization}} + WCHAR_T_TYPE t2[] = "x"; // expected-error {{initialization}} char t3[] = L"x"; // expected-error {{initialization}} } diff --git a/clang/tools/clang-cc/clang-cc.cpp b/clang/tools/clang-cc/clang-cc.cpp index 26ba42d67002..a7e8f51a3a6d 100644 --- a/clang/tools/clang-cc/clang-cc.cpp +++ b/clang/tools/clang-cc/clang-cc.cpp @@ -611,6 +611,10 @@ static llvm::cl::opt CharIsSigned("fsigned-char", llvm::cl::desc("Force char to be a signed/unsigned type")); +static llvm::cl::opt +ShortWChar("fshort-wchar", + llvm::cl::desc("Force wchar_t to be a short unsigned int")); + static llvm::cl::opt Trigraphs("trigraphs", llvm::cl::desc("Process trigraph sequences")); @@ -813,6 +817,8 @@ static void InitializeLanguageStandard(LangOptions &Options, LangKind LK, Options.Blocks = EnableBlocks; if (CharIsSigned.getPosition()) Options.CharIsSigned = CharIsSigned; + if (ShortWChar.getPosition()) + Options.ShortWChar = ShortWChar; if (!AllowBuiltins) Options.NoBuiltin = 1; @@ -877,6 +883,8 @@ static void InitializeLanguageStandard(LangOptions &Options, LangKind LK, if (MainFileName.getPosition()) Options.setMainFileName(MainFileName.c_str()); + + Target->setForcedLangOptions(Options); } //===----------------------------------------------------------------------===// -- GitLab