Compiler assistance with C++ exception safety
Proposes a compiler warning for implicit exception handling in C++ codebases transitioning from -fno-exceptions.
The proposer observes that C++ codebases previously compiled with -fno-exceptions can exhibit unexpected compiler and linker errors when exceptions are enabled. Even with LTO, a large number of exception handlers were found in temporary files when they were not intentionally used. The proposal suggests implementing a compiler warning to flag any implicit exception handling in user code, aiming to assist developers in making their codebases exception-safe.
- proposer
Suggests adding a compiler warning to flag implicit exception handling in C++ code to aid in making codebases exception-safe.
“Given how it can be rather difficult to see where exactly invisible exceptions and exception handling will appear implicitly in code, would a warning that shows any implicit exception handling in user code be worthwhile to implement, or is it just a silly idea?”
- contributor
Acknowledges the issue of implicit exceptions and the difficulty in tracking them, as described by the proposer.
“C++ exceptions can be a problematic issue when trying to enable them on a codebase that used to be compiled with -fno-exceptions set. Mysterious compiler and linker errors that used to not be an issue will suddenly appear if you try to remove the flag, or worse, exceptions and exception handling can invisibly be inserted into the assembled code in places where you don't want them to be in. In my…”
Technical Tradeoffs
- Increased compiler complexity and potential slowdown due to added analysis for implicit exception handling.
- Potential for false positives if the warning is too aggressive or cannot accurately distinguish between intentional and entirely unnecessary exception-handling code.
In Details
This discussion concerns the GCC C++ frontend's handling of exceptions. Specifically, it addresses the challenge of migrating large codebases from -fno-exceptions to enabling exceptions, where the compiler might implicitly generate exception-handling code (e.g., setting up unwind tables) even for functions that don't explicitly throw or catch exceptions. The proposer suggests a diagnostic to help identify these implicit handlers.
- -fno-exceptions
- A GCC compiler flag that disables exception handling support in the generated code, potentially reducing code size and improving performance by omitting exception-related runtime machinery.
- LTO
- Link-Time Optimization. A process where the compiler optimizes code across multiple source files during the linking stage, potentially enabling more aggressive optimizations than traditional whole-program optimization.
- RTL
- Register Transfer Language. An intermediate representation used by GCC to represent code before it is translated into machine code. Optimizations are often performed on RTL.