skvadrik / re2c

Lexer generator for C, C++, Go and Rust.

Home Page:https://re2c.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add traps after semantic actions to prevent undefined control flow.

skvadrik opened this issue · comments

User-defined semantic actions should have a control flow transfer statement at the end (one of goto, return, continue, break, etc). If the user forgets to add one, the program may fail in an unexpected way. This is especially confusing for new users.

The suggestion is to add some sort of assert(false) or __builtin_trap() statement after the user-defined semantic action. It should be unreachable (dead code) in correct programs, so some way of suppressing compiler warnings is needed.

What about a call to an undefined function? That will cause a link failure if not optimized away. GCC has an attribute that can be used to give a better message.

What about a call to an undefined function?

This is a nice hack, but re2c should not rely on the compiler optimizations: it can be TCC or GCC/Clang without optimizations.

What about a call to an undefined function?

This is a nice hack, but re2c should not rely on the compiler optimizations: it can be TCC or GCC/Clang without optimizations.

True, though with GCC there is a preprocessor macro that can be used to check if optimizations are on.