Global Metrics

path: .metrics.nexits.average
old: 5.0
new: 0.7777777777777778

path: .metrics.nexits.sum
old: 50.0
new: 28.0

path: .metrics.cyclomatic.average
old: 6.666666666666667
new: 1.0

path: .metrics.cyclomatic.sum
old: 80.0
new: 47.0

path: .metrics.cognitive.sum
old: 113.0
new: 0.0

path: .metrics.cognitive.average
old: 11.3
new: 0.0

path: .metrics.mi.mi_sei
old: -42.63953666843035
new: -14.221662223036468

path: .metrics.mi.mi_original
old: 3.248383305830771
new: 7.341029594775549

path: .metrics.mi.mi_visual_studio
old: 1.8996393601349535
new: 4.2929997630266366

path: .metrics.halstead.length
old: 1520.0
new: 1084.0

path: .metrics.halstead.n1
old: 26.0
new: 20.0

path: .metrics.halstead.n2
old: 188.0
new: 124.0

path: .metrics.halstead.level
old: 0.02370744010088273
new: 0.02924528301886792

path: .metrics.halstead.purity_ratio
old: 1.014785616307279
new: 0.8752388416842434

path: .metrics.halstead.bugs
old: 2.089617868640513
new: 1.377856923960261

path: .metrics.halstead.time
old: 27574.629570710655
new: 14764.391798668952

path: .metrics.halstead.volume
old: 11767.029819329742
new: 7772.198701563467

path: .metrics.halstead.difficulty
old: 42.180851063829785
new: 34.193548387096776

path: .metrics.halstead.N1
old: 910.0
new: 660.0

path: .metrics.halstead.N2
old: 610.0
new: 424.0

path: .metrics.halstead.estimated_program_length
old: 1542.4741367870645
new: 948.7589043857198

path: .metrics.halstead.effort
old: 496343.33227279177
new: 265759.05237604113

path: .metrics.halstead.vocabulary
old: 214.0
new: 144.0

path: .metrics.loc.ploc
old: 357.0
new: 231.0

path: .metrics.loc.sloc
old: 498.0
new: 706.0

path: .metrics.loc.blank
old: 105.0
new: 70.0

path: .metrics.loc.lloc
old: 180.0
new: 53.0

path: .metrics.loc.cloc
old: 36.0
new: 405.0

path: .metrics.nargs.average
old: 1.4
new: 0.2777777777777778

path: .metrics.nargs.sum
old: 14.0
new: 10.0

path: .metrics.nom.functions
old: 9.0
new: 36.0

path: .metrics.nom.total
old: 10.0
new: 36.0

path: .metrics.nom.closures
old: 1.0
new: 0.0

Spaces Data

Minimal test - lines (70, 458)

path: .spaces[0].metrics.nargs.average
old: 1.4
new: 0.3125

path: .spaces[0].metrics.nargs.sum
old: 14.0
new: 5.0

path: .spaces[0].metrics.cognitive.sum
old: 113.0
new: 0.0

path: .spaces[0].metrics.cognitive.average
old: 11.3
new: 0.0

path: .spaces[0].metrics.loc.blank
old: 101.0
new: 33.0

path: .spaces[0].metrics.loc.lloc
old: 180.0
new: 21.0

path: .spaces[0].metrics.loc.sloc
old: 468.0
new: 389.0

path: .spaces[0].metrics.loc.cloc
old: 30.0
new: 246.0

path: .spaces[0].metrics.loc.ploc
old: 337.0
new: 110.0

path: .spaces[0].metrics.nom.functions
old: 9.0
new: 16.0

path: .spaces[0].metrics.nom.closures
old: 1.0
new: 0.0

path: .spaces[0].metrics.nom.total
old: 10.0
new: 16.0

path: .spaces[0].metrics.nexits.sum
old: 50.0
new: 12.0

path: .spaces[0].metrics.nexits.average
old: 5.0
new: 0.75

path: .spaces[0].metrics.mi.mi_sei
old: -41.83629702483087
new: 11.772535349606123

path: .spaces[0].metrics.mi.mi_original
old: 4.649756341979639
new: 26.39238453984578

path: .spaces[0].metrics.mi.mi_visual_studio
old: 2.7191557555436487
new: 15.434143005757766

path: .spaces[0].metrics.cyclomatic.sum
old: 79.0
new: 22.0

path: .spaces[0].metrics.cyclomatic.average
old: 7.181818181818182
new: 1.0

path: .spaces[0].metrics.halstead.bugs
old: 2.156764437626717
new: 0.790095001440377

path: .spaces[0].metrics.halstead.difficulty
old: 45.654761904761905
new: 29.931506849315067

path: .spaces[0].metrics.halstead.level
old: 0.021903520208604955
new: 0.033409610983981694

path: .spaces[0].metrics.halstead.effort
old: 520458.3169604935
new: 115398.68326746416

path: .spaces[0].metrics.halstead.purity_ratio
old: 0.909413839799
new: 0.901129975556124

path: .spaces[0].metrics.halstead.N2
old: 590.0
new: 230.0

path: .spaces[0].metrics.halstead.volume
old: 11399.869263280692
new: 3855.425116029695

path: .spaces[0].metrics.halstead.n1
old: 26.0
new: 19.0

path: .spaces[0].metrics.halstead.time
old: 28914.35094224964
new: 6411.037959303564

path: .spaces[0].metrics.halstead.n2
old: 168.0
new: 73.0

path: .spaces[0].metrics.halstead.vocabulary
old: 194.0
new: 92.0

path: .spaces[0].metrics.halstead.estimated_program_length
old: 1364.1207596985005
new: 532.5678155536693

path: .spaces[0].metrics.halstead.length
old: 1500.0
new: 591.0

path: .spaces[0].metrics.halstead.N1
old: 910.0
new: 361.0

Code

namespace lul_test {
namespace test_assembler {

// A Label represents a value not yet known that we need to store in a
// section. As long as all the labels a section refers to are defined
// by the time we retrieve its contents as bytes, we can use undefined
// labels freely in that section's construction.
//
// A label can be in one of three states:
// - undefined,
// - defined as the sum of some other label and a constant, or
// - a constant.
//
// A label's value never changes, but it can accumulate constraints.
// Adding labels and integers is permitted, and yields a label.
// Subtracting a constant from a label is permitted, and also yields a
// label. Subtracting two labels that have some relationship to each
// other is permitted, and yields a constant.
//
// For example:
//
//   Label a;               // a's value is undefined
//   Label b;               // b's value is undefined
//   {
//     Label c = a + 4;     // okay, even though a's value is unknown
//     b = c + 4;           // also okay; b is now a+8
//   }
//   Label d = b - 2;       // okay; d == a+6, even though c is gone
//   d.Value();             // error: d's value is not yet known
//   d - a;                 // is 6, even though their values are not known
//   a = 12;                // now b == 20, and d == 18
//   d.Value();             // 18: no longer an error
//   b.Value();             // 20
//   d = 10;                // error: d is already defined.
//
// Label objects' lifetimes are unconstrained: notice that, in the
// above example, even though a and b are only related through c, and
// c goes out of scope, the assignment to a sets b's value as well. In
// particular, it's not necessary to ensure that a Label lives beyond
// Sections that refer to it.
class Label {
 public:
  Label();                         // An undefined label.
  explicit Label(uint64_t value);  // A label with a fixed value
  Label(const Label& value);       // A label equal to another.
  ~Label();

