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
4242
4343## Roadmap
4444
45 * unreachable <--> noreturn attribute
45 * parseh: unreachable <--> noreturn attribute
4646 * error for extern function with void parameter
4747 * unused label error
4848 * loops
src/parseh.cpp+28-25
......@@ -17,6 +17,7 @@ struct Fn {
1717};
1818
1919struct ParseH {
20 CXTranslationUnit tu;
2021 FILE *f;
2122 ZigList<Fn *> fn_list;
2223 Fn *cur_fn;
......@@ -243,18 +244,26 @@ static bool is_storage_class_export(CX_StorageClass storage_class) {
243244 zig_unreachable();
244245}
245246
246static void begin_fn(ParseH *p) {
247 assert(!p->cur_fn);
248 p->cur_fn = allocate<Fn>(1);
249}
247static enum CXChildVisitResult visit_fn_children(CXCursor cursor, CXCursor parent, CXClientData client_data) {
248 ParseH *p = (ParseH*)client_data;
249 enum CXCursorKind kind = clang_getCursorKind(cursor);
250250
251static void end_fn(ParseH *p) {
252 if (p->cur_fn) {
253 p->fn_list.append(p->cur_fn);
254 p->cur_fn = nullptr;
251 switch (kind) {
252 case CXCursor_ParmDecl:
253 {
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;
255263 }
256264}
257265
266
258267static enum CXChildVisitResult fn_visitor(CXCursor cursor, CXCursor parent, CXClientData client_data) {
259268 ParseH *p = (ParseH*)client_data;
260269 enum CXCursorKind kind = clang_getCursorKind(cursor);
......@@ -282,8 +291,8 @@ static enum CXChildVisitResult fn_visitor(CXCursor cursor, CXCursor parent, CXCl
282291 return CXChildVisit_Continue;
283292 }
284293
285 end_fn(p);
286 begin_fn(p);
294 assert(!p->cur_fn);
295 p->cur_fn = allocate<Fn>(1);
287296
288297 CXType return_type = clang_getResultType(fn_type);
289298 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
300309
301310 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
303317 return CXChildVisit_Recurse;
304318 }
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:
314319 case CXCursor_CompoundStmt:
315320 case CXCursor_FieldDecl:
316321 case CXCursor_TypedefDecl:
......@@ -330,7 +335,6 @@ void parse_h_file(const char *target_path, ZigList<const char *> *clang_argv, FI
330335 ParseH parse_h = {0};
331336 ParseH *p = &parse_h;
332337 p->f = f;
333 CXTranslationUnit tu;
334338 CXIndex index = clang_createIndex(1, 0);
335339
336340 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
355359 enum CXErrorCode err_code;
356360 if ((err_code = clang_parseTranslationUnit2(index, target_path,
357361 clang_argv->items, clang_argv->length - 1,
358 NULL, 0, CXTranslationUnit_None, &tu)))
362 NULL, 0, CXTranslationUnit_None, &p->tu)))
359363 {
360364 zig_panic("parse translation unit failure");
361365 }
362366
363367
364 unsigned diag_count = clang_getNumDiagnostics(tu);
368 unsigned diag_count = clang_getNumDiagnostics(p->tu);
365369
366370 if (diag_count > 0) {
367371 for (unsigned i = 0; i < diag_count; i += 1) {
368 CXDiagnostic diagnostic = clang_getDiagnostic(tu, i);
372 CXDiagnostic diagnostic = clang_getDiagnostic(p->tu, i);
369373 CXSourceLocation location = clang_getDiagnosticLocation(diagnostic);
370374
371375 CXFile file;
......@@ -381,9 +385,8 @@ void parse_h_file(const char *target_path, ZigList<const char *> *clang_argv, FI
381385 }
382386
383387
384 CXCursor cursor = clang_getTranslationUnitCursor(tu);
388 CXCursor cursor = clang_getTranslationUnitCursor(p->tu);
385389 clang_visitChildren(cursor, fn_visitor, p);
386 end_fn(p);
387390
388391 if (p->fn_list.length) {
389392 fprintf(f, "extern {\n");