4

I'm attempting to remove goto statement inside Mach7, because goto is not allowed in constexpr function:

#define MatchQ(s) {                                                            \
        XTL_MATCH_PREAMBULA(s)                                                 \
        enum { __base_counter = XTL_COUNTER };                                 \
        typedef mch::unified_switch<source_type> switch_traits;                \
        XTL_PRELOADABLE_LOCAL_STATIC(XTL_CPP0X_TYPENAME switch_traits::static_data_type,static_data,match_uid_type,XTL_EMPTY()); \
        XTL_CPP0X_TYPENAME switch_traits::local_data_type  local_data;         \
        bool processed = false;                                       \
        size_t jump_target = switch_traits::choose(subject_ptr,static_data,local_data); \
        XTL_CONCAT(ReMatch,__LINE__):                                          \
        switch (jump_target)                                                   \
        {                                                                      \
            XTL_NON_REDUNDANCY_ONLY(default:)                                  \
            { XTL_REDUNDANCY_ONLY(try){{                                       \
            if (switch_traits::on_default(jump_target,local_data,static_data)) \
                goto XTL_CONCAT(ReMatch,__LINE__);                             \
            XTL_SUBCLAUSE_FIRST

The codes above use goto here: goto XTL_CONCAT(ReMatch,__LINE__);, which is possible to jump to upside of switch statement.

How to replace goto here with something else?

Chen Li
  • 4,824
  • 3
  • 28
  • 55
  • 1
    Might enclosing the lines starting from XTL_CONCAT(ReMatch,__LINE__): all the way till the end of the closing bracket of switch with "while(true){", and then replacing "goto" statements with "break/continue" be a solution? – ozlsn Feb 27 '19 at 04:03
  • @ozlsn I have tried it before posting this question, but failed somehow, lemme gcc -E to dig into it. – Chen Li Feb 27 '19 at 04:31
  • XTL_REDUNDANCY_ONLY may set up another loop which a "continue;" would cycle through. We'd have to see all the macros. – Garr Godfrey Feb 27 '19 at 04:33
  • 1
    Why do you use such complex macro's? Can't you use regular functions instead? – JVApen Feb 27 '19 at 07:46
  • @JVApen I'm not the author of this pattern-matching library. So I cannot answer this question before get familiar enough with this library – Chen Li Feb 27 '19 at 07:49

1 Answers1

0

with the help of @ozlsn and gcc -E, the replacement is done. persuade codes:

while(true)
{
    bool continue_flag = false;
    switch(var)
    {
        default:
            if(something)
                continue_flag = true;
                break;
        // do something
        OtherCases:
            // do something
    }
    if (!contine_flag)
        break;
}

full commit here: https://github.com/FirstLoveLife/Mach7/commit/3db24a337a7643018ed9e12ac95f53f9a036251c

Here is a related QA: Using continue in a switch statement

Chen Li
  • 4,824
  • 3
  • 28
  • 55