authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-11-28 02:37:00-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-11-28 02:58:51-05:00
loge745544dacc5bda010fc65e5c8b81cb3b5249223
tree9e0a7501730d34fd8fe0dafdadb172733008e162
parentf537c51f2560564042da690e8fe3b6e85d9592c8

translate-c: detect macros referencing field lookup

as fn calls which assert the fn ptr is non-null

3 files changed, 91 insertions(+), 44 deletions(-)

src/c_tokenizer.cpp+1
......@@ -120,6 +120,7 @@ static void begin_token(CTokenize *ctok, CTokId id) {
120120 case CTokIdLParen:
121121 case CTokIdRParen:
122122 case CTokIdEOF:
123 case CTokIdDot:
123124 break;
124125 }
125126}
src/translate_c.cpp+87-43
......@@ -23,11 +23,6 @@
2323
2424using namespace clang;
2525
26struct MacroSymbol {
27 Buf *name;
28 Buf *value;
29};
30
3126struct Alias {
3227 Buf *new_name;
3328 Buf *canon_name;
......@@ -44,7 +39,6 @@ struct Context {
4439 HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> global_table;
4540 SourceManager *source_manager;
4641 ZigList<Alias> aliases;
47 ZigList<MacroSymbol> macro_symbols;
4842 AstNode *source_node;
4943 bool warnings_on;
5044
......@@ -351,8 +345,7 @@ static AstNode *trans_create_node_var_decl_local(Context *c, bool is_const, Buf
351345 return trans_create_node_var_decl(c, VisibModPrivate, is_const, var_name, type_node, init_node);
352346}
353347
354
355static AstNode *trans_create_node_inline_fn(Context *c, Buf *fn_name, Buf *var_name, AstNode *src_proto_node) {
348static AstNode *trans_create_node_inline_fn(Context *c, Buf *fn_name, AstNode *ref_node, AstNode *src_proto_node) {
356349 AstNode *fn_def = trans_create_node(c, NodeTypeFnDef);
357350 AstNode *fn_proto = trans_create_node(c, NodeTypeFnProto);
358351 fn_proto->data.fn_proto.visib_mod = c->visib_mod;
......@@ -363,7 +356,7 @@ static AstNode *trans_create_node_inline_fn(Context *c, Buf *fn_name, Buf *var_n
363356 fn_def->data.fn_def.fn_proto = fn_proto;
364357 fn_proto->data.fn_proto.fn_def_node = fn_def;
365358
366 AstNode *unwrap_node = trans_create_node_prefix_op(c, PrefixOpUnwrapMaybe, trans_create_node_symbol(c, var_name));
359 AstNode *unwrap_node = trans_create_node_prefix_op(c, PrefixOpUnwrapMaybe, ref_node);
367360 AstNode *fn_call_node = trans_create_node(c, NodeTypeFnCallExpr);
368361 fn_call_node->data.fn_call_expr.fn_ref_expr = unwrap_node;
369362
......@@ -3808,6 +3801,83 @@ static void render_aliases(Context *c) {
38083801 }
38093802}
38103803
3804static AstNode *trans_lookup_ast_container_typeof(Context *c, AstNode *ref_node);
3805
3806static AstNode *trans_lookup_ast_container(Context *c, AstNode *type_node) {
3807 if (type_node == nullptr) {
3808 return nullptr;
3809 } else if (type_node->type == NodeTypeContainerDecl) {
3810 return type_node;
3811 } else if (type_node->type == NodeTypePrefixOpExpr) {
3812 return type_node;
3813 } else if (type_node->type == NodeTypeSymbol) {
3814 AstNode *existing_node = get_global(c, type_node->data.symbol_expr.symbol);
3815 if (existing_node == nullptr)
3816 return nullptr;
3817 if (existing_node->type != NodeTypeVariableDeclaration)
3818 return nullptr;
3819 return trans_lookup_ast_container(c, existing_node->data.variable_declaration.expr);
3820 } else if (type_node->type == NodeTypeFieldAccessExpr) {
3821 AstNode *container_node = trans_lookup_ast_container_typeof(c, type_node->data.field_access_expr.struct_expr);
3822 if (container_node == nullptr)
3823 return nullptr;
3824 if (container_node->type != NodeTypeContainerDecl)
3825 return container_node;
3826
3827 for (size_t i = 0; i < container_node->data.container_decl.fields.length; i += 1) {
3828 AstNode *field_node = container_node->data.container_decl.fields.items[i];
3829 if (buf_eql_buf(field_node->data.struct_field.name, type_node->data.field_access_expr.field_name)) {
3830 return trans_lookup_ast_container(c, field_node->data.struct_field.type);
3831 }
3832 }
3833 return nullptr;
3834 } else {
3835 return nullptr;
3836 }
3837}
3838
3839static AstNode *trans_lookup_ast_container_typeof(Context *c, AstNode *ref_node) {
3840 if (ref_node->type == NodeTypeSymbol) {
3841 AstNode *existing_node = get_global(c, ref_node->data.symbol_expr.symbol);
3842 if (existing_node == nullptr)
3843 return nullptr;
3844 if (existing_node->type != NodeTypeVariableDeclaration)
3845 return nullptr;
3846 return trans_lookup_ast_container(c, existing_node->data.variable_declaration.type);
3847 } else if (ref_node->type == NodeTypeFieldAccessExpr) {
3848 AstNode *container_node = trans_lookup_ast_container_typeof(c, ref_node->data.field_access_expr.struct_expr);
3849 if (container_node == nullptr)
3850 return nullptr;
3851 if (container_node->type != NodeTypeContainerDecl)
3852 return container_node;
3853 for (size_t i = 0; i < container_node->data.container_decl.fields.length; i += 1) {
3854 AstNode *field_node = container_node->data.container_decl.fields.items[i];
3855 if (buf_eql_buf(field_node->data.struct_field.name, ref_node->data.field_access_expr.field_name)) {
3856 return trans_lookup_ast_container(c, field_node->data.struct_field.type);
3857 }
3858 }
3859 return nullptr;
3860 } else {
3861 return nullptr;
3862 }
3863}
3864
3865static AstNode *trans_lookup_ast_maybe_fn(Context *c, AstNode *ref_node) {
3866 AstNode *prefix_node = trans_lookup_ast_container_typeof(c, ref_node);
3867 if (prefix_node == nullptr)
3868 return nullptr;
3869 if (prefix_node->type != NodeTypePrefixOpExpr)
3870 return nullptr;
3871 if (prefix_node->data.prefix_op_expr.prefix_op != PrefixOpMaybe)
3872 return nullptr;
3873
3874 AstNode *fn_proto_node = prefix_node->data.prefix_op_expr.primary_expr;
3875 if (fn_proto_node->type != NodeTypeFnProto)
3876 return nullptr;
3877
3878 return fn_proto_node;
3879}
3880
38113881static void render_macros(Context *c) {
38123882 auto it = c->macro_table.entry_iterator();
38133883 for (;;) {
......@@ -3815,9 +3885,16 @@ static void render_macros(Context *c) {
38153885 if (!entry)
38163886 break;
38173887
3888 AstNode *proto_node;
38183889 AstNode *value_node = entry->value;
38193890 if (value_node->type == NodeTypeFnDef) {
38203891 add_top_level_decl(c, value_node->data.fn_def.fn_proto->data.fn_proto.name, value_node);
3892 } else if ((proto_node = trans_lookup_ast_maybe_fn(c, value_node))) {
3893 // If a macro aliases a global variable which is a function pointer, we conclude that
3894 // the macro is intended to represent a function that assumes the function pointer
3895 // variable is non-null and calls it.
3896 AstNode *inline_fn_node = trans_create_node_inline_fn(c, entry->key, value_node, proto_node);
3897 add_top_level_decl(c, entry->key, inline_fn_node);
38213898 } else {
38223899 add_global_var(c, entry->key, value_node);
38233900 }
......@@ -3944,40 +4021,8 @@ static void process_macro(Context *c, CTokenize *ctok, Buf *name, const char *ch
39444021 if (buf_eql_buf(name, symbol_name)) {
39454022 return;
39464023 }
3947 c->macro_symbols.append({name, symbol_name});
3948 } else {
3949 c->macro_table.put(name, result_node);
3950 }
3951}
3952
3953static void process_symbol_macros(Context *c) {
3954 for (size_t i = 0; i < c->macro_symbols.length; i += 1) {
3955 MacroSymbol ms = c->macro_symbols.at(i);
3956
3957 // Check if this macro aliases another top level declaration
3958 AstNode *existing_node = get_global(c, ms.value);
3959 if (!existing_node || name_exists_global(c, ms.name))
3960 continue;
3961
3962 // If a macro aliases a global variable which is a function pointer, we conclude that
3963 // the macro is intended to represent a function that assumes the function pointer
3964 // variable is non-null and calls it.
3965 if (existing_node->type == NodeTypeVariableDeclaration) {
3966 AstNode *var_type = existing_node->data.variable_declaration.type;
3967 if (var_type != nullptr && var_type->type == NodeTypePrefixOpExpr &&
3968 var_type->data.prefix_op_expr.prefix_op == PrefixOpMaybe)
3969 {
3970 AstNode *fn_proto_node = var_type->data.prefix_op_expr.primary_expr;
3971 if (fn_proto_node->type == NodeTypeFnProto) {
3972 AstNode *inline_fn_node = trans_create_node_inline_fn(c, ms.name, ms.value, fn_proto_node);
3973 c->macro_table.put(ms.name, inline_fn_node);
3974 continue;
3975 }
3976 }
3977 }
3978
3979 add_global_var(c, ms.name, trans_create_node_symbol(c, ms.value));
39804024 }
4025 c->macro_table.put(name, result_node);
39814026}
39824027
39834028static void process_preprocessor_entities(Context *c, ASTUnit &unit) {
......@@ -4194,7 +4239,6 @@ int parse_h_file(ImportTableEntry *import, ZigList<ErrorMsg *> *errors, const ch
41944239
41954240 process_preprocessor_entities(c, *ast_unit);
41964241
4197 process_symbol_macros(c);
41984242 render_macros(c);
41994243 render_aliases(c);
42004244
test/translate_c.zig+3-1
......@@ -1078,7 +1078,9 @@ pub fn addCases(cases: &tests.TranslateCContext) {
10781078 ,
10791079 \\pub const glClearPFN = PFNGLCLEARPROC;
10801080 ,
1081 \\pub const glClearUnion = glProcs.gl.Clear;
1081 \\pub inline fn glClearUnion(arg0: GLbitfield) {
1082 \\ (??glProcs.gl.Clear)(arg0)
1083 \\}
10821084 ,
10831085 \\pub const OpenGLProcs = union_OpenGLProcs;
10841086 );