<br>The following code is adapted from an online clang tutorial.  It will seg fault if it Lexes itself.<br><br>The seg fault happens somewhere in the vicinity of Preprocessor.h Diag() (line 625/631) and the function that I posted below. <br>
<br>My compiler could be choosing not to inline the function, thus the DiagnosticBuilder(this) that is created will be destroyed as the function is popped off the call-stack.  Thus using the return value of this function will cause a fault.<br>
<br>Here is the output of g++ --version on my machine<br><br>$ g++ --version<br>i686-apple-darwin10-g++-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5664)<br>Copyright (C) 2007 Free Software Foundation, Inc.<br>This is free software; see the source for copying conditions.  There is NO<br>
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.<br><br>---<br>/* Title: myclang.cxx */<br>#include <clang/Basic/Diagnostic.h><br>#include <clang/Basic/LangOptions.h><br>#include <clang/Basic/TargetOptions.h><br>
#include <clang/Basic/TargetInfo.h><br>#include <clang/Basic/SourceManager.h><br><br>#include <clang/Basic/FileManager.h><br>#include <clang/Lex/HeaderSearch.h><br><br>#include <clang/Lex/Preprocessor.h><br>
<br>#include <iostream><br><br>using namespace clang;<br><br>int main(void)<br>{<br>  Diagnostic diag;<br>  <br>  LangOptions langOp;<br>  TargetOptions targOps;<br>  targOps.Triple = LLVM_HOSTTRIPLE;<br>  <br>  TargetInfo *target = TargetInfo::CreateTargetInfo(diag,targOps);<br>
  <br>  SourceManager sm(diag);<br>  FileManager fm;<br>  HeaderSearch hs(fm);<br>  Preprocessor pp(diag,langOp,*target,sm,hs);<br>  const FileEntry* file = fm.getFile("myclang.cxx");<br>  <br>  FileID fi = sm.createMainFileID(file);<br>
  <br>  pp.EnterSourceFile(fi,0,SourceLocation());<br>  <br>  Token Tok;<br>  <br>  do {<br>    pp.Lex(Tok);  // read one token<br>    if (diag.hasErrorOccurred())  // stop lexing/pp on error<br>      break;<br>    <br>    pp.DumpToken(Tok);  // outputs to cerr<br>
    std::cerr << std::endl;<br>  } while (Tok.isNot(tok::eof));<br>  <br>  delete target;<br>  <br>  return 0;<br>}<br>---<br><br><br><div class="gmail_quote">On Tue, Nov 2, 2010 at 11:21 AM, Douglas Gregor <span dir="ltr"><<a href="mailto:dgregor@apple.com" target="_blank">dgregor@apple.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><div></div><div><br><div><div>On Nov 1, 2010, at 10:24 PM, Michael Mullin wrote:</div>

<br><blockquote type="cite">Hello<br><br>I believe I have found a bug in Diagnostic.h<br><pre><a href="http://clang.llvm.org/doxygen/classclang_1_1Diagnostic.html#aa0f6707a3958ae818534b2aca4d3b7af" target="_blank">00836</a> <span></span><span>inline</span> <a href="http://clang.llvm.org/doxygen/classclang_1_1DiagnosticBuilder.html" target="_blank">DiagnosticBuilder</a> <a href="http://clang.llvm.org/doxygen/classclang_1_1Diagnostic.html#aa0f6707a3958ae818534b2aca4d3b7af" target="_blank">Diagnostic::Report</a>(<a href="http://clang.llvm.org/doxygen/classclang_1_1FullSourceLoc.html" target="_blank">FullSourceLoc</a> <a href="http://clang.llvm.org/doxygen/classclang_1_1Loc.html" target="_blank">Loc</a>, <span>unsigned</span> DiagID){<br>


<a name="12c0d640edde2d4d_12c0d2da2bd593fb_l00837"></a>00837   assert(CurDiagID == ~0U && <span>"Multiple diagnostics in flight at once!"</span>);<br><a name="12c0d640edde2d4d_12c0d2da2bd593fb_l00838"></a>00838   CurDiagLoc = Loc;<br>
<a name="12c0d640edde2d4d_12c0d2da2bd593fb_l00839"></a>00839   CurDiagID = DiagID;<br>

<a name="12c0d640edde2d4d_12c0d2da2bd593fb_l00840"></a>00840   <span>return</span> <a href="http://clang.llvm.org/doxygen/classclang_1_1Diagnostic.html#a97ec9151bbe20af5173173f6de89f3bb" target="_blank">DiagnosticBuilder</a>(<span>this</span>);<br>


<a name="12c0d640edde2d4d_12c0d2da2bd593fb_l00841"></a>00841 }<br><br>840 will cause a segmentation fault if the return value is used by the calling function.<br></pre></blockquote><br></div></div></div><div>It seems to be working fairly well... why do you think there's an issue here?</div>

<div><br></div><div><span style="white-space:pre-wrap"> </span>- Doug</div><br></div></blockquote></div><br>