xref: /aosp_15_r20/external/clang/docs/MSVCCompatibility.rst (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li.. raw:: html
2*67e74705SXin Li
3*67e74705SXin Li  <style type="text/css">
4*67e74705SXin Li    .none { background-color: #FFCCCC }
5*67e74705SXin Li    .partial { background-color: #FFFF99 }
6*67e74705SXin Li    .good { background-color: #CCFF99 }
7*67e74705SXin Li  </style>
8*67e74705SXin Li
9*67e74705SXin Li.. role:: none
10*67e74705SXin Li.. role:: partial
11*67e74705SXin Li.. role:: good
12*67e74705SXin Li
13*67e74705SXin Li==================
14*67e74705SXin LiMSVC compatibility
15*67e74705SXin Li==================
16*67e74705SXin Li
17*67e74705SXin LiWhen Clang compiles C++ code for Windows, it attempts to be compatible with
18*67e74705SXin LiMSVC.  There are multiple dimensions to compatibility.
19*67e74705SXin Li
20*67e74705SXin LiFirst, Clang attempts to be ABI-compatible, meaning that Clang-compiled code
21*67e74705SXin Lishould be able to link against MSVC-compiled code successfully.  However, C++
22*67e74705SXin LiABIs are particularly large and complicated, and Clang's support for MSVC's C++
23*67e74705SXin LiABI is a work in progress.  If you don't require MSVC ABI compatibility or don't
24*67e74705SXin Liwant to use Microsoft's C and C++ runtimes, the mingw32 toolchain might be a
25*67e74705SXin Libetter fit for your project.
26*67e74705SXin Li
27*67e74705SXin LiSecond, Clang implements many MSVC language extensions, such as
28*67e74705SXin Li``__declspec(dllexport)`` and a handful of pragmas.  These are typically
29*67e74705SXin Licontrolled by ``-fms-extensions``.
30*67e74705SXin Li
31*67e74705SXin LiThird, MSVC accepts some C++ code that Clang will typically diagnose as
32*67e74705SXin Liinvalid.  When these constructs are present in widely included system headers,
33*67e74705SXin LiClang attempts to recover and continue compiling the user's program.  Most
34*67e74705SXin Liparsing and semantic compatibility tweaks are controlled by
35*67e74705SXin Li``-fms-compatibility`` and ``-fdelayed-template-parsing``, and they are a work
36*67e74705SXin Liin progress.
37*67e74705SXin Li
38*67e74705SXin LiFinally, there is :ref:`clang-cl`, a driver program for clang that attempts to
39*67e74705SXin Libe compatible with MSVC's cl.exe.
40*67e74705SXin Li
41*67e74705SXin LiABI features
42*67e74705SXin Li============
43*67e74705SXin Li
44*67e74705SXin LiThe status of major ABI-impacting C++ features:
45*67e74705SXin Li
46*67e74705SXin Li* Record layout: :good:`Complete`.  We've tested this with a fuzzer and have
47*67e74705SXin Li  fixed all known bugs.
48*67e74705SXin Li
49*67e74705SXin Li* Class inheritance: :good:`Mostly complete`.  This covers all of the standard
50*67e74705SXin Li  OO features you would expect: virtual method inheritance, multiple
51*67e74705SXin Li  inheritance, and virtual inheritance.  Every so often we uncover a bug where
52*67e74705SXin Li  our tables are incompatible, but this is pretty well in hand.  This feature
53*67e74705SXin Li  has also been fuzz tested.
54*67e74705SXin Li
55*67e74705SXin Li* Name mangling: :good:`Ongoing`.  Every new C++ feature generally needs its own
56*67e74705SXin Li  mangling.  For example, member pointer template arguments have an interesting
57*67e74705SXin Li  and distinct mangling.  Fortunately, incorrect manglings usually do not result
58*67e74705SXin Li  in runtime errors.  Non-inline functions with incorrect manglings usually
59*67e74705SXin Li  result in link errors, which are relatively easy to diagnose.  Incorrect
60*67e74705SXin Li  manglings for inline functions and templates result in multiple copies in the
61*67e74705SXin Li  final image.  The C++ standard requires that those addresses be equal, but few
62*67e74705SXin Li  programs rely on this.
63*67e74705SXin Li
64*67e74705SXin Li* Member pointers: :good:`Mostly complete`.  Standard C++ member pointers are
65*67e74705SXin Li  fully implemented and should be ABI compatible.  Both `#pragma
66*67e74705SXin Li  pointers_to_members`_ and the `/vm`_ flags are supported. However, MSVC
67*67e74705SXin Li  supports an extension to allow creating a `pointer to a member of a virtual
68*67e74705SXin Li  base class`_.  Clang does not yet support this.
69*67e74705SXin Li
70*67e74705SXin Li.. _#pragma pointers_to_members:
71*67e74705SXin Li  http://msdn.microsoft.com/en-us/library/83cch5a6.aspx
72*67e74705SXin Li.. _/vm: http://msdn.microsoft.com/en-us/library/yad46a6z.aspx
73*67e74705SXin Li.. _pointer to a member of a virtual base class: http://llvm.org/PR15713
74*67e74705SXin Li
75*67e74705SXin Li* Debug info: :partial:`Minimal`.  Clang emits both CodeView line tables
76*67e74705SXin Li  (similar to what MSVC emits when given the ``/Z7`` flag) and DWARF debug
77*67e74705SXin Li  information into the object file.
78*67e74705SXin Li  Microsoft's link.exe will transform the CodeView line tables into a PDB,
79*67e74705SXin Li  enabling stack traces in all modern Windows debuggers.  Clang does not emit
80*67e74705SXin Li  any CodeView-compatible type info or description of variable layout.
81*67e74705SXin Li  Binaries linked with either binutils' ld or LLVM's lld should be usable with
82*67e74705SXin Li  GDB however sophisticated C++ expressions are likely to fail.
83*67e74705SXin Li
84*67e74705SXin Li* RTTI: :good:`Complete`.  Generation of RTTI data structures has been
85*67e74705SXin Li  finished, along with support for the ``/GR`` flag.
86*67e74705SXin Li
87*67e74705SXin Li* C++ Exceptions: :good:`Mostly complete`.  Support for
88*67e74705SXin Li  C++ exceptions (``try`` / ``catch`` / ``throw``) have been implemented for
89*67e74705SXin Li  x86 and x64.  Our implementation has been well tested but we still get the
90*67e74705SXin Li  odd bug report now and again.
91*67e74705SXin Li  C++ exception specifications are ignored, but this is `consistent with Visual
92*67e74705SXin Li  C++`_.
93*67e74705SXin Li
94*67e74705SXin Li.. _consistent with Visual C++:
95*67e74705SXin Li  https://msdn.microsoft.com/en-us/library/wfa0edys.aspx
96*67e74705SXin Li
97*67e74705SXin Li* Asynchronous Exceptions (SEH): :partial:`Partial`.
98*67e74705SXin Li  Structured exceptions (``__try`` / ``__except`` / ``__finally``) mostly
99*67e74705SXin Li  work on x86 and x64.
100*67e74705SXin Li  LLVM does not model asynchronous exceptions, so it is currently impossible to
101*67e74705SXin Li  catch an asynchronous exception generated in the same frame as the catching
102*67e74705SXin Li  ``__try``.
103*67e74705SXin Li
104*67e74705SXin Li* Thread-safe initialization of local statics: :good:`Complete`.  MSVC 2015
105*67e74705SXin Li  added support for thread-safe initialization of such variables by taking an
106*67e74705SXin Li  ABI break.
107*67e74705SXin Li  We are ABI compatible with both the MSVC 2013 and 2015 ABI for static local
108*67e74705SXin Li  variables.
109*67e74705SXin Li
110*67e74705SXin Li* Lambdas: :good:`Mostly complete`.  Clang is compatible with Microsoft's
111*67e74705SXin Li  implementation of lambdas except for providing overloads for conversion to
112*67e74705SXin Li  function pointer for different calling conventions.  However, Microsoft's
113*67e74705SXin Li  extension is non-conforming.
114*67e74705SXin Li
115*67e74705SXin LiTemplate instantiation and name lookup
116*67e74705SXin Li======================================
117*67e74705SXin Li
118*67e74705SXin LiMSVC allows many invalid constructs in class templates that Clang has
119*67e74705SXin Lihistorically rejected.  In order to parse widely distributed headers for
120*67e74705SXin Lilibraries such as the Active Template Library (ATL) and Windows Runtime Library
121*67e74705SXin Li(WRL), some template rules have been relaxed or extended in Clang on Windows.
122*67e74705SXin Li
123*67e74705SXin LiThe first major semantic difference is that MSVC appears to defer all parsing
124*67e74705SXin Lian analysis of inline method bodies in class templates until instantiation
125*67e74705SXin Litime.  By default on Windows, Clang attempts to follow suit.  This behavior is
126*67e74705SXin Licontrolled by the ``-fdelayed-template-parsing`` flag.  While Clang delays
127*67e74705SXin Liparsing of method bodies, it still parses the bodies *before* template argument
128*67e74705SXin Lisubstitution, which is not what MSVC does.  The following compatibility tweaks
129*67e74705SXin Liare necessary to parse the template in those cases.
130*67e74705SXin Li
131*67e74705SXin LiMSVC allows some name lookup into dependent base classes.  Even on other
132*67e74705SXin Liplatforms, this has been a `frequently asked question`_ for Clang users.  A
133*67e74705SXin Lidependent base class is a base class that depends on the value of a template
134*67e74705SXin Liparameter.  Clang cannot see any of the names inside dependent bases while it
135*67e74705SXin Liis parsing your template, so the user is sometimes required to use the
136*67e74705SXin Li``typename`` keyword to assist the parser.  On Windows, Clang attempts to
137*67e74705SXin Lifollow the normal lookup rules, but if lookup fails, it will assume that the
138*67e74705SXin Liuser intended to find the name in a dependent base.  While parsing the
139*67e74705SXin Lifollowing program, Clang will recover as if the user had written the
140*67e74705SXin Licommented-out code:
141*67e74705SXin Li
142*67e74705SXin Li.. _frequently asked question:
143*67e74705SXin Li  http://clang.llvm.org/compatibility.html#dep_lookup
144*67e74705SXin Li
145*67e74705SXin Li.. code-block:: c++
146*67e74705SXin Li
147*67e74705SXin Li  template <typename T>
148*67e74705SXin Li  struct Foo : T {
149*67e74705SXin Li    void f() {
150*67e74705SXin Li      /*typename*/ T::UnknownType x =  /*this->*/unknownMember;
151*67e74705SXin Li    }
152*67e74705SXin Li  };
153*67e74705SXin Li
154*67e74705SXin LiAfter recovery, Clang warns the user that this code is non-standard and issues
155*67e74705SXin Lia hint suggesting how to fix the problem.
156*67e74705SXin Li
157*67e74705SXin LiAs of this writing, Clang is able to compile a simple ATL hello world
158*67e74705SXin Liapplication.  There are still issues parsing WRL headers for modern Windows 8
159*67e74705SXin Liapps, but they should be addressed soon.
160