skvadrik / re2c

Lexer generator for C, C++, Go and Rust.
https://re2c.org
Other
1.09k stars 171 forks source link

Add traps after semantic actions to prevent undefined control flow. #305

Open skvadrik opened 4 years ago

skvadrik commented 4 years ago

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.

DemiMarie commented 2 years ago

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.

skvadrik commented 2 years ago

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.

DemiMarie commented 2 years ago

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.