Is there a way to refer to a constant C that has never been defined / not declared?

I think the answer is no, and I usually have no problems with the source code, but I am a little new to C / C ++ and cannot find where this constant is declared.

I am looking for CMD_REQ_REDIS_MGET in the hiredis-vip client library for Redis . I did a github / google search and got results in two files for five occurrences. I also tried grep for the line in the source code.

 $ grep -rnw ./ -e "CMD_REQ_REDIS_MGET" ./command.c:241: case CMD_REQ_REDIS_MGET: ./command.c:574: r->type = CMD_REQ_REDIS_MGET; ./hircluster.c:3257: if (command->type == CMD_REQ_REDIS_MGET) { ./hircluster.c:3446: if (command->type == CMD_REQ_REDIS_MGET) { ./hircluster.c:3480: if (command->type == CMD_REQ_REDIS_MGET) { 

The source code does not contain any binary files and must be standalone. It does not contain any libraries from external sources related to Redis, and therefore I was just confused for several hours.

I need to know that I am trying to add another constant, like it, and I continue to get errors so that the ad cannot be found, so I wonder if there is any black magic here with C that I just don't know.

EDIT: I would like to indicate that this code will compile as is.

+5
source share
1 answer

It is not possible to use a constant that has not previously been declared. But in this case, the constant was declared, but not trivially.

You will not find anywhere else (it should be in the header files), because these values ​​are defined in the macro from command.h using point insertion (operator ## , which creates new identifiers, combining the old ones):

 #define DEFINE_ACTION(_name) CMD_##_name, typedef enum cmd_type { CMD_TYPE_CODEC(DEFINE_ACTION) } cmd_type_t; #undef DEFINE_ACTION 

so you never find CMD_ + your suffix. Then, using some magic (the macro name is probably overridden at some point), the following defines all the elements:

 #define CMD_TYPE_CODEC(ACTION) \ ACTION( UNKNOWN ) \ ACTION( REQ_REDIS_DEL ) /* redis commands - keys */ \ ACTION( REQ_REDIS_EXISTS ) \ ACTION( REQ_REDIS_EXPIRE ) \ ACTION( REQ_REDIS_EXPIREAT ) \ ACTION( REQ_REDIS_PEXPIRE ) \ ACTION( REQ_REDIS_PEXPIREAT ) \ ACTION( REQ_REDIS_PERSIST ) \ ACTION( REQ_REDIS_PTTL ) \ ACTION( REQ_REDIS_SORT ) \ ACTION( REQ_REDIS_TTL ) 

Such macros are very useful to avoid copy / paste, but this is hell when you try to find your way in code using grep .

+10
source

Source: https://habr.com/ru/post/1274108/


All Articles