xref: /minix3/external/bsd/llvm/dist/clang/docs/MSVCCompatibility.rst (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc.. raw:: html
2*0a6a1f1dSLionel Sambuc
3*0a6a1f1dSLionel Sambuc  <style type="text/css">
4*0a6a1f1dSLionel Sambuc    .none { background-color: #FFCCCC }
5*0a6a1f1dSLionel Sambuc    .partial { background-color: #FFFF99 }
6*0a6a1f1dSLionel Sambuc    .good { background-color: #CCFF99 }
7*0a6a1f1dSLionel Sambuc  </style>
8*0a6a1f1dSLionel Sambuc
9*0a6a1f1dSLionel Sambuc.. role:: none
10*0a6a1f1dSLionel Sambuc.. role:: partial
11*0a6a1f1dSLionel Sambuc.. role:: good
12*0a6a1f1dSLionel Sambuc
13*0a6a1f1dSLionel Sambuc==================
14*0a6a1f1dSLionel SambucMSVC compatibility
15*0a6a1f1dSLionel Sambuc==================
16*0a6a1f1dSLionel Sambuc
17*0a6a1f1dSLionel SambucWhen Clang compiles C++ code for Windows, it attempts to be compatible with
18*0a6a1f1dSLionel SambucMSVC.  There are multiple dimensions to compatibility.
19*0a6a1f1dSLionel Sambuc
20*0a6a1f1dSLionel SambucFirst, Clang attempts to be ABI-compatible, meaning that Clang-compiled code
21*0a6a1f1dSLionel Sambucshould be able to link against MSVC-compiled code successfully.  However, C++
22*0a6a1f1dSLionel SambucABIs are particularly large and complicated, and Clang's support for MSVC's C++
23*0a6a1f1dSLionel SambucABI is a work in progress.  If you don't require MSVC ABI compatibility or don't
24*0a6a1f1dSLionel Sambucwant to use Microsoft's C and C++ runtimes, the mingw32 toolchain might be a
25*0a6a1f1dSLionel Sambucbetter fit for your project.
26*0a6a1f1dSLionel Sambuc
27*0a6a1f1dSLionel SambucSecond, Clang implements many MSVC language extensions, such as
28*0a6a1f1dSLionel Sambuc``__declspec(dllexport)`` and a handful of pragmas.  These are typically
29*0a6a1f1dSLionel Sambuccontrolled by ``-fms-extensions``.
30*0a6a1f1dSLionel Sambuc
31*0a6a1f1dSLionel SambucThird, MSVC accepts some C++ code that Clang will typically diagnose as
32*0a6a1f1dSLionel Sambucinvalid.  When these constructs are present in widely included system headers,
33*0a6a1f1dSLionel SambucClang attempts to recover and continue compiling the user's program.  Most
34*0a6a1f1dSLionel Sambucparsing and semantic compatibility tweaks are controlled by
35*0a6a1f1dSLionel Sambuc``-fms-compatibility`` and ``-fdelayed-template-parsing``, and they are a work
36*0a6a1f1dSLionel Sambucin progress.
37*0a6a1f1dSLionel Sambuc
38*0a6a1f1dSLionel SambucFinally, there is :ref:`clang-cl`, a driver program for clang that attempts to
39*0a6a1f1dSLionel Sambucbe compatible with MSVC's cl.exe.
40*0a6a1f1dSLionel Sambuc
41*0a6a1f1dSLionel SambucABI features
42*0a6a1f1dSLionel Sambuc============
43*0a6a1f1dSLionel Sambuc
44*0a6a1f1dSLionel SambucThe status of major ABI-impacting C++ features:
45*0a6a1f1dSLionel Sambuc
46*0a6a1f1dSLionel Sambuc* Record layout: :good:`Complete`.  We've tested this with a fuzzer and have
47*0a6a1f1dSLionel Sambuc  fixed all known bugs.
48*0a6a1f1dSLionel Sambuc
49*0a6a1f1dSLionel Sambuc* Class inheritance: :good:`Mostly complete`.  This covers all of the standard
50*0a6a1f1dSLionel Sambuc  OO features you would expect: virtual method inheritance, multiple
51*0a6a1f1dSLionel Sambuc  inheritance, and virtual inheritance.  Every so often we uncover a bug where
52*0a6a1f1dSLionel Sambuc  our tables are incompatible, but this is pretty well in hand.  This feature
53*0a6a1f1dSLionel Sambuc  has also been fuzz tested.
54*0a6a1f1dSLionel Sambuc
55*0a6a1f1dSLionel Sambuc* Name mangling: :good:`Ongoing`.  Every new C++ feature generally needs its own
56*0a6a1f1dSLionel Sambuc  mangling.  For example, member pointer template arguments have an interesting
57*0a6a1f1dSLionel Sambuc  and distinct mangling.  Fortunately, incorrect manglings usually do not result
58*0a6a1f1dSLionel Sambuc  in runtime errors.  Non-inline functions with incorrect manglings usually
59*0a6a1f1dSLionel Sambuc  result in link errors, which are relatively easy to diagnose.  Incorrect
60*0a6a1f1dSLionel Sambuc  manglings for inline functions and templates result in multiple copies in the
61*0a6a1f1dSLionel Sambuc  final image.  The C++ standard requires that those addresses be equal, but few
62*0a6a1f1dSLionel Sambuc  programs rely on this.
63*0a6a1f1dSLionel Sambuc
64*0a6a1f1dSLionel Sambuc* Member pointers: :good:`Mostly complete`.  Standard C++ member pointers are
65*0a6a1f1dSLionel Sambuc  fully implemented and should be ABI compatible.  Both `#pragma
66*0a6a1f1dSLionel Sambuc  pointers_to_members`_ and the `/vm`_ flags are supported. However, MSVC
67*0a6a1f1dSLionel Sambuc  supports an extension to allow creating a `pointer to a member of a virtual
68*0a6a1f1dSLionel Sambuc  base class`_.  Clang does not yet support this.
69*0a6a1f1dSLionel Sambuc
70*0a6a1f1dSLionel Sambuc.. _#pragma pointers_to_members:
71*0a6a1f1dSLionel Sambuc  http://msdn.microsoft.com/en-us/library/83cch5a6.aspx
72*0a6a1f1dSLionel Sambuc.. _/vm: http://msdn.microsoft.com/en-us/library/yad46a6z.aspx
73*0a6a1f1dSLionel Sambuc.. _pointer to a member of a virtual base class: http://llvm.org/PR15713
74*0a6a1f1dSLionel Sambuc
75*0a6a1f1dSLionel Sambuc* Debug info: :partial:`Minimal`.  Clang emits both CodeView line tables
76*0a6a1f1dSLionel Sambuc  (similar to what MSVC emits when given the ``/Z7`` flag) and DWARF debug
77*0a6a1f1dSLionel Sambuc  information into the object file.
78*0a6a1f1dSLionel Sambuc  Microsoft's link.exe will transform the CodeView line tables into a PDB,
79*0a6a1f1dSLionel Sambuc  enabling stack traces in all modern Windows debuggers.  Clang does not emit
80*0a6a1f1dSLionel Sambuc  any CodeView-compatible type info or description of variable layout.
81*0a6a1f1dSLionel Sambuc  Binaries linked with either binutils' ld or LLVM's lld should be usable with
82*0a6a1f1dSLionel Sambuc  GDB however sophisticated C++ expressions are likely to fail.
83*0a6a1f1dSLionel Sambuc
84*0a6a1f1dSLionel Sambuc* RTTI: :good:`Complete`.  Generation of RTTI data structures has been
85*0a6a1f1dSLionel Sambuc  finished, along with support for the ``/GR`` flag.
86*0a6a1f1dSLionel Sambuc
87*0a6a1f1dSLionel Sambuc* Exceptions and SEH: :partial:`Minimal`.  Clang can parse both constructs, but
88*0a6a1f1dSLionel Sambuc  does not know how to emit compatible handlers.  Clang cannot throw exceptions
89*0a6a1f1dSLionel Sambuc  but it can rethrow them.
90*0a6a1f1dSLionel Sambuc
91*0a6a1f1dSLionel Sambuc* Thread-safe initialization of local statics: :none:`Unstarted`.  We are ABI
92*0a6a1f1dSLionel Sambuc  compatible with MSVC 2013, which does not support thread-safe local statics.
93*0a6a1f1dSLionel Sambuc  MSVC "14" changed the ABI to make initialization of local statics thread safe,
94*0a6a1f1dSLionel Sambuc  and we have not yet implemented this.
95*0a6a1f1dSLionel Sambuc
96*0a6a1f1dSLionel Sambuc* Lambdas: :good:`Mostly complete`.  Clang is compatible with Microsoft's
97*0a6a1f1dSLionel Sambuc  implementation of lambdas except for providing overloads for conversion to
98*0a6a1f1dSLionel Sambuc  function pointer for different calling conventions.  However, Microsoft's
99*0a6a1f1dSLionel Sambuc  extension is non-conforming.
100*0a6a1f1dSLionel Sambuc
101*0a6a1f1dSLionel SambucTemplate instantiation and name lookup
102*0a6a1f1dSLionel Sambuc======================================
103*0a6a1f1dSLionel Sambuc
104*0a6a1f1dSLionel SambucMSVC allows many invalid constructs in class templates that Clang has
105*0a6a1f1dSLionel Sambuchistorically rejected.  In order to parse widely distributed headers for
106*0a6a1f1dSLionel Sambuclibraries such as the Active Template Library (ATL) and Windows Runtime Library
107*0a6a1f1dSLionel Sambuc(WRL), some template rules have been relaxed or extended in Clang on Windows.
108*0a6a1f1dSLionel Sambuc
109*0a6a1f1dSLionel SambucThe first major semantic difference is that MSVC appears to defer all parsing
110*0a6a1f1dSLionel Sambucan analysis of inline method bodies in class templates until instantiation
111*0a6a1f1dSLionel Sambuctime.  By default on Windows, Clang attempts to follow suit.  This behavior is
112*0a6a1f1dSLionel Sambuccontrolled by the ``-fdelayed-template-parsing`` flag.  While Clang delays
113*0a6a1f1dSLionel Sambucparsing of method bodies, it still parses the bodies *before* template argument
114*0a6a1f1dSLionel Sambucsubstitution, which is not what MSVC does.  The following compatibility tweaks
115*0a6a1f1dSLionel Sambucare necessary to parse the the template in those cases.
116*0a6a1f1dSLionel Sambuc
117*0a6a1f1dSLionel SambucMSVC allows some name lookup into dependent base classes.  Even on other
118*0a6a1f1dSLionel Sambucplatforms, this has been a `frequently asked question`_ for Clang users.  A
119*0a6a1f1dSLionel Sambucdependent base class is a base class that depends on the value of a template
120*0a6a1f1dSLionel Sambucparameter.  Clang cannot see any of the names inside dependent bases while it
121*0a6a1f1dSLionel Sambucis parsing your template, so the user is sometimes required to use the
122*0a6a1f1dSLionel Sambuc``typename`` keyword to assist the parser.  On Windows, Clang attempts to
123*0a6a1f1dSLionel Sambucfollow the normal lookup rules, but if lookup fails, it will assume that the
124*0a6a1f1dSLionel Sambucuser intended to find the name in a dependent base.  While parsing the
125*0a6a1f1dSLionel Sambucfollowing program, Clang will recover as if the user had written the
126*0a6a1f1dSLionel Sambuccommented-out code:
127*0a6a1f1dSLionel Sambuc
128*0a6a1f1dSLionel Sambuc.. _frequently asked question:
129*0a6a1f1dSLionel Sambuc  http://clang.llvm.org/compatibility.html#dep_lookup
130*0a6a1f1dSLionel Sambuc
131*0a6a1f1dSLionel Sambuc.. code-block:: c++
132*0a6a1f1dSLionel Sambuc
133*0a6a1f1dSLionel Sambuc  template <typename T>
134*0a6a1f1dSLionel Sambuc  struct Foo : T {
135*0a6a1f1dSLionel Sambuc    void f() {
136*0a6a1f1dSLionel Sambuc      /*typename*/ T::UnknownType x =  /*this->*/unknownMember;
137*0a6a1f1dSLionel Sambuc    }
138*0a6a1f1dSLionel Sambuc  };
139*0a6a1f1dSLionel Sambuc
140*0a6a1f1dSLionel SambucAfter recovery, Clang warns the user that this code is non-standard and issues
141*0a6a1f1dSLionel Sambuca hint suggesting how to fix the problem.
142*0a6a1f1dSLionel Sambuc
143*0a6a1f1dSLionel SambucAs of this writing, Clang is able to compile a simple ATL hello world
144*0a6a1f1dSLionel Sambucapplication.  There are still issues parsing WRL headers for modern Windows 8
145*0a6a1f1dSLionel Sambucapps, but they should be addressed soon.
146