<div dir="ltr">Hi,<br><br>I think I ran into a rather subtle bug inside 
llvm/ADT/ArrayRef.h which only shows up when compiling the code with 
GCC-4.8 with switched-off optimizations. (Both clang and GCC-4.7 don't 
trigger the bug.)<br>
<br>I already filed a bug against GCC-4.8 which was rejected by the 
GCC-folks as being invalid, because the code (basically ArrayRef.h)  "is
 doing something bad - it's retaining a pointer to a temporary object 
across a function call." They also provided a detailed explanation for 
their opinion, which I think is correct. See this link for the full 
story: <a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61015" target="_blank">https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61015</a><br>
<br>The following program demonstrates the bug using LLVM's own ArrayRef:<br>#define __STDC_LIMIT_MACROS<br>#define __STDC_CONSTANT_MACROS<br><br>#include <cstdio><br>#include <llvm/ADT/ArrayRef.h><br><br>

class Obj {};<br>class SpecialObj : public Obj {};<br><br>int main()<br>{<br>  SpecialObj* pSpecial = new SpecialObj();<br><br>  llvm::ArrayRef<Obj*> arrayRef( pSpecial ); //Breaks on GCC-4.8.<br><br>  /* Obj* pObj = pSpecial;<br>

  llvm::ArrayRef<Obj*> arrayRef( pObj ); //Possible Workaround */<br><br>  int someStackArray[500];<br>  memset( someStackArray, 0xdd, sizeof(someStackArray) );<br><br>  if( arrayRef[0] != pSpecial )<br>    printf( "This shouldn't happen: %p\n", arrayRef[0] );<br>

  else<br>    printf( "Expected behaviour.\n" );<br>  return 0;<br>}<br><br>Compiling (and then executing) this program with <br>  g++-4.8 -Wall -O0 -std=c++11 -I./LLVM_SVN/installed/include main.cpp<br>prints: "This shouldn't happen: 0xdddddddddddddddd"<br>

<br>Compiling with<br>  clang++ -Wall -O0 -std=c++11 -I./LLVM_SVN/installed/include main.cpp<br>prints: "Expected behaviour." <br><br>I
 think (as a quick fix) we should remove the const qualifier from 
ArrayRef's CTor argument, so that the above code won't compile anymore 
and thus avoiding a silent failure.<br>
To be precise:<br>Change llvm/ADT/ArrayRef.h:57 from<br> ArrayRef(const T &OneElt)<br>      : Data(&OneElt), Length(1) {}<br>to<br> ArrayRef(T &OneElt)<br>      : Data(&OneElt), Length(1) {}<br><br>Best regards,<br>

Andreas</div>