8

How do I get the anonymous struct/union behaviour activated by -fplan9-extensions in GCC to work in Clang?

I'm getting errors assigning to members of anonymous when using designated initializers, and I'm not getting the free casting to the type of an anonymous member. Both these work under GCC with the aforementioned extension activated.

typedef struct {int hi;} Embedded;
typedef struct {Embedded;} Encapsulating;

Encapsulating poo = {.hi = 3;};
error: field designator 'hi' does not refer to any field in type 'Encapsulating'

void takes_embedded(Embedded *m);
takes_embedded(&poo);
warning: incompatible pointer types passing 'Encapsuating *' to parameter of type 'Embedded *'
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
  • 2
    I think it would be best just not to write code that's not valid C. – R.. GitHub STOP HELPING ICE Aug 15 '11 at 02:20
  • 2
    You can't use compiler specific extensions and then ask "how come it doesn't work on this other compiler?" You'll need to either stick to GCC or rewrite your code to be standard C. – Chris Lutz Aug 15 '11 at 02:29
  • 1
    @Chris Lutz: I was under the impression that clang was intended to be a mostly drop-in replacement for GCC. It certainly accepts the option, but does nothing about it. – Matt Joiner Aug 15 '11 at 02:52
  • 3
    In all seriousness, I think this is probably just a bug if clang accepts the option but does nothing with it. You should probably report it on their bug tracker. – R.. GitHub STOP HELPING ICE Aug 15 '11 at 03:04
  • @R..GitHubSTOPHELPINGICE, do you mean like _ever?_ Including not writing Python, ANSI C, nor K&R C? ;-) But in all seriousness, this is valid C, as in Plan9 C. An entire operating system was written using it, and there are major compilers supporting it including `tcc` and `gcc`. It is a dialect of C that _should_ be part of ANSI C, which is yet another dialect of C. Your comment sorta raises vibes of telling someone learning Scots that they should only speak valid English. :-/ – SO_fix_the_vote_sorting_bug Feb 19 '22 at 19:02

1 Answers1

8

Here's how to get the -fplan9-extensions functionality in Clang:

  • Ensure that the functionality meets the seven criteria for Clang extensions.
  • Mail the cfe-dev mailing list and ask whether the Clang community would accept a patch implementing this extension.
  • Familiarize yourself with the LLVM coding standards.
  • Check out and build LLVM and clang from SVN.
  • Make a patch implementing the functionality and mail it to the cfe-commits mailing list.
  • Respond to comments on that mailing list. For a first patch to Clang, you should expect to need to revise it several times before it is accepted by the community. Be patient and persevere.
  • If all goes well, your patch will be checked into Clang.

Some of the -fplan9-extensions functionality (the struct { Embedded; } part) is already available under the -fms-extensions argument, but designated initializers for such anonymous members are not supported. The other part is similar in spirit to GCC's __attribute__((transparent_union)) functionality, which Clang already supports.

Richard Smith
  • 13,696
  • 56
  • 78