[PATCH] D29003: [OptDiag] Split code region out of DiagnosticInfoOptimizationBase

Adam Nemet via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 25 10:43:45 PST 2017


anemet added a comment.

Thanks.



================
Comment at: lib/Analysis/OptimizationDiagnosticInfo.cpp:157
   if (Out) {
-    auto *P = &const_cast<DiagnosticInfoOptimizationBase &>(OptDiag);
+    auto *P =
+        const_cast<DiagnosticInfoOptimizationCommonBase *>(&OptDiagCommon);
----------------
MatzeB wrote:
> anemet wrote:
> > MatzeB wrote:
> > > dito.
> > Actually, this one needs to be a pointer because << requires an lvalue so we can't pass a temporary (&P).
> I am pretty sure this is equivalent and taking the address of a reference variable does produce a pointer that can still be used when the reference variable is out of scope (as long as the referenced object is still around which is the case here):
> ```
> auto &P = const_cast<DiagnosticInfoOptimizationBase&>(OptDiagBase);
> *out << &P;
> ```
I am pretty it's not ;) since that is what I tried.  &P is an rvalue for a pointer type so we can't take a reference of it.

Here is the actual error message:
```
../lib/Analysis/OptimizationDiagnosticInfo.cpp:161:10: error: invalid operands to binary expression ('yaml::Output' and 'llvm::DiagnosticInfoOptimizationBase *')
    *Out << &P;
    ~~~~ ^  ~~
../include/llvm/Support/YAMLTraits.h:1473:1: note: candidate function [with T = llvm::DiagnosticInfoOptimizationBase *] not viable: no known conversion from 'llvm::DiagnosticInfoOptimizationBase *' to 'llvm::DiagnosticInfoOptimizationBase *&' for 2nd argument
```
Arguably, operator<< should take a reference not a pointer.  I forgot why I used a pointer here.


https://reviews.llvm.org/D29003





More information about the llvm-commits mailing list