Skip to content

Commit 8f44d69

Browse files
committed
properly identify shadowed enums as enum types
1 parent d77b769 commit 8f44d69

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

clingwrapper/src/clingwrapper.cxx

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,35 @@ static const ClassRefs_t::size_type STD_HANDLE = GLOBAL_HANDLE + 1;
127127
typedef std::map<std::string, ClassRefs_t::size_type> Name2ClassRefIndex_t;
128128
static Name2ClassRefIndex_t g_name2classrefidx;
129129

130+
static std::map<std::string, std::string> resolved_enum_types;
131+
130132
namespace {
131133

132134
static inline
133-
Cppyy::TCppType_t find_memoized(const std::string& name)
135+
Cppyy::TCppType_t find_memoized_scope(const std::string& name)
134136
{
135137
auto icr = g_name2classrefidx.find(name);
136138
if (icr != g_name2classrefidx.end())
137139
return (Cppyy::TCppType_t)icr->second;
138140
return (Cppyy::TCppType_t)0;
139141
}
140142

143+
static inline
144+
std::string find_memoized_resolved_name(const std::string& name)
145+
{
146+
// resolved class types
147+
Cppyy::TCppType_t klass = find_memoized_scope(name);
148+
if (klass) return Cppyy::GetScopedFinalName(klass);
149+
150+
// resolved enum types
151+
auto res = resolved_enum_types.find(name);
152+
if (res != resolved_enum_types.end())
153+
return res->second;
154+
155+
// unknown ...
156+
return "";
157+
}
158+
141159
class CallWrapper {
142160
public:
143161
typedef const void* DeclId_t;
@@ -436,8 +454,8 @@ std::string Cppyy::ResolveName(const std::string& cppitem_name)
436454
// Fully resolve the given name to the final type name.
437455

438456
// try memoized type cache, in case seen before
439-
TCppType_t klass = find_memoized(cppitem_name);
440-
if (klass) return GetScopedFinalName(klass);
457+
std::string memoized = find_memoized_resolved_name(cppitem_name);
458+
if (!memoized.empty()) return memoized;
441459

442460
// remove global scope '::' if present
443461
std::string tclean = cppitem_name.compare(0, 2, "::") == 0 ?
@@ -535,7 +553,6 @@ static std::string extract_namespace(const std::string& name)
535553
return "";
536554
}
537555

538-
static std::map<std::string, std::string> resolved_enum_types;
539556
std::string Cppyy::ResolveEnum(const std::string& enum_type)
540557
{
541558
// The underlying type of a an enum may be any kind of integer.
@@ -620,7 +637,7 @@ static Cppyy::TCppIndex_t ArgSimilarityScore(void *argqtp, void *reqqtp)
620637
Cppyy::TCppScope_t Cppyy::GetScope(const std::string& sname)
621638
{
622639
// First, try cache
623-
TCppType_t result = find_memoized(sname);
640+
TCppType_t result = find_memoized_scope(sname);
624641
if (result) return result;
625642

626643
// Second, skip builtins before going through the more expensive steps of resolving
@@ -633,7 +650,7 @@ Cppyy::TCppScope_t Cppyy::GetScope(const std::string& sname)
633650
std::string scope_name = ResolveName(sname);
634651
bool bHasAlias1 = sname != scope_name;
635652
if (bHasAlias1) {
636-
result = find_memoized(scope_name);
653+
result = find_memoized_scope(scope_name);
637654
if (result) {
638655
g_name2classrefidx[sname] = result;
639656
return result;
@@ -650,7 +667,7 @@ Cppyy::TCppScope_t Cppyy::GetScope(const std::string& sname)
650667
// memoize found/created TClass
651668
bool bHasAlias2 = cr->GetName() != scope_name;
652669
if (bHasAlias2) {
653-
result = find_memoized(cr->GetName());
670+
result = find_memoized_scope(cr->GetName());
654671
if (result) {
655672
g_name2classrefidx[scope_name] = result;
656673
if (bHasAlias1) g_name2classrefidx[sname] = result;
@@ -1165,7 +1182,22 @@ bool Cppyy::IsEnum(const std::string& type_name)
11651182
if (tn_short.empty()) return false;
11661183

11671184
R__LOCKGUARD_CLING(gInterpreterMutex);
1168-
return gInterpreter->ClassInfo_IsEnum(tn_short.c_str());
1185+
if (gInterpreter->ClassInfo_IsEnum(tn_short.c_str()))
1186+
return true;
1187+
1188+
// ClassInfo_IsEnum may fail for shadowed enums
1189+
TEnum* ee = nullptr;
1190+
1191+
std::string scope_name = extract_namespace(tn_short);
1192+
if (scope_name.empty())
1193+
ee = ((TListOfEnums*)gROOT->GetListOfEnums())->GetObject(tn_short.c_str());
1194+
else {
1195+
TClass* cl = TClass::GetClass(scope_name.c_str());
1196+
if (cl) ee = ((TListOfEnums*)cl->GetListOfEnums())->GetObject(
1197+
tn_short.substr(scope_name.size()+2, std::string::npos).c_str());
1198+
}
1199+
1200+
return (bool)ee;
11691201
}
11701202

11711203
bool Cppyy::IsAggregate(TCppType_t type)

0 commit comments

Comments
 (0)