  Label& operator=(uint64_t value);
  Label& operator=(const Label& value);
  Label operator+(uint64_t addend) const;
  Label operator-(uint64_t subtrahend) const;
  uint64_t operator-(const Label& subtrahend) const;

  // We could also provide == and != that work on undefined, but
  // related, labels.

  // Return true if this label's value is known. If VALUE_P is given,
  // set *VALUE_P to the known value if returning true.
  bool IsKnownConstant(uint64_t* value_p = NULL) const;

  // Return true if the offset from LABEL to this label is known. If
  // OFFSET_P is given, set *OFFSET_P to the offset when returning true.
  //
  // You can think of l.KnownOffsetFrom(m, &d) as being like 'd = l-m',
  // except that it also returns a value indicating whether the
  // subtraction is possible given what we currently know of l and m.
  // It can be possible even if we don't know l and m's values. For
  // example:
  //
  //   Label l, m;
  //   m = l + 10;
  //   l.IsKnownConstant();             // false
  //   m.IsKnownConstant();             // false
  //   uint64_t d;
  //   l.IsKnownOffsetFrom(m, &d);      // true, and sets d to -10.
  //   l-m                              // -10
  //   m-l                              // 10
  //   m.Value()                        // error: m's value is not known
  bool IsKnownOffsetFrom(const Label& label, uint64_t* offset_p = NULL) const;

 private:
  // A label's value, or if that is not yet known, how the value is
  // related to other labels' values. A binding may be:
  // - a known constant,
  // - constrained to be equal to some other binding plus a constant, or
  // - unconstrained, and free to take on any value.
  //
  // Many labels may point to a single binding, and each binding may
  // refer to another, so bindings and labels form trees whose leaves
  // are labels, whose interior nodes (and roots) are bindings, and
  // where links point from children to parents. Bindings are
  // reference counted, allowing labels to be lightweight, copyable,
  // assignable, placed in containers, and so on.
  class Binding {
   public:
    Binding();
    explicit Binding(uint64_t addend);
    ~Binding();

    // Increment our reference count.
    void Acquire() { reference_count_++; };
    // Decrement our reference count, and return true if it is zero.
    bool Release() { return --reference_count_ == 0; }

    // Set this binding to be equal to BINDING + ADDEND. If BINDING is
    // NULL, then set this binding to the known constant ADDEND.
    // Update every binding on this binding's chain to point directly
    // to BINDING, or to be a constant, with addends adjusted
    // appropriately.
    void Set(Binding* binding, uint64_t value);

    // Return what we know about the value of this binding.
    // - If this binding's value is a known constant, set BASE to
    //   NULL, and set ADDEND to its value.
    // - If this binding is not a known constant but related to other
    //   bindings, set BASE to the binding at the end of the relation
    //   chain (which will always be unconstrained), and set ADDEND to the
    //   value to add to that binding's value to get this binding's
    //   value.
    // - If this binding is unconstrained, set BASE to this, and leave
    //   ADDEND unchanged.
    void Get(Binding** base, uint64_t* addend);

   private:
    // There are three cases:
    //
    // - A binding representing a known constant value has base_ NULL,
    //   and addend_ equal to the value.
    //
    // - A binding representing a completely unconstrained value has
    //   base_ pointing to this; addend_ is unused.
    //
    // - A binding whose value is related to some other binding's
    //   value has base_ pointing to that other binding, and addend_
    //   set to the amount to add to that binding's value to get this
    //   binding's value. We only represent relationships of the form
    //   x = y+c.
    //
    // Thus, the bind_ links form a chain terminating in either a
    // known constant value or a completely unconstrained value. Most
    // operations on bindings do path compression: they change every
    // binding on the chain to point directly to the final value,
    // adjusting addends as appropriate.
    Binding* base_;
    uint64_t addend_;

    // The number of Labels and Bindings pointing to this binding.
    // (When a binding points to itself, indicating a completely
    // unconstrained binding, that doesn't count as a reference.)
    int reference_count_;
  };

  // This label's value.
  Binding* value_;
};

// Conventions for representing larger numbers as sequences of bytes.
enum Endianness {
  kBigEndian,     // Big-endian: the most significant byte comes first.
  kLittleEndian,  // Little-endian: the least significant byte comes first.
  kUnsetEndian,   // used internally
};

// A section is a sequence of bytes, constructed by appending bytes
// to the end. Sections have a convenient and flexible set of member
// functions for appending data in various formats: big-endian and
// little-endian signed and unsigned values of different sizes;
// LEB128 and ULEB128 values (see below), and raw blocks of bytes.
//
// If you need to append a value to a section that is not convenient
// to compute immediately, you can create a label, append the
// label's value to the section, and then set the label's value
// later, when it's convenient to do so. Once a label's value is
// known, the section class takes care of updating all previously
// appended references to it.
//
// Once all the labels to which a section refers have had their
// values determined, you can get a copy of the section's contents
// as a string.
//
// Note that there is no specified "start of section" label. This is
// because there are typically several different meanings for "the
// start of a section": the offset of the section within an object
// file, the address in memory at which the section's content appear,
// and so on. It's up to the code that uses the Section class to
// keep track of these explicitly, as they depend on the application.
class Section {
 public:
  explicit Section(Endianness endianness = kUnsetEndian)
      : endianness_(endianness){};

  // A base class destructor should be either public and virtual,
  // or protected and nonvirtual.
  virtual ~Section(){};

  // Return the default endianness of this section.
  Endianness endianness() const { return endianness_; }

  // Append the SIZE bytes at DATA to the end of this section. Return
  // a reference to this section.
  Section& Append(const string& data) {
    contents_.append(data);
    return *this;
  };

  // Append SIZE copies of BYTE to the end of this section. Return a
  // reference to this section.
  Section& Append(size_t size, uint8_t byte) {
    contents_.append(size, (char)byte);
    return *this;
  }

  // Append NUMBER to this section. ENDIANNESS is the endianness to
  // use to write the number. SIZE is the length of the number in
  // bytes. Return a reference to this section.
  Section& Append(Endianness endianness, size_t size, uint64_t number);
  Section& Append(Endianness endianness, size_t size, const Label& label);

