On 09/04/2012 07:47 PM, John McCall wrote:
On Sep 2, 2012, at 11:02 PM, Enea Zaffanella wrote:
Resending to cfe-commit.

On 08/31/2012 12:09 PM, Enea Zaffanella wrote:
Hello.

According to language standards, the wchar_t type is
   - a library provided type in C
   - a native type in C++.

There are C++ compilers allowing users to enable/disable the native
support. For instance, MSVC has option /Zc:wchar_t. Even though the
default in newer versions of MSVC is to enable wchar_t support, there is
much code in the real world that will only work if this is disabled
(e.g., Qt X.Y.Z with X < 5).

The attached simple patch adds flag WChar to LangOptions.
The flag is similar to Bool: if set, the native wchar_t type is
supported (i.e., wchar_t will be lexed as a keyword), otherwise it won't
be supported (i.e., it will be a normal identifier). Currently, the flag
is initialized the same as CPlusPlus, so that the addition should cause
no visible change.

The addition of this flag will be enough for people using clang as a set
of libraries. For more "conventional" uses, it might be worth
considering the addition of a suitable command line option to toggle the
flag.

Opinions?

Please add a -cc1 option to suppress native wchar_t;  I think -fno-wchar
would be sufficient.

John.


Here is the revised patch, including a testcase.

OK to commit?

Enea.

Index: lib/Frontend/CompilerInvocation.cpp
===================================================================
--- lib/Frontend/CompilerInvocation.cpp	(revision 163196)
+++ lib/Frontend/CompilerInvocation.cpp	(working copy)
@@ -797,6 +797,8 @@
     Res.push_back("-fno-access-control");
   if (!Opts.CharIsSigned)
     Res.push_back("-fno-signed-char");
+  if (Opts.CPlusPlus && !Opts.WChar)
+    Res.push_back("-fno-wchar");
   if (Opts.ShortWChar)
     Res.push_back("-fshort-wchar");
   if (!Opts.ElideConstructors)
@@ -1871,6 +1873,9 @@
   // OpenCL and C++ both have bool, true, false keywords.
   Opts.Bool = Opts.OpenCL || Opts.CPlusPlus;
 
+  // C++ has wchar_t keyword.
+  Opts.WChar = Opts.CPlusPlus;
+
   Opts.GNUKeywords = Opts.GNUMode;
   Opts.CXXOperatorNames = Opts.CPlusPlus;
 
@@ -2078,6 +2083,7 @@
   Opts.BlocksRuntimeOptional = Args.hasArg(OPT_fblocks_runtime_optional);
   Opts.Modules = Args.hasArg(OPT_fmodules);
   Opts.CharIsSigned = !Args.hasArg(OPT_fno_signed_char);
+  Opts.WChar = Opts.CPlusPlus && !Args.hasArg(OPT_fno_wchar);
   Opts.ShortWChar = Args.hasArg(OPT_fshort_wchar);
   Opts.ShortEnums = Args.hasArg(OPT_fshort_enums);
   Opts.Freestanding = Args.hasArg(OPT_ffreestanding);
Index: lib/Frontend/InitPreprocessor.cpp
===================================================================
--- lib/Frontend/InitPreprocessor.cpp	(revision 163196)
+++ lib/Frontend/InitPreprocessor.cpp	(working copy)
@@ -420,11 +420,13 @@
     // Both __PRETTY_FUNCTION__ and __FUNCTION__ are GCC extensions, however
     // VC++ appears to only like __FUNCTION__.
     Builder.defineMacro("__PRETTY_FUNCTION__", "__FUNCTION__");
-    // Work around some issues with Visual C++ headerws.
-    if (LangOpts.CPlusPlus) {
-      // Since we define wchar_t in C++ mode.
+    // Work around some issues with Visual C++ headers.
+    if (LangOpts.WChar) {
+      // wchar_t supported as a keyword.
       Builder.defineMacro("_WCHAR_T_DEFINED");
       Builder.defineMacro("_NATIVE_WCHAR_T_DEFINED");
+    }
+    if (LangOpts.CPlusPlus) {
       // FIXME: Support Microsoft's __identifier extension in the lexer.
       Builder.append("#define __identifier(x) x");
       Builder.append("class type_info;");
Index: lib/Basic/IdentifierTable.cpp
===================================================================
--- lib/Basic/IdentifierTable.cpp	(revision 163196)
+++ lib/Basic/IdentifierTable.cpp	(working copy)
@@ -105,6 +105,7 @@
     KEYC11 = 0x400,
     KEYARC = 0x800,
     KEYNOMS = 0x01000,
+    WCHARSUPPORT = 0x02000,
     KEYALL = (0xffff & ~KEYNOMS) // Because KEYNOMS is used to exclude.
   };
 }
