Skip to content

Commit 4913394

Browse files
committed
Add Clang docs about MSVC compatibility
This documents some of the status of supported functionality in MSVC quirks mode. Some of this should be in http://clang.llvm.org/compatibility.html instead when things have stabilized. llvm-svn: 202559
1 parent 3331128 commit 4913394

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

clang/docs/MSVCCompatibility.rst

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
31+
Finally, MSVC accepts some C++ code that Clang will typically diagnose as
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+
38+
ABI features
39+
============
40+
41+
The status of major ABI-impacting C++ features:
42+
43+
* Record layout: :good:`Mostly complete`. We've attacked this with a fuzzer,
44+
and most of the remaining failures involve ``#pragma pack``,
45+
``__declspec(align(N))``, or other pragmas.
46+
47+
* Class inheritance: :good:`Mostly complete`. This covers all of the standard
48+
OO features you would expect: virtual method inheritance, multiple
49+
inheritance, and virtual inheritance. Every so often we uncover a bug where
50+
our tables are incompatible, but this is pretty well in hand.
51+
52+
* Name mangling: :good:`Ongoing`. Every new C++ feature generally needs its own
53+
mangling. For example, member pointer template arguments have an interesting
54+
and distinct mangling. Fortunately, incorrect manglings usually do not result
55+
in runtime errors. Non-inline functions with incorrect manglings usually
56+
result in link errors, which are relatively easy to diagnose. Incorrect
57+
manglings for inline functions and templates result in multiple copies in the
58+
final image. The C++ standard requires that those addresses be equal, but few
59+
programs rely on this.
60+
61+
* Member pointers: :good:`Mostly complete`. Standard C++ member pointers are
62+
fully implemented and should be ABI compatible. Both `#pragma
63+
pointers_to_members`_ and the `/vm`_ flags are supported. However, MSVC
64+
supports an extension to allow creating a `pointer to a member of a virtual
65+
base class`_. Clang does not yet support this.
66+
67+
.. _#pragma pointers_to_members:
68+
http://msdn.microsoft.com/en-us/library/83cch5a6.aspx
69+
.. _/vm: http://msdn.microsoft.com/en-us/library/yad46a6z.aspx
70+
.. _pointer to a member of a virtual base class: http://llvm.org/PR15713
71+
72+
* Debug info: :partial:`Minimal`. Clang emits CodeView line tables into the
73+
object file, similar to what MSVC emits when given the ``/Z7`` flag.
74+
Microsoft's link.exe will read this information and use it to create a PDB,
75+
enabling stack traces in all modern Windows debuggers. Clang does not emit
76+
any type info or description of variable layout.
77+
78+
* `RTTI`_: :none:`Unstarted`. See the bug for a discussion of what needs to
79+
happen first.
80+
81+
.. _RTTI: http://llvm.org/PR18951
82+
83+
* Exceptions and SEH: :none:`Unstarted`. Clang can parse both constructs, but
84+
does not know how to emit compatible handlers. This depends on RTTI.
85+
86+
* Thread-safe initialization of local statics: :none:`Unstarted`. We are ABI
87+
compatible with MSVC 2012, which does not support thread-safe local statics.
88+
MSVC 2013 changed the ABI to make initialization of local statics thread safe,
89+
and we have not yet implemented this.
90+
91+
* Lambdas in ABI boundaries: :none:`Infeasible`. It is unlikely that we will
92+
ever be fully ABI compatible with lambdas declared in inline functions due to
93+
what appears to be a hash code in the name mangling. Lambdas that are not
94+
externally visible should work fine.
95+
96+
Template instantiation and name lookup
97+
======================================
98+
99+
In addition to the usual `dependent name lookup FAQs `_, Clang is often unable
100+
to parse certain invalid C++ constructs that MSVC allows. As of this writing,
101+
Clang will reject code with missing ``typename`` annotations:
102+
103+
.. _dependent name lookup FAQs:
104+
http://clang.llvm.org/compatibility.html#dep_lookup
105+
106+
.. code-block:: c++
107+
108+
struct X {
109+
typedef int type;
110+
};
111+
template<typename T> int f() {
112+
// missing typename keyword
113+
return sizeof(/*typename*/ T::type);
114+
}
115+
template void f<X>();
116+
117+
Accepting code like this is ongoing work. Ultimately, it may be cleaner to
118+
`implement a token-based template instantiation mode`_ than it is to add
119+
compatibility hacks to the existing AST-based instantiation.
120+
121+
.. _implement a token-based template instantiation mode: http://llvm.org/PR18714

clang/docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Using Clang as a Compiler
2828
LeakSanitizer
2929
SanitizerSpecialCaseList
3030
Modules
31+
MSVCCompatibility
3132
FAQ
3233

3334
Using Clang as a Library

0 commit comments

Comments
 (0)