  // Append SECTION to the end of this section. The labels SECTION
  // refers to need not be defined yet.
  //
  // Note that this has no effect on any Labels' values, or on
  // SECTION. If placing SECTION within 'this' provides new
  // constraints on existing labels' values, then it's up to the
  // caller to fiddle with those labels as needed.
  Section& Append(const Section& section);

  // Append the contents of DATA as a series of bytes terminated by
  // a NULL character.
  Section& AppendCString(const string& data) {
    Append(data);
    contents_ += '\0';
    return *this;
  }

  // Append VALUE or LABEL to this section, with the given bit width and
  // endianness. Return a reference to this section.
  //
  // The names of these functions have the form :
  //  is either 'L' (little-endian, least significant byte first),
  //                        'B' (big-endian, most significant byte first), or
  //                        'D' (default, the section's default endianness)
  //  is 8, 16, 32, or 64.
  //
  // Since endianness doesn't matter for a single byte, all the
  // =8 functions are equivalent.
  //
  // These can be used to write both signed and unsigned values, as
  // the compiler will properly sign-extend a signed value before
  // passing it to the function, at which point the function's
  // behavior is the same either way.
  Section& L8(uint8_t value) {
    contents_ += value;
    return *this;
  }
  Section& B8(uint8_t value) {
    contents_ += value;
    return *this;
  }
  Section& D8(uint8_t value) {
    contents_ += value;
    return *this;
  }
  Section &L16(uint16_t), &L32(uint32_t), &L64(uint64_t), &B16(uint16_t),
      &B32(uint32_t), &B64(uint64_t), &D16(uint16_t), &D32(uint32_t),
      &D64(uint64_t);
  Section &L8(const Label&label), &L16(const Label&label),
      &L32(const Label&label), &L64(const Label&label), &B8(const Label&label),
      &B16(const Label&label), &B32(const Label&label), &B64(const Label&label),
      &D8(const Label&label), &D16(const Label&label), &D32(const Label&label),
      &D64(const Label&label);

  // Append VALUE in a signed LEB128 (Little-Endian Base 128) form.
  //
  // The signed LEB128 representation of an integer N is a variable
  // number of bytes:
  //
  // - If N is between -0x40 and 0x3f, then its signed LEB128
  //   representation is a single byte whose value is N.
  //
  // - Otherwise, its signed LEB128 representation is (N & 0x7f) |
  //   0x80, followed by the signed LEB128 representation of N / 128,
  //   rounded towards negative infinity.
  //
  // In other words, we break VALUE into groups of seven bits, put
  // them in little-endian order, and then write them as eight-bit
  // bytes with the high bit on all but the last.
  //
  // Note that VALUE cannot be a Label (we would have to implement
  // relaxation).
  Section& LEB128(long long value);

  // Append VALUE in unsigned LEB128 (Little-Endian Base 128) form.
  //
  // The unsigned LEB128 representation of an integer N is a variable
  // number of bytes:
  //
  // - If N is between 0 and 0x7f, then its unsigned LEB128
  //   representation is a single byte whose value is N.
  //
  // - Otherwise, its unsigned LEB128 representation is (N & 0x7f) |
  //   0x80, followed by the unsigned LEB128 representation of N /
  //   128, rounded towards negative infinity.
  //
  // Note that VALUE cannot be a Label (we would have to implement
  // relaxation).
  Section& ULEB128(uint64_t value);

  // Jump to the next location aligned on an ALIGNMENT-byte boundary,
  // relative to the start of the section. Fill the gap with PAD_BYTE.
  // ALIGNMENT must be a power of two. Return a reference to this
  // section.
  Section& Align(size_t alignment, uint8_t pad_byte = 0);

  // Return the current size of the section.
  size_t Size() const { return contents_.size(); }

  // Return a label representing the start of the section.
  //
  // It is up to the user whether this label represents the section's
  // position in an object file, the section's address in memory, or
  // what have you; some applications may need both, in which case
  // this simple-minded interface won't be enough. This class only
  // provides a single start label, for use with the Here and Mark
  // member functions.
  //
  // Ideally, we'd provide this in a subclass that actually knows more
  // about the application at hand and can provide an appropriate
  // collection of start labels. But then the appending member
  // functions like Append and D32 would return a reference to the
  // base class, not the derived class, and the chaining won't work.
  // Since the only value here is in pretty notation, that's a fatal
  // flaw.
  Label start() const { return start_; }

  // Return a label representing the point at which the next Appended
  // item will appear in the section, relative to start().
  Label Here() const { return start_ + Size(); }

  // Set *LABEL to Here, and return a reference to this section.
  Section& Mark(Label* label) {
    *label = Here();
    return *this;
  }

  // If there are no undefined label references left in this
  // section, set CONTENTS to the contents of this section, as a
  // string, and clear this section. Return true on success, or false
  // if there were still undefined labels.
  bool GetContents(string* contents);

 private:
  // Used internally. A reference to a label's value.
  struct Reference {
    Reference(size_t set_offset, Endianness set_endianness, size_t set_size,
              const Label& set_label)
        : offset(set_offset),
          endianness(set_endianness),
          size(set_size),
          label(set_label) {}

    // The offset of the reference within the section.
    size_t offset;

    // The endianness of the reference.
    Endianness endianness;

    // The size of the reference.
    size_t size;

    // The label to which this is a reference.
    Label label;
  };

  // The default endianness of this section.
  Endianness endianness_;

  // The contents of the section.
  string contents_;

  // References to labels within those contents.
  vector references_;

  // A label referring to the beginning of the section.
  Label start_;
};

}  // namespace test_assembler
}  // namespace lul_test

Minimal test - lines (71, 457)

path: .spaces[0].spaces[0].metrics.nom.functions
old: 9.0
new: 16.0

path: .spaces[0].spaces[0].metrics.nom.total
old: 10.0
new: 16.0

path: .spaces[0].spaces[0].metrics.nom.closures
old: 1.0
new: 0.0

path: .spaces[0].spaces[0].metrics.nargs.average
old: 1.4
new: 0.3125

path: .spaces[0].spaces[0].metrics.nargs.sum
old: 14.0
new: 5.0

path: .spaces[0].spaces[0].metrics.halstead.estimated_program_length
old: 1355.2900495148351
new: 524.9452228592745

path: .spaces[0].spaces[0].metrics.halstead.bugs
old: 2.159581674578532
new: 0.7920073814494211

path: .spaces[0].spaces[0].metrics.halstead.volume
old: 11373.500641827584
new: 3833.091043077032

path: .spaces[0].spaces[0].metrics.halstead.length
old: 1498.0
new: 589.0

path: .spaces[0].spaces[0].metrics.halstead.purity_ratio
old: 0.9047330103570328
new: 0.8912482561278006

path: .spaces[0].spaces[0].metrics.halstead.n1
old: 26.0
new: 19.0

