Hi,<div><br></div><div>The attached patches to compiler-rt and Clang add a new runtime library which -fcatch-undefined-behavior will call into when it detects undefined behavior. Output looks like this (complete with Clang-style highlighting if it's writing to a terminal):</div>
<div><br></div><div><div><font face="courier new, monospace">$ echo '</font></div><div><font face="courier new, monospace">typedef int X;</font></div><div><font face="courier new, monospace">int main(int argc, char**) {</font></div>
<div><font face="courier new, monospace">  return *(X*)argc;</font></div><div><font face="courier new, monospace">}' | ./bin/clang -x c++ - -fcatch-undefined-behavior</font></div><div><font face="courier new, monospace">$ ./a.out </font></div>
<div><font face="courier new, monospace"><b><stdin>:4:10: <font color="#cc0000">fatal error:</font> load of misaligned address 0x1 for type 'X' (aka 'int'), which requires 4 byte alignment</b></font></div>
<div><font face="courier new, monospace">zsh: illegal hardware instruction (core dumped)  ./a.out</font></div></div><div><font face="courier new, monospace">$</font></div><div><br></div><div>I've left -ftrapv's behavior alone, for now at least.</div>
<div><br></div><div>Review appreciated!</div><div><br></div><div>Thanks,</div><div>Richard</div>