-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathMPWClangImporter.m
327 lines (271 loc) · 11.3 KB
/
MPWClangImporter.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
//
// MPWClangImporter.m
// MPWCSourceKit
//
// Created by Marcel Weiher on 7/23/18.
//
#import "MPWClangImporter.h"
#include "clang-c/Index.h"
#include "clang-c/CXCompilationDatabase.h"
@interface MPWClangImporter()
@property (nonatomic, strong) NSMutableDictionary *globals;
@property (nonatomic, strong) NSMutableDictionary *enumValues;
@end
@implementation MPWClangImporter
{
CXIndex clangIndex;
CXTranslationUnit translationUnit;
CXFile file;
}
-(instancetype)init
{
self=[super init];
self.globals=[NSMutableDictionary dictionary];
self.enumValues=[NSMutableDictionary dictionary];
return self;
}
+(instancetype)importer
{
return [[[self alloc] init] autorelease];
}
-(void)initializeTranslationUnit:(nullable NSString*)filename
{
const char *argv[]={
"-F","/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks","-framework","AppKit",
// "-F","/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks", "-framework","UIKit",
NULL};
int argc=2;
const char *mainFile = "/tmp/hi.m";
// NSString *syntheticMainfile=[NSString stringWithFormat:@"#define nullable \n#define nonnull \n#import <Foundation/Foundation.h>\n"];
// NSString *syntheticMainfile=[NSString stringWithFormat:@"#import <UIKit/UIKit.h>\n"];
NSString *syntheticMainfile=[NSString stringWithFormat:@"#import <AppKit/AppKit.h>\n"];
struct CXUnsavedFile unsaved[] = {
{mainFile, [syntheticMainfile UTF8String], [syntheticMainfile length]},
{NULL, NULL, 0}};
clangIndex = clang_createIndex(1, 1);
translationUnit =
clang_parseTranslationUnit(clangIndex, mainFile, argv, argc, unsaved, 1,
clang_defaultEditingTranslationUnitOptions());
}
-(NSString*)cxstringToNSString:(CXString)cxname
{
const char *cstrname=clang_getCString(cxname);
NSString *s=[NSString stringWithUTF8String:cstrname];
return s;
}
-(NSString*)nameAtCursor:(CXCursor)aCursor
{
return [self cxstringToNSString:clang_getCursorSpelling(aCursor)];
}
-(NSString*)typeAtCursor:(CXCursor)aCursor
{
return [self cxstringToNSString:clang_getDeclObjCTypeEncoding(aCursor)];
}
-(long long)enumValueAtCursor:(CXCursor)aCursor
{
return clang_getEnumConstantDeclValue(aCursor);
}
-(void)handleBlockArgumentsAndReturnTypes:(CXCursor)classCursor
{
return ;
printf(" block objc types %s\n",[[self typeAtCursor:classCursor] UTF8String]);
clang_visitChildrenWithBlock(classCursor,
^ enum CXChildVisitResult (CXCursor blockArgCursor, CXCursor blockParent)
{
CXType argType=clang_getCursorType(blockArgCursor);
printf(" arg of block name=%s type=%s\n",
[[self nameAtCursor:blockArgCursor] UTF8String],
[[self cxstringToNSString:clang_getTypeSpelling(argType)] UTF8String]);
return CXChildVisit_Continue;
});
}
-(void)handleBlockArg:(CXCursor)argCursor
{
return ;
CXType blockType=clang_getCursorType(argCursor);
printf(" block name: %s type: %s\n",
[[self nameAtCursor:argCursor] UTF8String],
[[self cxstringToNSString:clang_getTypeSpelling(blockType)] UTF8String]);
clang_visitChildrenWithBlock(argCursor,
^ enum CXChildVisitResult (CXCursor blockCursor, CXCursor blockParent)
{
switch (blockCursor.kind) {
case CXCursor_ParmDecl:
{
[self handleBlockArgumentsAndReturnTypes:blockCursor];
}
case CXCursor_UnexposedAttr:
break;
case CXCursor_ObjCClassRef:
{
CXType argType=clang_getCursorType(blockCursor);
printf(" block arg classref name=%s type=%s\n",
[[self nameAtCursor:blockCursor] UTF8String],
[[self cxstringToNSString:clang_getTypeSpelling(argType)] UTF8String]);
break;
}
default:
printf(" unhandled in block: %d\n",blockCursor.kind);
}
return CXChildVisit_Continue;
});
}
-(void)handleArgumentsAndReturnTypes:(CXCursor)classCursor
{
// printf(" %s qualifiers: %x\n",[[self nameAtCursor:classCursor] UTF8String],clang_Cursor_getObjCDeclQualifiers(classCursor));
// printf(" objc types %s\n",[[self typeAtCursor:classCursor] UTF8String]);
int numArgs=clang_Cursor_getNumArguments(classCursor);
// printf(" %d arguments\n",numArgs);
for (int i=0;i<numArgs;i++) {
CXCursor argCursor=clang_Cursor_getArgument(classCursor,i);
CXType argType=clang_getCursorType(argCursor);
// printf(" arg[%d] name=%s type=%s\n",i,
// [[self nameAtCursor:argCursor] UTF8String],
// [[self cxstringToNSString:clang_getTypeSpelling(argType)] UTF8String]
// );
if (argType.kind==CXType_BlockPointer) {
[self handleBlockArg:argCursor];
}
}
}
-(void)handleInterface:(CXCursor)cursor
{
// printf("interface %s\n",[[self nameAtCursor:cursor] UTF8String]);
clang_visitChildrenWithBlock(cursor,
^ enum CXChildVisitResult (CXCursor classCursor, CXCursor parent)
{
switch ( classCursor.kind) {
case CXCursor_ObjCInstanceMethodDecl:
[self handleArgumentsAndReturnTypes:classCursor];
break;
case CXCursor_ObjCClassMethodDecl:
// printf(" class method %s\n",[[self nameAtCursor:classCursor] UTF8String]);
break;
case CXCursor_ObjCPropertyDecl:
// printf(" property decl %s\n",[[self nameAtCursor:classCursor] UTF8String]);
break;
case CXCursor_TemplateTypeParameter:
// printf(" generic %s\n",[[self nameAtCursor:classCursor] UTF8String]);
break;
default:
// printf(" unhandled in interface: %d\n",classCursor.kind);
break;
}
return CXChildVisit_Continue;
});
}
-(void)handleEnumConstant:(CXCursor)cursor
{
// printf("enum constant %s\n",[[self nameAtCursor:cursor] UTF8String]);
clang_visitChildrenWithBlock(cursor,
^ enum CXChildVisitResult (CXCursor enumCursor, CXCursor parent)
{
switch ( enumCursor.kind) {
case CXCursor_EnumConstantDecl:
[self enumCase:[self nameAtCursor:enumCursor] value:(long)[self enumValueAtCursor:enumCursor]];
break;
case CXCursor_FirstAttr:
// printf(" first attr in enum constant %s\n",[[self nameAtCursor:enumCursor] UTF8String]);
break;
default:
// printf(" unhandled in enum constant: %d\n",enumCursor.kind);
break;
}
return CXChildVisit_Continue;
});
}
-(void)handleEnum:(CXCursor)cursor
{
clang_visitChildrenWithBlock(cursor,
^ enum CXChildVisitResult (CXCursor classCursor, CXCursor parent)
{
switch ( classCursor.kind) {
case CXCursor_EnumConstantDecl:
[self handleEnumConstant:cursor];
// printf(" enum constant %s\n",[[self nameAtCursor:cursor] UTF8String]);
break;
case CXCursor_FirstAttr:
// printf(" first attr in enum %s\n",[[self nameAtCursor:cursor] UTF8String]);
break;
default:
// printf(" unhandled in enum: %d\n",classCursor.kind);
break;
}
return CXChildVisit_Continue;
});
}
-parseAFile
{
[self initializeTranslationUnit:nil];
clang_visitChildrenWithBlock(clang_getTranslationUnitCursor(translationUnit),
^ enum CXChildVisitResult (CXCursor cursor, CXCursor parent)
{
switch ( cursor.kind) {
case CXCursor_TranslationUnit:
// printf("translation unit %s\n",[[self nameAtCursor:cursor] UTF8String]);
break;
case CXCursor_ObjCProtocolDecl:
/// printf("protocol declaration %s\n",[[self nameAtCursor:cursor] UTF8String]);
break;
case CXCursor_StructDecl:
// printf("struct/union %s\n",[[self nameAtCursor:cursor] UTF8String]);
break;
case CXCursor_ObjCInterfaceDecl:
[self handleInterface:cursor];
break;
case CXCursor_ObjCCategoryDecl:
// printf("category %s\n",[[self nameAtCursor:cursor] UTF8String]);
break;
case CXCursor_ObjCProtocolRef:
// printf("@protocol %s\n",[[self nameAtCursor:cursor] UTF8String]);
break;
case CXCursor_ObjCClassRef:
// printf("@class %s\n",[[self nameAtCursor:cursor] UTF8String]);
break;
case CXCursor_FunctionDecl:
// printf("function declaration\n");
break;
case CXCursor_EnumDecl:
[self handleEnum:cursor];
break;
case CXCursor_VarDecl:
[self globalVariable:[self nameAtCursor:cursor] type:[self typeAtCursor:cursor]];
break;
case CXCursor_TypedefDecl:
// printf("typedef: %s\n",[[self nameAtCursor:cursor] UTF8String]);
break;
default:
// printf("unhandled: %d\n",cursor.kind);
;
}
return CXChildVisit_Continue;
});
[[NSJSONSerialization dataWithJSONObject:self.enumValues options:0 error:nil] writeToFile:@"/tmp/enums.json" atomically:YES];
[[NSJSONSerialization dataWithJSONObject:self.globals options:0 error:nil] writeToFile:@"/tmp/globals.json" atomically:YES];
return nil;
}
-(void)globalVariable:(NSString*)name type:(NSString*)type
{
// printf("global variable: %s type %s\n",[name UTF8String],[type UTF8String]);
self.globals[name]=type;
}
-(void)enumCase:(NSString*)name value:(long)value
{
self.enumValues[name]=@(value);
// printf("%s = %ld\n",[name UTF8String],value);
}
@end
#import <MPWFoundation/DebugMacros.h>
@implementation MPWClangImporter(testing)
+(void)testGetInfoFromHeader
{
MPWClangImporter *importer=[self importer];
[importer parseAFile];
}
+testSelectors
{
return @[
@"testGetInfoFromHeader",
];
}
@end