path: .spaces[0].spaces[0].metrics.halstead.vocabulary
old: 193.0
new: 91.0

path: .spaces[0].spaces[0].metrics.halstead.N1
old: 909.0
new: 360.0

path: .spaces[0].spaces[0].metrics.halstead.N2
old: 589.0
new: 229.0

path: .spaces[0].spaces[0].metrics.halstead.time
old: 28971.022759305994
new: 6434.328367449139

path: .spaces[0].spaces[0].metrics.halstead.difficulty
old: 45.8502994011976
new: 30.21527777777778

path: .spaces[0].spaces[0].metrics.halstead.level
old: 0.02181010839754473
new: 0.033095840036773155

path: .spaces[0].spaces[0].metrics.halstead.effort
old: 521478.40966750786
new: 115817.9106140845

path: .spaces[0].spaces[0].metrics.halstead.n2
old: 167.0
new: 72.0

path: .spaces[0].spaces[0].metrics.loc.cloc
old: 29.0
new: 245.0

path: .spaces[0].spaces[0].metrics.loc.lloc
old: 180.0
new: 21.0

path: .spaces[0].spaces[0].metrics.loc.blank
old: 102.0
new: 34.0

path: .spaces[0].spaces[0].metrics.loc.ploc
old: 335.0
new: 108.0

path: .spaces[0].spaces[0].metrics.loc.sloc
old: 466.0
new: 387.0

path: .spaces[0].spaces[0].metrics.cyclomatic.sum
old: 78.0
new: 21.0

path: .spaces[0].spaces[0].metrics.cyclomatic.average
old: 7.8
new: 1.0

path: .spaces[0].spaces[0].metrics.nexits.sum
old: 50.0
new: 12.0

path: .spaces[0].spaces[0].metrics.nexits.average
old: 5.0
new: 0.75

path: .spaces[0].spaces[0].metrics.mi.mi_sei
old: -41.75555085411859
new: 12.177655068133944

path: .spaces[0].spaces[0].metrics.mi.mi_visual_studio
old: 2.901273289684791
new: 15.635146503948576

path: .spaces[0].spaces[0].metrics.mi.mi_original
old: 4.9611773253609925
new: 26.736100521752064

path: .spaces[0].spaces[0].metrics.cognitive.average
old: 11.3
new: 0.0

path: .spaces[0].spaces[0].metrics.cognitive.sum
old: 113.0
new: 0.0

Code

namespace test_assembler {

// A Label represents a value not yet known that we need to store in a
// section. As long as all the labels a section refers to are defined
// by the time we retrieve its contents as bytes, we can use undefined
// labels freely in that section's construction.
//
// A label can be in one of three states:
// - undefined,
// - defined as the sum of some other label and a constant, or
// - a constant.
//
// A label's value never changes, but it can accumulate constraints.
// Adding labels and integers is permitted, and yields a label.
// Subtracting a constant from a label is permitted, and also yields a
// label. Subtracting two labels that have some relationship to each
// other is permitted, and yields a constant.
//
// For example:
//
//   Label a;               // a's value is undefined
//   Label b;               // b's value is undefined
//   {
//     Label c = a + 4;     // okay, even though a's value is unknown
//     b = c + 4;           // also okay; b is now a+8
//   }
//   Label d = b - 2;       // okay; d == a+6, even though c is gone
//   d.Value();             // error: d's value is not yet known
//   d - a;                 // is 6, even though their values are not known
//   a = 12;                // now b == 20, and d == 18
//   d.Value();             // 18: no longer an error
//   b.Value();             // 20
//   d = 10;                // error: d is already defined.
//
// Label objects' lifetimes are unconstrained: notice that, in the
// above example, even though a and b are only related through c, and
// c goes out of scope, the assignment to a sets b's value as well. In
// particular, it's not necessary to ensure that a Label lives beyond
// Sections that refer to it.
class Label {
 public:
  Label();                         // An undefined label.
  explicit Label(uint64_t value);  // A label with a fixed value
  Label(const Label& value);       // A label equal to another.
  ~Label();

  Label& operator=(uint64_t value);
  Label& operator=(const Label& value);
  Label operator+(uint64_t addend) const;
  Label operator-(uint64_t subtrahend) const;
  uint64_t operator-(const Label& subtrahend) const;

  // We could also provide == and != that work on undefined, but
  // related, labels.

  // Return true if this label's value is known. If VALUE_P is given,
  // set *VALUE_P to the known value if returning true.
  bool IsKnownConstant(uint64_t* value_p = NULL) const;

  // Return true if the offset from LABEL to this label is known. If
  // OFFSET_P is given, set *OFFSET_P to the offset when returning true.
  //
  // You can think of l.KnownOffsetFrom(m, &d) as being like 'd = l-m',
  // except that it also returns a value indicating whether the
  // subtraction is possible given what we currently know of l and m.
  // It can be possible even if we don't know l and m's values. For
  // example:
  //
  //   Label l, m;
  //   m = l + 10;
  //   l.IsKnownConstant();             // false
  //   m.IsKnownConstant();             // false
  //   uint64_t d;
  //   l.IsKnownOffsetFrom(m, &d);      // true, and sets d to -10.
  //   l-m                              // -10
  //   m-l                              // 10
  //   m.Value()                        // error: m's value is not known
  bool IsKnownOffsetFrom(const Label& label, uint64_t* offset_p = NULL) const;

 private:
  // A label's value, or if that is not yet known, how the value is
  // related to other labels' values. A binding may be:
  // - a known constant,
  // - constrained to be equal to some other binding plus a constant, or
  // - unconstrained, and free to take on any value.
  //
  // Many labels may point to a single binding, and each binding may
  // refer to another, so bindings and labels form trees whose leaves
  // are labels, whose interior nodes (and roots) are bindings, and
  // where links point from children to parents. Bindings are
  // reference counted, allowing labels to be lightweight, copyable,
  // assignable, placed in containers, and so on.
  class Binding {
   public:
    Binding();
    explicit Binding(uint64_t addend);
    ~Binding();

    // Increment our reference count.
    void Acquire() { reference_count_++; };
    // Decrement our reference count, and return true if it is zero.
    bool Release() { return --reference_count_ == 0; }

    // Set this binding to be equal to BINDING + ADDEND. If BINDING is
    // NULL, then set this binding to the known constant ADDEND.
    // Update every binding on this binding's chain to point directly
    // to BINDING, or to be a constant, with addends adjusted
    // appropriately.
    void Set(Binding* binding, uint64_t value);

    // Return what we know about the value of this binding.
    // - If this binding's value is a known constant, set BASE to
    //   NULL, and set ADDEND to its value.
    // - If this binding is not a known constant but related to other
    //   bindings, set BASE to the binding at the end of the relation
    //   chain (which will always be unconstrained), and set ADDEND to the
    //   value to add to that binding's value to get this binding's
    //   value.
    // - If this binding is unconstrained, set BASE to this, and leave
    //   ADDEND unchanged.
    void Get(Binding** base, uint64_t* addend);

