-
Notifications
You must be signed in to change notification settings - Fork 5
/
SCKClangSourceFile.m
1383 lines (1263 loc) · 43.1 KB
/
SCKClangSourceFile.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#import "SCKClangSourceFile.h"
#import "SourceCodeKit.h"
#import <Cocoa/Cocoa.h>
#import <EtoileFoundation/EtoileFoundation.h>
#include <time.h>
//#define NSLog(...)
/**
* Converts a clang source range into an NSRange within its enclosing file.
*/
NSRange NSRangeFromCXSourceRange(CXSourceRange sr)
{
unsigned start, end;
CXSourceLocation s = clang_getRangeStart(sr);
CXSourceLocation e = clang_getRangeEnd(sr);
clang_getInstantiationLocation(s, 0, 0, 0, &start);
clang_getInstantiationLocation(e, 0, 0, 0, &end);
if (end < start)
{
NSRange r = {end, start-end};
return r;
}
NSRange r = {start, end - start};
return r;
}
static void freestring(CXString *str)
{
clang_disposeString(*str);
}
#define SCOPED_STR(name, value)\
__attribute__((unused))\
__attribute__((cleanup(freestring))) CXString name ## str = value;\
const char *name = clang_getCString(name ## str);
@implementation SCKSourceLocation
@synthesize file, offset;
- (id)initWithClangSourceLocation: (CXSourceLocation)l
{
SUPERINIT;
CXFile f;
unsigned o;
clang_getInstantiationLocation(l, &f, 0, 0, &o);
offset = o;
SCOPED_STR(fileName, clang_getFileName(f));
file = [[NSString alloc] initWithUTF8String: fileName];
return self;
}
- (NSString*)description
{
return [NSString stringWithFormat: @"%@:%d", file, (int)offset];
}
@end
#ifdef GNUSTEP
/*
* GNUstep has a few private methods in NSBundle that let you determine the
* correct include path in a non-flattened filesystem setup. We need to expose
* it here.
*/
@interface NSBundle (ExposeGNUstepInternals)
+ (NSString*)_gnustep_target_dir;
+ (NSString*)_library_combo;
@end
/**
* Read the relevant environment variables and construct an array of -I
* directives that the compiler should search for GNUstep headers.
*
* FIXME: This might not actually work with Windows style path-separators ('\').
*/
NSArray *GNUstepIncludeDirectories()
{
NSDictionary *environment = [[NSProcessInfo processInfo] environment];
NSString *pathList = [environment objectForKey: @"GNUSTEP_PATHLIST"];
NSInteger length = [pathList length];
if (0 == length)
{
return nil;
}
BOOL isFlattened = [[environment objectForKey: @"GNUSTEP_IS_FLATTENED"] boolValue];
NSMutableArray *accumulator = [NSMutableArray array];
NSScanner *scanner = [NSScanner scannerWithString: pathList];
[scanner setCharactersToBeSkipped: [NSCharacterSet newlineCharacterSet]];
NSCharacterSet *stopSet = [NSCharacterSet characterSetWithCharactersInString: @":\\"];
NSMutableString *thisPath = [NSMutableString string];
@autoreleasepool {
while (NO == [scanner isAtEnd])
{
NSString *nextPart = nil;
BOOL foundPath = [scanner scanUpToCharactersFromSet: stopSet
intoString: &nextPart];
if (foundPath)
{
[thisPath appendString: nextPart];
}
NSInteger location = [scanner scanLocation];
// If we encounter the escape char '\' we advance the scan location and
// copy the two characters to the path list, but only if the escape char
// is not the last character.
if ((location < (length - 1))
&& ((unichar)'\\' == [pathList characterAtIndex: (location)]))
{
NSString *twoChars = [pathList substringWithRange: NSMakeRange(location, 2)];
[thisPath appendString: twoChars];
[scanner setScanLocation: (location + 2)];
}
else
{
// The other only stop character is the proper delimiter ':', so we
// know that the path is complete if we encounter everything but a
// '\'. We thus add a copy to the accumulator and reset the string.
[accumulator addObject: [thisPath copy]];
[thisPath setString: @""];
[scanner setScanLocation: (location + 1)];
}
}
}
// Fetch any remaining path (this might happen if a path ends with an
// escape sequence)
if (0 != [thisPath length])
{
[accumulator addObject: [thisPath copy]];
}
NSRange nullRange = NSMakeRange(0,0);
[[accumulator map] stringByReplacingCharactersInRange: nullRange
withString: @"-I"];
if (isFlattened)
{
[[accumulator map] stringByAppendingPathComponent: @"Library/Headers"];
}
else
{
NSString *subDir = [@"Library/Headers" stringByAppendingPathComponent: [NSBundle _library_combo]];
// These are the normal headers:
[[accumulator map] stringByAppendingPathComponent: subDir];
// And these are the architecture dependent ones
[accumulator addObjectsFromArray: (NSArray*)[[accumulator mappedCollection] stringByAppendingPathComponent: [NSBundle _gnustep_target_dir]]];
}
return [accumulator copy];
}
#endif
@interface SCKClangIndex : NSObject
@property (readonly) CXIndex clangIndex;
//FIXME: We should have different default arguments for C, C++ and ObjC.
@property (nonatomic, copy) NSMutableArray *defaultArguments;
@end
@implementation SCKClangIndex
@synthesize clangIndex, defaultArguments;
- (id)init
{
SUPERINIT;
clang_toggleCrashRecovery(0);
clangIndex = clang_createIndex(1, 1);
/*
* NOTE: If BuildKit becomes usable, it might be sensible to store these
* defaults in the BuildKit configuration and let BuildKit generate the
* command line switches for us.
*/
NSString *plistPath = [[NSBundle bundleForClass: [SCKClangIndex class]]
pathForResource: @"DefaultArguments"
ofType: @"plist"];
NSData *plistData = [NSData dataWithContentsOfFile: plistPath];
// Load the options required to compile GNUstep apps
defaultArguments = [(NSArray*)[NSPropertyListSerialization propertyListFromData: plistData
mutabilityOption: NSPropertyListImmutable
format: NULL
errorDescription: NULL] mutableCopy];
# ifdef GNUSTEP
NSArray *gsIncludeDirs = GNUstepIncludeDirectories();
if (nil != gsIncludeDirs)
{
[defaultArguments addObjectsFromArray: gsIncludeDirs];
}
# endif
[defaultArguments addObject: @"-I/usr/lib/gcc/i686-linux-gnu/4.6/include-fixed/"];
[defaultArguments addObject: @"-I/usr/lib/gcc/i686-linux-gnu/4.6/include/"];
return self;
}
- (void)dealloc
{
clang_disposeIndex(clangIndex);
}
@end
@interface SCKClangSourceFile ()
- (void)highlightRange: (CXSourceRange)r syntax: (BOOL)highightSyntax;
@end
@implementation SCKClangSourceFile
@synthesize functions, enumerations, enumerationValues, macros;
/*
static enum CXChildVisitResult findClass(CXCursor cursor, CXCursor parent, CXClientData client_data)
{
if (CXCursor_ObjCClassRef == cursor.kind)
{
NSString **strPtr = (NSString**)client_data;
SCOPED_STR(name, clang_getCursorSpelling(cursor));
*strPtr = [NSString stringWithUTF8String: name];
return CXChildVisit_Break;
}
return CXChildVisit_Continue;
}
*/
static NSString *classNameFromCategory(CXCursor category)
{
__block NSString *className = nil;
clang_visitChildrenWithBlock(category,
^ enum CXChildVisitResult (CXCursor cursor, CXCursor parent)
{
if (CXCursor_ObjCClassRef == cursor.kind)
{
SCOPED_STR(name, clang_getCursorSpelling(cursor));
className = [NSString stringWithUTF8String: name];
return CXChildVisit_Break;
}
return CXChildVisit_Continue;
} );
return className;
}
static BOOL isIBOutletFromPropertyOrIvar(CXCursor cursor)
{
__block BOOL isIBOutlet = NO;
clang_visitChildrenWithBlock(cursor,
^ enum CXChildVisitResult (CXCursor subcursor, CXCursor parent)
{
if (CXCursor_IBOutletAttr == subcursor.kind)
{
isIBOutlet = YES;
return CXChildVisit_Break;
}
return CXChildVisit_Continue;
});
return isIBOutlet;
}
- (void) setLocation: (SCKSourceLocation*)aLocation
forClass: (NSString*)aClassName
withSuperclass: (NSString*)aSuperclassName
isDefinition: (BOOL)isDefinition
isForwardDeclaration: (BOOL)isForwardDeclaration
{
SCKClass *class = [[self collection] classForName: aClassName];
if (isForwardDeclaration)
{
ETAssert(aSuperclassName == nil && isDefinition == NO);
return;
}
// If we are parsing the definition, the superclass name is nil
if (aSuperclassName != nil)
{
[class setSuperclass: [[self collection] classForName: aSuperclassName]];
}
if (isDefinition)
{
[class setDefinition: aLocation];
}
else
{
[class setDeclaration: aLocation];
}
}
- (void)setLocation: (SCKSourceLocation*)aLocation
forCategory: (NSString*)aCategoryName
isDefinition: (BOOL)isDefinition
ofClass: (NSString*)aClassName
{
SCKClass *class = [[self collection] classForName: aClassName];
SCKCategory *category = [[class categories] objectForKey: aCategoryName];
if (nil == category)
{
category = [SCKCategory new];
[category setName: aCategoryName];
[category setParent: class];
[[class categories] setObject: category forKey: aCategoryName];
}
if (isDefinition)
{
[category setDefinition: aLocation];
}
else
{
[category setDeclaration: aLocation];
}
}
- (void)setLocation: (SCKSourceLocation*)aLocation
forMethod: (NSString*)methodName
withTypeEncoding: (NSString*)typeEncoding
isClassMethod: (BOOL)isClassMethod
isDefinition: (BOOL)isDefinition
inClass: (NSString*)className
category: (NSString*)categoryName
{
SCKClass *cls = [[self collection] classForName: className];
NSMutableDictionary *methods = cls.methods;
SCKCategory *cat = nil;
if (nil != categoryName)
{
cat = [cls.categories objectForKey: categoryName];
ETAssert(cat != nil);
methods = cat.methods;
}
SCKMethod *m = [methods objectForKey: methodName];
if (nil == m)
{
m = [SCKMethod new];
[m setName: methodName];
[m setTypeEncoding: typeEncoding];
[m setIsClassMethod: isClassMethod];
[m setParent: cls];
[[cat methods] setObject: m forKey: methodName];
[[cls methods] setObject: m forKey: methodName];
}
if (isDefinition)
{
m.definition = aLocation;
}
else
{
m.declaration = aLocation;
}
}
- (SCKFunction*)functionForName: (NSString*)aName
{
SCKFunction *function = [functions objectForKey: aName];
if (nil != function)
{
return function;
}
function = [SCKFunction new];
[function setName: aName];
[functions setObject: function forKey: aName];
return function;
}
- (void)setLocation: (SCKSourceLocation*)l
forFunction: (NSString*)name
withTypeEncoding: (NSString*)type
isStatic: (BOOL)isStatic
isDefinition: (BOOL)isDefinition
{
BOOL isIncludedFunction = (![[l file] isEqualToString: [self fileName]]);
if (isIncludedFunction && [[self collection] ignoresIncludedSymbols])
{
return;
}
id owner = (isStatic ? self : [self collection]);
SCKFunction *function = [owner functionForName: name];
[function setTypeEncoding: type];
if (isDefinition)
{
function.definition = l;
}
else
{
function.declaration = l;
}
//NSLog(@"Found %@ function %@ (%@) %@ at %@", (isStatic ? @"static" : @"global"), [function name], [function typeEncoding], (isDefinition ? @"defined" : @"declared"), l);
}
- (void)setLocation: (SCKSourceLocation*)l
forVariable: (NSString*)name
withTypeEncoding: (NSString*)type
isDefinition: (BOOL)isDefinition
{
SCKGlobal *variable = [[self collection] globalForName: name];
[variable setTypeEncoding: type];
if (isDefinition)
{
variable.definition = l;
}
else
{
variable.declaration = l;
}
//NSLog(@"Found %@ variable %@ (%@) %@ at %@", (isStatic ? @"static" : @"global"), [variable name], [variable typeEncoding], (isDefinition ? @"defined" : @"declared"), l);
}
#if CINDEX_VERSION < 21
#define CXObjCPropertyAttrKind int
#endif
- (void)setLocation: (SCKSourceLocation*)sourceLocation
forProperty: (NSString*)propertyName
withTypeEncoding: (NSString*)typeEncoding
attributes: (CXObjCPropertyAttrKind)propertyAttributes
isIBOutlet: (BOOL)isIBOutlet
inClass: (NSString *)className
{
SCKClass *class = [[self collection] classForName: className];
SCKProperty *property = [class propertyForName: propertyName];
if (nil == property)
{
property = [SCKProperty new];
[property setName: propertyName];
[property setParent: class];
[[class properties] addObject: property];
}
// TODO: Convert CXObjCPropertyAttrKind into a SCK equivalent enum
[property setTypeEncoding: typeEncoding];
[property setIsIBOutlet: isIBOutlet];
[property setDeclaration: sourceLocation];
}
- (void)setLocation: (SCKSourceLocation*)sourceLocation
forMacro: (NSString*)macroName
{
SCKMacro *macro = [macros objectForKey: macroName];
if (nil == macro)
{
macro = [SCKMacro new];
[macro setName: macroName];
[macros setObject: macro forKey: macroName];
}
[macro setDefinition: sourceLocation];
[macro setDeclaration: sourceLocation];
}
- (void)setLocation: (SCKSourceLocation*)sourceLocation
forIvar: (NSString*)ivarName
withTypeEncoding: (NSString*)typeEncoding
isIBOutlet: (BOOL)isIBOutlet
inClass: (NSString*)className
{
SCKClass *class = [[self collection] classForName: className];
SCKIvar *ivar = [class ivarForName: ivarName];
if (nil == ivar)
{
ivar = [SCKIvar new];
[ivar setName: ivarName];
[ivar setTypeEncoding: typeEncoding];
[ivar setIsIBOutlet: isIBOutlet];
[ivar setParent: class];
[[class ivars] addObject: ivar];
}
[ivar setDeclaration: sourceLocation];
}
- (void) setLocation: (SCKSourceLocation*)sourceLocation
forProtocol: (NSString*)protocolName
isForwardDeclaration: (BOOL)isForwardDeclaration
{
SCKProtocol *protocol = [[self collection] protocolForName: protocolName];
if (isForwardDeclaration)
{
return;
}
[protocol setDeclaration: sourceLocation];
[protocol setDefinition: sourceLocation];
}
- (void)setLocation: (SCKSourceLocation*)sourceLocation
forMethod: (NSString*)methodName
withTypeEncoding: (NSString*)typeEncoding
isClassMethod: (BOOL)isClassMethod
isRequired: (BOOL)isRequired
isDefinition: (BOOL)isDefinition
inProtocol: (NSString*)protocolName
{
SCKProtocol *protocol = [[self collection] protocolForName: protocolName];
SCKMethod *method = nil;
if (isRequired)
{
method = [[protocol requiredMethods] objectForKey: methodName];
}
else
{
method = [[protocol optionalMethods] objectForKey: methodName];
}
if (nil == method)
{
method = [SCKMethod new];
[method setName: methodName];
[method setTypeEncoding: typeEncoding];
[method setIsClassMethod: isClassMethod];
[method setParent: protocol];
if (isRequired)
{
[[protocol requiredMethods] setObject: method forKey: methodName];
}
else
{
[[protocol optionalMethods] setObject: method forKey: methodName];
}
}
if (isDefinition)
{
[method setDefinition: sourceLocation];
}
else
{
[method setDeclaration: sourceLocation];
}
}
- (void)setLocation: (SCKSourceLocation*)sourceLocation
forProperty: (NSString*)propertyName
withTypeEncoding: (NSString*)typeEncoding
attributes: (CXObjCPropertyAttrKind)attributes
isIBOutlet: (BOOL)isIBOutlet
isRequired: (BOOL)isRequired
inProtocol: (NSString*)protocolName
{
SCKProtocol *protocol = [[self collection] protocolForName: protocolName];
SCKProperty *property = nil;
if (isRequired)
{
property = [protocol requiredPropertyForName: propertyName];
}
else
{
property = [protocol optionalPropertyForName: propertyName];
}
if (nil == property)
{
property = [SCKProperty new];
[property setName: propertyName];
[property setTypeEncoding: typeEncoding];
[property setParent: protocol];
if (isRequired)
{
[[protocol requiredProperties] addObject: property];
}
else
{
[[protocol optionalProperties] addObject: property];
}
}
[property setDeclaration: sourceLocation];
}
- (void)setLocation: (SCKSourceLocation*)sourceLocation
forProperty: (NSString*)propertyName
withTypeEncoding: (NSString*)typeEncoding
attributes: (CXObjCPropertyAttrKind)attributes
isDefinition: (BOOL)isDefinition
inClass: (NSString*)className
category: (NSString*)categoryName
{
SCKClass *class = [[self collection] classForName: className];
SCKCategory *category = nil;
if (nil != categoryName)
{
category = [[class categories] objectForKey: categoryName];
ETAssert(category != nil);
}
SCKProperty *property = [category propertyForName: propertyName];
if (nil == property)
{
property = [SCKProperty new];
[property setName: propertyName];
[property setTypeEncoding: typeEncoding];
[property setParent: class];
[[category properties] addObject: property];
[[class properties] addObject: property];
}
// @dynamic is assumed definition
if (isDefinition)
{
[property setDefinition: sourceLocation];
}
else
{
[property setDeclaration: sourceLocation];
}
}
- (void)rebuildIndex
{
if (0 == translationUnit) { return; }
clang_visitChildrenWithBlock(clang_getTranslationUnitCursor(translationUnit),
^ enum CXChildVisitResult (CXCursor cursor, CXCursor parent)
{
switch(cursor.kind)
{
default:
{
#if 0
SCOPED_STR(name, clang_getCursorSpelling(cursor));
SCOPED_STR(kind, clang_getCursorKindSpelling(clang_getCursorKind(cursor)));
NSLog(@"Unhandled cursor type: %s (%s)", kind, name);
#endif
break;
}
case CXCursor_ObjCInterfaceDecl:
{
SCKSourceLocation *classLoc = [[SCKSourceLocation alloc]
initWithClangSourceLocation: clang_getCursorLocation(cursor)];
SCOPED_STR(className, clang_getCursorSpelling(cursor));
NSString __block *superclassName = nil;
BOOL __block isForwardDeclaration = NO;
clang_visitChildrenWithBlock(cursor,
^ enum CXChildVisitResult (CXCursor classCursor, CXCursor parent)
{
switch (classCursor.kind)
{
case CXCursor_ObjCClassRef:
{
isForwardDeclaration = YES;
break;
}
case CXCursor_ObjCSuperClassRef:
{
SCOPED_STR(name, clang_getCursorSpelling(classCursor));
superclassName = [NSString stringWithUTF8String: name];
break;
}
case CXCursor_ObjCIvarDecl:
{
SCOPED_STR(name, clang_getCursorSpelling(classCursor));
SCOPED_STR(typeEncoding, clang_getDeclObjCTypeEncoding(classCursor));
SCKSourceLocation *sourceLocation = [[SCKSourceLocation alloc]
initWithClangSourceLocation: clang_getCursorLocation(classCursor)];
[self setLocation: sourceLocation
forIvar: [NSString stringWithUTF8String: name]
withTypeEncoding: [NSString stringWithUTF8String: typeEncoding]
isIBOutlet: isIBOutletFromPropertyOrIvar(classCursor)
inClass: [NSString stringWithUTF8String: className]];
break;
}
case CXCursor_ObjCPropertyDecl:
{
SCOPED_STR(name, clang_getCursorSpelling(classCursor));
SCOPED_STR(type, clang_getDeclObjCTypeEncoding(classCursor));
SCKSourceLocation *sourceLocation = [[SCKSourceLocation alloc]
initWithClangSourceLocation: clang_getCursorLocation(classCursor)];
CXObjCPropertyAttrKind attributes = 0;
#if CINDEX_VERSION >= 21
attributes = clang_Cursor_getObjCPropertyAttributes(classCursor, 0);
#endif
[self setLocation: sourceLocation
forProperty: [NSString stringWithUTF8String: name]
withTypeEncoding: [NSString stringWithUTF8String: type]
attributes: attributes
isIBOutlet: isIBOutletFromPropertyOrIvar(classCursor)
inClass: [NSString stringWithUTF8String: className]];
break;
}
case CXCursor_ObjCInstanceMethodDecl:
case CXCursor_ObjCClassMethodDecl:
{
SCOPED_STR(name, clang_getCursorSpelling(classCursor));
SCOPED_STR(type, clang_getDeclObjCTypeEncoding(classCursor));
SCKSourceLocation *sourceLocation = [[SCKSourceLocation alloc]
initWithClangSourceLocation: clang_getCursorLocation(classCursor)];
[self setLocation: sourceLocation
forMethod: [NSString stringWithUTF8String: name]
withTypeEncoding: [NSString stringWithUTF8String: type]
isClassMethod: (classCursor.kind == CXCursor_ObjCClassMethodDecl)
isDefinition: clang_isCursorDefinition(classCursor)
inClass: [NSString stringWithUTF8String: className]
category: nil];
break;
}
default:
break;
}
return CXChildVisit_Continue;
});
/* We must visit the class cursor children to know whether
CXCursor_ObjCInterfaceDecl refers to a @interface or
@class declaration, and also to get the superclass and
protocol references. */
[self setLocation: classLoc
forClass: [NSString stringWithUTF8String: className]
withSuperclass: superclassName
isDefinition: clang_isCursorDefinition(cursor)
isForwardDeclaration: isForwardDeclaration];
break;
}
case CXCursor_ObjCImplementationDecl:
{
SCKSourceLocation *classLoc = [[SCKSourceLocation alloc]
initWithClangSourceLocation: clang_getCursorLocation(cursor)];
SCOPED_STR(className, clang_getCursorSpelling(cursor));
[self setLocation: classLoc
forClass: [NSString stringWithUTF8String: className]
withSuperclass: nil
isDefinition: clang_isCursorDefinition(cursor)
isForwardDeclaration: NO];
clang_visitChildrenWithBlock(cursor,
^ enum CXChildVisitResult (CXCursor classCursor, CXCursor parent)
{
SCOPED_STR(kind, clang_getCursorKindSpelling(clang_getCursorKind(classCursor)));
if (CXCursor_ObjCInstanceMethodDecl == classCursor.kind
|| CXCursor_ObjCClassMethodDecl == classCursor.kind)
{
SCOPED_STR(methodName, clang_getCursorSpelling(classCursor));
SCOPED_STR(type, clang_getDeclObjCTypeEncoding(classCursor));
SCOPED_STR(className, clang_getCursorSpelling(parent));
SCKSourceLocation *l = [[SCKSourceLocation alloc]
initWithClangSourceLocation: clang_getCursorLocation(classCursor)];
[self setLocation: l
forMethod: [NSString stringWithUTF8String: methodName]
withTypeEncoding: [NSString stringWithUTF8String: type]
isClassMethod: (classCursor.kind == CXCursor_ObjCClassMethodDecl)
isDefinition: clang_isCursorDefinition(classCursor)
inClass: [NSString stringWithUTF8String: className]
category: nil];
}
return CXChildVisit_Continue;
});
break;
}
case CXCursor_ObjCCategoryDecl:
case CXCursor_ObjCCategoryImplDecl:
{
SCKSourceLocation *categoryLoc = [[SCKSourceLocation alloc]
initWithClangSourceLocation: clang_getCursorLocation(cursor)];
SCOPED_STR(categoryName, clang_getCursorSpelling(cursor));
NSString *className = classNameFromCategory(cursor);
[self setLocation: categoryLoc
forCategory: [NSString stringWithUTF8String: categoryName]
isDefinition: clang_isCursorDefinition(cursor)
ofClass: className];
clang_visitChildrenWithBlock(cursor,
^ enum CXChildVisitResult (CXCursor categoryCursor, CXCursor parent)
{
switch (categoryCursor.kind)
{
case CXCursor_ObjCInstanceMethodDecl:
case CXCursor_ObjCClassMethodDecl:
{
SCKSourceLocation *sourceLocation = [[SCKSourceLocation alloc]
initWithClangSourceLocation: clang_getCursorLocation(categoryCursor)];
SCOPED_STR(name, clang_getCursorSpelling(categoryCursor));
SCOPED_STR(type, clang_getDeclObjCTypeEncoding(categoryCursor));
[self setLocation: sourceLocation
forMethod: [NSString stringWithUTF8String: name]
withTypeEncoding: [NSString stringWithUTF8String: type]
isClassMethod: (CXCursor_ObjCClassMethodDecl == categoryCursor.kind)
isDefinition: clang_isCursorDefinition(cursor)
inClass: className
category: [NSString stringWithUTF8String: categoryName]];
break;
}
case CXCursor_ObjCDynamicDecl:
case CXCursor_ObjCPropertyDecl:
{
SCKSourceLocation *sourceLocation = [[SCKSourceLocation alloc]
initWithClangSourceLocation: clang_getCursorLocation(categoryCursor)];
SCOPED_STR(name, clang_getCursorSpelling(categoryCursor));
SCOPED_STR(type, clang_getDeclObjCTypeEncoding(categoryCursor));
CXObjCPropertyAttrKind attributes = 0;
#if CINDEX_VERSION >= 21
attributes = clang_Cursor_getObjCPropertyAttributes(categoryCursor, 0);
#endif
[self setLocation: sourceLocation
forProperty: [NSString stringWithUTF8String: name]
withTypeEncoding: [NSString stringWithUTF8String: type]
attributes: attributes
isDefinition: clang_isCursorDefinition(cursor)
inClass: className
category: [NSString stringWithUTF8String: categoryName]];
break;
}
default:
break;
}
return CXChildVisit_Continue;
});
break;
}
case CXCursor_ObjCProtocolDecl:
{
BOOL isForwardDecl = (clang_equalCursors(cursor, clang_getCursorDefinition(cursor)) == 0);
SCOPED_STR(protocolName, clang_getCursorSpelling(cursor));
SCKSourceLocation *sourceLocation = [[SCKSourceLocation alloc]
initWithClangSourceLocation: clang_getCursorLocation(cursor)];
// NOTE: We could use CXCursor_ObjCProtocolDecl to parse protocol
// forward declarations as we do with CXCursor_ObjCClassDecl
[self setLocation: sourceLocation
forProtocol: [NSString stringWithUTF8String: protocolName]
isForwardDeclaration: (clang_isCursorDefinition(cursor) == NO)];
clang_visitChildrenWithBlock(cursor,
^enum CXChildVisitResult(CXCursor protocolCursor, CXCursor parent)
{
SCOPED_STR(name, clang_getCursorSpelling(protocolCursor));
SCOPED_STR(type, clang_getDeclObjCTypeEncoding(protocolCursor));
SCKSourceLocation *location = [[SCKSourceLocation alloc]
initWithClangSourceLocation: clang_getCursorLocation(protocolCursor)];
BOOL isRequired = YES;
switch (protocolCursor.kind)
{
case CXCursor_ObjCPropertyDecl:
{
CXObjCPropertyAttrKind attributes = 0;
#if CINDEX_VERSION >= 21
attributes = clang_Cursor_getObjCPropertyAttributes(protocolCursor, 0);
isRequired = (clang_Cursor_isObjCOptional(protocolCursor) == 0);
#else
#warning Your libclang does not support checking for optional property declarations
#endif
[self setLocation: location
forProperty: [NSString stringWithUTF8String: name]
withTypeEncoding: [NSString stringWithUTF8String: type]
attributes: attributes
isIBOutlet: (CXCursor_IBOutletAttr == protocolCursor.kind)
isRequired: isRequired
inProtocol: [NSString stringWithUTF8String: protocolName]];
break;
}
case CXCursor_ObjCInstanceMethodDecl:
case CXCursor_ObjCClassMethodDecl:
{
#if CINDEX_VERSION >= 21
isRequired = (clang_Cursor_isObjCOptional(protocolCursor) == 0);
#else
#warning Your libclang does not support checking for optional method declarations
#endif
[self setLocation: location
forMethod: [NSString stringWithUTF8String: name]
withTypeEncoding: [NSString stringWithUTF8String: type]
isClassMethod: (CXCursor_ObjCClassMethodDecl == protocolCursor.kind)
isRequired: isRequired
isDefinition: clang_isCursorDefinition(protocolCursor)
inProtocol: [NSString stringWithUTF8String: protocolName]];
break;
}
default:
break;
}
return CXChildVisit_Recurse;
});
break;
}
case CXCursor_FunctionDecl:
{
SCOPED_STR(name, clang_getCursorSpelling(cursor));
SCOPED_STR(type, clang_getDeclObjCTypeEncoding(cursor));
STACK_SCOPED SCKSourceLocation *l = [[SCKSourceLocation alloc]
initWithClangSourceLocation: clang_getCursorLocation(cursor)];
enum CXLinkageKind linkage = clang_getCursorLinkage(cursor);
ETAssert(linkage != CXLinkage_NoLinkage);
if (linkage == CXLinkage_Invalid)
{
NSLog(@"WARNING: no linkage infos for function %s in %@", name, self);
break;
}
[self setLocation: l
forFunction: [NSString stringWithUTF8String: name]
withTypeEncoding: [NSString stringWithUTF8String: type]
isStatic: (linkage == CXLinkage_Internal)
isDefinition: clang_isCursorDefinition(cursor)];
break;
}
case CXCursor_VarDecl:
{
enum CXLinkageKind linkage = clang_getCursorLinkage(cursor);
ETAssert(linkage != CXLinkage_NoLinkage);
// 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:
{
SCOPED_STR(macroName, clang_getCursorSpelling(cursor));
SCKSourceLocation *sourceLocation = [[SCKSourceLocation alloc]
initWithClangSourceLocation:clang_getCursorLocation(cursor)];
[self setLocation: sourceLocation
forMacro: [NSString stringWithUTF8String: macroName]];
break;
}
case CXCursor_EnumDecl:
{
SCOPED_STR(enumName, clang_getCursorSpelling(cursor));
NSString *name = [NSString stringWithUTF8String: enumName];
SCKEnumeration *e = [enumerations objectForKey: name];
__block BOOL foundType;
if (e == nil)
{
e = [SCKEnumeration new];
foundType = NO;
e.name = name;
e.declaration = [[SCKSourceLocation alloc] initWithClangSourceLocation: clang_getCursorLocation(cursor)];
[enumerations setObject: e forKey: name];
[[self collection] addEnumeration: e];
}
else
{
foundType = e.typeEncoding != nil;
}
clang_visitChildrenWithBlock(cursor,
^ enum CXChildVisitResult (CXCursor enumCursor, CXCursor parent)
{
if (enumCursor.kind == CXCursor_EnumConstantDecl)
{
if (!foundType)
{
SCOPED_STR(type, clang_getDeclObjCTypeEncoding(enumCursor));
foundType = YES;
e.typeEncoding = [NSString stringWithUTF8String: type];
}
SCOPED_STR(valName, clang_getCursorSpelling(enumCursor));
NSString *vName = [NSString stringWithUTF8String: valName];
SCKEnumerationValue *v = [e.values objectForKey: vName];
if (nil == v)
{
v = [SCKEnumerationValue new];