restoreIP function in IRBuilder not recovering CurrentDebugLocation correctly

Hi all
I recently noticed following issue in the IRBuilder’s restoreIP function:


  /// Sets the current insert point to a previously-saved location.
  void restoreIP(InsertPoint IP) {
    if (IP.isSet())
      SetInsertPoint(IP.getBlock(), IP.getPoint());
    else
      ClearInsertionPoint();
  }

This appears to call following:


  /// This specifies that created instructions should be inserted at the
  /// specified point.
  void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
    BB = TheBB;
    InsertPt = IP;
    if (IP != TheBB->end())
      SetCurrentDebugLocation(IP->getDebugLoc());
  }

And the “if” statement above means restoreIP will not recover the currentDebugLocation if point we want to restore is at the end basic block.

I feel this is a legal and rather common use case. Since if we are generating LLVM IR while parsing, it is nature that the insert point is at the end of the current basic block:


// save the current insert point A at the end of basic block. currentDebugLocation is not saved since the insert point is end of a basic block.
            auto A = IRBuilder.saveIP();
// set insert point to B to generate some code there. This will also set currentDebugLocation to debugLoc of B.
            IRBuilder.SetInsertPoint(B);
               ...
// restore to insert point A and since it’s at the end of basic block, currentDebugLocation is not restored and remain the debugloc of B.
            IRBuilder.restoreIP(A);

Unless the above usecase is not the intended one. If that’s the case, could someone help enlighten me on the correct way to change insert point and restore it later?