Skip to content

Instantly share code, notes, and snippets.

@mojtabacazi
Last active June 28, 2017 07:07
Show Gist options
  • Save mojtabacazi/14ee2adf2db0c6d5af35f53cc330e283 to your computer and use it in GitHub Desktop.
Save mojtabacazi/14ee2adf2db0c6d5af35f53cc330e283 to your computer and use it in GitHub Desktop.
Find Unused Headers
//
// main.m
// Unsused Import Finder
//
// Created by Mojtaba Cazi on 6/27/17.
// Copyright © 2017 CaziSoft, LLC. All rights reserved.
//
#import <Foundation/Foundation.h>
NSString * fileNameFromImport(NSString *line) {
if (![line hasPrefix:@"#import"]) {
return nil;
}
NSString *header = [[line componentsSeparatedByString:@"/"] lastObject];
header = [[header componentsSeparatedByString:@"."] firstObject];
header = [[header componentsSeparatedByString:@"+"] firstObject];
header = [[header componentsSeparatedByString:@"\""] lastObject];
header = [[header componentsSeparatedByString:@"<"] lastObject];
return header;
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSDate *start = [NSDate date];
__strong NSArray **prints = (__strong NSArray **)calloc(argc, sizeof(NSArray *));
dispatch_apply(argc - 1, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(size_t i) {
NSString *path = [NSString stringWithUTF8String:argv[i + 1]];
NSData *fileData = [[NSData alloc] initWithContentsOfFile:path];
NSString *fileContents = [[NSString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];
__block NSUInteger charCount = 0;
[fileContents enumerateLinesUsingBlock:^(NSString * _Nonnull line, BOOL * _Nonnull stop) {
if (!line.length) {
return;
}
unichar firstChar = [line characterAtIndex:0];
if (firstChar != '#' && firstChar != '/') {
*stop = YES;
} else {
charCount += line.length + 1; // +1 \n
}
}];
NSString *importLines = [fileContents substringWithRange:(NSRange){0, charCount}];
const char *nonImportCString = [fileContents substringWithRange:(NSRange){charCount, fileContents.length - charCount}].UTF8String;
NSMutableArray *importNames = [NSMutableArray arrayWithCapacity:100];
[importLines enumerateLinesUsingBlock:^(NSString * _Nonnull line, BOOL * _Nonnull stop) {
NSString *importName = fileNameFromImport(line);
if (importName.length && strstr(nonImportCString, importName.UTF8String) == NULL) {
[importNames addObject:importName];
}
}];
if (importNames.count) {
NSMutableArray *internalArray = [NSMutableArray arrayWithArray:importNames];
[internalArray insertObject:[@"Potential unused import found in: " stringByAppendingString:path] atIndex:0];
prints[i] = internalArray;
}
}); // dispatch_apply
NSDate *end = [NSDate date];
for (int i = 0; i < argc; i++) {
NSArray *lines = prints[i];
for (NSString *string in lines) {
printf("%s\n", string.UTF8String);
}
if (lines.count) {
printf("\n");
}
}
printf("Elapsed Time: %.2fms\nProcessed %d files\n", [end timeIntervalSinceDate:start] * 1000, argc);
}
return 0;
}
// find . -name "*.m" | xargs ./uif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment