[cfe-dev] Different class layouts between clang and MSVC

Don Williamson don.williamson at yahoo.com
Mon Sep 5 03:23:18 PDT 2011


Hi,

I am synced to 134398 as of 2011-07-05, 00:09:02+0100.

I'm using the clang frontend to calculate class layouts which I'm hoping are identical to MSVC. Before I go any further, I suppose the first questions are:

* Is the goal of MSRecordLayoutBuilder to be as identical to MSVC layout as is knowable?
* Has that goal been achieved or are their known defects?

My test case is the following code:


   #pragma pack(push, 8)
   struct BaseStruct
   {
      // BaseStruct() { }
      double v0;
      float v1;
   };

   struct DerivedStruct : public BaseStruct
   {
      int x;
   };
   #pragma pack(pop)

With the constructor above commented out, I get the following:

sizeof(BaseStruct):         16
clang size:                 16
sizeof(DerivedStruct):      24
clang size:                 24
offsetof(DerivedStruct::x): 16
clang offset:               16

Which all matches up. However, with the constructor restored, I get:

sizeof(BaseStruct):         16
clang size:                 16
sizeof(DerivedStruct):      24
clang size:                 16
offsetof(DerivedStruct::x): 16
clang offset:               12

clang seems to be changing its behaviour based on the addition of the constructor. I'm currently tracing through the code trying to figure out how it all works in the hope of diagnosing the issue. Does anybody have any clues as to what's going on that could help me?

Thanks,
- Don




More information about the cfe-dev mailing list