| ... | @@ -17,6 +17,7 @@ struct Fn { | ... | @@ -17,6 +17,7 @@ struct Fn { |
| 17 | }; | 17 | }; |
| 18 | | 18 | |
| 19 | struct ParseH { | 19 | struct ParseH { |
| | 20 | CXTranslationUnit tu; |
| 20 | FILE *f; | 21 | FILE *f; |
| 21 | ZigList<Fn *> fn_list; | 22 | ZigList<Fn *> fn_list; |
| 22 | Fn *cur_fn; | 23 | Fn *cur_fn; |
| ... | @@ -243,18 +244,26 @@ static bool is_storage_class_export(CX_StorageClass storage_class) { | ... | @@ -243,18 +244,26 @@ static bool is_storage_class_export(CX_StorageClass storage_class) { |
| 243 | zig_unreachable(); | 244 | zig_unreachable(); |
| 244 | } | 245 | } |
| 245 | | 246 | |
| 246 | static void begin_fn(ParseH *p) { | 247 | static enum CXChildVisitResult visit_fn_children(CXCursor cursor, CXCursor parent, CXClientData client_data) { |
| 247 | assert(!p->cur_fn); | 248 | ParseH *p = (ParseH*)client_data; |
| 248 | p->cur_fn = allocate<Fn>(1); | 249 | enum CXCursorKind kind = clang_getCursorKind(cursor); |
| 249 | } | | |
| 250 | | 250 | |
| 251 | static void end_fn(ParseH *p) { | 251 | switch (kind) { |
| 252 | if (p->cur_fn) { | 252 | case CXCursor_ParmDecl: |
| 253 | p->fn_list.append(p->cur_fn); | 253 | { |
| 254 | p->cur_fn = nullptr; | 254 | assert(p->cur_fn); |
| | 255 | assert(p->arg_index < p->cur_fn->arg_count); |
| | 256 | CXString name = clang_getCursorSpelling(cursor); |
| | 257 | buf_init_from_str(&p->cur_fn->args[p->arg_index].name, clang_getCString(name)); |
| | 258 | p->arg_index += 1; |
| | 259 | return CXChildVisit_Continue; |
| | 260 | } |
| | 261 | default: |
| | 262 | return CXChildVisit_Recurse; |
| 255 | } | 263 | } |
| 256 | } | 264 | } |
| 257 | | 265 | |
| | 266 | |
| 258 | static enum CXChildVisitResult fn_visitor(CXCursor cursor, CXCursor parent, CXClientData client_data) { | 267 | static enum CXChildVisitResult fn_visitor(CXCursor cursor, CXCursor parent, CXClientData client_data) { |
| 259 | ParseH *p = (ParseH*)client_data; | 268 | ParseH *p = (ParseH*)client_data; |
| 260 | enum CXCursorKind kind = clang_getCursorKind(cursor); | 269 | enum CXCursorKind kind = clang_getCursorKind(cursor); |
| ... | @@ -282,8 +291,8 @@ static enum CXChildVisitResult fn_visitor(CXCursor cursor, CXCursor parent, CXCl | ... | @@ -282,8 +291,8 @@ static enum CXChildVisitResult fn_visitor(CXCursor cursor, CXCursor parent, CXCl |
| 282 | return CXChildVisit_Continue; | 291 | return CXChildVisit_Continue; |
| 283 | } | 292 | } |
| 284 | | 293 | |
| 285 | end_fn(p); | 294 | assert(!p->cur_fn); |
| 286 | begin_fn(p); | 295 | p->cur_fn = allocate<Fn>(1); |
| 287 | | 296 | |
| 288 | CXType return_type = clang_getResultType(fn_type); | 297 | CXType return_type = clang_getResultType(fn_type); |
| 289 | p->cur_fn->return_type = to_zig_type(p, return_type); | 298 | p->cur_fn->return_type = to_zig_type(p, return_type); |
| ... | @@ -300,17 +309,13 @@ static enum CXChildVisitResult fn_visitor(CXCursor cursor, CXCursor parent, CXCl | ... | @@ -300,17 +309,13 @@ static enum CXChildVisitResult fn_visitor(CXCursor cursor, CXCursor parent, CXCl |
| 300 | | 309 | |
| 301 | p->arg_index = 0; | 310 | p->arg_index = 0; |
| 302 | | 311 | |
| | 312 | clang_visitChildren(cursor, visit_fn_children, p); |
| | 313 | |
| | 314 | p->fn_list.append(p->cur_fn); |
| | 315 | p->cur_fn = nullptr; |
| | 316 | |
| 303 | return CXChildVisit_Recurse; | 317 | return CXChildVisit_Recurse; |
| 304 | } | 318 | } |
| 305 | case CXCursor_ParmDecl: | | |
| 306 | { | | |
| 307 | assert(p->cur_fn); | | |
| 308 | assert(p->arg_index < p->cur_fn->arg_count); | | |
| 309 | buf_init_from_str(&p->cur_fn->args[p->arg_index].name, clang_getCString(name)); | | |
| 310 | p->arg_index += 1; | | |
| 311 | return CXChildVisit_Continue; | | |
| 312 | } | | |
| 313 | case CXCursor_UnexposedAttr: | | |
| 314 | case CXCursor_CompoundStmt: | 319 | case CXCursor_CompoundStmt: |
| 315 | case CXCursor_FieldDecl: | 320 | case CXCursor_FieldDecl: |
| 316 | case CXCursor_TypedefDecl: | 321 | case CXCursor_TypedefDecl: |
| ... | @@ -330,7 +335,6 @@ void parse_h_file(const char *target_path, ZigList<const char *> *clang_argv, FI | ... | @@ -330,7 +335,6 @@ void parse_h_file(const char *target_path, ZigList<const char *> *clang_argv, FI |
| 330 | ParseH parse_h = {0}; | 335 | ParseH parse_h = {0}; |
| 331 | ParseH *p = &parse_h; | 336 | ParseH *p = &parse_h; |
| 332 | p->f = f; | 337 | p->f = f; |
| 333 | CXTranslationUnit tu; | | |
| 334 | CXIndex index = clang_createIndex(1, 0); | 338 | CXIndex index = clang_createIndex(1, 0); |
| 335 | | 339 | |
| 336 | char *ZIG_PARSEH_CFLAGS = getenv("ZIG_PARSEH_CFLAGS"); | 340 | char *ZIG_PARSEH_CFLAGS = getenv("ZIG_PARSEH_CFLAGS"); |
| ... | @@ -355,17 +359,17 @@ void parse_h_file(const char *target_path, ZigList<const char *> *clang_argv, FI | ... | @@ -355,17 +359,17 @@ void parse_h_file(const char *target_path, ZigList<const char *> *clang_argv, FI |
| 355 | enum CXErrorCode err_code; | 359 | enum CXErrorCode err_code; |
| 356 | if ((err_code = clang_parseTranslationUnit2(index, target_path, | 360 | if ((err_code = clang_parseTranslationUnit2(index, target_path, |
| 357 | clang_argv->items, clang_argv->length - 1, | 361 | clang_argv->items, clang_argv->length - 1, |
| 358 | NULL, 0, CXTranslationUnit_None, &tu))) | 362 | NULL, 0, CXTranslationUnit_None, &p->tu))) |
| 359 | { | 363 | { |
| 360 | zig_panic("parse translation unit failure"); | 364 | zig_panic("parse translation unit failure"); |
| 361 | } | 365 | } |
| 362 | | 366 | |
| 363 | | 367 | |
| 364 | unsigned diag_count = clang_getNumDiagnostics(tu); | 368 | unsigned diag_count = clang_getNumDiagnostics(p->tu); |
| 365 | | 369 | |
| 366 | if (diag_count > 0) { | 370 | if (diag_count > 0) { |
| 367 | for (unsigned i = 0; i < diag_count; i += 1) { | 371 | for (unsigned i = 0; i < diag_count; i += 1) { |
| 368 | CXDiagnostic diagnostic = clang_getDiagnostic(tu, i); | 372 | CXDiagnostic diagnostic = clang_getDiagnostic(p->tu, i); |
| 369 | CXSourceLocation location = clang_getDiagnosticLocation(diagnostic); | 373 | CXSourceLocation location = clang_getDiagnosticLocation(diagnostic); |
| 370 | | 374 | |
| 371 | CXFile file; | 375 | CXFile file; |
| ... | @@ -381,9 +385,8 @@ void parse_h_file(const char *target_path, ZigList<const char *> *clang_argv, FI | ... | @@ -381,9 +385,8 @@ void parse_h_file(const char *target_path, ZigList<const char *> *clang_argv, FI |
| 381 | } | 385 | } |
| 382 | | 386 | |
| 383 | | 387 | |
| 384 | CXCursor cursor = clang_getTranslationUnitCursor(tu); | 388 | CXCursor cursor = clang_getTranslationUnitCursor(p->tu); |
| 385 | clang_visitChildren(cursor, fn_visitor, p); | 389 | clang_visitChildren(cursor, fn_visitor, p); |
| 386 | end_fn(p); | | |
| 387 | | 390 | |
| 388 | if (p->fn_list.length) { | 391 | if (p->fn_list.length) { |
| 389 | fprintf(f, "extern {\n"); | 392 | fprintf(f, "extern {\n"); |