Skip to content

Commit 991e445

Browse files
author
Zachary Turner
committedOct 25, 2018
Don't type-erase the SymbolContextItem enumeration.
When we get the `resolve_scope` parameter from the SB API, it's a `uint32_t`. We then pass it through all of LLDB this way, as a uint32. This is unfortunate, because it means the user of an API never actually knows what they're dealing with. We can call it something like `resolve_scope` and have comments saying "this is a value from the `SymbolContextItem` enumeration, but it makes more sense to just have it actually *be* the correct type in the actual C++ type system to begin with. This way the person reading the code just knows what it is. The reason to use integers instead of enumerations for flags is because when you do bitwise operations on enumerations they get promoted to integers, so it makes it tedious to constantly be casting them back to the enumeration types, so I've introduced a macro to make this happen magically. By writing LLDB_MARK_AS_BITMASK_ENUM after defining an enumeration, it will define overloaded operators so that the returned type will be the original enum. This should address all the mechanical issues surrounding using rich enum types directly. This way, we get a better debugger experience, and new users to the codebase can get more easily acquainted with the codebase because their IDE features can help them understand what the types mean. Differential Revision: https://reviews.llvm.org/D53597 llvm-svn: 345313
1 parent 970f38e commit 991e445

37 files changed

+159
-117
lines changed
 

