[DwarfDebug] Restore code that make comments stay aligned in DwarfDebug::emitDebugLocEntry

Commit 2bddab25db removed a piece of code from
DwarfDebug::emitDebugLocEntry that according to code comments
"Make sure comments stay aligned".

This patch restores that piece of code, together with the addition
of some extra checks in an existing lit test to work as a regression
test. Without this patch we incorrectly get
  .byte   159                             # 0
instead of
  .byte   159                             # DW_OP_stack_value

Differential Revision: https://reviews.llvm.org/D117441
This commit is contained in:
Bjorn Pettersson 2022-01-16 23:12:21 +01:00
parent 2d9198cec9
commit 65fbe38f0a
3 changed files with 27 additions and 5 deletions

View File

@ -33,7 +33,7 @@ class ByteStreamer {
virtual void emitSLEB128(uint64_t DWord, const Twine &Comment = "") = 0;
virtual void emitULEB128(uint64_t DWord, const Twine &Comment = "",
unsigned PadTo = 0) = 0;
virtual void emitDIERef(const DIE &D) = 0;
virtual unsigned emitDIERef(const DIE &D) = 0;
};
class APByteStreamer final : public ByteStreamer {
@ -55,11 +55,14 @@ public:
AP.OutStreamer->AddComment(Comment);
AP.emitULEB128(DWord, nullptr, PadTo);
}
void emitDIERef(const DIE &D) override {
unsigned emitDIERef(const DIE &D) override {
uint64_t Offset = D.getOffset();
static constexpr unsigned ULEB128PadSize = 4;
assert(Offset < (1ULL << (ULEB128PadSize * 7)) && "Offset wont fit");
emitULEB128(Offset, "", ULEB128PadSize);
// Return how many comments to skip in DwarfDebug::emitDebugLocEntry to keep
// comments aligned with debug loc entries.
return ULEB128PadSize;
}
};
@ -78,7 +81,10 @@ class HashingByteStreamer final : public ByteStreamer {
unsigned PadTo) override {
Hash.addULEB128(DWord);
}
void emitDIERef(const DIE &D) override { Hash.hashRawTypeReference(D); }
unsigned emitDIERef(const DIE &D) override {
Hash.hashRawTypeReference(D);
return 0; // Only used together with the APByteStreamer.
}
};
class BufferByteStreamer final : public ByteStreamer {
@ -125,11 +131,12 @@ public:
Comments.push_back("");
}
}
void emitDIERef(const DIE &D) override {
unsigned emitDIERef(const DIE &D) override {
uint64_t Offset = D.getOffset();
static constexpr unsigned ULEB128PadSize = 4;
assert(Offset < (1ULL << (ULEB128PadSize * 7)) && "Offset wont fit");
emitULEB128(Offset, "", ULEB128PadSize);
return 0; // Only used together with the APByteStreamer.
}
};

View File

@ -2539,7 +2539,12 @@ void DwarfDebug::emitDebugLocEntry(ByteStreamer &Streamer,
if (Op.getDescription().Op[I] == Encoding::SizeNA)
continue;
if (Op.getDescription().Op[I] == Encoding::BaseTypeRef) {
Streamer.emitDIERef(*CU->ExprRefedBaseTypes[Op.getRawOperand(I)].Die);
unsigned Length =
Streamer.emitDIERef(*CU->ExprRefedBaseTypes[Op.getRawOperand(I)].Die);
// Make sure comments stay aligned.
for (unsigned J = 0; J < Length; ++J)
if (Comment != End)
Comment++;
} else {
for (uint64_t J = Offset; J < Op.getOperandEndOffset(I); ++J)
Streamer.emitInt8(Data.getData()[J], Comment != End ? *(Comment++) : "");

View File

@ -19,6 +19,16 @@
; extra bytes would be emitted into the output assembly in no
; particular/intentional section - so let's check they don't show up at all:
; ASM-NOT: .asciz "\200\200\200"
; ASM: .byte 10 # Loc expr size
; ASM-NEXT: .byte 17 # DW_OP_consts
; ASM-NEXT: .byte 7 # 7
; ASM-NEXT: .byte 48 # DW_OP_lit0
; ASM-NEXT: .byte 34 # DW_OP_plus
; ASM-NEXT: .byte 168 # DW_OP_convert
; ASM-NEXT: .asciz "\232\200\200" #
; ASM-NEXT: .byte 159 # DW_OP_stack_value
; ASM-NEXT: .byte 0 # DW_LLE_end_of_list
; ASM-NOT: .asciz "\200\200\200"
; CHECK: 0x{{0*}}[[TYPE:.*]]: DW_TAG_base_type
; CHECK-NEXT: DW_AT_name ("DW_ATE_unsigned_32")