MSVCCompatibility.rst 7.1 KB

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