Diagnostic.h : Diagnostic::Report line 836

Hello

I believe I have found a bug in Diagnostic.h

[00836](http://clang.llvm.org/doxygen/classclang_1_1Diagnostic.html#aa0f6707a3958ae818534b2aca4d3b7af) inline [DiagnosticBuilder](http://clang.llvm.org/doxygen/classclang_1_1DiagnosticBuilder.html) [Diagnostic::Report](http://clang.llvm.org/doxygen/classclang_1_1Diagnostic.html#aa0f6707a3958ae818534b2aca4d3b7af)([FullSourceLoc](http://clang.llvm.org/doxygen/classclang_1_1FullSourceLoc.html) [Loc](http://clang.llvm.org/doxygen/classclang_1_1Loc.html), unsigned DiagID){

00837   assert(CurDiagID == ~0U && "Multiple diagnostics in flight at once!");
00838   CurDiagLoc = Loc;
00839   CurDiagID = DiagID;

00840   return [DiagnosticBuilder](http://clang.llvm.org/doxygen/classclang_1_1Diagnostic.html#a97ec9151bbe20af5173173f6de89f3bb)(this);

00841 }

840 will cause a segmentation fault if the return value is used by the calling function.

It seems to be working fairly well… why do you think there’s an issue here?

  • Doug

The following code is adapted from an online clang tutorial. It will seg fault if it Lexes itself.

The seg fault happens somewhere in the vicinity of Preprocessor.h Diag() (line 625/631) and the function that I posted below.

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.

Here is the output of g++ --version on my machine

$ g++ --version
i686-apple-darwin10-g+±4.2.1 (GCC) 4.2.1 (Apple Inc. build 5664)
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

The following code is adapted from an online clang tutorial. It will seg fault if it Lexes itself.

The seg fault happens somewhere in the vicinity of Preprocessor.h Diag() (line 625/631) and the function that I posted below.

I'm pretty sure you need to set a DiagnosticClient on the Diagnostic object; that is probably why you're getting a crash. Is there not an assert about this?

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.

The struct is being returned by value, so the caller gets a copy. That's how C++ works.

John.

The following code is adapted from an online clang tutorial. It will seg fault if it Lexes itself.

The seg fault happens somewhere in the vicinity of Preprocessor.h Diag() (line 625/631) and the function that I posted below.

I’m pretty sure you need to set a DiagnosticClient on the Diagnostic object; that is probably why you’re getting a crash. Is there not an assert about this?

Yes that was why I was getting a crash. Thanks for the tip. No there was not an assert about this.

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.

The struct is being returned by value, so the caller gets a copy. That’s how C++ works.

:slight_smile: yup, you are correct. Thank you.