[PDB] Resolve a symbol context block info correctly

Summary:
This patch allows to resolve a symbol context block info even if a function
info was not requested. Also it adds the correct resolving of nested blocks
(the previous implementation used function blocks instead of them).

Reviewers: zturner, asmith, labath

Reviewed By: asmith

Subscribers: lldb-commits, stella.stamenova

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D51104

llvm-svn: 340901
diff --git a/lldb/tools/lldb-test/lldb-test.cpp b/lldb/tools/lldb-test/lldb-test.cpp
index c9225f6..9055b4c 100644
--- a/lldb/tools/lldb-test/lldb-test.cpp
+++ b/lldb/tools/lldb-test/lldb-test.cpp
@@ -103,6 +103,7 @@
 enum class FindType {
   None,
   Function,
+  Block,
   Namespace,
   Type,
   Variable,
@@ -112,6 +113,7 @@
     cl::values(
         clEnumValN(FindType::None, "none", "No search, just dump the module."),
         clEnumValN(FindType::Function, "function", "Find functions."),
+        clEnumValN(FindType::Block, "block", "Find blocks."),
         clEnumValN(FindType::Namespace, "namespace", "Find namespaces."),
         clEnumValN(FindType::Type, "type", "Find types."),
         clEnumValN(FindType::Variable, "variable", "Find global variables.")),
@@ -158,6 +160,7 @@
 static Expected<CompilerDeclContext> getDeclContext(SymbolVendor &Vendor);
 
 static Error findFunctions(lldb_private::Module &Module);
+static Error findBlocks(lldb_private::Module &Module);
 static Error findNamespaces(lldb_private::Module &Module);
 static Error findTypes(lldb_private::Module &Module);
 static Error findVariables(lldb_private::Module &Module);
@@ -383,6 +386,42 @@
   return Error::success();
 }
 
+Error opts::symbols::findBlocks(lldb_private::Module &Module) {
+  assert(!Regex);
+  assert(!File.empty());
+  assert(Line != 0);
+
+  SymbolContextList List;
+
+  FileSpec src_file(File, false);
+  size_t cu_count = Module.GetNumCompileUnits();
+  for (size_t i = 0; i < cu_count; i++) {
+    lldb::CompUnitSP cu_sp = Module.GetCompileUnitAtIndex(i);
+    if (!cu_sp)
+      continue;
+
+    LineEntry le;
+    cu_sp->FindLineEntry(0, Line, &src_file, false, &le);
+    if (!le.IsValid())
+      continue;
+
+    auto addr = le.GetSameLineContiguousAddressRange().GetBaseAddress();
+    if (!addr.IsValid())
+      continue;
+
+    SymbolContext sc;
+    uint32_t resolved = addr.CalculateSymbolContext(&sc, eSymbolContextBlock);
+    if (resolved & eSymbolContextBlock)
+      List.Append(sc);
+  }
+
+  outs() << formatv("Found {0} blocks:\n", List.GetSize());
+  StreamString Stream;
+  List.Dump(&Stream, nullptr);
+  outs() << Stream.GetData() << "\n";
+  return Error::success();
+}
+
 Error opts::symbols::findNamespaces(lldb_private::Module &Module) {
   SymbolVendor &Vendor = *Module.GetSymbolVendor();
   Expected<CompilerDeclContext> ContextOr = getDeclContext(Vendor);
@@ -566,6 +605,15 @@
                                "when searching a function.");
     return findFunctions;
 
+  case FindType::Block:
+    if (File.empty() || Line == 0)
+      return make_string_error("Both file name and line number must be "
+                               "specified when searching a block.");
+    if (Regex || getFunctionNameFlags() != 0)
+      return make_string_error("Cannot use regular expression or "
+                               "function-flags for searching a block.");
+    return findBlocks;
+
   case FindType::Namespace:
     if (Regex || !File.empty() || Line != 0)
       return make_string_error("Cannot search for namespaces using regular "