   private:
    // There are three cases:
    //
    // - A binding representing a known constant value has base_ NULL,
    //   and addend_ equal to the value.
    //
    // - A binding representing a completely unconstrained value has
    //   base_ pointing to this; addend_ is unused.
    //
    // - A binding whose value is related to some other binding's
    //   value has base_ pointing to that other binding, and addend_
    //   set to the amount to add to that binding's value to get this
    //   binding's value. We only represent relationships of the form
    //   x = y+c.
    //
    // Thus, the bind_ links form a chain terminating in either a
    // known constant value or a completely unconstrained value. Most
    // operations on bindings do path compression: they change every
    // binding on the chain to point directly to the final value,
    // adjusting addends as appropriate.
    Binding* base_;
    uint64_t addend_;

    // The number of Labels and Bindings pointing to this binding.
    // (When a binding points to itself, indicating a completely
    // unconstrained binding, that doesn't count as a reference.)
    int reference_count_;
  };

  // This label's value.
  Binding* value_;
};

// Conventions for representing larger numbers as sequences of bytes.
enum Endianness {
  kBigEndian,     // Big-endian: the most significant byte comes first.
  kLittleEndian,  // Little-endian: the least significant byte comes first.
  kUnsetEndian,   // used internally
};

// A section is a sequence of bytes, constructed by appending bytes
// to the end. Sections have a convenient and flexible set of member
// functions for appending data in various formats: big-endian and
// little-endian signed and unsigned values of different sizes;
// LEB128 and ULEB128 values (see below), and raw blocks of bytes.
//
// If you need to append a value to a section that is not convenient
// to compute immediately, you can create a label, append the
// label's value to the section, and then set the label's value
// later, when it's convenient to do so. Once a label's value is
// known, the section class takes care of updating all previously
// appended references to it.
//
// Once all the labels to which a section refers have had their
// values determined, you can get a copy of the section's contents
// as a string.
//
// Note that there is no specified "start of section" label. This is
// because there are typically several different meanings for "the
// start of a section": the offset of the section within an object
// file, the address in memory at which the section's content appear,
// and so on. It's up to the code that uses the Section class to
// keep track of these explicitly, as they depend on the application.
class Section {
 public:
  explicit Section(Endianness endianness = kUnsetEndian)
      : endianness_(endianness){};

  // A base class destructor should be either public and virtual,
  // or protected and nonvirtual.
  virtual ~Section(){};

  // Return the default endianness of this section.
  Endianness endianness() const { return endianness_; }

  // Append the SIZE bytes at DATA to the end of this section. Return
  // a reference to this section.
  Section& Append(const string& data) {
    contents_.append(data);
    return *this;
  };

  // Append SIZE copies of BYTE to the end of this section. Return a
  // reference to this section.
  Section& Append(size_t size, uint8_t byte) {
    contents_.append(size, (char)byte);
    return *this;
  }

  // Append NUMBER to this section. ENDIANNESS is the endianness to
  // use to write the number. SIZE is the length of the number in
  // bytes. Return a reference to this section.
  Section& Append(Endianness endianness, size_t size, uint64_t number);
  Section& Append(Endianness endianness, size_t size, const Label& label);

  // Append SECTION to the end of this section. The labels SECTION
  // refers to need not be defined yet.
  //
  // Note that this has no effect on any Labels' values, or on
  // SECTION. If placing SECTION within 'this' provides new
  // constraints on existing labels' values, then it's up to the
  // caller to fiddle with those labels as needed.
  Section& Append(const Section& section);

  // Append the contents of DATA as a series of bytes terminated by
  // a NULL character.
  Section& AppendCString(const string& data) {
    Append(data);
    contents_ += '\0';
    return *this;
  }

  // Append VALUE or LABEL to this section, with the given bit width and
  // endianness. Return a reference to this section.
  //
  // The names of these functions have the form :
  //  is either 'L' (little-endian, least significant byte first),
  //                        'B' (big-endian, most significant byte first), or
  //                        'D' (default, the section's default endianness)
  //  is 8, 16, 32, or 64.
  //
  // Since endianness doesn't matter for a single byte, all the
  // =8 functions are equivalent.
  //
  // These can be used to write both signed and unsigned values, as
  // the compiler will properly sign-extend a signed value before
  // passing it to the function, at which point the function's
  // behavior is the same either way.
  Section& L8(uint8_t value) {
    contents_ += value;
    return *this;
  }
  Section& B8(uint8_t value) {
    contents_ += value;
    return *this;
  }
  Section& D8(uint8_t value) {
    contents_ += value;
    return *this;
  }
  Section &L16(uint16_t), &L32(uint32_t), &L64(uint64_t), &B16(uint16_t),
      &B32(uint32_t), &B64(uint64_t), &D16(uint16_t), &D32(uint32_t),
      &D64(uint64_t);
  Section &L8(const Label&label), &L16(const Label&label),
      &L32(const Label&label), &L64(const Label&label), &B8(const Label&label),
      &B16(const Label&label), &B32(const Label&label), &B64(const Label&label),
      &D8(const Label&label), &D16(const Label&label), &D32(const Label&label),
      &D64(const Label&label);

  // Append VALUE in a signed LEB128 (Little-Endian Base 128) form.
  //
  // The signed LEB128 representation of an integer N is a variable
  // number of bytes:
  //
  // - If N is between -0x40 and 0x3f, then its signed LEB128
  //   representation is a single byte whose value is N.
  //
  // - Otherwise, its signed LEB128 representation is (N & 0x7f) |
  //   0x80, followed by the signed LEB128 representation of N / 128,
  //   rounded towards negative infinity.
  //
  // In other words, we break VALUE into groups of seven bits, put
  // them in little-endian order, and then write them as eight-bit
  // bytes with the high bit on all but the last.
  //
  // Note that VALUE cannot be a Label (we would have to implement
  // relaxation).
  Section& LEB128(long long value);

  // Append VALUE in unsigned LEB128 (Little-Endian Base 128) form.
  //
  // The unsigned LEB128 representation of an integer N is a variable
  // number of bytes:
  //
  // - If N is between 0 and 0x7f, then its unsigned LEB128
  //   representation is a single byte whose value is N.
  //
  // - Otherwise, its unsigned LEB128 representation is (N & 0x7f) |
  //   0x80, followed by the unsigned LEB128 representation of N /
  //   128, rounded towards negative infinity.
  //
  // Note that VALUE cannot be a Label (we would have to implement
  // relaxation).
  Section& ULEB128(uint64_t value);

  // Jump to the next location aligned on an ALIGNMENT-byte boundary,
  // relative to the start of the section. Fill the gap with PAD_BYTE.
  // ALIGNMENT must be a power of two. Return a reference to this
  // section.
  Section& Align(size_t alignment, uint8_t pad_byte = 0);

