[Lldb-commits] [lldb] r370994 - [Disassembler] Simplify a few methods (NFC)

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Wed Sep 4 15:38:20 PDT 2019


Author: jdevlieghere
Date: Wed Sep  4 15:38:20 2019
New Revision: 370994

URL: http://llvm.org/viewvc/llvm-project?rev=370994&view=rev
Log:
[Disassembler] Simplify a few methods (NFC)

Use early returns to highlight preconditions and make the code easier to
follow.

Modified:
    lldb/trunk/include/lldb/Symbol/SymbolContext.h
    lldb/trunk/source/Core/Disassembler.cpp
    lldb/trunk/source/Symbol/SymbolContext.cpp

Modified: lldb/trunk/include/lldb/Symbol/SymbolContext.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Symbol/SymbolContext.h?rev=370994&r1=370993&r2=370994&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Symbol/SymbolContext.h (original)
+++ lldb/trunk/include/lldb/Symbol/SymbolContext.h Wed Sep  4 15:38:20 2019
@@ -470,6 +470,8 @@ public:
   ///     Returns the number of symbol context objects in the list.
   uint32_t GetSize() const;
 
+  bool IsEmpty() const;
+
   uint32_t NumLineEntriesWithLine(uint32_t line) const;
 
   void GetDescription(Stream *s, lldb::DescriptionLevel level,

Modified: lldb/trunk/source/Core/Disassembler.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/Disassembler.cpp?rev=370994&r1=370993&r2=370994&view=diff
==============================================================================
--- lldb/trunk/source/Core/Disassembler.cpp (original)
+++ lldb/trunk/source/Core/Disassembler.cpp Wed Sep  4 15:38:20 2019
@@ -158,52 +158,59 @@ size_t Disassembler::Disassemble(Debugge
   return success_count;
 }
 
-bool Disassembler::Disassemble(Debugger &debugger, const ArchSpec &arch,
-                               const char *plugin_name, const char *flavor,
-                               const ExecutionContext &exe_ctx,
-                               ConstString name, Module *module,
-                               uint32_t num_instructions,
-                               bool mixed_source_and_assembly,
-                               uint32_t num_mixed_context_lines,
-                               uint32_t options, Stream &strm) {
+bool Disassembler::Disassemble(
+    Debugger &debugger, const ArchSpec &arch, const char *plugin_name,
+    const char *flavor, const ExecutionContext &exe_ctx, ConstString name,
+    Module *module, uint32_t num_instructions, bool mixed_source_and_assembly,
+    uint32_t num_mixed_context_lines, uint32_t options, Stream &strm) {
+  // If no name is given there's nothing to disassemble.
+  if (!name)
+    return false;
+
+  const bool include_symbols = true;
+  const bool include_inlines = true;
+
+  // Find functions matching the given name.
   SymbolContextList sc_list;
-  if (name) {
-    const bool include_symbols = true;
-    const bool include_inlines = true;
-    if (module) {
-      module->FindFunctions(name, nullptr, eFunctionNameTypeAuto,
-                            include_symbols, include_inlines, true, sc_list);
-    } else if (exe_ctx.GetTargetPtr()) {
-      exe_ctx.GetTargetPtr()->GetImages().FindFunctions(
-          name, eFunctionNameTypeAuto, include_symbols, include_inlines, false,
-          sc_list);
-    }
+  if (module) {
+    module->FindFunctions(name, nullptr, eFunctionNameTypeAuto, include_symbols,
+                          include_inlines, true, sc_list);
+  } else if (exe_ctx.GetTargetPtr()) {
+    exe_ctx.GetTargetPtr()->GetImages().FindFunctions(
+        name, eFunctionNameTypeAuto, include_symbols, include_inlines, false,
+        sc_list);
   }
 
-  if (sc_list.GetSize()) {
-    return Disassemble(debugger, arch, plugin_name, flavor, exe_ctx, sc_list,
-                       num_instructions, mixed_source_and_assembly,
-                       num_mixed_context_lines, options, strm);
-  }
-  return false;
+  // If no functions were found there's nothing to disassemble.
+  if (sc_list.IsEmpty())
+    return false;
+
+  return Disassemble(debugger, arch, plugin_name, flavor, exe_ctx, sc_list,
+                     num_instructions, mixed_source_and_assembly,
+                     num_mixed_context_lines, options, strm);
 }
 
 lldb::DisassemblerSP Disassembler::DisassembleRange(
     const ArchSpec &arch, const char *plugin_name, const char *flavor,
     const ExecutionContext &exe_ctx, const AddressRange &range,
     bool prefer_file_cache) {
-  lldb::DisassemblerSP disasm_sp;
-  if (range.GetByteSize() > 0 && range.GetBaseAddress().IsValid()) {
-    disasm_sp = Disassembler::FindPluginForTarget(exe_ctx.GetTargetSP(), arch,
-                                                  flavor, plugin_name);
-
-    if (disasm_sp) {
-      size_t bytes_disassembled = disasm_sp->ParseInstructions(
-          &exe_ctx, range, nullptr, prefer_file_cache);
-      if (bytes_disassembled == 0)
-        disasm_sp.reset();
-    }
-  }
+  if (range.GetByteSize() <= 0)
+    return {};
+
+  if (!range.GetBaseAddress().IsValid())
+    return {};
+
+  lldb::DisassemblerSP disasm_sp = Disassembler::FindPluginForTarget(
+      exe_ctx.GetTargetSP(), arch, flavor, plugin_name);
+
+  if (!disasm_sp)
+    return {};
+
+  const size_t bytes_disassembled =
+      disasm_sp->ParseInstructions(&exe_ctx, range, nullptr, prefer_file_cache);
+  if (bytes_disassembled == 0)
+    return {};
+
   return disasm_sp;
 }
 
@@ -212,20 +219,20 @@ Disassembler::DisassembleBytes(const Arc
                                const char *flavor, const Address &start,
                                const void *src, size_t src_len,
                                uint32_t num_instructions, bool data_from_file) {
-  lldb::DisassemblerSP disasm_sp;
+  if (!src)
+    return {};
 
-  if (src) {
-    disasm_sp = Disassembler::FindPlugin(arch, flavor, plugin_name);
+  lldb::DisassemblerSP disasm_sp =
+      Disassembler::FindPlugin(arch, flavor, plugin_name);
 
-    if (disasm_sp) {
-      DataExtractor data(src, src_len, arch.GetByteOrder(),
-                         arch.GetAddressByteSize());
-
-      (void)disasm_sp->DecodeInstructions(start, data, 0, num_instructions,
-                                          false, data_from_file);
-    }
-  }
+  if (!disasm_sp)
+    return {};
+
+  DataExtractor data(src, src_len, arch.GetByteOrder(),
+                     arch.GetAddressByteSize());
 
+  (void)disasm_sp->DecodeInstructions(start, data, 0, num_instructions, false,
+                                      data_from_file);
   return disasm_sp;
 }
 

Modified: lldb/trunk/source/Symbol/SymbolContext.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Symbol/SymbolContext.cpp?rev=370994&r1=370993&r2=370994&view=diff
==============================================================================
--- lldb/trunk/source/Symbol/SymbolContext.cpp (original)
+++ lldb/trunk/source/Symbol/SymbolContext.cpp Wed Sep  4 15:38:20 2019
@@ -759,9 +759,8 @@ bool SymbolContext::GetAddressRangeFromH
   }
 
   Block *func_block = GetFunctionBlock();
-  if (func_block &&
-      func_block->GetRangeIndexContainingAddress(
-          end_entry.range.GetBaseAddress()) == UINT32_MAX) {
+  if (func_block && func_block->GetRangeIndexContainingAddress(
+                        end_entry.range.GetBaseAddress()) == UINT32_MAX) {
     error.SetErrorStringWithFormat(
         "end line number %d is not contained within the current function.",
         end_line);
@@ -774,8 +773,8 @@ bool SymbolContext::GetAddressRangeFromH
   return true;
 }
 
-const Symbol *
-SymbolContext::FindBestGlobalDataSymbol(ConstString name, Status &error) {
+const Symbol *SymbolContext::FindBestGlobalDataSymbol(ConstString name,
+                                                      Status &error) {
   error.Clear();
 
   if (!target_sp) {
@@ -785,8 +784,9 @@ SymbolContext::FindBestGlobalDataSymbol(
   Target &target = *target_sp;
   Module *module = module_sp.get();
 
-  auto ProcessMatches = [this, &name, &target, module]
-  (SymbolContextList &sc_list, Status &error) -> const Symbol* {
+  auto ProcessMatches = [this, &name, &target,
+                         module](SymbolContextList &sc_list,
+                                 Status &error) -> const Symbol * {
     llvm::SmallVector<const Symbol *, 1> external_symbols;
     llvm::SmallVector<const Symbol *, 1> internal_symbols;
     const uint32_t matches = sc_list.GetSize();
@@ -799,77 +799,77 @@ SymbolContext::FindBestGlobalDataSymbol(
 
         if (sym_address.IsValid()) {
           switch (symbol->GetType()) {
-            case eSymbolTypeData:
-            case eSymbolTypeRuntime:
-            case eSymbolTypeAbsolute:
-            case eSymbolTypeObjCClass:
-            case eSymbolTypeObjCMetaClass:
-            case eSymbolTypeObjCIVar:
-              if (symbol->GetDemangledNameIsSynthesized()) {
-                // If the demangled name was synthesized, then don't use it for
-                // expressions. Only let the symbol match if the mangled named
-                // matches for these symbols.
-                if (symbol->GetMangled().GetMangledName() != name)
-                  break;
-              }
-              if (symbol->IsExternal()) {
-                external_symbols.push_back(symbol);
-              } else {
-                internal_symbols.push_back(symbol);
-              }
-              break;
-            case eSymbolTypeReExported: {
-              ConstString reexport_name = symbol->GetReExportedSymbolName();
-              if (reexport_name) {
-                ModuleSP reexport_module_sp;
-                ModuleSpec reexport_module_spec;
-                reexport_module_spec.GetPlatformFileSpec() =
-                symbol->GetReExportedSymbolSharedLibrary();
-                if (reexport_module_spec.GetPlatformFileSpec()) {
-                  reexport_module_sp =
-                  target.GetImages().FindFirstModule(reexport_module_spec);
-                  if (!reexport_module_sp) {
-                    reexport_module_spec.GetPlatformFileSpec()
-                    .GetDirectory()
-                    .Clear();
-                    reexport_module_sp =
+          case eSymbolTypeData:
+          case eSymbolTypeRuntime:
+          case eSymbolTypeAbsolute:
+          case eSymbolTypeObjCClass:
+          case eSymbolTypeObjCMetaClass:
+          case eSymbolTypeObjCIVar:
+            if (symbol->GetDemangledNameIsSynthesized()) {
+              // If the demangled name was synthesized, then don't use it for
+              // expressions. Only let the symbol match if the mangled named
+              // matches for these symbols.
+              if (symbol->GetMangled().GetMangledName() != name)
+                break;
+            }
+            if (symbol->IsExternal()) {
+              external_symbols.push_back(symbol);
+            } else {
+              internal_symbols.push_back(symbol);
+            }
+            break;
+          case eSymbolTypeReExported: {
+            ConstString reexport_name = symbol->GetReExportedSymbolName();
+            if (reexport_name) {
+              ModuleSP reexport_module_sp;
+              ModuleSpec reexport_module_spec;
+              reexport_module_spec.GetPlatformFileSpec() =
+                  symbol->GetReExportedSymbolSharedLibrary();
+              if (reexport_module_spec.GetPlatformFileSpec()) {
+                reexport_module_sp =
                     target.GetImages().FindFirstModule(reexport_module_spec);
-                  }
+                if (!reexport_module_sp) {
+                  reexport_module_spec.GetPlatformFileSpec()
+                      .GetDirectory()
+                      .Clear();
+                  reexport_module_sp =
+                      target.GetImages().FindFirstModule(reexport_module_spec);
                 }
-                // Don't allow us to try and resolve a re-exported symbol if it
-                // is the same as the current symbol
-                if (name == symbol->GetReExportedSymbolName() &&
-                    module == reexport_module_sp.get())
-                  return nullptr;
-
-                return FindBestGlobalDataSymbol(
-                    symbol->GetReExportedSymbolName(), error);
               }
-            } break;
+              // Don't allow us to try and resolve a re-exported symbol if it
+              // is the same as the current symbol
+              if (name == symbol->GetReExportedSymbolName() &&
+                  module == reexport_module_sp.get())
+                return nullptr;
+
+              return FindBestGlobalDataSymbol(symbol->GetReExportedSymbolName(),
+                                              error);
+            }
+          } break;
 
-            case eSymbolTypeCode: // We already lookup functions elsewhere
-            case eSymbolTypeVariable:
-            case eSymbolTypeLocal:
-            case eSymbolTypeParam:
-            case eSymbolTypeTrampoline:
-            case eSymbolTypeInvalid:
-            case eSymbolTypeException:
-            case eSymbolTypeSourceFile:
-            case eSymbolTypeHeaderFile:
-            case eSymbolTypeObjectFile:
-            case eSymbolTypeCommonBlock:
-            case eSymbolTypeBlock:
-            case eSymbolTypeVariableType:
-            case eSymbolTypeLineEntry:
-            case eSymbolTypeLineHeader:
-            case eSymbolTypeScopeBegin:
-            case eSymbolTypeScopeEnd:
-            case eSymbolTypeAdditional:
-            case eSymbolTypeCompiler:
-            case eSymbolTypeInstrumentation:
-            case eSymbolTypeUndefined:
-            case eSymbolTypeResolver:
-              break;
+          case eSymbolTypeCode: // We already lookup functions elsewhere
+          case eSymbolTypeVariable:
+          case eSymbolTypeLocal:
+          case eSymbolTypeParam:
+          case eSymbolTypeTrampoline:
+          case eSymbolTypeInvalid:
+          case eSymbolTypeException:
+          case eSymbolTypeSourceFile:
+          case eSymbolTypeHeaderFile:
+          case eSymbolTypeObjectFile:
+          case eSymbolTypeCommonBlock:
+          case eSymbolTypeBlock:
+          case eSymbolTypeVariableType:
+          case eSymbolTypeLineEntry:
+          case eSymbolTypeLineHeader:
+          case eSymbolTypeScopeBegin:
+          case eSymbolTypeScopeEnd:
+          case eSymbolTypeAdditional:
+          case eSymbolTypeCompiler:
+          case eSymbolTypeInstrumentation:
+          case eSymbolTypeUndefined:
+          case eSymbolTypeResolver:
+            break;
           }
         }
       }
@@ -930,7 +930,6 @@ SymbolContext::FindBestGlobalDataSymbol(
   return nullptr; // no error; we just didn't find anything
 }
 
-
 //
 //  SymbolContextSpecifier
 //
@@ -1293,6 +1292,8 @@ bool SymbolContextList::RemoveContextAtI
 
 uint32_t SymbolContextList::GetSize() const { return m_symbol_contexts.size(); }
 
+bool SymbolContextList::IsEmpty() const { return m_symbol_contexts.empty(); }
+
 uint32_t SymbolContextList::NumLineEntriesWithLine(uint32_t line) const {
   uint32_t match_count = 0;
   const size_t size = m_symbol_contexts.size();




More information about the lldb-commits mailing list