]> granicus.if.org Git - llvm/commitdiff
Move the accessor functions from DIExpression::iterator into a wrapper
authorAdrian Prantl <aprantl@apple.com>
Fri, 23 Jan 2015 21:24:41 +0000 (21:24 +0000)
committerAdrian Prantl <aprantl@apple.com>
Fri, 23 Jan 2015 21:24:41 +0000 (21:24 +0000)
DIExpression::Operand, so we can write range-based for loops.

Thanks to David Blaikie for the idea.

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

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

index b2fc6fa95d4f6e7b03f3d029747a4df5958789cd..9292f4b5c355c92128150800b7611f661bb76a4d 100644 (file)
@@ -867,16 +867,17 @@ public:
   /// \brief Return the size of this piece in bytes.
   uint64_t getPieceSize() const;
 
+  class Operand;
+
   /// \brief An iterator for DIExpression elements.
   class iterator : public std::iterator<std::forward_iterator_tag, StringRef,
                                         unsigned, const uint64_t *, uint64_t> {
     DIHeaderFieldIterator I;
     iterator(DIHeaderFieldIterator I) : I(I) {}
-
   public:
     iterator() {}
     iterator(const DIExpression &Expr) : I(++Expr.header_begin()) {}
-    uint64_t operator*() const { return I.getNumber<uint64_t>(); }
+    Operand operator*() const { return Operand(I); }
     iterator &operator++() {
       increment();
       return *this;
@@ -889,14 +890,7 @@ public:
     bool operator==(const iterator &X) const { return I == X.I; }
     bool operator!=(const iterator &X) const { return !(*this == X); }
 
-    uint64_t getArg(unsigned N) const {
-      auto In = I;
-      std::advance(In, N);
-      return In.getNumber<uint64_t>();
-    }
-
     const DIHeaderFieldIterator &getBase() const { return I; }
-
   private:
     void increment() {
       switch (**this) {
@@ -911,6 +905,22 @@ 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 eb101ca2dcbbeee70ee45ee63f284a560e39d43b..8515dd020c8c622637635f5380460fa209724635 100644 (file)
@@ -210,8 +210,8 @@ 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));
@@ -219,7 +219,7 @@ bool DwarfExpression::AddMachineRegExpression(DIExpression Expr,
   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);
+      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 4a7bc67ab58f93ef2e6e6ac9b06516e4603d7c1d..095fcbffde35660e63c21394b5e22c9ccff2add0 100644 (file)
@@ -1406,16 +1406,15 @@ void DIVariable::printInternal(raw_ostream &OS) const {
 }
 
 void DIExpression::printInternal(raw_ostream &OS) const {
-  for (auto E = end(), I = begin(); I != E; ++I) {
-    uint64_t OpCode = *I;
-    OS << " [" << OperationEncodingString(OpCode);
-    switch (OpCode) {
+  for (auto Op : *this) {
+    OS << " [" << OperationEncodingString(Op);
+    switch (Op) {
     case DW_OP_plus: {
-      OS << " " << I.getArg(1);
+      OS << " " << Op.getArg(1);
       break;
     }
     case DW_OP_piece: {
-      OS << " offset=" << I.getArg(1) << ", size=" << I.getArg(2);
+      OS << " offset=" << Op.getArg(1) << ", size=" << Op.getArg(2);
       break;
     }
     case DW_OP_deref: