0
#define STR_L(x) #x
#define STR(x) STR_L(x)

#define CAT_L(x,y) x##y
#define CAT(x,y) CAT_L(x,y)

#define FOLDER folder/

#define PLATINCLUDE(x) STR( CAT(FOLDER ,x) )

#include PLATINCLUDE(file.h) // results in #include "folder/file.h"

That works on GCC and MSVC. However Clang is stupid, it seems to understand that code and knows it's correct, but it just breaks...

So, if the file does not exist, it complains that #include "folder/file.h" points to a non-existing file (as expected), but if the file exists it gives the following: Pasting formed '/file', an invalid preprocessing token

Any workaround for this?

LastCoder
  • 166
  • 5
  • 1
    It's a good idea to assume your code is invalid before assuming the compiler is broken. Compiling on a different compiler also doesn't say anything definitively because not all cases of invalid code require a diagnostic. In addition, GCC [gives a similar error](https://gcc.godbolt.org/z/yN2z48) and MSVC [warns](https://gcc.godbolt.org/z/LZ65kM) with its new PP implementation. – chris Sep 24 '19 at 22:16

1 Answers1

0

Ok, no idea why (I think that's a bug on Clang) but the problem seems to be related by ending or starting a preprocessor directive with slash (/).

so, this works:

#define STR_L(x) #x
#define STR(x) STR_L(x)

#define CAT_L(x,y) x##y
#define CAT(x,y) CAT_L(x,y)

#define FOLDER folder

#define PLATINCLUDE(x) STR( CAT(FOLDER ,x) )

#include PLATINCLUDE(_ext/file.h) // results in #include "folder_ext/file.h" 

Notice how the slash is not in the beginning nor the end of any macro. That's how I got it working, I'm not happy though...

LastCoder
  • 166
  • 5