Question

I found a new form of "#define" in C but I don't understand what it means. This is source code:

#define OPT_SET_INT(s, l, v, h, i)  { OPTION_SET_INT, (s), (l), (v), NULL, \
           (h), PARSE_OPT_NOARG, NULL, (i) }

This is the definition of OPTION_SET_INT:

enum parse_opt_type {
    /* special types */
    OPTION_END,
    OPTION_ARGUMENT,
    OPTION_GROUP,
    OPTION_NUMBER,
    /* options with no arguments */
    OPTION_BIT,
    OPTION_NEGBIT,
    OPTION_COUNTUP,
    OPTION_SET_INT,
    OPTION_SET_PTR,
    OPTION_CMDMODE,
    /* options with arguments (usually) */
    OPTION_STRING,
    OPTION_INTEGER,
    OPTION_CALLBACK,
    OPTION_LOWLEVEL_CALLBACK,
    OPTION_FILENAME
};

It is in parse-option.h in this repository: https://github.com/git/git

Thanks.

Was it helpful?

Solution

There's nothing special about this whatsoever. Everything after the macro name (and parenthesis) gets plopped in-place verbatim, with the exception of the macro parameters, which are replaced.

In this case, the macro is used to populate one entry in an array of struct option.

E.g. In some C file you might have:

 struct option options[] = {
    OPT_SET_INT(foo, bar, snap, crackle, pop),
    OPT_SET_INT(somethingelse, runningout, offake, names, forthis),
 };

which becomes:

 struct option options[] = {
     { OPTION_SET_INT, foo, bar, snap, NULL, crackle, PARSE_OPT_NOARG, NULL, pop },
     { OPTION_SET_INT, somethingelse, runningout, offake, NULL, names, PARSE_OPT_NOARG, NULL, forthis},
 };

(note the \ in the macro definition is escaping the newline, so that the definition can span multiple lines).

See GCC Macro Expansion for more information.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top