  // Return the current size of the section.
  size_t Size() const { return contents_.size(); }

  // Return a label representing the start of the section.
  //
  // It is up to the user whether this label represents the section's
  // position in an object file, the section's address in memory, or
  // what have you; some applications may need both, in which case
  // this simple-minded interface won't be enough. This class only
  // provides a single start label, for use with the Here and Mark
  // member functions.
  //
  // Ideally, we'd provide this in a subclass that actually knows more
  // about the application at hand and can provide an appropriate
  // collection of start labels. But then the appending member
  // functions like Append and D32 would return a reference to the
  // base class, not the derived class, and the chaining won't work.
  // Since the only value here is in pretty notation, that's a fatal
  // flaw.
  Label start() const { return start_; }

  // Return a label representing the point at which the next Appended
  // item will appear in the section, relative to start().
  Label Here() const { return start_ + Size(); }

  // Set *LABEL to Here, and return a reference to this section.
  Section& Mark(Label* label) {
    *label = Here();
    return *this;
  }

  // If there are no undefined label references left in this
  // section, set CONTENTS to the contents of this section, as a
  // string, and clear this section. Return true on success, or false
  // if there were still undefined labels.
  bool GetContents(string* contents);

 private:
  // Used internally. A reference to a label's value.
  struct Reference {
    Reference(size_t set_offset, Endianness set_endianness, size_t set_size,
              const Label& set_label)
        : offset(set_offset),
          endianness(set_endianness),
          size(set_size),
          label(set_label) {}

    // The offset of the reference within the section.
    size_t offset;

    // The endianness of the reference.
    Endianness endianness;

    // The size of the reference.
    size_t size;

    // The label to which this is a reference.
    Label label;
  };

  // The default endianness of this section.
  Endianness endianness_;

  // The contents of the section.
  string contents_;

  // References to labels within those contents.
  vector references_;

  // A label referring to the beginning of the section.
  Label start_;
};

}  // namespace test_assembler

Minimal test - lines (110, 224)

path: .spaces[0].spaces[0].spaces[0].metrics.cyclomatic.average
old: 4.0
new: 1.0

path: .spaces[0].spaces[0].spaces[0].metrics.cognitive.sum
old: 3.0
new: 0.0

path: .spaces[0].spaces[0].spaces[0].metrics.cognitive.average
old: 3.0
new: 0.0

path: .spaces[0].spaces[0].spaces[0].metrics.loc.cloc
old: 3.0
new: 77.0

path: .spaces[0].spaces[0].spaces[0].metrics.loc.ploc
old: 26.0
new: 30.0

path: .spaces[0].spaces[0].spaces[0].metrics.loc.lloc
old: 18.0
new: 2.0

path: .spaces[0].spaces[0].spaces[0].metrics.loc.sloc
old: 35.0
new: 115.0

path: .spaces[0].spaces[0].spaces[0].metrics.loc.blank
old: 6.0
new: 8.0

path: .spaces[0].spaces[0].spaces[0].metrics.mi.mi_original
old: 77.8395665125886
new: 58.82620781671396

path: .spaces[0].spaces[0].spaces[0].metrics.mi.mi_visual_studio
old: 45.52021433484714
new: 34.401291120885354

path: .spaces[0].spaces[0].spaces[0].metrics.mi.mi_sei
old: 58.9134838632916
new: 57.29492142313361

path: .spaces[0].spaces[0].spaces[0].metrics.nom.functions
old: 1.0
new: 2.0

path: .spaces[0].spaces[0].spaces[0].metrics.nom.total
old: 1.0
new: 2.0

path: .spaces[0].spaces[0].spaces[0].metrics.nexits.average
old: 3.0
new: 0.5

path: .spaces[0].spaces[0].spaces[0].metrics.nexits.sum
old: 3.0
new: 1.0

path: .spaces[0].spaces[0].spaces[0].metrics.halstead.difficulty
old: 15.648148148148149
new: 16.227272727272727

path: .spaces[0].spaces[0].spaces[0].metrics.halstead.n2
old: 27.0
new: 22.0

path: .spaces[0].spaces[0].spaces[0].metrics.halstead.level
old: 0.06390532544378698
new: 0.06162464985994398

path: .spaces[0].spaces[0].spaces[0].metrics.halstead.N1
old: 82.0
new: 93.0

path: .spaces[0].spaces[0].spaces[0].metrics.halstead.volume
old: 782.3234299484423
new: 744.4692002076929

path: .spaces[0].spaces[0].spaces[0].metrics.halstead.effort
old: 12241.912931600624
new: 12080.704748824834

path: .spaces[0].spaces[0].spaces[0].metrics.halstead.purity_ratio
old: 1.2005964550629105
new: 1.0514615591585208

path: .spaces[0].spaces[0].spaces[0].metrics.halstead.N2
old: 65.0
new: 51.0

path: .spaces[0].spaces[0].spaces[0].metrics.halstead.vocabulary
old: 40.0
new: 36.0

path: .spaces[0].spaces[0].spaces[0].metrics.halstead.n1
old: 13.0
new: 14.0

path: .spaces[0].spaces[0].spaces[0].metrics.halstead.length
old: 147.0
new: 144.0

path: .spaces[0].spaces[0].spaces[0].metrics.halstead.bugs
old: 0.17705638905629675
new: 0.17549857407537675

path: .spaces[0].spaces[0].spaces[0].metrics.halstead.estimated_program_length
old: 176.48767889424784
new: 151.41046451882698

path: .spaces[0].spaces[0].spaces[0].metrics.halstead.time
old: 680.1062739778124
new: 671.1502638236019

Code

class Label {
 public:
  Label();                         // An undefined label.
  explicit Label(uint64_t value);  // A label with a fixed value
  Label(const Label& value);       // A label equal to another.
  ~Label();

  Label& operator=(uint64_t value);
  Label& operator=(const Label& value);
  Label operator+(uint64_t addend) const;
  Label operator-(uint64_t subtrahend) const;
  uint64_t operator-(const Label& subtrahend) const;

  // We could also provide == and != that work on undefined, but
  // related, labels.

  // Return true if this label's value is known. If VALUE_P is given,
  // set *VALUE_P to the known value if returning true.
  bool IsKnownConstant(uint64_t* value_p = NULL) const;

  // Return true if the offset from LABEL to this label is known. If
  // OFFSET_P is given, set *OFFSET_P to the offset when returning true.
  //
  // You can think of l.KnownOffsetFrom(m, &d) as being like 'd = l-m',
  // except that it also returns a value indicating whether the
  // subtraction is possible given what we currently know of l and m.
  // It can be possible even if we don't know l and m's values. For
  // example:
  //
  //   Label l, m;
  //   m = l + 10;
  //   l.IsKnownConstant();             // false
  //   m.IsKnownConstant();             // false
  //   uint64_t d;
  //   l.IsKnownOffsetFrom(m, &d);      // true, and sets d to -10.
  //   l-m                              // -10
  //   m-l                              // 10
  //   m.Value()                        // error: m's value is not known
  bool IsKnownOffsetFrom(const Label& label, uint64_t* offset_p = NULL) const;

