diff options
| author | Veikka Tuominen <git@vexu.eu> | 2022-05-27 16:43:21 +0300 |
|---|---|---|
| committer | Veikka Tuominen <git@vexu.eu> | 2022-05-29 12:07:29 +0300 |
| commit | 0274e2f1fd3b7a81344080d532cfd2d384427cd2 (patch) | |
| tree | f2e882f35401602799e46b76990a9e9403e3c909 | |
| parent | 36b4658752257a27b1d7db5a4396132784801997 (diff) | |
| download | zig-0274e2f1fd3b7a81344080d532cfd2d384427cd2.tar.gz zig-0274e2f1fd3b7a81344080d532cfd2d384427cd2.zip | |
translate-c: check variable types being demoted to opaque
| -rw-r--r-- | src/translate_c.zig | 9 | ||||
| -rw-r--r-- | test/translate_c.zig | 16 |
2 files changed, 22 insertions, 3 deletions
diff --git a/src/translate_c.zig b/src/translate_c.zig index 06ccf53f63..3bbe1e6f46 100644 --- a/src/translate_c.zig +++ b/src/translate_c.zig @@ -793,6 +793,10 @@ fn visitVarDecl(c: *Context, var_decl: *const clang.VarDecl, mangled_name: ?[]co var is_extern = storage_class == .Extern and !has_init; var is_export = !is_extern and storage_class != .Static; + if (!is_extern and qualTypeWasDemotedToOpaque(c, qual_type)) { + return failDecl(c, var_decl_loc, var_name, "non-extern variable has opaque type", .{}); + } + const type_node = transQualTypeMaybeInitialized(c, scope, qual_type, decl_init, var_decl_loc) catch |err| switch (err) { error.UnsupportedTranslation, error.UnsupportedType => { return failDecl(c, var_decl_loc, var_name, "unable to resolve variable type", .{}); @@ -1839,6 +1843,7 @@ fn transDeclStmtOne( .Var => { const var_decl = @ptrCast(*const clang.VarDecl, decl); const decl_init = var_decl.getInit(); + const loc = decl.getLocation(); const qual_type = var_decl.getTypeSourceInfo_getType(); const name = try c.str(@ptrCast(*const clang.NamedDecl, var_decl).getName_bytes_begin()); @@ -1848,12 +1853,12 @@ fn transDeclStmtOne( // This is actually a global variable, put it in the global scope and reference it. // `_ = mangled_name;` return visitVarDecl(c, var_decl, mangled_name); + } else if (qualTypeWasDemotedToOpaque(c, qual_type)) { + return fail(c, error.UnsupportedTranslation, loc, "local variable has opaque type", .{}); } const is_static_local = var_decl.isStaticLocal(); const is_const = qual_type.isConstQualified(); - - const loc = decl.getLocation(); const type_node = try transQualTypeMaybeInitialized(c, scope, qual_type, decl_init, loc); var init_node = if (decl_init) |expr| diff --git a/test/translate_c.zig b/test/translate_c.zig index 590e52f2ae..f5748f0659 100644 --- a/test/translate_c.zig +++ b/test/translate_c.zig @@ -6,6 +6,20 @@ const CrossTarget = std.zig.CrossTarget; pub fn addCases(cases: *tests.TranslateCContext) void { const default_enum_type = if (builtin.abi == .msvc) "c_int" else "c_uint"; + cases.add("variables check for opaque demotion", + \\struct A { + \\ _Atomic int a; + \\} a; + \\int main(void) { + \\ struct A a; + \\} + , &[_][]const u8{ + \\pub const struct_A = opaque {}; + \\pub const a = @compileError("non-extern variable has opaque type"); + , + \\pub extern fn main() c_int; + }); + cases.add("field access is grouped if necessary", \\unsigned long foo(unsigned long x) { \\ return ((union{unsigned long _x}){x})._x; @@ -3587,7 +3601,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\ struct my_struct S = {.a = 1, .b = 2}; \\} , &[_][]const u8{ - \\warning: cannot initialize opaque type + \\warning: local variable has opaque type , \\warning: unable to translate function, demoted to extern \\pub extern fn initialize() void; |
