authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-29 17:02:51-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-29 17:02:51-07:00
log436e35516ac997ec5fc0d769386d9b1128195b16
treeeeee1d4ccaf9fad2eb4843e1ed20c0c3b4d849cb
parente4b0435946ded78b2d4664bf4cf3cf387fe4a12e

parseh properly ignores anonymous structs

and nodes get valid create_index values

4 files changed, 37 insertions(+), 10 deletions(-)

src/analyze.cpp+1-1
...@@ -1111,7 +1111,7 @@ static void resolve_c_import_decl(CodeGen *g, ImportTableEntry *parent_import, A...@@ -1111,7 +1111,7 @@ static void resolve_c_import_decl(CodeGen *g, ImportTableEntry *parent_import, A
11111111
1112 int err;1112 int err;
1113 if ((err = parse_h_buf(child_import, &errors, child_context->c_import_buf, g->clang_argv, g->clang_argv_len,1113 if ((err = parse_h_buf(child_import, &errors, child_context->c_import_buf, g->clang_argv, g->clang_argv_len,
1114 buf_ptr(g->libc_include_path), false)))1114 buf_ptr(g->libc_include_path), false, &g->next_node_index)))
1115 {1115 {
1116 zig_panic("unable to parse h file: %s\n", err_str(err));1116 zig_panic("unable to parse h file: %s\n", err_str(err));
1117 }1117 }
src/main.cpp+2-1
...@@ -221,7 +221,8 @@ static int parseh(const char *arg0, int argc, char **argv) {...@@ -221,7 +221,8 @@ static int parseh(const char *arg0, int argc, char **argv) {
221221
222 ImportTableEntry import = {0};222 ImportTableEntry import = {0};
223 ZigList<ErrorMsg *> errors = {0};223 ZigList<ErrorMsg *> errors = {0};
224 int err = parse_h_file(&import, &errors, &clang_argv, warnings_on);224 uint32_t next_node_index = 0;
225 int err = parse_h_file(&import, &errors, &clang_argv, warnings_on, &next_node_index);
225226
226 if (err) {227 if (err) {
227 fprintf(stderr, "unable to parse .h file: %s\n", err_str(err));228 fprintf(stderr, "unable to parse .h file: %s\n", err_str(err));
src/parseh.cpp+32-6
...@@ -40,6 +40,7 @@ struct Context {...@@ -40,6 +40,7 @@ struct Context {
40 SourceManager *source_manager;40 SourceManager *source_manager;
41 ZigList<AstNode *> aliases;41 ZigList<AstNode *> aliases;
42 ZigList<MacroSymbol> macro_symbols;42 ZigList<MacroSymbol> macro_symbols;
43 uint32_t *next_node_index;
43};44};
4445
45static AstNode *make_qual_type_node(Context *c, QualType qt, const Decl *decl);46static AstNode *make_qual_type_node(Context *c, QualType qt, const Decl *decl);
...@@ -76,6 +77,8 @@ static AstNode *create_node(Context *c, NodeType type) {...@@ -76,6 +77,8 @@ static AstNode *create_node(Context *c, NodeType type) {
76 AstNode *node = allocate<AstNode>(1);77 AstNode *node = allocate<AstNode>(1);
77 node->type = type;78 node->type = type;
78 node->owner = c->import;79 node->owner = c->import;
80 node->create_index = *c->next_node_index;
81 *c->next_node_index += 1;
79 return node;82 return node;
80}83}
8184
...@@ -398,7 +401,10 @@ static AstNode *make_type_node(Context *c, const Type *ty, const Decl *decl,...@@ -398,7 +401,10 @@ static AstNode *make_type_node(Context *c, const Type *ty, const Decl *decl,
398 {401 {
399 const RecordType *record_ty = static_cast<const RecordType*>(ty);402 const RecordType *record_ty = static_cast<const RecordType*>(ty);
400 Buf *record_name = buf_create_from_str(decl_name(record_ty->getDecl()));403 Buf *record_name = buf_create_from_str(decl_name(record_ty->getDecl()));
401 if (type_table->maybe_get(record_name)) {404 if (buf_len(record_name) == 0) {
405 emit_warning(c, decl, "unhandled anonymous struct");
406 return nullptr;
407 } else if (type_table->maybe_get(record_name)) {
402 const char *prefix_str;408 const char *prefix_str;
403 if (type_table == &c->enum_type_table) {409 if (type_table == &c->enum_type_table) {
404 prefix_str = "enum_";410 prefix_str = "enum_";
...@@ -416,7 +422,10 @@ static AstNode *make_type_node(Context *c, const Type *ty, const Decl *decl,...@@ -416,7 +422,10 @@ static AstNode *make_type_node(Context *c, const Type *ty, const Decl *decl,
416 {422 {
417 const EnumType *enum_ty = static_cast<const EnumType*>(ty);423 const EnumType *enum_ty = static_cast<const EnumType*>(ty);
418 Buf *record_name = buf_create_from_str(decl_name(enum_ty->getDecl()));424 Buf *record_name = buf_create_from_str(decl_name(enum_ty->getDecl()));
419 if (type_table->maybe_get(record_name)) {425 if (buf_len(record_name) == 0) {
426 emit_warning(c, decl, "unhandled anonymous enum");
427 return nullptr;
428 } else if (type_table->maybe_get(record_name)) {
420 const char *prefix_str;429 const char *prefix_str;
421 if (type_table == &c->enum_type_table) {430 if (type_table == &c->enum_type_table) {
422 prefix_str = "enum_";431 prefix_str = "enum_";
...@@ -560,6 +569,10 @@ static void visit_typedef_decl(Context *c, const TypedefNameDecl *typedef_decl)...@@ -560,6 +569,10 @@ static void visit_typedef_decl(Context *c, const TypedefNameDecl *typedef_decl)
560 return;569 return;
561 }570 }
562571
572 // if the underlying type is anonymous, we can special case it to just
573 // use the name of this typedef
574 // TODO
575
563 add_typedef_node(c, type_name, make_qual_type_node(c, child_qt, typedef_decl));576 add_typedef_node(c, type_name, make_qual_type_node(c, child_qt, typedef_decl));
564}577}
565578
...@@ -650,13 +663,24 @@ static void visit_enum_decl(Context *c, const EnumDecl *enum_decl) {...@@ -650,13 +663,24 @@ static void visit_enum_decl(Context *c, const EnumDecl *enum_decl) {
650}663}
651664
652static void visit_record_decl(Context *c, const RecordDecl *record_decl) {665static void visit_record_decl(Context *c, const RecordDecl *record_decl) {
653 Buf *bare_name = buf_create_from_str(decl_name(record_decl));666 const char *raw_name = decl_name(record_decl);
667
668 if (record_decl->isAnonymousStructOrUnion() || raw_name[0] == 0) {
669 return;
670 }
671
672 Buf *bare_name = buf_create_from_str(raw_name);
654673
655 if (!record_decl->isStruct()) {674 if (!record_decl->isStruct()) {
656 emit_warning(c, record_decl, "skipping record %s, not a struct", buf_ptr(bare_name));675 emit_warning(c, record_decl, "skipping record %s, not a struct", buf_ptr(bare_name));
657 return;676 return;
658 }677 }
659678
679 if (buf_len(bare_name) == 0) {
680 emit_warning(c, record_decl, "skipping anonymous struct");
681 return;
682 }
683
660 Buf *full_type_name = buf_sprintf("struct_%s", buf_ptr(bare_name));684 Buf *full_type_name = buf_sprintf("struct_%s", buf_ptr(bare_name));
661685
662 if (c->struct_type_table.maybe_get(bare_name)) {686 if (c->struct_type_table.maybe_get(bare_name)) {
...@@ -1030,7 +1054,8 @@ static void process_preprocessor_entities(Context *c, ASTUnit &unit) {...@@ -1030,7 +1054,8 @@ static void process_preprocessor_entities(Context *c, ASTUnit &unit) {
1030}1054}
10311055
1032int parse_h_buf(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, Buf *source,1056int parse_h_buf(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, Buf *source,
1033 const char **args, int args_len, const char *libc_include_path, bool warnings_on)1057 const char **args, int args_len, const char *libc_include_path, bool warnings_on,
1058 uint32_t *next_node_index)
1034{1059{
1035 int err;1060 int err;
1036 Buf tmp_file_path = BUF_INIT;1061 Buf tmp_file_path = BUF_INIT;
...@@ -1047,7 +1072,7 @@ int parse_h_buf(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, Buf *sour...@@ -1047,7 +1072,7 @@ int parse_h_buf(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, Buf *sour
1047 clang_argv.append(args[i]);1072 clang_argv.append(args[i]);
1048 }1073 }
10491074
1050 err = parse_h_file(import, errors, &clang_argv, warnings_on);1075 err = parse_h_file(import, errors, &clang_argv, warnings_on, next_node_index);
10511076
1052 os_delete_file(&tmp_file_path);1077 os_delete_file(&tmp_file_path);
10531078
...@@ -1055,7 +1080,7 @@ int parse_h_buf(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, Buf *sour...@@ -1055,7 +1080,7 @@ int parse_h_buf(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, Buf *sour
1055}1080}
10561081
1057int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors,1082int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors,
1058 ZigList<const char *> *clang_argv, bool warnings_on)1083 ZigList<const char *> *clang_argv, bool warnings_on, uint32_t *next_node_index)
1059{1084{
1060 Context context = {0};1085 Context context = {0};
1061 Context *c = &context;1086 Context *c = &context;
...@@ -1068,6 +1093,7 @@ int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors,...@@ -1068,6 +1093,7 @@ int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors,
1068 c->struct_type_table.init(8);1093 c->struct_type_table.init(8);
1069 c->fn_table.init(8);1094 c->fn_table.init(8);
1070 c->macro_table.init(8);1095 c->macro_table.init(8);
1096 c->next_node_index = next_node_index;
10711097
1072 char *ZIG_PARSEH_CFLAGS = getenv("ZIG_PARSEH_CFLAGS");1098 char *ZIG_PARSEH_CFLAGS = getenv("ZIG_PARSEH_CFLAGS");
1073 if (ZIG_PARSEH_CFLAGS) {1099 if (ZIG_PARSEH_CFLAGS) {
src/parseh.hpp+2-2
...@@ -12,9 +12,9 @@...@@ -12,9 +12,9 @@
12#include "all_types.hpp"12#include "all_types.hpp"
1313
14int parse_h_file(ImportTableEntry *out_import, ZigList<ErrorMsg *> *out_errs,14int parse_h_file(ImportTableEntry *out_import, ZigList<ErrorMsg *> *out_errs,
15 ZigList<const char *> *clang_argv, bool warnings_on);15 ZigList<const char *> *clang_argv, bool warnings_on, uint32_t *next_node_index);
16int parse_h_buf(ImportTableEntry *out_import, ZigList<ErrorMsg *> *out_errs,16int parse_h_buf(ImportTableEntry *out_import, ZigList<ErrorMsg *> *out_errs,
17 Buf *source, const char **args, int args_len, const char *libc_include_path,17 Buf *source, const char **args, int args_len, const char *libc_include_path,
18 bool warnings_on);18 bool warnings_on, uint32_t *next_node_index);
1919
20#endif20#endif