 private:
  // A label's value, or if that is not yet known, how the value is
  // related to other labels' values. A binding may be:
  // - a known constant,
  // - constrained to be equal to some other binding plus a constant, or
  // - unconstrained, and free to take on any value.
  //
  // Many labels may point to a single binding, and each binding may
  // refer to another, so bindings and labels form trees whose leaves
  // are labels, whose interior nodes (and roots) are bindings, and
  // where links point from children to parents. Bindings are
  // reference counted, allowing labels to be lightweight, copyable,
  // assignable, placed in containers, and so on.
  class Binding {
   public:
    Binding();
    explicit Binding(uint64_t addend);
    ~Binding();

    // Increment our reference count.
    void Acquire() { reference_count_++; };
    // Decrement our reference count, and return true if it is zero.
    bool Release() { return --reference_count_ == 0; }

    // Set this binding to be equal to BINDING + ADDEND. If BINDING is
    // NULL, then set this binding to the known constant ADDEND.
    // Update every binding on this binding's chain to point directly
    // to BINDING, or to be a constant, with addends adjusted
    // appropriately.
    void Set(Binding* binding, uint64_t value);

    // Return what we know about the value of this binding.
    // - If this binding's value is a known constant, set BASE to
    //   NULL, and set ADDEND to its value.
    // - If this binding is not a known constant but related to other
    //   bindings, set BASE to the binding at the end of the relation
    //   chain (which will always be unconstrained), and set ADDEND to the
    //   value to add to that binding's value to get this binding's
    //   value.
    // - If this binding is unconstrained, set BASE to this, and leave
    //   ADDEND unchanged.
    void Get(Binding** base, uint64_t* addend);

   private:
    // There are three cases:
    //
    // - A binding representing a known constant value has base_ NULL,
    //   and addend_ equal to the value.
    //
    // - A binding representing a completely unconstrained value has
    //   base_ pointing to this; addend_ is unused.
    //
    // - A binding whose value is related to some other binding's
    //   value has base_ pointing to that other binding, and addend_
    //   set to the amount to add to that binding's value to get this
    //   binding's value. We only represent relationships of the form
    //   x = y+c.
    //
    // Thus, the bind_ links form a chain terminating in either a
    // known constant value or a completely unconstrained value. Most
    // operations on bindings do path compression: they change every
    // binding on the chain to point directly to the final value,
    // adjusting addends as appropriate.
    Binding* base_;
    uint64_t addend_;

    // The number of Labels and Bindings pointing to this binding.
    // (When a binding points to itself, indicating a completely
    // unconstrained binding, that doesn't count as a reference.)
    int reference_count_;
  };

  // This label's value.
  Binding* value_;
};

Minimal test - lines (256, 455)

path: .spaces[0].spaces[0].spaces[1].metrics.mi.mi_sei
old: 89.64995099177216
new: 29.382645910189524

path: .spaces[0].spaces[0].spaces[1].metrics.mi.mi_visual_studio
old: 66.94227520151524
new: 23.717738139218476

path: .spaces[0].spaces[0].spaces[1].metrics.mi.mi_original
old: 114.47129059459108
new: 40.55733221806359

path: .spaces[0].spaces[0].spaces[1].metrics.halstead.estimated_program_length
old: 35.161259458730164
new: 353.29369244054624

path: .spaces[0].spaces[0].spaces[1].metrics.halstead.time
old: 16.789032054529027
new: 3950.808206867556

path: .spaces[0].spaces[0].spaces[1].metrics.halstead.bugs
old: 0.01501104248806686
new: 0.5721602908005651

path: .spaces[0].spaces[0].spaces[1].metrics.halstead.n2
old: 6.0
new: 51.0

path: .spaces[0].spaces[0].spaces[1].metrics.halstead.N1
old: 13.0
new: 259.0

path: .spaces[0].spaces[0].spaces[1].metrics.halstead.level
old: 0.2448979591836735
new: 0.03684971098265896

path: .spaces[0].spaces[0].spaces[1].metrics.halstead.n1
old: 7.0
new: 16.0

path: .spaces[0].spaces[0].spaces[1].metrics.halstead.N2
old: 7.0
new: 173.0

path: .spaces[0].spaces[0].spaces[1].metrics.halstead.effort
old: 302.2025769815225
new: 71114.54772361601

path: .spaces[0].spaces[0].spaces[1].metrics.halstead.length
old: 20.0
new: 432.0

path: .spaces[0].spaces[0].spaces[1].metrics.halstead.vocabulary
old: 13.0
new: 67.0

path: .spaces[0].spaces[0].spaces[1].metrics.halstead.volume
old: 74.00879436282185
new: 2620.5505302777574

path: .spaces[0].spaces[0].spaces[1].metrics.halstead.purity_ratio
old: 1.7580629729365085
new: 0.8178094732420051

path: .spaces[0].spaces[0].spaces[1].metrics.halstead.difficulty
old: 4.083333333333333
new: 27.137254901960784

path: .spaces[0].spaces[0].spaces[1].metrics.loc.cloc
old: 0.0
new: 104.0

path: .spaces[0].spaces[0].spaces[1].metrics.loc.ploc
old: 7.0
new: 71.0

path: .spaces[0].spaces[0].spaces[1].metrics.loc.blank
old: 1.0
new: 25.0

path: .spaces[0].spaces[0].spaces[1].metrics.loc.lloc
old: 4.0
new: 19.0

path: .spaces[0].spaces[0].spaces[1].metrics.loc.sloc
old: 8.0
new: 200.0

path: .spaces[0].spaces[0].spaces[1].metrics.nexits.sum
old: 2.0
new: 11.0

path: .spaces[0].spaces[0].spaces[1].metrics.nexits.average
old: 2.0
new: 0.7857142857142857

path: .spaces[0].spaces[0].spaces[1].metrics.nom.functions
old: 1.0
new: 14.0

path: .spaces[0].spaces[0].spaces[1].metrics.nom.total
old: 1.0
new: 14.0

path: .spaces[0].spaces[0].spaces[1].metrics.cyclomatic.average
old: 2.0
new: 1.0

path: .spaces[0].spaces[0].spaces[1].metrics.cyclomatic.sum
old: 2.0
new: 16.0

path: .spaces[0].spaces[0].spaces[1].metrics.cognitive.sum
old: 1.0
new: 0.0

path: .spaces[0].spaces[0].spaces[1].metrics.cognitive.average
old: 1.0
new: 0.0