‎lldb/include/lldb/Core/Address.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -508,9 +508,9 @@ class Address {
508508
///
509509
/// @see SymbolContextScope::CalculateSymbolContext(SymbolContext*)
510510
//------------------------------------------------------------------
511-
uint32_t CalculateSymbolContext(
512-
SymbolContext *sc,
513-
uint32_t resolve_scope = lldb::eSymbolContextEverything) const;
511+
uint32_t CalculateSymbolContext(SymbolContext *sc,
512+
lldb::SymbolContextItem resolve_scope =
513+
lldb::eSymbolContextEverything) const;
514514

515515
lldb::ModuleSP CalculateSymbolContextModule() const;
516516

‎lldb/include/lldb/Core/Module.h

+10-13
Original file line numberDiff line numberDiff line change
@@ -816,10 +816,9 @@ class Module : public std::enable_shared_from_this<Module>,
816816
///
817817
/// @see SymbolContext::Scope
818818
//------------------------------------------------------------------
819-
uint32_t
820-
ResolveSymbolContextForAddress(const Address &so_addr, uint32_t resolve_scope,
821-
SymbolContext &sc,
822-
bool resolve_tail_call_address = false);
819+
uint32_t ResolveSymbolContextForAddress(
820+
const Address &so_addr, lldb::SymbolContextItem resolve_scope,
821+
SymbolContext &sc, bool resolve_tail_call_address = false);
823822

824823
//------------------------------------------------------------------
825824
/// Resolve items in the symbol context for a given file and line.
@@ -862,10 +861,9 @@ class Module : public std::enable_shared_from_this<Module>,
862861
///
863862
/// @see SymbolContext::Scope
864863
//------------------------------------------------------------------
865-
uint32_t ResolveSymbolContextForFilePath(const char *file_path, uint32_t line,
866-
bool check_inlines,
867-
uint32_t resolve_scope,
868-
SymbolContextList &sc_list);
864+
uint32_t ResolveSymbolContextForFilePath(
865+
const char *file_path, uint32_t line, bool check_inlines,
866+
lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list);
869867

870868
//------------------------------------------------------------------
871869
/// Resolve items in the symbol context for a given file and line.
@@ -909,10 +907,9 @@ class Module : public std::enable_shared_from_this<Module>,
909907
///
910908
/// @see SymbolContext::Scope
911909
//------------------------------------------------------------------
912-
uint32_t ResolveSymbolContextsForFileSpec(const FileSpec &file_spec,
913-
uint32_t line, bool check_inlines,
914-
uint32_t resolve_scope,
915-
SymbolContextList &sc_list);
910+
uint32_t ResolveSymbolContextsForFileSpec(
911+
const FileSpec &file_spec, uint32_t line, bool check_inlines,
912+
lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list);
916913

917914
void SetFileSpecAndObjectName(const FileSpec &file,
918915
const ConstString &object_name);
@@ -1155,7 +1152,7 @@ class Module : public std::enable_shared_from_this<Module>,
11551152
//------------------------------------------------------------------
11561153
uint32_t ResolveSymbolContextForAddress(lldb::addr_t vm_addr,
11571154
bool vm_addr_is_file_addr,
1158-
uint32_t resolve_scope,
1155+
lldb::SymbolContextItem resolve_scope,
11591156
Address &so_addr, SymbolContext &sc);
11601157

11611158
void SymbolIndicesToSymbolContextList(Symtab *symtab,

‎lldb/include/lldb/Core/ModuleList.h

+7-9
Original file line numberDiff line numberDiff line change
@@ -495,26 +495,24 @@ class ModuleList {
495495
/// &,uint32_t,SymbolContext&)
496496
//------------------------------------------------------------------
497497
uint32_t ResolveSymbolContextForAddress(const Address &so_addr,
498-
uint32_t resolve_scope,
498+
lldb::SymbolContextItem resolve_scope,
499499
SymbolContext &sc) const;
500500

501501
//------------------------------------------------------------------
502502
/// @copydoc Module::ResolveSymbolContextForFilePath (const char
503503
/// *,uint32_t,bool,uint32_t,SymbolContextList&)
504504
//------------------------------------------------------------------
505-
uint32_t ResolveSymbolContextForFilePath(const char *file_path, uint32_t line,
506-
bool check_inlines,
507-
uint32_t resolve_scope,
508-
SymbolContextList &sc_list) const;
505+
uint32_t ResolveSymbolContextForFilePath(
506+
const char *file_path, uint32_t line, bool check_inlines,
507+
lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list) const;
509508

510509
//------------------------------------------------------------------
511510
/// @copydoc Module::ResolveSymbolContextsForFileSpec (const FileSpec
512511
/// &,uint32_t,bool,uint32_t,SymbolContextList&)
513512
//------------------------------------------------------------------
514-
uint32_t ResolveSymbolContextsForFileSpec(const FileSpec &file_spec,
515-
uint32_t line, bool check_inlines,
516-
uint32_t resolve_scope,
517-
SymbolContextList &sc_list) const;
513+
uint32_t ResolveSymbolContextsForFileSpec(
514+
const FileSpec &file_spec, uint32_t line, bool check_inlines,
515+
lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list) const;
518516

519517
//------------------------------------------------------------------
520518
/// Gets the size of the module list.

‎lldb/include/lldb/Symbol/CompileUnit.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ class CompileUnit : public std::enable_shared_from_this<CompileUnit>,
391391
//------------------------------------------------------------------
392392
uint32_t ResolveSymbolContext(const FileSpec &file_spec, uint32_t line,
393393
bool check_inlines, bool exact,
394-
uint32_t resolve_scope,
394+
lldb::SymbolContextItem resolve_scope,
395395
SymbolContextList &sc_list);
396396

397397
//------------------------------------------------------------------

‎lldb/include/lldb/Symbol/SymbolFile.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,11 @@ class SymbolFile : public PluginInterface {
155155
return CompilerDeclContext();
156156
}
157157
virtual uint32_t ResolveSymbolContext(const Address &so_addr,
158-
uint32_t resolve_scope,
158+
lldb::SymbolContextItem resolve_scope,
159159
SymbolContext &sc) = 0;
160160
virtual uint32_t ResolveSymbolContext(const FileSpec &file_spec,
161161
uint32_t line, bool check_inlines,
162-
uint32_t resolve_scope,
162+
lldb::SymbolContextItem resolve_scope,
163163
SymbolContextList &sc_list);
164164
virtual uint32_t
165165
FindGlobalVariables(const ConstString &name,

‎lldb/include/lldb/Symbol/SymbolVendor.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ class SymbolVendor : public ModuleChild, public PluginInterface {
7171
virtual Type *ResolveTypeUID(lldb::user_id_t type_uid);
7272

7373
virtual uint32_t ResolveSymbolContext(const Address &so_addr,
74-
uint32_t resolve_scope,
74+
lldb::SymbolContextItem resolve_scope,
7575
SymbolContext &sc);
7676

7777
virtual uint32_t ResolveSymbolContext(const FileSpec &file_spec,
7878
uint32_t line, bool check_inlines,
79-
uint32_t resolve_scope,
79+
lldb::SymbolContextItem resolve_scope,
8080
SymbolContextList &sc_list);
8181

8282
virtual size_t FindGlobalVariables(const ConstString &name,

‎lldb/include/lldb/Target/StackFrame.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ class StackFrame : public ExecutionContextScope,
173173
/// A SymbolContext reference which includes the types of information
174174
/// requested by resolve_scope, if they are available.
175175
//------------------------------------------------------------------
176-
const SymbolContext &GetSymbolContext(uint32_t resolve_scope);
176+
const SymbolContext &GetSymbolContext(lldb::SymbolContextItem resolve_scope);
177177

178178
//------------------------------------------------------------------
179179
/// Return the Canonical Frame Address (DWARF term) for this frame.

‎lldb/include/lldb/lldb-enumerations.h

+52-22
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,36 @@
1010
#ifndef LLDB_lldb_enumerations_h_
1111
#define LLDB_lldb_enumerations_h_
1212

13+
#include <type_traits>
14+
15+
// Macro to enable bitmask operations on an enum. Without this, Enum | Enum
16+
// gets promoted to an int, so you have to say Enum a = Enum(eFoo | eBar). If
17+
// you mark Enum with LLDB_MARK_AS_BITMASK_ENUM(Enum), however, you can simply
18+
// write Enum a = eFoo | eBar.
19+
#define LLDB_MARK_AS_BITMASK_ENUM(Enum) \
20+
inline Enum operator|(Enum a, Enum b) { \
21+
return static_cast<Enum>( \
22+
static_cast<std::underlying_type<Enum>::type>(a) | \
23+
static_cast<std::underlying_type<Enum>::type>(b)); \
24+
} \
25+
inline Enum operator&(Enum a, Enum b) { \
26+
return static_cast<Enum>( \
27+
static_cast<std::underlying_type<Enum>::type>(a) & \
28+
static_cast<std::underlying_type<Enum>::type>(b)); \
29+
} \
30+
inline Enum operator~(Enum a) { \
31+
return static_cast<Enum>( \
32+
~static_cast<std::underlying_type<Enum>::type>(a)); \
33+
} \
34+
inline Enum &operator|=(Enum &a, Enum b) { \
35+
a = a | b; \
36+
return a; \
37+
} \
38+
inline Enum &operator&=(Enum &a, Enum b) { \
39+
a = a & b; \
40+
return a; \
41+
}
42+
1343
#ifndef SWIG
1444
// With MSVC, the default type of an enum is always signed, even if one of the
1545
// enumerator values is too large to fit into a signed integer but would
@@ -327,39 +357,40 @@ enum InputReaderGranularity {
327357
//------------------------------------------------------------------
328358
FLAGS_ENUM(SymbolContextItem){
329359
eSymbolContextTarget = (1u << 0), ///< Set when \a target is requested from
330-
///a query, or was located in query
331-
///results
360+
/// a query, or was located in query
361+
/// results
332362
eSymbolContextModule = (1u << 1), ///< Set when \a module is requested from
333-
///a query, or was located in query
334-
///results
363+
/// a query, or was located in query
364+
/// results
335365
eSymbolContextCompUnit = (1u << 2), ///< Set when \a comp_unit is requested
336-
///from a query, or was located in query
337-
///results
366+
/// from a query, or was located in
367+
/// query results
338368
eSymbolContextFunction = (1u << 3), ///< Set when \a function is requested
339-
///from a query, or was located in query
340-
///results
369+
/// from a query, or was located in
370+
/// query results
341371
eSymbolContextBlock = (1u << 4), ///< Set when the deepest \a block is
342-
///requested from a query, or was located
343-
///in query results
372+
/// requested from a query, or was located
373+
/// in query results
344374
eSymbolContextLineEntry = (1u << 5), ///< Set when \a line_entry is
345-
///requested from a query, or was
346-
///located in query results
375+
/// requested from a query, or was
376+
/// located in query results
347377
eSymbolContextSymbol = (1u << 6), ///< Set when \a symbol is requested from
348-
///a query, or was located in query
349-
///results
378+
/// a query, or was located in query
379+
/// results
350380
eSymbolContextEverything = ((eSymbolContextSymbol << 1) -
351381
1u), ///< Indicates to try and lookup everything
352-
///up during a routine symbol context
353-
///query.
354-
eSymbolContextVariable = (1u << 7) ///< Set when \a global or static
355-
///variable is requested from a query, or
356-
///was located in query results.
382+
/// up during a routine symbol context
383+
/// query.
384+
eSymbolContextVariable = (1u << 7), ///< Set when \a global or static
385+
/// variable is requested from a query,
386+
/// or was located in query results.
357387
///< eSymbolContextVariable is potentially expensive to lookup so it isn't
358-
///included in
388+
/// included in
359389
///< eSymbolContextEverything which stops it from being used during frame PC
360-
///lookups and
390+
/// lookups and
361391
///< many other potential address to symbol context lookups.
362392
};
393+
LLDB_MARK_AS_BITMASK_ENUM(SymbolContextItem)
363394

364395
FLAGS_ENUM(Permissions){ePermissionsWritable = (1u << 0),
365396
ePermissionsReadable = (1u << 1),
@@ -1086,7 +1117,6 @@ enum TypeSummaryCapping {
10861117
eTypeSummaryCapped = true,
10871118
eTypeSummaryUncapped = false
10881119
};
1089-
10901120
} // namespace lldb
10911121

10921122
#endif // LLDB_lldb_enumerations_h_

‎lldb/source/API/SBAddress.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,9 @@ SBModule SBAddress::GetModule() {
198198

199199
SBSymbolContext SBAddress::GetSymbolContext(uint32_t resolve_scope) {
200200
SBSymbolContext sb_sc;
201+
SymbolContextItem scope = static_cast<SymbolContextItem>(resolve_scope);
201202
if (m_opaque_ap->IsValid())
202-
m_opaque_ap->CalculateSymbolContext(&sb_sc.ref(), resolve_scope);
203+
m_opaque_ap->CalculateSymbolContext(&sb_sc.ref(), scope);
203204
return sb_sc;
204205
}
205206

‎lldb/source/API/SBFrame.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ SBSymbolContext SBFrame::GetSymbolContext(uint32_t resolve_scope) const {
112112
SBSymbolContext sb_sym_ctx;
113113
std::unique_lock<std::recursive_mutex> lock;
114114
ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
115-
115+
SymbolContextItem scope = static_cast<SymbolContextItem>(resolve_scope);
116116
StackFrame *frame = nullptr;
117117
Target *target = exe_ctx.GetTargetPtr();
118118
Process *process = exe_ctx.GetProcessPtr();
@@ -121,7 +121,7 @@ SBSymbolContext SBFrame::GetSymbolContext(uint32_t resolve_scope) const {
121121
if (stop_locker.TryLock(&process->GetRunLock())) {
122122
frame = exe_ctx.GetFramePtr();
123123
if (frame) {
124-
sb_sym_ctx.SetSymbolContext(&frame->GetSymbolContext(resolve_scope));
124+
sb_sym_ctx.SetSymbolContext(&frame->GetSymbolContext(scope));
125125
} else {
126126
if (log)
127127
log->Printf("SBFrame::GetVariables () => error: could not "

‎lldb/source/API/SBModule.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,9 @@ SBModule::ResolveSymbolContextForAddress(const SBAddress &addr,
217217
uint32_t resolve_scope) {
218218
SBSymbolContext sb_sc;
219219
ModuleSP module_sp(GetSP());
220+
SymbolContextItem scope = static_cast<SymbolContextItem>(resolve_scope);
220221
if (module_sp && addr.IsValid())
221-
module_sp->ResolveSymbolContextForAddress(addr.ref(), resolve_scope,
222-
*sb_sc);
222+
module_sp->ResolveSymbolContextForAddress(addr.ref(), scope, *sb_sc);
223223
return sb_sc;
224224
}
225225

‎lldb/source/API/SBTarget.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -660,11 +660,12 @@ SBSymbolContext
660660
SBTarget::ResolveSymbolContextForAddress(const SBAddress &addr,
661661
uint32_t resolve_scope) {
662662
SBSymbolContext sc;
663+
SymbolContextItem scope = static_cast<SymbolContextItem>(resolve_scope);
663664
if (addr.IsValid()) {
664665
TargetSP target_sp(GetSP());
665666
if (target_sp)
666-
target_sp->GetImages().ResolveSymbolContextForAddress(
667-
addr.ref(), resolve_scope, sc.ref());
667+
target_sp->GetImages().ResolveSymbolContextForAddress(addr.ref(), scope,
668+
sc.ref());
668669
}
669670
return sc;
670671
}

‎lldb/source/Commands/CommandObjectSource.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -1210,7 +1210,9 @@ class CommandObjectSourceList : public CommandObjectParsed {
12101210
target->GetImages().FindModules(module_spec, matching_modules);
12111211
num_matches += matching_modules.ResolveSymbolContextForFilePath(
12121212
filename, 0, check_inlines,
1213-
eSymbolContextModule | eSymbolContextCompUnit, sc_list);
1213+
SymbolContextItem(eSymbolContextModule |
1214+
eSymbolContextCompUnit),
1215+
sc_list);
12141216
}
12151217
}
12161218
} else {

‎lldb/source/Core/Address.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -779,8 +779,9 @@ bool Address::SectionWasDeletedPrivate() const {
779779
m_section_wp.owner_before(empty_section_wp);
780780
}
781781

782-
uint32_t Address::CalculateSymbolContext(SymbolContext *sc,
783-
uint32_t resolve_scope) const {
782+
uint32_t
783+
Address::CalculateSymbolContext(SymbolContext *sc,
784+
SymbolContextItem resolve_scope) const {
784785
sc->Clear(false);
785786
// Absolute addresses don't have enough information to reconstruct even their
786787
// target.

‎lldb/source/Core/Disassembler.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -429,9 +429,9 @@ bool Disassembler::PrintInstructions(Disassembler *disasm_ptr,
429429
const Address &addr = inst->GetAddress();
430430
ModuleSP module_sp(addr.GetModule());
431431
if (module_sp) {
432-
const uint32_t resolve_mask = eSymbolContextFunction |
433-
eSymbolContextSymbol |
434-
eSymbolContextLineEntry;
432+
const SymbolContextItem resolve_mask = eSymbolContextFunction |
433+
eSymbolContextSymbol |
434+
eSymbolContextLineEntry;
435435
uint32_t resolved_mask =
436436
module_sp->ResolveSymbolContextForAddress(addr, resolve_mask, sc);
437437
if (resolved_mask) {

0 commit comments

Comments
 (0)
Please sign in to comment.