@@ -129,6 +130,7 @@
   else if (LangOpts.MicrosoftExt && (Flags & KEYMS)) AddResult = 1;
   else if (LangOpts.Borland && (Flags & KEYBORLAND)) AddResult = 1;
   else if (LangOpts.Bool && (Flags & BOOLSUPPORT)) AddResult = 2;
+  else if (LangOpts.WChar && (Flags & WCHARSUPPORT)) AddResult = 2;
   else if (LangOpts.AltiVec && (Flags & KEYALTIVEC)) AddResult = 2;
   else if (LangOpts.OpenCL && (Flags & KEYOPENCL)) AddResult = 2;
   else if (!LangOpts.CPlusPlus && (Flags & KEYNOCXX)) AddResult = 2;
Index: test/SemaCXX/no-wchar.cpp
===================================================================
--- test/SemaCXX/no-wchar.cpp	(revision 0)
+++ test/SemaCXX/no-wchar.cpp	(revision 0)
@@ -0,0 +1,2 @@
+// RUN: %clang_cc1 -fsyntax-only -fno-wchar -verify %s
+wchar_t x; // expected-error {{unknown type name 'wchar_t'}}
Index: include/clang/Basic/TokenKinds.def
===================================================================
--- include/clang/Basic/TokenKinds.def	(revision 163196)
+++ include/clang/Basic/TokenKinds.def	(working copy)
@@ -217,6 +217,7 @@
 //   KEYALTIVEC - This is a keyword in AltiVec
 //   KEYBORLAND - This is a keyword if Borland extensions are enabled
 //   BOOLSUPPORT - This is a keyword if 'bool' is a built-in type
+//   WCHARSUPPORT - This is a keyword if 'wchar_t' is a built-in type
 //
 KEYWORD(auto                        , KEYALL)
 KEYWORD(break                       , KEYALL)
@@ -295,7 +296,7 @@
 KEYWORD(typeid                      , KEYCXX)
 KEYWORD(using                       , KEYCXX)
 KEYWORD(virtual                     , KEYCXX)
-KEYWORD(wchar_t                     , KEYCXX)
+KEYWORD(wchar_t                     , WCHARSUPPORT)
 
 // C++ 2.5p2: Alternative Representations.
 CXX_KEYWORD_OPERATOR(and     , ampamp)
Index: include/clang/Basic/LangOptions.def
===================================================================
--- include/clang/Basic/LangOptions.def	(revision 163196)
+++ include/clang/Basic/LangOptions.def	(working copy)
@@ -57,6 +57,7 @@
 LANGOPT(Trigraphs         , 1, 0,"trigraphs")
 LANGOPT(BCPLComment       , 1, 0, "BCPL-style '//' comments")
 LANGOPT(Bool              , 1, 0, "bool, true, and false keywords")
+LANGOPT(WChar             , 1, CPlusPlus, "wchar_t keyword")
 BENIGN_LANGOPT(DollarIdents   , 1, 1, "'$' in identifiers")
 BENIGN_LANGOPT(AsmPreprocessor, 1, 0, "preprocessor in asm mode")
 BENIGN_LANGOPT(GNUMode        , 1, 1, "GNU extensions")
Index: include/clang/Driver/CC1Options.td
===================================================================
--- include/clang/Driver/CC1Options.td	(revision 163196)
+++ include/clang/Driver/CC1Options.td	(working copy)
@@ -391,6 +391,8 @@
   HelpText<"Main file name to use for debug info">;
 def fno_signed_char : Flag<"-fno-signed-char">,
   HelpText<"Char is unsigned">;
+def fno_wchar : Flag<"-fno-wchar">,
+  HelpText<"Disable C++ builtin type wchar_t">;
 def fconstant_string_class : Separate<"-fconstant-string-class">,
   MetaVarName<"<class name>">,
   HelpText<"Specify the class to use for constant Objective-C string objects.">;
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to