@no_wasm2js('TODO: source maps in wasm2js')
@no_fastcomp('DWARF is only supported in upstream')
def test_dwarf(self):
    self.emcc_args.append('-g')
    create_test_file('src.cpp', '\n      #include <emscripten.h>\n      EM_JS(int, out_to_js, (int x), {})\n      void foo() {\n        out_to_js(0); // line 5\n        out_to_js(1); // line 6\n        out_to_js(2); // line 7\n        // A silly possible recursion to avoid binaryen doing any inlining.\n        if (out_to_js(3)) foo();\n      }\n      int main() {\n        foo();\n      }\n    ')
    js_filename = 'a.out.js'
    wasm_filename = 'a.out.wasm'
    building.emcc('src.cpp', (self.serialize_settings() + self.emcc_args), js_filename)
    LLVM_DWARFDUMP = os.path.join(LLVM_ROOT, 'llvm-dwarfdump')
    out = run_process([LLVM_DWARFDUMP, wasm_filename, '-all'], stdout=PIPE).stdout
    sections = {}
    curr_section_name = ''
    curr_section_body = ''

    def add_section():
        if curr_section_name:
            sections[curr_section_name] = curr_section_body
    for line in out.splitlines():
        if (' contents:' in line):
            add_section()
            curr_section_name = line.split(' ')[0]
            curr_section_body = ''
        elif curr_section_name:
            curr_section_body += (line + '\n')
    add_section()
    self.assertIn('.debug_abbrev', sections)
    self.assertIn('.debug_info', sections)
    self.assertIn('.debug_line', sections)
    self.assertIn('.debug_str', sections)
    self.assertIn('.debug_ranges', sections)
    self.assertIn('"src.cpp"', sections['.debug_info'])
    src_to_addr = {}
    for line in sections['.debug_line'].splitlines():
        if line.startswith('0x'):
            while ('  ' in line):
                line = line.replace('  ', ' ')
            (addr, line, col) = line.split(' ')[:3]
            key = (int(line), int(col))
            src_to_addr.setdefault(key, []).append(addr)
    self.assertIn((5, 9), src_to_addr)
    self.assertIn((6, 9), src_to_addr)
    self.assertIn((7, 9), src_to_addr)

    def get_dwarf_addr(line, col):
        addrs = src_to_addr[(line, col)]
        assert (len(addrs) == 1), 'we assume the simple calls have one address'
        return int(addrs[0], 0)
    self.assertLess(get_dwarf_addr(5, 9), get_dwarf_addr(6, 9))
    self.assertLess(get_dwarf_addr(6, 9), get_dwarf_addr(7, 9))
    wat = run_process([os.path.join(building.get_binaryen_bin(), 'wasm-opt'), wasm_filename, '-g', '--print'], stdout=PIPE).stdout

    def get_wat_addr(call_index):
        call_loc = (- 1)
        for i in range((call_index + 1)):
            call_loc = wat.find('call $out_to_js', (call_loc + 1))
            assert (call_loc > 0)
        start_addr_loc = wat.find('0x', call_loc)
        assert (start_addr_loc > 0)
        start_addr_loc_end = wat.find('\n', start_addr_loc)
        start_addr = int(wat[start_addr_loc:start_addr_loc_end], 0)
        end_addr_loc = wat.rfind('drop', 0, call_loc)
        assert (end_addr_loc > 0)
        end_addr_loc = wat.rfind('0x', 0, end_addr_loc)
        assert (end_addr_loc > 0)
        end_addr_loc_end = wat.find('\n', end_addr_loc)
        assert (end_addr_loc_end > 0)
        end_addr = int(wat[end_addr_loc:end_addr_loc_end], 0)
        return (start_addr, end_addr)
    for i in range(3):
        dwarf_addr = get_dwarf_addr((5 + i), 9)
        (start_wat_addr, end_wat_addr) = get_wat_addr(i)
        self.assertLessEqual(start_wat_addr, dwarf_addr)
        self.assertLessEqual(dwarf_addr, end_wat_addr)
