2

I have seen this question:

How to generate random variable names in C++ using macros?

with the following answer: https://stackoverflow.com/a/1675203/551045

And I've tried to implement it in clang.

Here is my declaration:

#define TRACE(stream) FuncTrace x#__COUNTER__ (llvm::errs(), "hallo", 1)

I tried all variations x##__COUNTER__; x ## __COUNTER__ and so on but none seem to work.

Could this be a clang bug? The clang help page says it has the __COUNTER__ macro.

In the end the macro I need something like this:

#define TRACE(stream) FuncTrace x#__COUNTER__ (stream, __FUNCTION__, __LINE__)
Community
  • 1
  • 1
RedX
  • 14,749
  • 1
  • 53
  • 76

1 Answers1

10
  1. To concatenate two tokens into one you use the ## operator. The # operator is used to turn a token into a string.

  2. x ## __COUNTER__ will just produce x__COUNTER__. You need to fully expand __COUNTER__ first. One possible method is add a few more indirections, e.g.

    #define YTRACE(x, y) FuncTrace x##y (llvm::errs(), __FUNCTION__, __LINE__)
    #define XTRACE(x, y) YTRACE(x, y)
    #define TRACE(x) XTRACE(x, __COUNTER__)
    
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • That's pretty much it. I had the wrong syntax when i tried this indirection approach. – RedX Oct 02 '12 at 13:08