From 6e26ae97b62e7ef94a0ad3d121ea02b8f62d959f Mon Sep 17 00:00:00 2001 From: Muhammad Hussein Nasrollahpour Date: Sat, 10 May 2014 08:30:10 -0700 Subject: [PATCH] Add static variables parsing. --- SCKClangSourceFile.h | 2 ++ SCKClangSourceFile.m | 67 ++++++++++++++++++++++++++++--------- TODO | 2 -- Tests/ParsingTestFiles/AB.h | 3 ++ Tests/TestClangParsing.m | 24 +++++++++++++ 5 files changed, 81 insertions(+), 17 deletions(-) diff --git a/SCKClangSourceFile.h b/SCKClangSourceFile.h index 3c06b71..fd95823 100644 --- a/SCKClangSourceFile.h +++ b/SCKClangSourceFile.h @@ -23,11 +23,13 @@ NSMutableDictionary *enumerations; NSMutableDictionary *enumerationValues; NSMutableDictionary *macros; + NSMutableDictionary *variables; } @property (nonatomic, readonly) NSDictionary *functions; @property (nonatomic, readonly) NSDictionary *enumerations; @property (nonatomic, readonly) NSDictionary *enumerationValues; @property (nonatomic, readonly) NSDictionary *macros; +@property (nonatomic, readonly) NSDictionary *variables; @end diff --git a/SCKClangSourceFile.m b/SCKClangSourceFile.m index f17711d..82031c1 100644 --- a/SCKClangSourceFile.m +++ b/SCKClangSourceFile.m @@ -204,7 +204,7 @@ - (void)highlightRange: (CXSourceRange)r syntax: (BOOL)highightSyntax; @implementation SCKClangSourceFile -@synthesize functions, enumerations, enumerationValues, macros; +@synthesize functions, enumerations, enumerationValues, macros, variables; /* static enum CXChildVisitResult findClass(CXCursor cursor, CXCursor parent, CXClientData client_data) @@ -400,12 +400,31 @@ - (void)setLocation: (SCKSourceLocation*)l //NSLog(@"Found %@ function %@ (%@) %@ at %@", (isStatic ? @"static" : @"global"), [function name], [function typeEncoding], (isDefinition ? @"defined" : @"declared"), l); } +- (SCKGlobal *)globalForName: (NSString*)aName +{ + SCKGlobal *variable = [variables objectForKey: aName]; + + if (nil != variable) + { + return variable; + } + + variable = [SCKGlobal new]; + [variable setName: aName]; + [variables setObject: variable forKey: aName]; + + return variable; +} + + - (void)setLocation: (SCKSourceLocation*)l forVariable: (NSString*)name withTypeEncoding: (NSString*)type + isStatic: (BOOL)isStatic isDefinition: (BOOL)isDefinition { - SCKGlobal *variable = [[self collection] globalForName: name]; + id owner = (isStatic ? self : [self collection]); + SCKGlobal *variable = [owner globalForName: name]; [variable setTypeEncoding: type]; @@ -935,20 +954,37 @@ - (void)rebuildIndex { enum CXLinkageKind linkage = clang_getCursorLinkage(cursor); ETAssert(linkage != CXLinkage_NoLinkage); + SCOPED_STR(name, clang_getCursorSpelling(cursor)); + SCOPED_STR(type, clang_getDeclObjCTypeEncoding(cursor)); + SCKSourceLocation *sourceLocation = [[SCKSourceLocation alloc] initWithClangSourceLocation: clang_getCursorLocation(cursor)]; + BOOL isStatic = (linkage == CXLinkage_Internal ? YES : NO); + + switch (linkage) + { + case CXLinkage_External: + { + [self setLocation: sourceLocation + forVariable: [NSString stringWithUTF8String: name] + withTypeEncoding: [NSString stringWithUTF8String: type] + isStatic: isStatic + isDefinition: clang_isCursorDefinition(cursor)]; + + break; + } + case CXLinkage_Internal: + { + [self setLocation: sourceLocation + forVariable: [NSString stringWithUTF8String: name] + withTypeEncoding: [NSString stringWithUTF8String: type] + isStatic: isStatic + isDefinition: clang_isCursorDefinition(cursor)]; + + break; + } + default: + break; + } - // TODO: Parse static variables - if (linkage != CXLinkage_Internal && linkage != CXLinkage_Invalid) - { - SCOPED_STR(name, clang_getCursorSpelling(cursor)); - SCOPED_STR(type, clang_getDeclObjCTypeEncoding(cursor)); - STACK_SCOPED SCKSourceLocation *l = [[SCKSourceLocation alloc] - initWithClangSourceLocation: clang_getCursorLocation(cursor)]; - - [self setLocation: l - forVariable: [NSString stringWithUTF8String: name] - withTypeEncoding: [NSString stringWithUTF8String: type] - isDefinition: clang_isCursorDefinition(cursor)]; - } break; } case CXCursor_MacroDefinition: @@ -1044,6 +1080,7 @@ - (id)initUsingIndex: (SCKIndex*)anIndex macros = [NSMutableDictionary new]; enumerations = [NSMutableDictionary new]; enumerationValues = [NSMutableDictionary new]; + variables = [NSMutableDictionary new]; return self; } - (void)addIncludePath: (NSString*)includePath diff --git a/TODO b/TODO index 0842c83..c147ff0 100644 --- a/TODO +++ b/TODO @@ -1,7 +1,5 @@ SourceCodeKit TODO ================== - -- Static variable parsing - Macro parsing diff --git a/Tests/ParsingTestFiles/AB.h b/Tests/ParsingTestFiles/AB.h index 7d680a3..9134819 100644 --- a/Tests/ParsingTestFiles/AB.h +++ b/Tests/ParsingTestFiles/AB.h @@ -14,6 +14,9 @@ char function2(char arg1); extern NSDate *kGlobal1; extern NSString *kGlobal2; +static int *staticVar1; +static float *staticVar2; + enum enum1 {value1, value2, value3}; enum enum2 {value4, value5, value6}; diff --git a/Tests/TestClangParsing.m b/Tests/TestClangParsing.m index c5f3d4a..b196cda 100644 --- a/Tests/TestClangParsing.m +++ b/Tests/TestClangParsing.m @@ -434,4 +434,28 @@ - (void)testGlobal UKTrue([[kGlobal2 definition] offset] > [[kGlobal1 definition] offset]); } +- (void)testStaticVariable +{ + SCKClangSourceFile *sourceFile = [self parsedFileForName: @"AB.h"]; + + UKObjectsEqual(A(@"staticVar1", @"staticVar2"), [[[[sourceFile variables] allValues] mappedCollection] name]); + + UKIntsEqual(2, (int)[[sourceFile variables] count]); + + SCKGlobal *staticVar1 = [[sourceFile variables] objectForKey: @"staticVar1"]; + SCKGlobal *staticVar2 = [[sourceFile variables] objectForKey: @"staticVar2"]; + SCKFunction *function2 = [[self parsedFunctionsForNames: A(@"function2")] firstObject]; + + UKNotNil(staticVar1); + UKNotNil(staticVar2); + + UKStringsEqual(@"staticVar1", [staticVar1 name]); + UKStringsEqual(@"staticVar2", [staticVar2 name]); + + UKStringsEqual(@"AB.h", [[[staticVar1 declaration] file] lastPathComponent]); + UKTrue([[staticVar1 declaration] offset] < [[staticVar2 declaration] offset]); + UKTrue([[staticVar2 declaration] offset] > [[function2 declaration] offset]); +} + + @end