@no_wasm2js('TODO: source maps in wasm2js')
@no_fastcomp('DWARF is only supported in upstream')
def test_dwarf(self):
    self.emcc_args.append('-g')
    create_test_file('src.cpp', '\n      #include <emscripten.h>\n      EM_JS(int, out_to_js, (int x), {})\n      void foo() {\n        out_to_js(0); // line 5\n        out_to_js(1); // line 6\n        out_to_js(2); // line 7\n        // A silly possible recursion to avoid binaryen doing any inlining.\n        if (out_to_js(3)) foo();\n      }\n      int main() {\n        foo();\n      }\n    ')
    js_filename = 'a.out.js'
    wasm_filename = 'a.out.wasm'
    building.emcc('src.cpp', (self.serialize_settings() + self.emcc_args), js_filename)
    LLVM_DWARFDUMP = os.path.join(LLVM_ROOT, 'llvm-dwarfdump')
    out = run_process([LLVM_DWARFDUMP, wasm_filename, '-all'], stdout=PIPE).stdout
    sections = {}
    curr_section_name = 
    curr_section_body = 

    def add_section():
        if curr_section_name:
            sections[curr_section_name] = curr_section_body
    for line in out.splitlines():
        if (' contents:' in line):
            add_section()
            curr_section_name = line.split(' ')[0]
            curr_section_body = 
        elif curr_section_name:
            curr_section_body += (line + '\n')
    add_section()
    self.assertIn('.debug_abbrev', sections)
    self.assertIn('.debug_info', sections)
    self.assertIn('.debug_line', sections)
    self.assertIn('.debug_str', sections)
    self.assertIn('.debug_ranges', sections)
    self.assertIn('"src.cpp"', sections['.debug_info'])
    src_to_addr = {}
    for line in sections['.debug_line'].splitlines():
        if line.startswith('0x'):
            while ('  ' in line):
                line = line.replace('  ', ' ')
            (addr, line, col) = line.split(' ')[:3]
            key = (int(line), int(col))
            src_to_addr.setdefault(key, []).append(addr)
    self.assertIn((5, 9), src_to_addr)
    self.assertIn((6, 9), src_to_addr)
    self.assertIn((7, 9), src_to_addr)

    def get_dwarf_addr(line, col):
        addrs = src_to_addr[(line, col)]
        assert (len(addrs) == 1), 'we assume the simple calls have one address'
        return int(addrs[0], 0)
    self.assertLess(get_dwarf_addr(5, 9), get_dwarf_addr(6, 9))
    self.assertLess(get_dwarf_addr(6, 9), get_dwarf_addr(7, 9))
    wat = run_process([os.path.join(building.get_binaryen_bin(), 'wasm-opt'), wasm_filename, '-g', '--print'], stdout=PIPE).stdout

    def get_wat_addr(call_index):
        call_loc = (- 1)
        for i in range((call_index + 1)):
            call_loc = wat.find('call $out_to_js', (call_loc + 1))
            assert (call_loc > 0)
        start_addr_loc = wat.find('0x', call_loc)
        assert (start_addr_loc > 0)
        start_addr_loc_end = wat.find('\n', start_addr_loc)
        start_addr = int(wat[start_addr_loc:start_addr_loc_end], 0)
        end_addr_loc = wat.rfind('drop', 0, call_loc)
        assert (end_addr_loc > 0)
        end_addr_loc = wat.rfind('0x', 0, end_addr_loc)
        assert (end_addr_loc > 0)
        end_addr_loc_end = wat.find('\n', end_addr_loc)
        assert (end_addr_loc_end > 0)
        end_addr = int(wat[end_addr_loc:end_addr_loc_end], 0)
        return (start_addr, end_addr)
    for i in range(3):
        dwarf_addr = get_dwarf_addr((5 + i), 9)
        (start_wat_addr, end_wat_addr) = get_wat_addr(i)
        self.assertLessEqual(start_wat_addr, dwarf_addr)
        self.assertLessEqual(dwarf_addr, end_wat_addr)