cppinsights icon indicating copy to clipboard operation
cppinsights copied to clipboard

Complex macros are not expanded

Open SpriteOvO opened this issue 4 years ago • 1 comments

Issue example: https://cppinsights.io/s/8931a54c

Input:

/* ... better-enums library code ... */

#include <cstdio>
#include <iostream>

BETTER_ENUM(Word, int, Hello, World)

int main()
{
#define ARR_SIZE 10
  
    const char arr[ARR_SIZE]{2,4,6,8};

    for(const char& c : arr)
    {
      printf("c=%c\n", c);
    }
  
    std::cout << (+Word::Hello)._to_string() << ", "
              << (+Word::World)._to_string() << "!"
              << std::endl;
}

Output:

/* ... better-enums library code ... */

#include <cstdio>
#include <iostream>

BETTER_ENUM(Word, int, Hello, World)

int main()
{
  const char arr[10] = {2, 4, 6, 8, '\0', '\0', '\0', '\0', '\0', '\0'};
  {
    char const (&__range1)[10] = arr;
    const char * __begin1 = __range1;
    const char * __end1 = __range1 + 10L;
    for(; __begin1 != __end1; ++__begin1) 
    {
      const char & c = *__begin1;
      printf("c=%c\n", static_cast<int>(c));
    }
    
  }
  std::operator<<(std::operator<<(std::operator<<(std::operator<<(std::cout, (operator+(Word::Hello))._to_string()), ", "), (operator+(Word::World))._to_string()), "!").operator<<(std::endl);
}

I'm exploring a library better-enums and I'd like to see what the BETTER_ENUM macro looks like when it expanded for the compiler, but Cppinsights' macro expansion doesn't work for it, although it works fine for ARR_SIZE macro.

I guess because the BETTER_ENUM macro is too complex, will there be plans to support complex macro expansion?


And is there a way to directly include an online url like Godbold? So that users no longer need to add library code manually.

#include <https://raw.githubusercontent.com/aantron/better-enums/master/enum.h>

:heart: Thank you for the great site!

SpriteOvO avatar Feb 17 '21 02:02 SpriteOvO

Hello @SpriteOvO,

thanks for reporting this issue. Macros are, well, difficult. I assume some of the matchers does not work here. In general macros do work. In your case, a workaround is to wrap BETTER_ENUM(Word, int, Hello, World) in a namespace:

namespace test {
BETTER_ENUM(Word, int, Hello, World)
}

Then it gets expanded.

And is there a way to directly include an online url like Godbold? So that users no longer need to add library code manually. No this doesn't work in C++ Insights. You're welcome to send me a PR.

❤️ Thank you for the great site! My pleasure.

Andreas

andreasfertig avatar Feb 18 '21 11:02 andreasfertig