The only tests we have for the DWARF parser are the tests that use llvm-dwarfdump and expect output from textual dumps.
More DWARF parser modification are coming in the next few weeks and I wanted to add tests that can verify that we can encode and decode all form types, as well as test some other basic DWARF APIs where we ask DIE objects for their children and siblings.
DwarfGenerator.cpp was added in the lib/CodeGen directory. This file contains the code necessary to easily create DWARF for tests:
DwarfGen DG; Triple Triple("x86_64--"); // Initialize the DWARF generator bool success = DG.init(Triple, Version); // Make a compile unit DwarfGenCU &CU = DG.addCompileUnit(); // Get the compile unit DIE DwarfGenDIE CUDie = CU.getUnitDIE(); CUDie.addAttribute(DW_AT_name, DW_FORM_strp, "/tmp/main.c"); CUDie.addAttribute(DW_AT_language, DW_FORM_data2, DW_LANG_C); DwarfGenDIE SubprogramDie = CUDie.addChild(DW_TAG_subprogram); SubprogramDie.addAttribute(DW_AT_name, DW_FORM_strp, "main"); SubprogramDie.addAttribute(DW_AT_low_pc, DW_FORM_addr, 0x1000U); SubprogramDie.addAttribute(DW_AT_high_pc, DW_FORM_addr, 0x2000U); DwarfGenDIE IntDie = CUDie.addChild(DW_TAG_base_type); IntDie.addAttribute(DW_AT_name, DW_FORM_strp, "int"); IntDie.addAttribute(DW_AT_encoding, DW_FORM_data1, DW_ATE_signed); IntDie.addAttribute(DW_AT_byte_size, DW_FORM_data1, 4); DwarfGenDIE ArgcDie = SubprogramDie.addChild(DW_TAG_formal_parameter); ArgcDie.addAttribute(DW_AT_name, DW_FORM_strp, "argc"); ArgcDie.addAttribute(DW_AT_type, DW_FORM_ref4, IntDie); StringRef FileBytes = DG.generate(); MemoryBufferRef FileBuffer(FileBytes, "dwarf"); auto Obj = object::ObjectFile::createObjectFile(FileBuffer); if (Obj) { DWARFContextInMemory DwarfContext(*Obj.get()); uint32_t NumCUs = DwarfContext.getNumCompileUnits(); ... }
This code is backed by the AsmPrinter code that emits DWARF for the actual compiler. There are places in the AsmPrinter where it was expecting to always have an instance of llvm::DwarfDebug in order to emit certain things, but both the DwarfGen class and the the DwarfLinker both use AsmPrinters to generate DWARF that doesn't used the DwarfDebug class. Code was modified in the AsmPrinter to allow users to specify the DWARF version manually so that DW_FORM_ref_addr and a few other attribute can now be emitted correctly.
This doesn't need to be here. I will remove it in next patch.