90

While browsing through the gcc compiler source code (gcc/c-family/c-pragma.c) I see:

typedef struct GTY(()) align_stack {
  int                  alignment;
  tree                 id;
  struct align_stack * prev;
} align_stack;

and regardless of having lots of C programming years behind me, these bits: (()) are totally unknown to me yet. Can someone please explain what they mean? Google does not seem to find it.

Christian Rau
  • 45,360
  • 10
  • 108
  • 185
Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167

2 Answers2

82

They are GCC internal "magic", i.e. part of the compiler implementation itself.

See this page which talks about their use. The macro is used to mark types for garbage-collection purposes. There can be arguments too, see this page for details.

UPDATE:: As pointed out by Drew Dorman in a comment, the actual double parenthesis are not part of the "internalness" of the GNU implementation; they're commonly used when you want to collect an entire list of arguments into a single argument for the called macro. This can be useful sometimes when wrapping e.g. printf(), too. See this question, for more on this technique.

Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
  • 5
    @Krishnabhadra explanation can be found on the linked site. Further explanation about the GCC features related to the GTY-marker imo would be beyond the scope of this particular question and answer. – Arne Mertz Feb 15 '13 at 09:06
  • 30
    `(())` itself is **not** gcc magic. It allows an text that includes commas to be passed to a macro as a single argument. For any C/C++ compiler. – Drew Dormann Feb 15 '13 at 16:36
44

In general, it's used with macros to shield commas. Given #define foo(a,b), the macro invocation foo(1,2,3) would be illegal. Using an extra pair of parenthesis clarifies which comma is shielded: foo((1,2),3) versus foo(1,(2,3)).

In this case, the GTY can take multiple arguments, separated by commas, but all these commas must be shielded. That's why the inner () surround all arguments.

Community
  • 1
  • 1
MSalters
  • 173,980
  • 10
  • 155
  • 350
  • 2
    Can you explain why somebody should use such a call ? – swaechter Feb 15 '13 at 11:11
  • 5
    E.g. `#define PRINT_A_LOT(a,b) printf("prefix\n"); printf a; printf("infix\n"); printf b; printf("suffix\n");` (In C++ there are nicer solutions than macro's, of course). – MSalters Feb 15 '13 at 12:52
  • @Albertus: would also be nice, if you pass templates to a macro `Macro((Pair), ...)`. Although then you run in additional trouble, getting rid of the parenthesis within the macro – BeniBela Feb 21 '13 at 13:46