Index: source/Commands/CommandObjectTarget.cpp =================================================================== --- source/Commands/CommandObjectTarget.cpp +++ source/Commands/CommandObjectTarget.cpp @@ -1908,6 +1908,7 @@ const uint32_t max_num_matches = UINT32_MAX; size_t num_matches = 1; bool name_is_fully_qualified = false; + uint32_t best_match_idx = 0; ConstString name(name_cstr); num_matches = sym_ctx.module_sp->FindTypes(sym_ctx, name, name_is_fully_qualified, max_num_matches, type_list); @@ -1918,8 +1919,28 @@ strm.PutCString("Best match found in "); DumpFullpath (strm, &sym_ctx.module_sp->GetFileSpec(), 0); strm.PutCString(":\n"); - - TypeSP type_sp (type_list.GetTypeAtIndex(0)); + Block * curr_block = sym_ctx.block; + bool found = false; + while (curr_block != nullptr) + { + for (uint32_t i = 0; i < num_matches; i++) + { + TypeSP type_sp_temp (type_list.GetTypeAtIndex(i)); + if (curr_block == type_sp_temp->GetSymbolContextScope()->CalculateSymbolContextBlock()) + { + best_match_idx = i; + found = true; + break; + } + } + if (found) + break; + else + { + curr_block = curr_block->GetParent(); + } + } + TypeSP type_sp (type_list.GetTypeAtIndex(best_match_idx)); if (type_sp) { // Resolve the clang type so that any forward references Index: source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp =================================================================== --- source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp +++ source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp @@ -3047,6 +3047,17 @@ CompileUnit* lldb_cu = GetCompUnitForDWARFCompUnit(die.GetCU()); assert (lldb_cu); SymbolContext sc(lldb_cu); + const DWARFDebugInfoEntry* parent_die = die.GetParent().GetDIE(); + while (parent_die != nullptr) + { + if (parent_die->Tag() == DW_TAG_subprogram) + break; + parent_die = parent_die->GetParent(); + } + SymbolContext sc_backup = sc; + if (parent_die != nullptr && !GetFunction(DWARFDIE(die.GetCU(),parent_die), sc)) + sc = sc_backup; + type_sp = ParseType(sc, die, NULL); } else if (type_ptr != DIE_IS_BEING_PARSED) Index: test/lang/c/typedef/Makefile =================================================================== --- /dev/null +++ test/lang/c/typedef/Makefile @@ -0,0 +1,5 @@ +LEVEL = ../../../make + +C_SOURCES := main.c + +include $(LEVEL)/Makefile.rules Index: test/lang/c/typedef/Testtypedef.py =================================================================== --- /dev/null +++ test/lang/c/typedef/Testtypedef.py @@ -0,0 +1,51 @@ +"""Look up type information for typedefs of same name at different lexical scope and check for correct display.""" + +import os, time +import unittest2 +import lldb +from lldbtest import * +import lldbutil + +class TypedefTestCase(TestBase): + + mydir = TestBase.compute_mydir(__file__) + + @skipUnlessDarwin + @dsym_test + @expectedFailureClang("llvm.org/pr19238") + def test_with_dsym(self): + """Test 'image lookup -t a' and check for correct display at different scopes.""" + self.buildDsym() + self.image_lookup_for_multiple_typedefs() + + @dwarf_test + @expectedFailureClang("llvm.org/pr19238") + def test_with_dwarf(self): + """Test 'image lookup -t a' and check for correct display at different scopes.""" + self.buildDwarf() + self.image_lookup_for_multiple_typedefs() + + def image_lookup_for_multiple_typedefs(self): + """Test 'image lookup -t a' at different scopes and check for correct display.""" + exe = os.path.join(os.getcwd(), "a.out") + self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET) + typearray = ("float", "float", "char", "float", "int", "double", "float", "float") + arraylen = len(typearray)+1 + for i in range(1,arraylen): + loc_line = line_number('main.c', '// Set break point ' + str(i) + '.') + lldbutil.run_break_set_by_file_and_line (self, "main.c",loc_line, num_expected_locations=1, loc_exact=True) + + self.runCmd("run", RUN_SUCCEEDED) + + for t in typearray: + self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT, + substrs = ['stopped', 'stop reason = breakpoint']) + self.expect("image lookup -t a", DATA_TYPES_DISPLAYED_CORRECTLY, + substrs = ['name = "' + t + '"']) + self.runCmd("continue") + +if __name__ == '__main__': + import atexit + lldb.SBDebugger.Initialize() + atexit.register(lambda: lldb.SBDebugger.Terminate()) + unittest2.main() Index: test/lang/c/typedef/main.c =================================================================== --- /dev/null +++ test/lang/c/typedef/main.c @@ -0,0 +1,40 @@ +//===-- main.c --------------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +int main (int argc, char const *argv[]) +{ + typedef float a; + int i = 0; // Set break point 1. + i++; + a floatvariable = 2.7; // Set break point 2. + { + typedef char a; + i++; + a charvariable = 'a'; // Set break point 3. + } + { + int c = 0; + c++; // Set break point 4. + for(i = 0 ; i < 1 ; i++) + { + typedef int a; + a b; + b = 7; // Set break point 5. + } + for(i = 0 ; i < 1 ; i++) + { + typedef double a; + a b; + b = 3.14; // Set break point 6. + } + c = 1; // Set break point 7. + } + floatvariable = 2.5; + floatvariable = 2.8; // Set break point 8. + return 0; +}