path: .spaces[0].spaces[0].spaces[1].metrics.nargs.sum
old: 0.0
new: 5.0

path: .spaces[0].spaces[0].spaces[1].metrics.nargs.average
old: 0.0
new: 0.35714285714285715

Code

class Section {
 public:
  explicit Section(Endianness endianness = kUnsetEndian)
      : endianness_(endianness){};

  // A base class destructor should be either public and virtual,
  // or protected and nonvirtual.
  virtual ~Section(){};

  // Return the default endianness of this section.
  Endianness endianness() const { return endianness_; }

  // Append the SIZE bytes at DATA to the end of this section. Return
  // a reference to this section.
  Section& Append(const string& data) {
    contents_.append(data);
    return *this;
  };

  // Append SIZE copies of BYTE to the end of this section. Return a
  // reference to this section.
  Section& Append(size_t size, uint8_t byte) {
    contents_.append(size, (char)byte);
    return *this;
  }

  // Append NUMBER to this section. ENDIANNESS is the endianness to
  // use to write the number. SIZE is the length of the number in
  // bytes. Return a reference to this section.
  Section& Append(Endianness endianness, size_t size, uint64_t number);
  Section& Append(Endianness endianness, size_t size, const Label& label);

  // Append SECTION to the end of this section. The labels SECTION
  // refers to need not be defined yet.
  //
  // Note that this has no effect on any Labels' values, or on
  // SECTION. If placing SECTION within 'this' provides new
  // constraints on existing labels' values, then it's up to the
  // caller to fiddle with those labels as needed.
  Section& Append(const Section& section);

  // Append the contents of DATA as a series of bytes terminated by
  // a NULL character.
  Section& AppendCString(const string& data) {
    Append(data);
    contents_ += '\0';
    return *this;
  }

  // Append VALUE or LABEL to this section, with the given bit width and
  // endianness. Return a reference to this section.
  //
  // The names of these functions have the form :
  //  is either 'L' (little-endian, least significant byte first),
  //                        'B' (big-endian, most significant byte first), or
  //                        'D' (default, the section's default endianness)
  //  is 8, 16, 32, or 64.
  //
  // Since endianness doesn't matter for a single byte, all the
  // =8 functions are equivalent.
  //
  // These can be used to write both signed and unsigned values, as
  // the compiler will properly sign-extend a signed value before
  // passing it to the function, at which point the function's
  // behavior is the same either way.
  Section& L8(uint8_t value) {
    contents_ += value;
    return *this;
  }
  Section& B8(uint8_t value) {
    contents_ += value;
    return *this;
  }
  Section& D8(uint8_t value) {
    contents_ += value;
    return *this;
  }
  Section &L16(uint16_t), &L32(uint32_t), &L64(uint64_t), &B16(uint16_t),
      &B32(uint32_t), &B64(uint64_t), &D16(uint16_t), &D32(uint32_t),
      &D64(uint64_t);
  Section &L8(const Label&label), &L16(const Label&label),
      &L32(const Label&label), &L64(const Label&label), &B8(const Label&label),
      &B16(const Label&label), &B32(const Label&label), &B64(const Label&label),
      &D8(const Label&label), &D16(const Label&label), &D32(const Label&label),
      &D64(const Label&label);

  // Append VALUE in a signed LEB128 (Little-Endian Base 128) form.
  //
  // The signed LEB128 representation of an integer N is a variable
  // number of bytes:
  //
  // - If N is between -0x40 and 0x3f, then its signed LEB128
  //   representation is a single byte whose value is N.
  //
  // - Otherwise, its signed LEB128 representation is (N & 0x7f) |
  //   0x80, followed by the signed LEB128 representation of N / 128,
  //   rounded towards negative infinity.
  //
  // In other words, we break VALUE into groups of seven bits, put
  // them in little-endian order, and then write them as eight-bit
  // bytes with the high bit on all but the last.
  //
  // Note that VALUE cannot be a Label (we would have to implement
  // relaxation).
  Section& LEB128(long long value);

  // Append VALUE in unsigned LEB128 (Little-Endian Base 128) form.
  //
  // The unsigned LEB128 representation of an integer N is a variable
  // number of bytes:
  //
  // - If N is between 0 and 0x7f, then its unsigned LEB128
  //   representation is a single byte whose value is N.
  //
  // - Otherwise, its unsigned LEB128 representation is (N & 0x7f) |
  //   0x80, followed by the unsigned LEB128 representation of N /
  //   128, rounded towards negative infinity.
  //
  // Note that VALUE cannot be a Label (we would have to implement
  // relaxation).
  Section& ULEB128(uint64_t value);

  // Jump to the next location aligned on an ALIGNMENT-byte boundary,
  // relative to the start of the section. Fill the gap with PAD_BYTE.
  // ALIGNMENT must be a power of two. Return a reference to this
  // section.
  Section& Align(size_t alignment, uint8_t pad_byte = 0);

  // Return the current size of the section.
  size_t Size() const { return contents_.size(); }

  // Return a label representing the start of the section.
  //
  // It is up to the user whether this label represents the section's
  // position in an object file, the section's address in memory, or
  // what have you; some applications may need both, in which case
  // this simple-minded interface won't be enough. This class only
  // provides a single start label, for use with the Here and Mark
  // member functions.
  //
  // Ideally, we'd provide this in a subclass that actually knows more
  // about the application at hand and can provide an appropriate
  // collection of start labels. But then the appending member
  // functions like Append and D32 would return a reference to the
  // base class, not the derived class, and the chaining won't work.
  // Since the only value here is in pretty notation, that's a fatal
  // flaw.
  Label start() const { return start_; }

  // Return a label representing the point at which the next Appended
  // item will appear in the section, relative to start().
  Label Here() const { return start_ + Size(); }

  // Set *LABEL to Here, and return a reference to this section.
  Section& Mark(Label* label) {
    *label = Here();
    return *this;
  }

  // If there are no undefined label references left in this
  // section, set CONTENTS to the contents of this section, as a
  // string, and clear this section. Return true on success, or false
  // if there were still undefined labels.
  bool GetContents(string* contents);

 private:
  // Used internally. A reference to a label's value.
  struct Reference {
    Reference(size_t set_offset, Endianness set_endianness, size_t set_size,
              const Label& set_label)
        : offset(set_offset),
          endianness(set_endianness),
          size(set_size),
          label(set_label) {}

    // The offset of the reference within the section.
    size_t offset;

    // The endianness of the reference.
    Endianness endianness;

    // The size of the reference.
    size_t size;

    // The label to which this is a reference.
    Label label;
  };

  // The default endianness of this section.
  Endianness endianness_;

  // The contents of the section.
  string contents_;

  // References to labels within those contents.
  vector references_;

  // A label referring to the beginning of the section.
  Label start_;
};