Skip to content

Commit 4be6706

Browse files
committed
[Disassembler] Simplify a few methods (NFC)
Use early returns to highlight preconditions and make the code easier to follow. llvm-svn: 370994
1 parent 71c37a8 commit 4be6706

File tree

3 files changed

+132
-122
lines changed

3 files changed

+132
-122
lines changed

lldb/include/lldb/Symbol/SymbolContext.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,8 @@ class SymbolContextList {
470470
/// Returns the number of symbol context objects in the list.
471471
uint32_t GetSize() const;
472472

473+
bool IsEmpty() const;
474+
473475
uint32_t NumLineEntriesWithLine(uint32_t line) const;
474476

475477
void GetDescription(Stream *s, lldb::DescriptionLevel level,

lldb/source/Core/Disassembler.cpp

Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -158,52 +158,59 @@ size_t Disassembler::Disassemble(Debugger &debugger, const ArchSpec &arch,
158158
return success_count;
159159
}
160160

161-
bool Disassembler::Disassemble(Debugger &debugger, const ArchSpec &arch,
162-
const char *plugin_name, const char *flavor,
163-
const ExecutionContext &exe_ctx,
164-
ConstString name, Module *module,
165-
uint32_t num_instructions,
166-
bool mixed_source_and_assembly,
167-
uint32_t num_mixed_context_lines,
168-
uint32_t options, Stream &strm) {
161+
bool Disassembler::Disassemble(
162+
Debugger &debugger, const ArchSpec &arch, const char *plugin_name,
163+
const char *flavor, const ExecutionContext &exe_ctx, ConstString name,
164+
Module *module, uint32_t num_instructions, bool mixed_source_and_assembly,
165+
uint32_t num_mixed_context_lines, uint32_t options, Stream &strm) {
166+
// If no name is given there's nothing to disassemble.
167+
if (!name)
168+
return false;
169+
170+
const bool include_symbols = true;
171+
const bool include_inlines = true;
172+
173+
// Find functions matching the given name.
169174
SymbolContextList sc_list;
170-
if (name) {
171-
const bool include_symbols = true;
172-
const bool include_inlines = true;
173-
if (module) {
174-
module->FindFunctions(name, nullptr, eFunctionNameTypeAuto,
175-
include_symbols, include_inlines, true, sc_list);
176-
} else if (exe_ctx.GetTargetPtr()) {
177-
exe_ctx.GetTargetPtr()->GetImages().FindFunctions(
178-
name, eFunctionNameTypeAuto, include_symbols, include_inlines, false,
179-
sc_list);
180-
}
175+
if (module) {
176+
module->FindFunctions(name, nullptr, eFunctionNameTypeAuto, include_symbols,
177+
include_inlines, true, sc_list);
178+
} else if (exe_ctx.GetTargetPtr()) {
179+
exe_ctx.GetTargetPtr()->GetImages().FindFunctions(
180+
name, eFunctionNameTypeAuto, include_symbols, include_inlines, false,
181+
sc_list);
181182
}
182183

183-
if (sc_list.GetSize()) {
184-
return Disassemble(debugger, arch, plugin_name, flavor, exe_ctx, sc_list,
185-
num_instructions, mixed_source_and_assembly,
186-
num_mixed_context_lines, options, strm);
187-
}
188-
return false;
184+
// If no functions were found there's nothing to disassemble.
185+
if (sc_list.IsEmpty())
186+
return false;
187+
188+
return Disassemble(debugger, arch, plugin_name, flavor, exe_ctx, sc_list,
189+
num_instructions, mixed_source_and_assembly,
190+
num_mixed_context_lines, options, strm);
189191
}
190192

191193
lldb::DisassemblerSP Disassembler::DisassembleRange(
192194
const ArchSpec &arch, const char *plugin_name, const char *flavor,
193195
const ExecutionContext &exe_ctx, const AddressRange &range,
194196
bool prefer_file_cache) {
195-
lldb::DisassemblerSP disasm_sp;
196-
if (range.GetByteSize() > 0 && range.GetBaseAddress().IsValid()) {
197-
disasm_sp = Disassembler::FindPluginForTarget(exe_ctx.GetTargetSP(), arch,
198-
flavor, plugin_name);
197+
if (range.GetByteSize() <= 0)
198+
return {};
199+
200+
if (!range.GetBaseAddress().IsValid())
201+
return {};
202+
203+
lldb::DisassemblerSP disasm_sp = Disassembler::FindPluginForTarget(
204+
exe_ctx.GetTargetSP(), arch, flavor, plugin_name);
205+
206+
if (!disasm_sp)
207+
return {};
208+
209+
const size_t bytes_disassembled =
210+
disasm_sp->ParseInstructions(&exe_ctx, range, nullptr, prefer_file_cache);
211+
if (bytes_disassembled == 0)
212+
return {};
199213

200-
if (disasm_sp) {
201-
size_t bytes_disassembled = disasm_sp->ParseInstructions(
202-
&exe_ctx, range, nullptr, prefer_file_cache);
203-
if (bytes_disassembled == 0)
204-
disasm_sp.reset();
205-
}
206-
}
207214
return disasm_sp;
208215
}
209216

@@ -212,20 +219,20 @@ Disassembler::DisassembleBytes(const ArchSpec &arch, const char *plugin_name,
212219
const char *flavor, const Address &start,
213220
const void *src, size_t src_len,
214221
uint32_t num_instructions, bool data_from_file) {
215-
lldb::DisassemblerSP disasm_sp;
222+
if (!src)
223+
return {};
216224

217-
if (src) {
218-
disasm_sp = Disassembler::FindPlugin(arch, flavor, plugin_name);
225+
lldb::DisassemblerSP disasm_sp =
226+
Disassembler::FindPlugin(arch, flavor, plugin_name);
219227

220-
if (disasm_sp) {
221-
DataExtractor data(src, src_len, arch.GetByteOrder(),
222-
arch.GetAddressByteSize());
228+
if (!disasm_sp)
229+
return {};
223230

224-
(void)disasm_sp->DecodeInstructions(start, data, 0, num_instructions,
225-
false, data_from_file);
226-
}
227-
}
231+
DataExtractor data(src, src_len, arch.GetByteOrder(),
232+
arch.GetAddressByteSize());
228233

234+
(void)disasm_sp->DecodeInstructions(start, data, 0, num_instructions, false,
235+
data_from_file);
229236
return disasm_sp;
230237
}
231238

lldb/source/Symbol/SymbolContext.cpp

Lines changed: 77 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -759,9 +759,8 @@ bool SymbolContext::GetAddressRangeFromHereToEndLine(uint32_t end_line,
759759
}
760760

761761
Block *func_block = GetFunctionBlock();
762-
if (func_block &&
763-
func_block->GetRangeIndexContainingAddress(
764-
end_entry.range.GetBaseAddress()) == UINT32_MAX) {
762+
if (func_block && func_block->GetRangeIndexContainingAddress(
763+
end_entry.range.GetBaseAddress()) == UINT32_MAX) {
765764
error.SetErrorStringWithFormat(
766765
"end line number %d is not contained within the current function.",
767766
end_line);
@@ -774,8 +773,8 @@ bool SymbolContext::GetAddressRangeFromHereToEndLine(uint32_t end_line,
774773
return true;
775774
}
776775

777-
const Symbol *
778-
SymbolContext::FindBestGlobalDataSymbol(ConstString name, Status &error) {
776+
const Symbol *SymbolContext::FindBestGlobalDataSymbol(ConstString name,
777+
Status &error) {
779778
error.Clear();
780779

781780
if (!target_sp) {
@@ -785,8 +784,9 @@ SymbolContext::FindBestGlobalDataSymbol(ConstString name, Status &error) {
785784
Target &target = *target_sp;
786785
Module *module = module_sp.get();
787786

788-
auto ProcessMatches = [this, &name, &target, module]
789-
(SymbolContextList &sc_list, Status &error) -> const Symbol* {
787+
auto ProcessMatches = [this, &name, &target,
788+
module](SymbolContextList &sc_list,
789+
Status &error) -> const Symbol * {
790790
llvm::SmallVector<const Symbol *, 1> external_symbols;
791791
llvm::SmallVector<const Symbol *, 1> internal_symbols;
792792
const uint32_t matches = sc_list.GetSize();
@@ -799,77 +799,77 @@ SymbolContext::FindBestGlobalDataSymbol(ConstString name, Status &error) {
799799

800800
if (sym_address.IsValid()) {
801801
switch (symbol->GetType()) {
802-
case eSymbolTypeData:
803-
case eSymbolTypeRuntime:
804-
case eSymbolTypeAbsolute:
805-
case eSymbolTypeObjCClass:
806-
case eSymbolTypeObjCMetaClass:
807-
case eSymbolTypeObjCIVar:
808-
if (symbol->GetDemangledNameIsSynthesized()) {
809-
// If the demangled name was synthesized, then don't use it for
810-
// expressions. Only let the symbol match if the mangled named
811-
// matches for these symbols.
812-
if (symbol->GetMangled().GetMangledName() != name)
813-
break;
814-
}
815-
if (symbol->IsExternal()) {
816-
external_symbols.push_back(symbol);
817-
} else {
818-
internal_symbols.push_back(symbol);
819-
}
820-
break;
821-
case eSymbolTypeReExported: {
822-
ConstString reexport_name = symbol->GetReExportedSymbolName();
823-
if (reexport_name) {
824-
ModuleSP reexport_module_sp;
825-
ModuleSpec reexport_module_spec;
826-
reexport_module_spec.GetPlatformFileSpec() =
827-
symbol->GetReExportedSymbolSharedLibrary();
828-
if (reexport_module_spec.GetPlatformFileSpec()) {
829-
reexport_module_sp =
830-
target.GetImages().FindFirstModule(reexport_module_spec);
831-
if (!reexport_module_sp) {
832-
reexport_module_spec.GetPlatformFileSpec()
833-
.GetDirectory()
834-
.Clear();
835-
reexport_module_sp =
802+
case eSymbolTypeData:
803+
case eSymbolTypeRuntime:
804+
case eSymbolTypeAbsolute:
805+
case eSymbolTypeObjCClass:
806+
case eSymbolTypeObjCMetaClass:
807+
case eSymbolTypeObjCIVar:
808+
if (symbol->GetDemangledNameIsSynthesized()) {
809+
// If the demangled name was synthesized, then don't use it for
810+
// expressions. Only let the symbol match if the mangled named
811+
// matches for these symbols.
812+
if (symbol->GetMangled().GetMangledName() != name)
813+
break;
814+
}
815+
if (symbol->IsExternal()) {
816+
external_symbols.push_back(symbol);
817+
} else {
818+
internal_symbols.push_back(symbol);
819+
}
820+
break;
821+
case eSymbolTypeReExported: {
822+
ConstString reexport_name = symbol->GetReExportedSymbolName();
823+
if (reexport_name) {
824+
ModuleSP reexport_module_sp;
825+
ModuleSpec reexport_module_spec;
826+
reexport_module_spec.GetPlatformFileSpec() =
827+
symbol->GetReExportedSymbolSharedLibrary();
828+
if (reexport_module_spec.GetPlatformFileSpec()) {
829+
reexport_module_sp =
836830
target.GetImages().FindFirstModule(reexport_module_spec);
837-
}
831+
if (!reexport_module_sp) {
832+
reexport_module_spec.GetPlatformFileSpec()
833+
.GetDirectory()
834+
.Clear();
835+
reexport_module_sp =
836+
target.GetImages().FindFirstModule(reexport_module_spec);
838837
}
839-
// Don't allow us to try and resolve a re-exported symbol if it
840-
// is the same as the current symbol
841-
if (name == symbol->GetReExportedSymbolName() &&
842-
module == reexport_module_sp.get())
843-
return nullptr;
844-
845-
return FindBestGlobalDataSymbol(
846-
symbol->GetReExportedSymbolName(), error);
847838
}
848-
} break;
849-
850-
case eSymbolTypeCode: // We already lookup functions elsewhere
851-
case eSymbolTypeVariable:
852-
case eSymbolTypeLocal:
853-
case eSymbolTypeParam:
854-
case eSymbolTypeTrampoline:
855-
case eSymbolTypeInvalid:
856-
case eSymbolTypeException:
857-
case eSymbolTypeSourceFile:
858-
case eSymbolTypeHeaderFile:
859-
case eSymbolTypeObjectFile:
860-
case eSymbolTypeCommonBlock:
861-
case eSymbolTypeBlock:
862-
case eSymbolTypeVariableType:
863-
case eSymbolTypeLineEntry:
864-
case eSymbolTypeLineHeader:
865-
case eSymbolTypeScopeBegin:
866-
case eSymbolTypeScopeEnd:
867-
case eSymbolTypeAdditional:
868-
case eSymbolTypeCompiler:
869-
case eSymbolTypeInstrumentation:
870-
case eSymbolTypeUndefined:
871-
case eSymbolTypeResolver:
872-
break;
839+
// Don't allow us to try and resolve a re-exported symbol if it
840+
// is the same as the current symbol
841+
if (name == symbol->GetReExportedSymbolName() &&
842+
module == reexport_module_sp.get())
843+
return nullptr;
844+
845+
return FindBestGlobalDataSymbol(symbol->GetReExportedSymbolName(),
846+
error);
847+
}
848+
} break;
849+
850+
case eSymbolTypeCode: // We already lookup functions elsewhere
851+
case eSymbolTypeVariable:
852+
case eSymbolTypeLocal:
853+
case eSymbolTypeParam:
854+
case eSymbolTypeTrampoline:
855+
case eSymbolTypeInvalid:
856+
case eSymbolTypeException:
857+
case eSymbolTypeSourceFile:
858+
case eSymbolTypeHeaderFile:
859+
case eSymbolTypeObjectFile:
860+
case eSymbolTypeCommonBlock:
861+
case eSymbolTypeBlock:
862+
case eSymbolTypeVariableType:
863+
case eSymbolTypeLineEntry:
864+
case eSymbolTypeLineHeader:
865+
case eSymbolTypeScopeBegin:
866+
case eSymbolTypeScopeEnd:
867+
case eSymbolTypeAdditional:
868+
case eSymbolTypeCompiler:
869+
case eSymbolTypeInstrumentation:
870+
case eSymbolTypeUndefined:
871+
case eSymbolTypeResolver:
872+
break;
873873
}
874874
}
875875
}
@@ -930,7 +930,6 @@ SymbolContext::FindBestGlobalDataSymbol(ConstString name, Status &error) {
930930
return nullptr; // no error; we just didn't find anything
931931
}
932932

933-
934933
//
935934
// SymbolContextSpecifier
936935
//
@@ -1293,6 +1292,8 @@ bool SymbolContextList::RemoveContextAtIndex(size_t idx) {
12931292

12941293
uint32_t SymbolContextList::GetSize() const { return m_symbol_contexts.size(); }
12951294

1295+
bool SymbolContextList::IsEmpty() const { return m_symbol_contexts.empty(); }
1296+
12961297
uint32_t SymbolContextList::NumLineEntriesWithLine(uint32_t line) const {
12971298
uint32_t match_count = 0;
12981299
const size_t size = m_symbol_contexts.size();

0 commit comments

Comments
 (0)