[GSoC] Header Generation for C/C++

Andrei Alexandrescu SeeWebsiteForEmail at erdani.com
Tue Jul 30 15:08:34 UTC 2019


On 7/30/19 10:50 AM, Eduard Staniloiu wrote:
> On Tuesday, 30 July 2019 at 14:43:01 UTC, Eduard Staniloiu wrote:
>> On Monday, 29 July 2019 at 01:19:36 UTC, Manu wrote:
>>> On Sun, Jul 28, 2019 at 5:20 PM Gregor Mückl via Digitalmars-d 
>>> <digitalmars-d at puremagic.com> wrote:
>>>>
>>>> On Monday, 22 July 2019 at 18:12:01 UTC, Manu wrote:
>>>> > [...]
>>>>
>>>> That's a pretty neat hack!
>>>>
>>>> I'm just worried about two things:
>>>> - it makes the generated code harder to read (not that big of a
>>>> deal)
>>>
>>> Barely... and a generated header is not really for human consumption 
>>> either way.
>>>
>>>> - if you happen to include headers with conflicting D_ENUM* defines 
>>>> (e.g. a potential 3rd party D wrapper), you might get some nasty 
>>>> surprises :/
>>>
>>> C/C++ programmers now how to do macros and what to expect.
>>
>> So should we go ahead with generating code based on the `-extern-std` 
>> value or by using defines?
>>
>> After sleeping a bit on this, I prefer `-extern-std`.
> 
> BTW, I went with `-extern-std` in the current implementation (on GH).
> 
> Another question is how should `align(n)` be generated. See the 
> following two:
> 
> 1) __attribute__((packed, aligned(n)))
> 
> or
> 
> 2) __attribute__((aligned(n)))
> 
> Note the missing `packed` between 1) and 2)
> 
> Currently (on GH) the entire generated output looks like this
> 
> struct
> #if defined(__GNUC__) || defined(__clang__)
>      __attribute__((packed, aligned(8)))
> #elif defined(_MSC_VER)
>      __declspec(align(8))
> #elif defined(__DMC__)
>      #pragma pack(push, 8)
> #endif
> MyStruct
> {
>    /* fields */
> }
> #if defined(__DMC__)
>      #pragma pack(pop)
> #endif
> 

Better yet:

#if defined(__GNUC__) || defined(__clang__)
     #define BEGIN_PACK(n) __attribute__((packed, aligned(n)))
#elif defined(_MSC_VER)
     #define BEGIN_PACK(n) __declspec(align(n))
#elif defined(__DMC__)
     #define BEGIN_PACK(n) #pragma pack(push, n)
#endif

#if defined(__DMC__)
     #define END_PACK() #pragma pack(pop)
#else
     #define END_PACK()
#endif



More information about the Digitalmars-d mailing list