Address more review comments for DIExpression::iterator.
authorAdrian Prantl <aprantl@apple.com>
Fri, 23 Jan 2015 23:40:47 +0000 (23:40 +0000)
committerAdrian Prantl <aprantl@apple.com>
Fri, 23 Jan 2015 23:40:47 +0000 (23:40 +0000)
- input_iterator
- define an operator->
- make constructors private were possible

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@226967 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/IR/DebugInfo.h
lib/CodeGen/AsmPrinter/DwarfExpression.cpp
lib/IR/DebugInfo.cpp

index 9292f4b5c355c92128150800b7611f661bb76a4d..539025f0df2c7347ecf96631342d9eedf2247733 100644 (file)
@@ -59,7 +59,7 @@ class DIObjCProperty;
 typedef DenseMap<const MDString *, MDNode *> DITypeIdentifierMap;
 
 class DIHeaderFieldIterator
-    : public std::iterator<std::forward_iterator_tag, StringRef, std::ptrdiff_t,
+    : public std::iterator<std::input_iterator_tag, StringRef, std::ptrdiff_t,
                            const StringRef *, StringRef> {
   StringRef Header;
   StringRef Current;
@@ -867,17 +867,40 @@ public:
   /// \brief Return the size of this piece in bytes.
   uint64_t getPieceSize() const;
 
-  class Operand;
+  class iterator;
+  /// \brief A lightweight wrapper around an element of a DIExpression.
+  class Operand {
+    friend class iterator;
+    DIHeaderFieldIterator I;
+    Operand() {}
+    Operand(DIHeaderFieldIterator I) : I(I) {}
+  public:
+    /// \brief Operands such as DW_OP_piece have explicit (non-stack) arguments.
+    /// Argument 0 is the operand itself.
+    uint64_t getArg(unsigned N) const {
+      DIHeaderFieldIterator In = I;
+      std::advance(In, N);
+      return In.getNumber<uint64_t>();
+    }
+    operator uint64_t () const { return I.getNumber<uint64_t>(); }
+    /// \brief Returns underlying DIHeaderFieldIterator.
+    const DIHeaderFieldIterator &getBase() const { return I; }
+    /// \brief Returns the next operand.
+    const Operand &getNext() const;
+  };
 
   /// \brief An iterator for DIExpression elements.
-  class iterator : public std::iterator<std::forward_iterator_tag, StringRef,
-                                        unsigned, const uint64_t *, uint64_t> {
+  class iterator : public std::iterator<std::input_iterator_tag, StringRef,
+                                        unsigned, const Operand*, Operand> {
+    friend class Operand;
     DIHeaderFieldIterator I;
+    Operand Tmp;
     iterator(DIHeaderFieldIterator I) : I(I) {}
   public:
     iterator() {}
     iterator(const DIExpression &Expr) : I(++Expr.header_begin()) {}
-    Operand operator*() const { return Operand(I); }
+    const Operand &operator*() { return Tmp = Operand(I); }
+    const Operand *operator->() { return &(Tmp = Operand(I)); }
     iterator &operator++() {
       increment();
       return *this;
@@ -890,7 +913,6 @@ public:
     bool operator==(const iterator &X) const { return I == X.I; }
     bool operator!=(const iterator &X) const { return !(*this == X); }
 
-    const DIHeaderFieldIterator &getBase() const { return I; }
   private:
     void increment() {
       switch (**this) {
@@ -905,22 +927,6 @@ public:
 
   iterator begin() const;
   iterator end() const;
-
-  /// \brief A lightweight wrapper around an element of a DIExpression.
-  class Operand {
-    DIHeaderFieldIterator I;
-  public:
-    Operand(DIHeaderFieldIterator I) : I(I) {}
-    /// \brief Operands such as DW_OP_piece have explicit (non-stack) arguments.
-    /// Argument 0 is the operand itself.
-    uint64_t getArg(unsigned N) const {
-      DIHeaderFieldIterator In = I;
-      std::advance(In, N);
-      return In.getNumber<uint64_t>();
-    }
-
-    operator uint64_t () const { return I.getNumber<uint64_t>(); }
-  };
 };
 
 /// \brief This object holds location information.
index 8515dd020c8c622637635f5380460fa209724635..f3d3fb45bb2a99b98d22340e3bd0b292c0b53195 100644 (file)
@@ -210,16 +210,16 @@ bool DwarfExpression::AddMachineRegExpression(DIExpression Expr,
   switch (*I) {
   case dwarf::DW_OP_piece: {
     unsigned SizeOfByte = 8;
-    unsigned OffsetInBits = (*I).getArg(1) * SizeOfByte;
-    unsigned SizeInBits   = (*I).getArg(2) * SizeOfByte;
+    unsigned OffsetInBits = I->getArg(1) * SizeOfByte;
+    unsigned SizeInBits   = I->getArg(2) * SizeOfByte;
     // Piece always comes at the end of the expression.
     return AddMachineRegPiece(MachineReg, SizeInBits,
                getOffsetOrZero(OffsetInBits, PieceOffsetInBits));
   }
   case dwarf::DW_OP_plus:
     // [DW_OP_reg,Offset,DW_OP_plus,DW_OP_deref] --> [DW_OP_breg,Offset].
-    if (*std::next(I) == dwarf::DW_OP_deref) {
-      unsigned Offset = (*I).getArg(1);
+    if (I->getNext() == dwarf::DW_OP_deref) {
+      unsigned Offset = I->getArg(1);
       ValidReg = AddMachineRegIndirect(MachineReg, Offset);
       std::advance(I, 2);
       break;
@@ -248,14 +248,14 @@ void DwarfExpression::AddExpression(DIExpression::iterator I,
     switch (*I) {
     case dwarf::DW_OP_piece: {
       unsigned SizeOfByte = 8;
-      unsigned OffsetInBits = (*I).getArg(1) * SizeOfByte;
-      unsigned SizeInBits   = (*I).getArg(2) * SizeOfByte;
+      unsigned OffsetInBits = I->getArg(1) * SizeOfByte;
+      unsigned SizeInBits   = I->getArg(2) * SizeOfByte;
       AddOpPiece(SizeInBits, getOffsetOrZero(OffsetInBits, PieceOffsetInBits));
       break;
     }
     case dwarf::DW_OP_plus:
       EmitOp(dwarf::DW_OP_plus_uconst);
-      EmitUnsigned((*I).getArg(1));
+      EmitUnsigned(I->getArg(1));
       break;
     case dwarf::DW_OP_deref:
       EmitOp(dwarf::DW_OP_deref);
index 095fcbffde35660e63c21394b5e22c9ccff2add0..9b5a9c3da4723927f8f4b7724b567e38cb17df16 100644 (file)
@@ -170,6 +170,11 @@ DIExpression::iterator DIExpression::end() const {
  return DIExpression::iterator();
 }
 
+const DIExpression::Operand &DIExpression::Operand::getNext() const {
+  iterator it(I);
+  return *(++it);
+}
+
 //===----------------------------------------------------------------------===//
 // Predicates
 //===----------------------------------------------------------------------===//
@@ -606,13 +611,13 @@ bool DIExpression::Verify() const {
   if (!(isExpression() && DbgNode->getNumOperands() == 1))
     return false;
 
-  for (auto E = end(), I = begin(); I != E; ++I)
-    switch (*I) {
+  for (auto Op : *this)
+    switch (Op) {
     case DW_OP_piece:
       // Must be the last element of the expression.
-      return std::distance(I.getBase(), DIHeaderFieldIterator()) == 3;
+      return std::distance(Op.getBase(), DIHeaderFieldIterator()) == 3;
     case DW_OP_plus:
-      if (std::distance(I.getBase(), DIHeaderFieldIterator()) < 2)
+      if (std::distance(Op.getBase(), DIHeaderFieldIterator()) < 2)
         return false;
       break;
     case DW_OP_deref: