Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add static variables parsing. #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions SCKClangSourceFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
67 changes: 52 additions & 15 deletions SCKClangSourceFile.m
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
SourceCodeKit TODO
==================

- Static variable parsing

- Macro parsing

Expand Down
3 changes: 3 additions & 0 deletions Tests/ParsingTestFiles/AB.h
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
24 changes: 24 additions & 0 deletions Tests/TestClangParsing.m
Original file line number Diff line number Diff line change
Expand Up @@ -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