r181004 - Support __wchar_t in -fms-extensions and -fms-compatibility modes.

Hans Wennborg hans at hanshq.net
Fri May 3 02:10:17 PDT 2013


Author: hans
Date: Fri May  3 04:10:16 2013
New Revision: 181004

URL: http://llvm.org/viewvc/llvm-project?rev=181004&view=rev
Log:
Support __wchar_t in -fms-extensions and -fms-compatibility modes.

MSVC provides __wchar_t, either as an alias for the built-in wchar_t
type, or as a separate type depending on language (C vs C++) and flags
(-fno-wchar).

In -fms-extensions, Clang will simply accept __wchar_t as an alias for
whatever type is used for wide character literals. In -fms-compatibility, we
try to mimic MSVC's behavior by always making __wchar_t a builtin type.

This fixes PR15815.

Added:
    cfe/trunk/test/Lexer/ms-compatibility-wchar.c   (with props)
    cfe/trunk/test/Lexer/ms-extensions-wchar.c   (with props)
Modified:
    cfe/trunk/include/clang/AST/PrettyPrinter.h
    cfe/trunk/include/clang/Basic/TokenKinds.def
    cfe/trunk/lib/AST/ASTContext.cpp
    cfe/trunk/lib/AST/Type.cpp

Modified: cfe/trunk/include/clang/AST/PrettyPrinter.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/PrettyPrinter.h?rev=181004&r1=181003&r2=181004&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/PrettyPrinter.h (original)
+++ cfe/trunk/include/clang/AST/PrettyPrinter.h Fri May  3 04:10:16 2013
@@ -40,7 +40,8 @@ struct PrintingPolicy {
       SuppressUnwrittenScope(false), SuppressInitializers(false),
       ConstantArraySizeAsWritten(false), AnonymousTagLocations(true),
       SuppressStrongLifetime(false), Bool(LO.Bool),
-      TerseOutput(false), PolishForDeclaration(false) { }
+      TerseOutput(false), PolishForDeclaration(false),
+      MSWChar(LO.MicrosoftMode && !LO.WChar) { }
 
   /// \brief What language we're printing.
   LangOptions LangOpts;
@@ -146,6 +147,10 @@ struct PrintingPolicy {
   /// declaration tag; such as, do not print attributes attached to the declaration.
   ///
   unsigned PolishForDeclaration : 1;
+
+  /// \brief When true, print the built-in wchar_t type as __wchar_t. For use in
+  /// Microsoft mode when wchar_t is not available.
+  unsigned MSWChar : 1;
 };
 
 } // end namespace clang

Modified: cfe/trunk/include/clang/Basic/TokenKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/TokenKinds.def?rev=181004&r1=181003&r2=181004&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/TokenKinds.def (original)
+++ cfe/trunk/include/clang/Basic/TokenKinds.def Fri May  3 04:10:16 2013
@@ -524,6 +524,7 @@ KEYWORD(__interface                   ,
 ALIAS("__int8"           , char       , KEYMS)
 ALIAS("__int16"          , short      , KEYMS)
 ALIAS("__int32"          , int        , KEYMS)
+ALIAS("__wchar_t"        , wchar_t    , KEYMS)
 ALIAS("_asm"             , asm        , KEYMS)
 ALIAS("_alignof"         , __alignof  , KEYMS)
 ALIAS("__builtin_alignof", __alignof  , KEYMS)

Modified: cfe/trunk/lib/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTContext.cpp?rev=181004&r1=181003&r2=181004&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Fri May  3 04:10:16 2013
@@ -897,12 +897,13 @@ void ASTContext::InitBuiltinTypes(const
   InitBuiltinType(Int128Ty,            BuiltinType::Int128);
   InitBuiltinType(UnsignedInt128Ty,    BuiltinType::UInt128);
 
-  if (LangOpts.CPlusPlus && LangOpts.WChar) { // C++ 3.9.1p5
+  if ((LangOpts.CPlusPlus && LangOpts.WChar) || LangOpts.MicrosoftMode) {
+    // C++ 3.9.1p5 or -fms-compatibility.
     if (TargetInfo::isTypeSigned(Target.getWCharType()))
       InitBuiltinType(WCharTy,           BuiltinType::WChar_S);
     else  // -fshort-wchar makes wchar_t be unsigned.
       InitBuiltinType(WCharTy,           BuiltinType::WChar_U);
-  } else // C99 (or C++ using -fno-wchar)
+  } else // C99 (or C++ using -fno-wchar) in non-MicrosoftMode.
     WCharTy = getFromTargetType(Target.getWCharType());
 
   WIntTy = getFromTargetType(Target.getWIntType());

Modified: cfe/trunk/lib/AST/Type.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Type.cpp?rev=181004&r1=181003&r2=181004&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Type.cpp (original)
+++ cfe/trunk/lib/AST/Type.cpp Fri May  3 04:10:16 2013
@@ -1521,7 +1521,7 @@ StringRef BuiltinType::getName(const Pri
   case Double:            return "double";
   case LongDouble:        return "long double";
   case WChar_S:
-  case WChar_U:           return "wchar_t";
+  case WChar_U:           return Policy.MSWChar ? "__wchar_t" : "wchar_t";
   case Char16:            return "char16_t";
   case Char32:            return "char32_t";
   case NullPtr:           return "nullptr_t";

Added: cfe/trunk/test/Lexer/ms-compatibility-wchar.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Lexer/ms-compatibility-wchar.c?rev=181004&view=auto
==============================================================================
--- cfe/trunk/test/Lexer/ms-compatibility-wchar.c (added)
+++ cfe/trunk/test/Lexer/ms-compatibility-wchar.c Fri May  3 04:10:16 2013
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -fms-compatibility -triple i386-pc-win32 %s
+
+// C++ mode with -fno-wchar works the same as C mode for wchar_t.
+// RUN: %clang_cc1 -x c++ -fno-wchar -fsyntax-only -verify -fms-compatibility -triple i386-pc-win32 %s
+
+wchar_t f(); // expected-error{{unknown type name 'wchar_t'}}
+
+// __wchar_t is available as an MS extension.
+__wchar_t g; // expected-note {{previous}}
+
+// __wchar_t is a distinct type, separate from the target's integer type for wide chars.
+unsigned short g; // expected-error {{redefinition of 'g' with a different type: 'unsigned short' vs '__wchar_t'}}

Propchange: cfe/trunk/test/Lexer/ms-compatibility-wchar.c
------------------------------------------------------------------------------
    svn:eol-style = LF

Added: cfe/trunk/test/Lexer/ms-extensions-wchar.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Lexer/ms-extensions-wchar.c?rev=181004&view=auto
==============================================================================
--- cfe/trunk/test/Lexer/ms-extensions-wchar.c (added)
+++ cfe/trunk/test/Lexer/ms-extensions-wchar.c Fri May  3 04:10:16 2013
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -fms-extensions -triple i386-pc-win32 %s
+
+// C++ mode with -fno-wchar works the same as C mode for wchar_t.
+// RUN: %clang_cc1 -x c++ -fno-wchar -fsyntax-only -verify -fms-extensions -triple i386-pc-win32 %s
+
+wchar_t f(); // expected-error{{unknown type name 'wchar_t'}}
+
+// __wchar_t is available as an MS extension.
+__wchar_t g(); // No error.
+
+// __wchar_t is the same as the target's integer type for wide chars.
+unsigned short g(); // No error.

Propchange: cfe/trunk/test/Lexer/ms-extensions-wchar.c
------------------------------------------------------------------------------
    svn:eol-style = LF





More information about the cfe-commits mailing list