phaistos-networks / TANK

A very high performance distributed log service
Apache License 2.0
938 stars 70 forks source link

Defined macro names collide because they're very common #85

Open hadrielk opened 1 year ago

hadrielk commented 1 year ago

We're using an older version of TANK than the current head, but it looks like the problem persists even now...

tank_client.h #includes Switch/switch.h, which #defines the following macro names:

#define require(x) assert(x)
#define Drequire(x) assert(x)
#define EXPECT(x) assert(x)
#define DEXPECT(x) assert(x)

...

#define sizeof_array(x) sizeof(detail::SIZEOF_ARRAY_REQUIRES_ARRAY_ARGUMENT(x))

#define STRLEN(p) (uint32_t)(sizeof(p) - 1)
#define STRWITHLEN(p) (p), (uint32_t)(sizeof(p) - 1)
#define LENWITHSTR(p) (uint32_t)(sizeof(p) - 1), (p)
#define STRWLEN(p) STRWITHLEN(p)
#define LENWSTR(p) LENWITHSTR(p)
#define _S(p) STRWITHLEN(p)

...

#define TOKEN_PASTE(x, y) x##y
#define TOKEN_PASTE2(x, y) TOKEN_PASTE(x, y)

#define DEFER(...) auto TOKEN_PASTE2(__deferred, __COUNTER__) = make_scope_guard([&] { __VA_ARGS__; });

But those are very common tokens/names, and will cause collisions with names by consuming user code, as well as other libraries.

For example boost/asio uses require() as a regular C++ function name.

Since macro names are not namespaced, the usual thing to do is prefix them with library name. E.g., TANK_REQUIRE and TANK_EXPECT, etc.