Skip to content

Commit

Permalink
Improve clarity/naming/consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Lee committed Jan 5, 2018
1 parent 6d85102 commit c3180f4
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions Chisel/Chisel/CHLObjcInstanceCommands.mm
Expand Up @@ -103,9 +103,9 @@ static bool objectIsMatch(NSPredicate *predicate, id obj, const std::unordered_s

// Function reimplementation of +[NSObject isSubclassOf:] to avoid the objc runtime side
// effects that can happen when calling methods, like realizing classes, +initialize, etc.
static bool isSubclassOf(Class base, Class target)
static bool isSubclassOfClass(Class self, Class target)
{
for (auto cls = base; cls != Nil; cls = class_getSuperclass(cls)) {
for (auto cls = self; cls != Nil; cls = class_getSuperclass(cls)) {
if (cls == target) {
return true;
}
Expand All @@ -115,9 +115,9 @@ static bool isSubclassOf(Class base, Class target)

// Function reimplementation of +[NSObject conformsToProtocol:] to avoid the objc runtime side
// effects that can happen when calling methods, like realizing classes, +initialize, etc.
static bool conformsToProtocol(Class base, Protocol *protocol)
static bool conformsToProtocol(Class self, Protocol *protocol)
{
for (auto cls = base; cls != Nil; cls = class_getSuperclass(cls)) {
for (auto cls = self; cls != Nil; cls = class_getSuperclass(cls)) {
if (class_conformsToProtocol(cls, protocol)) {
return true;
}
Expand Down Expand Up @@ -155,30 +155,32 @@ void PrintInstances(const char *type, const char *pred)
++type;
}

auto addMatch = [&](Class cls) {
// Helper lambda that only exists so that it can be called more than once, as in the
// rare case where `type` corresponds to more than one Swift class.
auto addMatch = [&](Class baseClass) {
if (exactClass) {
matchClasses.insert(cls);
matchClasses.insert(baseClass);
} else {
for (auto c : objcClasses) {
if (isSubclassOf(c, cls)) {
matchClasses.insert(c);
for (auto cls : objcClasses) {
if (isSubclassOfClass(cls, baseClass)) {
matchClasses.insert(cls);
}
}
}
};

Class cls = objc_getClass(type);
if (cls != Nil) {
addMatch(cls);
Class baseClass = objc_getClass(type);
if (baseClass != Nil) {
addMatch(baseClass);
} else {
// The given class name hasn't been found, this could be a Swift class which case has
// a module name prefix. Loop over all classes to look for matching class names.
for (auto c : objcClasses) {
for (auto cls : objcClasses) {
// SwiftModule.ClassName
// ^- dot + 1
auto dot = strchr(class_getName(c), '.');
auto dot = strchr(class_getName(cls), '.');
if (dot && strcmp(type, dot + 1) == 0) {
addMatch(c);
addMatch(cls);
}
}
}
Expand Down

0 comments on commit c3180f4

Please sign in to comment.