authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-12-04 11:45:11-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-12-04 11:45:11-07:00
logcd6283e8c442e6a456a325a4c974103a2a0d9e40
treedbdd14ab87aff273bda39e4631b9b303c6790a15
parent5144c4fa37c3c433d814c9c3b2182fa224ea6826

parseh: fix wrong fn parameters in some cases


2 files changed, 29 insertions(+), 26 deletions(-)

README.md+1-1
...@@ -42,7 +42,7 @@ make...@@ -42,7 +42,7 @@ make
4242
43## Roadmap43## Roadmap
4444
45 * unreachable <--> noreturn attribute45 * parseh: unreachable <--> noreturn attribute
46 * error for extern function with void parameter46 * error for extern function with void parameter
47 * unused label error47 * unused label error
48 * loops48 * loops
src/parseh.cpp+28-25
...@@ -17,6 +17,7 @@ struct Fn {...@@ -17,6 +17,7 @@ struct Fn {
17};17};
1818
19struct ParseH {19struct 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}
245246
246static void begin_fn(ParseH *p) {247static 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}
250250
251static 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}
257265
266
258static enum CXChildVisitResult fn_visitor(CXCursor cursor, CXCursor parent, CXClientData client_data) {267static 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 }
284293
285 end_fn(p);294 assert(!p->cur_fn);
286 begin_fn(p);295 p->cur_fn = allocate<Fn>(1);
287296
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
300309
301 p->arg_index = 0;310 p->arg_index = 0;
302311
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);
335339
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 }
362366
363367
364 unsigned diag_count = clang_getNumDiagnostics(tu);368 unsigned diag_count = clang_getNumDiagnostics(p->tu);
365369
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);
370374
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 }
382386
383387
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);
387390
388 if (p->fn_list.length) {391 if (p->fn_list.length) {
389 fprintf(f, "extern {\n");392 fprintf(f, "extern {\n");