| Reid Kleckner | 4913394 | 2014-02-28 23:46:04 +0000 | [diff] [blame] | 1 | .. raw:: html |
| 2 | |
| 3 | <style type="text/css"> |
| 4 | .none { background-color: #FFCCCC } |
| 5 | .partial { background-color: #FFFF99 } |
| 6 | .good { background-color: #CCFF99 } |
| 7 | </style> |
| 8 | |
| 9 | .. role:: none |
| 10 | .. role:: partial |
| 11 | .. role:: good |
| 12 | |
| 13 | ================== |
| 14 | MSVC compatibility |
| 15 | ================== |
| 16 | |
| 17 | When Clang compiles C++ code for Windows, it attempts to be compatible with |
| 18 | MSVC. There are multiple dimensions to compatibility. |
| 19 | |
| 20 | First, Clang attempts to be ABI-compatible, meaning that Clang-compiled code |
| 21 | should be able to link against MSVC-compiled code successfully. However, C++ |
| 22 | ABIs are particular large and complicated, and Clang's support for MSVC's C++ |
| 23 | ABI is a work in progress. If you don't require MSVC ABI compatibility or don't |
| 24 | want to use Microsoft's C and C++ runtimes, the mingw32 toolchain might be a |
| 25 | better fit for your project. |
| 26 | |
| 27 | Second, Clang implements many MSVC language extensions, such as |
| 28 | ``__declspec(dllexport)`` and a handful of pragmas. These are typically |
| 29 | controlled by ``-fms-extensions``. |
| 30 | |
| Nico Weber | fcf6128 | 2014-03-05 20:18:59 +0000 | [diff] [blame] | 31 | Third, MSVC accepts some C++ code that Clang will typically diagnose as |
| Reid Kleckner | 4913394 | 2014-02-28 23:46:04 +0000 | [diff] [blame] | 32 | invalid. When these constructs are present in widely included system headers, |
| 33 | Clang attempts to recover and continue compiling the user's program. Most |
| 34 | parsing and semantic compatibility tweaks are controlled by |
| 35 | ``-fms-compatibility`` and ``-fdelayed-template-parsing``, and they are a work |
| 36 | in progress. |
| 37 | |
| Nico Weber | fcf6128 | 2014-03-05 20:18:59 +0000 | [diff] [blame] | 38 | Finally, there is :ref:`clang-cl`, a driver program for clang that attempts to |
| 39 | be compatible with MSVC's cl.exe. |
| 40 | |
| Reid Kleckner | 4913394 | 2014-02-28 23:46:04 +0000 | [diff] [blame] | 41 | ABI features |
| 42 | ============ |
| 43 | |
| 44 | The status of major ABI-impacting C++ features: |
| 45 | |
| David Majnemer | fe828ad | 2014-07-02 17:26:04 +0000 | [diff] [blame] | 46 | * Record layout: :good:`Complete`. We've tested this with a fuzzer and have |
| 47 | fixed all known bugs. |
| Reid Kleckner | 4913394 | 2014-02-28 23:46:04 +0000 | [diff] [blame] | 48 | |
| 49 | * Class inheritance: :good:`Mostly complete`. This covers all of the standard |
| 50 | OO features you would expect: virtual method inheritance, multiple |
| 51 | inheritance, and virtual inheritance. Every so often we uncover a bug where |
| David Majnemer | fe828ad | 2014-07-02 17:26:04 +0000 | [diff] [blame] | 52 | our tables are incompatible, but this is pretty well in hand. This feature |
| 53 | has also been fuzz tested. |
| Reid Kleckner | 4913394 | 2014-02-28 23:46:04 +0000 | [diff] [blame] | 54 | |
| 55 | * Name mangling: :good:`Ongoing`. Every new C++ feature generally needs its own |
| 56 | mangling. For example, member pointer template arguments have an interesting |
| 57 | and distinct mangling. Fortunately, incorrect manglings usually do not result |
| 58 | in runtime errors. Non-inline functions with incorrect manglings usually |
| 59 | result in link errors, which are relatively easy to diagnose. Incorrect |
| 60 | manglings for inline functions and templates result in multiple copies in the |
| 61 | final image. The C++ standard requires that those addresses be equal, but few |
| 62 | programs rely on this. |
| 63 | |
| 64 | * Member pointers: :good:`Mostly complete`. Standard C++ member pointers are |
| 65 | fully implemented and should be ABI compatible. Both `#pragma |
| 66 | pointers_to_members`_ and the `/vm`_ flags are supported. However, MSVC |
| 67 | supports an extension to allow creating a `pointer to a member of a virtual |
| 68 | base class`_. Clang does not yet support this. |
| 69 | |
| 70 | .. _#pragma pointers_to_members: |
| 71 | http://msdn.microsoft.com/en-us/library/83cch5a6.aspx |
| 72 | .. _/vm: http://msdn.microsoft.com/en-us/library/yad46a6z.aspx |
| 73 | .. _pointer to a member of a virtual base class: http://llvm.org/PR15713 |
| 74 | |
| 75 | * Debug info: :partial:`Minimal`. Clang emits CodeView line tables into the |
| 76 | object file, similar to what MSVC emits when given the ``/Z7`` flag. |
| 77 | Microsoft's link.exe will read this information and use it to create a PDB, |
| 78 | enabling stack traces in all modern Windows debuggers. Clang does not emit |
| 79 | any type info or description of variable layout. |
| 80 | |
| David Majnemer | ac64d2b | 2014-07-02 21:09:33 +0000 | [diff] [blame^] | 81 | * RTTI: :good:`Complete`. Generation of RTTI data structures has been |
| David Majnemer | fe828ad | 2014-07-02 17:26:04 +0000 | [diff] [blame] | 82 | finished, along with support for the ``/GR`` flag. |
| Reid Kleckner | 4913394 | 2014-02-28 23:46:04 +0000 | [diff] [blame] | 83 | |
| 84 | * Exceptions and SEH: :none:`Unstarted`. Clang can parse both constructs, but |
| 85 | does not know how to emit compatible handlers. This depends on RTTI. |
| 86 | |
| 87 | * Thread-safe initialization of local statics: :none:`Unstarted`. We are ABI |
| David Majnemer | fe828ad | 2014-07-02 17:26:04 +0000 | [diff] [blame] | 88 | compatible with MSVC 2013, which does not support thread-safe local statics. |
| 89 | MSVC "14" changed the ABI to make initialization of local statics thread safe, |
| Reid Kleckner | 4913394 | 2014-02-28 23:46:04 +0000 | [diff] [blame] | 90 | and we have not yet implemented this. |
| 91 | |
| David Majnemer | fe828ad | 2014-07-02 17:26:04 +0000 | [diff] [blame] | 92 | * Lambdas: :none:`Mostly complete`. Clang is compatible with Microsoft's |
| 93 | implementation of lambdas except for providing overloads for conversion to |
| 94 | function pointer for different calling conventions. However, Microsoft's |
| 95 | extension is non-conforming. |
| Reid Kleckner | 4913394 | 2014-02-28 23:46:04 +0000 | [diff] [blame] | 96 | |
| 97 | Template instantiation and name lookup |
| 98 | ====================================== |
| 99 | |
| Reid Kleckner | 77d7698 | 2014-03-03 18:22:25 +0000 | [diff] [blame] | 100 | In addition to the usual `dependent name lookup FAQs`_, Clang is often unable to |
| 101 | parse certain invalid C++ constructs that MSVC allows. As of this writing, |
| Reid Kleckner | 4913394 | 2014-02-28 23:46:04 +0000 | [diff] [blame] | 102 | Clang will reject code with missing ``typename`` annotations: |
| 103 | |
| 104 | .. _dependent name lookup FAQs: |
| 105 | http://clang.llvm.org/compatibility.html#dep_lookup |
| 106 | |
| 107 | .. code-block:: c++ |
| 108 | |
| 109 | struct X { |
| 110 | typedef int type; |
| 111 | }; |
| 112 | template<typename T> int f() { |
| 113 | // missing typename keyword |
| 114 | return sizeof(/*typename*/ T::type); |
| 115 | } |
| 116 | template void f<X>(); |
| 117 | |
| 118 | Accepting code like this is ongoing work. Ultimately, it may be cleaner to |
| 119 | `implement a token-based template instantiation mode`_ than it is to add |
| 120 | compatibility hacks to the existing AST-based instantiation. |
| 121 | |
| 122 | .. _implement a token-based template instantiation mode: http://llvm.org/PR18714 |