make std.mem.toSlice use null terminated pointers

and fix the fallout
This commit is contained in:
Andrew Kelley 2019-11-24 21:12:01 -05:00
parent 34b1ebefaa
commit 15d415e10b
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9
17 changed files with 76 additions and 75 deletions

View File

@ -72,11 +72,11 @@ pub const Buffer = struct {
self.list.deinit(); self.list.deinit();
} }
pub fn toSlice(self: Buffer) []u8 { pub fn toSlice(self: Buffer) [:0]u8 {
return self.list.toSlice()[0..self.len()]; return self.list.toSlice()[0..self.len()];
} }
pub fn toSliceConst(self: Buffer) []const u8 { pub fn toSliceConst(self: Buffer) [:0]const u8 {
return self.list.toSliceConst()[0..self.len()]; return self.list.toSliceConst()[0..self.len()];
} }
@ -131,11 +131,6 @@ pub const Buffer = struct {
try self.resize(m.len); try self.resize(m.len);
mem.copy(u8, self.list.toSlice(), m); mem.copy(u8, self.list.toSlice(), m);
} }
/// For passing to C functions.
pub fn ptr(self: Buffer) [*]u8 {
return self.list.items.ptr;
}
}; };
test "simple Buffer" { test "simple Buffer" {

View File

@ -110,7 +110,7 @@ pub extern "c" fn nanosleep(rqtp: *const timespec, rmtp: ?*timespec) c_int;
pub extern "c" fn setreuid(ruid: c_uint, euid: c_uint) c_int; pub extern "c" fn setreuid(ruid: c_uint, euid: c_uint) c_int;
pub extern "c" fn setregid(rgid: c_uint, egid: c_uint) c_int; pub extern "c" fn setregid(rgid: c_uint, egid: c_uint) c_int;
pub extern "c" fn rmdir(path: [*]const u8) c_int; pub extern "c" fn rmdir(path: [*]const u8) c_int;
pub extern "c" fn getenv(name: [*]const u8) ?[*]u8; pub extern "c" fn getenv(name: [*:0]const u8) ?[*:0]u8;
pub extern "c" fn sysctl(name: [*]const c_int, namelen: c_uint, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int; pub extern "c" fn sysctl(name: [*]const c_int, namelen: c_uint, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int;
pub extern "c" fn sysctlbyname(name: [*]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int; pub extern "c" fn sysctlbyname(name: [*]const u8, oldp: ?*c_void, oldlenp: ?*usize, newp: ?*c_void, newlen: usize) c_int;
pub extern "c" fn sysctlnametomib(name: [*]const u8, mibp: ?*c_int, sizep: ?*usize) c_int; pub extern "c" fn sysctlnametomib(name: [*]const u8, mibp: ?*c_int, sizep: ?*usize) c_int;

View File

@ -533,7 +533,7 @@ pub const Dir = struct {
const next_index = self.index + linux_entry.reclen(); const next_index = self.index + linux_entry.reclen();
self.index = next_index; self.index = next_index;
const name = mem.toSlice(u8, @ptrCast([*]u8, &linux_entry.d_name)); const name = mem.toSlice(u8, @ptrCast([*:0]u8, &linux_entry.d_name));
// skip . and .. entries // skip . and .. entries
if (mem.eql(u8, name, ".") or mem.eql(u8, name, "..")) { if (mem.eql(u8, name, ".") or mem.eql(u8, name, "..")) {

View File

@ -356,17 +356,17 @@ pub fn eql(comptime T: type, a: []const T, b: []const T) bool {
return true; return true;
} }
pub fn len(comptime T: type, ptr: [*]const T) usize { pub fn len(comptime T: type, ptr: [*:0]const T) usize {
var count: usize = 0; var count: usize = 0;
while (ptr[count] != 0) : (count += 1) {} while (ptr[count] != 0) : (count += 1) {}
return count; return count;
} }
pub fn toSliceConst(comptime T: type, ptr: [*]const T) []const T { pub fn toSliceConst(comptime T: type, ptr: [*:0]const T) [:0]const T {
return ptr[0..len(T, ptr)]; return ptr[0..len(T, ptr)];
} }
pub fn toSlice(comptime T: type, ptr: [*]T) []T { pub fn toSlice(comptime T: type, ptr: [*:0]T) [:0]T {
return ptr[0..len(T, ptr)]; return ptr[0..len(T, ptr)];
} }

View File

@ -360,7 +360,7 @@ pub const Address = extern union {
unreachable; unreachable;
} }
const path_len = std.mem.len(u8, &self.un.path); const path_len = std.mem.len(u8, @ptrCast([*:0]const u8, &self.un.path));
return @intCast(os.socklen_t, @sizeOf(os.sockaddr_un) - self.un.path.len + path_len); return @intCast(os.socklen_t, @sizeOf(os.sockaddr_un) - self.un.path.len + path_len);
}, },
else => unreachable, else => unreachable,
@ -1271,7 +1271,7 @@ fn dnsParseCallback(ctx: dpc_ctx, rr: u8, data: []const u8, packet: []const u8)
var tmp: [256]u8 = undefined; var tmp: [256]u8 = undefined;
// Returns len of compressed name. strlen to get canon name. // Returns len of compressed name. strlen to get canon name.
_ = try os.dn_expand(packet, data, &tmp); _ = try os.dn_expand(packet, data, &tmp);
const canon_name = mem.toSliceConst(u8, &tmp); const canon_name = mem.toSliceConst(u8, @ptrCast([*:0]const u8, &tmp));
if (isValidHostName(canon_name)) { if (isValidHostName(canon_name)) {
try ctx.canon.replaceContents(canon_name); try ctx.canon.replaceContents(canon_name);
} }

View File

@ -66,12 +66,12 @@ pub const system = if (builtin.link_libc) std.c else switch (builtin.os) {
pub usingnamespace @import("os/bits.zig"); pub usingnamespace @import("os/bits.zig");
/// See also `getenv`. Populated by startup code before main(). /// See also `getenv`. Populated by startup code before main().
pub var environ: [][*]u8 = undefined; pub var environ: [][*:0]u8 = undefined;
/// Populated by startup code before main(). /// Populated by startup code before main().
/// Not available on Windows. See `std.process.args` /// Not available on Windows. See `std.process.args`
/// for obtaining the process arguments. /// for obtaining the process arguments.
pub var argv: [][*]u8 = undefined; pub var argv: [][*:0]u8 = undefined;
/// To obtain errno, call this function with the return value of the /// To obtain errno, call this function with the return value of the
/// system function call. For some systems this will obtain the value directly /// system function call. For some systems this will obtain the value directly
@ -784,7 +784,7 @@ pub fn execveC(path: [*]const u8, child_argv: [*]const ?[*]const u8, envp: [*]co
/// matching the syscall API on all targets. This removes the need for an allocator. /// matching the syscall API on all targets. This removes the need for an allocator.
/// This function also uses the PATH environment variable to get the full path to the executable. /// This function also uses the PATH environment variable to get the full path to the executable.
/// If `file` is an absolute path, this is the same as `execveC`. /// If `file` is an absolute path, this is the same as `execveC`.
pub fn execvpeC(file: [*]const u8, child_argv: [*]const ?[*]const u8, envp: [*]const ?[*]const u8) ExecveError { pub fn execvpeC(file: [*:0]const u8, child_argv: [*]const ?[*:0]const u8, envp: [*]const ?[*:0]const u8) ExecveError {
const file_slice = mem.toSliceConst(u8, file); const file_slice = mem.toSliceConst(u8, file);
if (mem.indexOfScalar(u8, file_slice, '/') != null) return execveC(file, child_argv, envp); if (mem.indexOfScalar(u8, file_slice, '/') != null) return execveC(file, child_argv, envp);
@ -820,8 +820,8 @@ pub fn execvpe(
argv_slice: []const []const u8, argv_slice: []const []const u8,
env_map: *const std.BufMap, env_map: *const std.BufMap,
) (ExecveError || error{OutOfMemory}) { ) (ExecveError || error{OutOfMemory}) {
const argv_buf = try allocator.alloc(?[*]u8, argv_slice.len + 1); const argv_buf = try allocator.alloc(?[*:0]u8, argv_slice.len + 1);
mem.set(?[*]u8, argv_buf, null); mem.set(?[*:0]u8, argv_buf, null);
defer { defer {
for (argv_buf) |arg| { for (argv_buf) |arg| {
const arg_buf = if (arg) |ptr| mem.toSlice(u8, ptr) else break; const arg_buf = if (arg) |ptr| mem.toSlice(u8, ptr) else break;
@ -834,7 +834,8 @@ pub fn execvpe(
@memcpy(arg_buf.ptr, arg.ptr, arg.len); @memcpy(arg_buf.ptr, arg.ptr, arg.len);
arg_buf[arg.len] = 0; arg_buf[arg.len] = 0;
argv_buf[i] = arg_buf.ptr; // TODO avoid @ptrCast using slice syntax with https://github.com/ziglang/zig/issues/3731
argv_buf[i] = @ptrCast([*:0]u8, arg_buf.ptr);
} }
argv_buf[argv_slice.len] = null; argv_buf[argv_slice.len] = null;
@ -844,10 +845,10 @@ pub fn execvpe(
return execvpeC(argv_buf.ptr[0].?, argv_buf.ptr, envp_buf.ptr); return execvpeC(argv_buf.ptr[0].?, argv_buf.ptr, envp_buf.ptr);
} }
pub fn createNullDelimitedEnvMap(allocator: *mem.Allocator, env_map: *const std.BufMap) ![]?[*]u8 { pub fn createNullDelimitedEnvMap(allocator: *mem.Allocator, env_map: *const std.BufMap) ![]?[*:0]u8 {
const envp_count = env_map.count(); const envp_count = env_map.count();
const envp_buf = try allocator.alloc(?[*]u8, envp_count + 1); const envp_buf = try allocator.alloc(?[*:0]u8, envp_count + 1);
mem.set(?[*]u8, envp_buf, null); mem.set(?[*:0]u8, envp_buf, null);
errdefer freeNullDelimitedEnvMap(allocator, envp_buf); errdefer freeNullDelimitedEnvMap(allocator, envp_buf);
{ {
var it = env_map.iterator(); var it = env_map.iterator();
@ -859,7 +860,8 @@ pub fn createNullDelimitedEnvMap(allocator: *mem.Allocator, env_map: *const std.
@memcpy(env_buf.ptr + pair.key.len + 1, pair.value.ptr, pair.value.len); @memcpy(env_buf.ptr + pair.key.len + 1, pair.value.ptr, pair.value.len);
env_buf[env_buf.len - 1] = 0; env_buf[env_buf.len - 1] = 0;
envp_buf[i] = env_buf.ptr; // TODO avoid @ptrCast using slice syntax with https://github.com/ziglang/zig/issues/3731
envp_buf[i] = @ptrCast([*:0]u8, env_buf.ptr);
} }
assert(i == envp_count); assert(i == envp_count);
} }
@ -867,7 +869,7 @@ pub fn createNullDelimitedEnvMap(allocator: *mem.Allocator, env_map: *const std.
return envp_buf; return envp_buf;
} }
pub fn freeNullDelimitedEnvMap(allocator: *mem.Allocator, envp_buf: []?[*]u8) void { pub fn freeNullDelimitedEnvMap(allocator: *mem.Allocator, envp_buf: []?[*:0]u8) void {
for (envp_buf) |env| { for (envp_buf) |env| {
const env_buf = if (env) |ptr| ptr[0 .. mem.len(u8, ptr) + 1] else break; const env_buf = if (env) |ptr| ptr[0 .. mem.len(u8, ptr) + 1] else break;
allocator.free(env_buf); allocator.free(env_buf);
@ -896,8 +898,7 @@ pub fn getenv(key: []const u8) ?[]const u8 {
/// Get an environment variable with a null-terminated name. /// Get an environment variable with a null-terminated name.
/// See also `getenv`. /// See also `getenv`.
/// TODO https://github.com/ziglang/zig/issues/265 pub fn getenvC(key: [*:0]const u8) ?[]const u8 {
pub fn getenvC(key: [*]const u8) ?[]const u8 {
if (builtin.link_libc) { if (builtin.link_libc) {
const value = system.getenv(key) orelse return null; const value = system.getenv(key) orelse return null;
return mem.toSliceConst(u8, value); return mem.toSliceConst(u8, value);
@ -922,7 +923,7 @@ pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 {
break :blk errno(system.getcwd(out_buffer.ptr, out_buffer.len)); break :blk errno(system.getcwd(out_buffer.ptr, out_buffer.len));
}; };
switch (err) { switch (err) {
0 => return mem.toSlice(u8, out_buffer.ptr), 0 => return mem.toSlice(u8, @ptrCast([*:0]u8, out_buffer.ptr)),
EFAULT => unreachable, EFAULT => unreachable,
EINVAL => unreachable, EINVAL => unreachable,
ENOENT => return error.CurrentWorkingDirectoryUnlinked, ENOENT => return error.CurrentWorkingDirectoryUnlinked,
@ -2865,7 +2866,7 @@ pub fn gethostname(name_buffer: *[HOST_NAME_MAX]u8) GetHostNameError![]u8 {
var uts: utsname = undefined; var uts: utsname = undefined;
switch (errno(system.uname(&uts))) { switch (errno(system.uname(&uts))) {
0 => { 0 => {
const hostname = mem.toSlice(u8, &uts.nodename); const hostname = mem.toSlice(u8, @ptrCast([*:0]u8, &uts.nodename));
mem.copy(u8, name_buffer, hostname); mem.copy(u8, name_buffer, hostname);
return name_buffer[0..hostname.len]; return name_buffer[0..hostname.len];
}, },

View File

@ -65,7 +65,8 @@ pub fn lookup(vername: []const u8, name: []const u8) usize {
if (0 == (@as(u32, 1) << @intCast(u5, syms[i].st_info & 0xf) & OK_TYPES)) continue; if (0 == (@as(u32, 1) << @intCast(u5, syms[i].st_info & 0xf) & OK_TYPES)) continue;
if (0 == (@as(u32, 1) << @intCast(u5, syms[i].st_info >> 4) & OK_BINDS)) continue; if (0 == (@as(u32, 1) << @intCast(u5, syms[i].st_info >> 4) & OK_BINDS)) continue;
if (0 == syms[i].st_shndx) continue; if (0 == syms[i].st_shndx) continue;
if (!mem.eql(u8, name, mem.toSliceConst(u8, strings + syms[i].st_name))) continue; const sym_name = @ptrCast([*:0]const u8, strings + syms[i].st_name);
if (!mem.eql(u8, name, mem.toSliceConst(u8, sym_name))) continue;
if (maybe_versym) |versym| { if (maybe_versym) |versym| {
if (!checkver(maybe_verdef.?, versym[i], vername, strings)) if (!checkver(maybe_verdef.?, versym[i], vername, strings))
continue; continue;
@ -87,5 +88,6 @@ fn checkver(def_arg: *elf.Verdef, vsym_arg: i32, vername: []const u8, strings: [
def = @intToPtr(*elf.Verdef, @ptrToInt(def) + def.vd_next); def = @intToPtr(*elf.Verdef, @ptrToInt(def) + def.vd_next);
} }
const aux = @intToPtr(*elf.Verdaux, @ptrToInt(def) + def.vd_aux); const aux = @intToPtr(*elf.Verdaux, @ptrToInt(def) + def.vd_aux);
return mem.eql(u8, vername, mem.toSliceConst(u8, strings + aux.vda_name)); const vda_name = @ptrCast([*:0]const u8, strings + aux.vda_name);
return mem.eql(u8, vername, mem.toSliceConst(u8, vda_name));
} }

View File

@ -123,12 +123,12 @@ fn posixCallMainAndExit() noreturn {
@setAlignStack(16); @setAlignStack(16);
} }
const argc = starting_stack_ptr[0]; const argc = starting_stack_ptr[0];
const argv = @ptrCast([*][*]u8, starting_stack_ptr + 1); const argv = @ptrCast([*][*:0]u8, starting_stack_ptr + 1);
const envp_optional = @ptrCast([*]?[*]u8, argv + argc + 1); const envp_optional = @ptrCast([*:null]?[*:0]u8, argv + argc + 1);
var envp_count: usize = 0; var envp_count: usize = 0;
while (envp_optional[envp_count]) |_| : (envp_count += 1) {} while (envp_optional[envp_count]) |_| : (envp_count += 1) {}
const envp = @ptrCast([*][*]u8, envp_optional)[0..envp_count]; const envp = @ptrCast([*][*:0]u8, envp_optional)[0..envp_count];
if (builtin.os == .linux) { if (builtin.os == .linux) {
// Find the beginning of the auxiliary vector // Find the beginning of the auxiliary vector
@ -168,7 +168,7 @@ fn posixCallMainAndExit() noreturn {
std.os.exit(@inlineCall(callMainWithArgs, argc, argv, envp)); std.os.exit(@inlineCall(callMainWithArgs, argc, argv, envp));
} }
fn callMainWithArgs(argc: usize, argv: [*][*]u8, envp: [][*]u8) u8 { fn callMainWithArgs(argc: usize, argv: [*][*:0]u8, envp: [][*:0]u8) u8 {
std.os.argv = argv[0..argc]; std.os.argv = argv[0..argc];
std.os.environ = envp; std.os.environ = envp;
@ -177,10 +177,10 @@ fn callMainWithArgs(argc: usize, argv: [*][*]u8, envp: [][*]u8) u8 {
return initEventLoopAndCallMain(); return initEventLoopAndCallMain();
} }
extern fn main(c_argc: i32, c_argv: [*][*]u8, c_envp: [*]?[*]u8) i32 { extern fn main(c_argc: i32, c_argv: [*][*:0]u8, c_envp: [*:null]?[*:0]u8) i32 {
var env_count: usize = 0; var env_count: usize = 0;
while (c_envp[env_count] != null) : (env_count += 1) {} while (c_envp[env_count] != null) : (env_count += 1) {}
const envp = @ptrCast([*][*]u8, c_envp)[0..env_count]; const envp = @ptrCast([*][*:0]u8, c_envp)[0..env_count];
return @inlineCall(callMainWithArgs, @intCast(usize, c_argc), c_argv, envp); return @inlineCall(callMainWithArgs, @intCast(usize, c_argc), c_argv, envp);
} }

View File

@ -708,7 +708,7 @@ pub const ZigClangStringLiteral_StringKind = extern enum {
}; };
pub extern fn ZigClangSourceManager_getSpellingLoc(self: ?*const struct_ZigClangSourceManager, Loc: struct_ZigClangSourceLocation) struct_ZigClangSourceLocation; pub extern fn ZigClangSourceManager_getSpellingLoc(self: ?*const struct_ZigClangSourceManager, Loc: struct_ZigClangSourceLocation) struct_ZigClangSourceLocation;
pub extern fn ZigClangSourceManager_getFilename(self: *const struct_ZigClangSourceManager, SpellingLoc: struct_ZigClangSourceLocation) ?[*]const u8; pub extern fn ZigClangSourceManager_getFilename(self: *const struct_ZigClangSourceManager, SpellingLoc: struct_ZigClangSourceLocation) ?[*:0]const u8;
pub extern fn ZigClangSourceManager_getSpellingLineNumber(self: ?*const struct_ZigClangSourceManager, Loc: struct_ZigClangSourceLocation) c_uint; pub extern fn ZigClangSourceManager_getSpellingLineNumber(self: ?*const struct_ZigClangSourceManager, Loc: struct_ZigClangSourceLocation) c_uint;
pub extern fn ZigClangSourceManager_getSpellingColumnNumber(self: ?*const struct_ZigClangSourceManager, Loc: struct_ZigClangSourceLocation) c_uint; pub extern fn ZigClangSourceManager_getSpellingColumnNumber(self: ?*const struct_ZigClangSourceManager, Loc: struct_ZigClangSourceLocation) c_uint;
pub extern fn ZigClangSourceManager_getCharacterData(self: ?*const struct_ZigClangSourceManager, SL: struct_ZigClangSourceLocation) [*c]const u8; pub extern fn ZigClangSourceManager_getCharacterData(self: ?*const struct_ZigClangSourceManager, SL: struct_ZigClangSourceLocation) [*c]const u8;
@ -746,7 +746,7 @@ pub extern fn ZigClangQualType_isRestrictQualified(self: struct_ZigClangQualType
pub extern fn ZigClangType_getTypeClass(self: ?*const struct_ZigClangType) ZigClangTypeClass; pub extern fn ZigClangType_getTypeClass(self: ?*const struct_ZigClangType) ZigClangTypeClass;
pub extern fn ZigClangType_getPointeeType(self: ?*const struct_ZigClangType) struct_ZigClangQualType; pub extern fn ZigClangType_getPointeeType(self: ?*const struct_ZigClangType) struct_ZigClangQualType;
pub extern fn ZigClangType_isVoidType(self: ?*const struct_ZigClangType) bool; pub extern fn ZigClangType_isVoidType(self: ?*const struct_ZigClangType) bool;
pub extern fn ZigClangType_getTypeClassName(self: *const struct_ZigClangType) [*]const u8; pub extern fn ZigClangType_getTypeClassName(self: *const struct_ZigClangType) [*:0]const u8;
pub extern fn ZigClangStmt_getBeginLoc(self: *const struct_ZigClangStmt) struct_ZigClangSourceLocation; pub extern fn ZigClangStmt_getBeginLoc(self: *const struct_ZigClangStmt) struct_ZigClangSourceLocation;
pub extern fn ZigClangStmt_getStmtClass(self: ?*const struct_ZigClangStmt) ZigClangStmtClass; pub extern fn ZigClangStmt_getStmtClass(self: ?*const struct_ZigClangStmt) ZigClangStmtClass;
pub extern fn ZigClangStmt_classof_Expr(self: ?*const struct_ZigClangStmt) bool; pub extern fn ZigClangStmt_classof_Expr(self: ?*const struct_ZigClangStmt) bool;
@ -904,7 +904,7 @@ pub extern fn ZigClangLoadFromCommandLine(
) ?*ZigClangASTUnit; ) ?*ZigClangASTUnit;
pub extern fn ZigClangDecl_getKind(decl: *const ZigClangDecl) ZigClangDeclKind; pub extern fn ZigClangDecl_getKind(decl: *const ZigClangDecl) ZigClangDeclKind;
pub extern fn ZigClangDecl_getDeclKindName(decl: *const struct_ZigClangDecl) [*]const u8; pub extern fn ZigClangDecl_getDeclKindName(decl: *const struct_ZigClangDecl) [*:0]const u8;
pub const ZigClangCompoundStmt_const_body_iterator = [*c]const *struct_ZigClangStmt; pub const ZigClangCompoundStmt_const_body_iterator = [*c]const *struct_ZigClangStmt;

View File

@ -490,8 +490,8 @@ pub const Compilation = struct {
// LLVM creates invalid binaries on Windows sometimes. // LLVM creates invalid binaries on Windows sometimes.
// See https://github.com/ziglang/zig/issues/508 // See https://github.com/ziglang/zig/issues/508
// As a workaround we do not use target native features on Windows. // As a workaround we do not use target native features on Windows.
var target_specific_cpu_args: ?[*]u8 = null; var target_specific_cpu_args: ?[*:0]u8 = null;
var target_specific_cpu_features: ?[*]u8 = null; var target_specific_cpu_features: ?[*:0]u8 = null;
defer llvm.DisposeMessage(target_specific_cpu_args); defer llvm.DisposeMessage(target_specific_cpu_args);
defer llvm.DisposeMessage(target_specific_cpu_features); defer llvm.DisposeMessage(target_specific_cpu_features);
if (target == Target.Native and !target.isWindows()) { if (target == Target.Native and !target.isWindows()) {
@ -501,7 +501,7 @@ pub const Compilation = struct {
comp.target_machine = llvm.CreateTargetMachine( comp.target_machine = llvm.CreateTargetMachine(
comp.llvm_target, comp.llvm_target,
comp.llvm_triple.ptr(), comp.llvm_triple.toSliceConst(),
target_specific_cpu_args orelse "", target_specific_cpu_args orelse "",
target_specific_cpu_features orelse "", target_specific_cpu_features orelse "",
opt_level, opt_level,

View File

@ -83,16 +83,16 @@ pub const X86FP80TypeInContext = c.LLVMX86FP80TypeInContext;
pub const X86MMXTypeInContext = c.LLVMX86MMXTypeInContext; pub const X86MMXTypeInContext = c.LLVMX86MMXTypeInContext;
pub const AddGlobal = LLVMAddGlobal; pub const AddGlobal = LLVMAddGlobal;
extern fn LLVMAddGlobal(M: *Module, Ty: *Type, Name: [*]const u8) ?*Value; extern fn LLVMAddGlobal(M: *Module, Ty: *Type, Name: [*:0]const u8) ?*Value;
pub const ConstStringInContext = LLVMConstStringInContext; pub const ConstStringInContext = LLVMConstStringInContext;
extern fn LLVMConstStringInContext(C: *Context, Str: [*]const u8, Length: c_uint, DontNullTerminate: Bool) ?*Value; extern fn LLVMConstStringInContext(C: *Context, Str: [*:0]const u8, Length: c_uint, DontNullTerminate: Bool) ?*Value;
pub const ConstInt = LLVMConstInt; pub const ConstInt = LLVMConstInt;
extern fn LLVMConstInt(IntTy: *Type, N: c_ulonglong, SignExtend: Bool) ?*Value; extern fn LLVMConstInt(IntTy: *Type, N: c_ulonglong, SignExtend: Bool) ?*Value;
pub const BuildLoad = LLVMBuildLoad; pub const BuildLoad = LLVMBuildLoad;
extern fn LLVMBuildLoad(arg0: *Builder, PointerVal: *Value, Name: [*]const u8) ?*Value; extern fn LLVMBuildLoad(arg0: *Builder, PointerVal: *Value, Name: [*:0]const u8) ?*Value;
pub const ConstNull = LLVMConstNull; pub const ConstNull = LLVMConstNull;
extern fn LLVMConstNull(Ty: *Type) ?*Value; extern fn LLVMConstNull(Ty: *Type) ?*Value;
@ -110,24 +110,24 @@ pub const CreateEnumAttribute = LLVMCreateEnumAttribute;
extern fn LLVMCreateEnumAttribute(C: *Context, KindID: c_uint, Val: u64) ?*Attribute; extern fn LLVMCreateEnumAttribute(C: *Context, KindID: c_uint, Val: u64) ?*Attribute;
pub const AddFunction = LLVMAddFunction; pub const AddFunction = LLVMAddFunction;
extern fn LLVMAddFunction(M: *Module, Name: [*]const u8, FunctionTy: *Type) ?*Value; extern fn LLVMAddFunction(M: *Module, Name: [*:0]const u8, FunctionTy: *Type) ?*Value;
pub const CreateCompileUnit = ZigLLVMCreateCompileUnit; pub const CreateCompileUnit = ZigLLVMCreateCompileUnit;
extern fn ZigLLVMCreateCompileUnit( extern fn ZigLLVMCreateCompileUnit(
dibuilder: *DIBuilder, dibuilder: *DIBuilder,
lang: c_uint, lang: c_uint,
difile: *DIFile, difile: *DIFile,
producer: [*]const u8, producer: [*:0]const u8,
is_optimized: bool, is_optimized: bool,
flags: [*]const u8, flags: [*:0]const u8,
runtime_version: c_uint, runtime_version: c_uint,
split_name: [*]const u8, split_name: [*:0]const u8,
dwo_id: u64, dwo_id: u64,
emit_debug_info: bool, emit_debug_info: bool,
) ?*DICompileUnit; ) ?*DICompileUnit;
pub const CreateFile = ZigLLVMCreateFile; pub const CreateFile = ZigLLVMCreateFile;
extern fn ZigLLVMCreateFile(dibuilder: *DIBuilder, filename: [*]const u8, directory: [*]const u8) ?*DIFile; extern fn ZigLLVMCreateFile(dibuilder: *DIBuilder, filename: [*:0]const u8, directory: [*:0]const u8) ?*DIFile;
pub const ArrayType = LLVMArrayType; pub const ArrayType = LLVMArrayType;
extern fn LLVMArrayType(ElementType: *Type, ElementCount: c_uint) ?*Type; extern fn LLVMArrayType(ElementType: *Type, ElementCount: c_uint) ?*Type;
@ -145,7 +145,7 @@ pub const IntTypeInContext = LLVMIntTypeInContext;
extern fn LLVMIntTypeInContext(C: *Context, NumBits: c_uint) ?*Type; extern fn LLVMIntTypeInContext(C: *Context, NumBits: c_uint) ?*Type;
pub const ModuleCreateWithNameInContext = LLVMModuleCreateWithNameInContext; pub const ModuleCreateWithNameInContext = LLVMModuleCreateWithNameInContext;
extern fn LLVMModuleCreateWithNameInContext(ModuleID: [*]const u8, C: *Context) ?*Module; extern fn LLVMModuleCreateWithNameInContext(ModuleID: [*:0]const u8, C: *Context) ?*Module;
pub const VoidTypeInContext = LLVMVoidTypeInContext; pub const VoidTypeInContext = LLVMVoidTypeInContext;
extern fn LLVMVoidTypeInContext(C: *Context) ?*Type; extern fn LLVMVoidTypeInContext(C: *Context) ?*Type;
@ -157,7 +157,7 @@ pub const ContextDispose = LLVMContextDispose;
extern fn LLVMContextDispose(C: *Context) void; extern fn LLVMContextDispose(C: *Context) void;
pub const CopyStringRepOfTargetData = LLVMCopyStringRepOfTargetData; pub const CopyStringRepOfTargetData = LLVMCopyStringRepOfTargetData;
extern fn LLVMCopyStringRepOfTargetData(TD: *TargetData) ?[*]u8; extern fn LLVMCopyStringRepOfTargetData(TD: *TargetData) ?[*:0]u8;
pub const CreateTargetDataLayout = LLVMCreateTargetDataLayout; pub const CreateTargetDataLayout = LLVMCreateTargetDataLayout;
extern fn LLVMCreateTargetDataLayout(T: *TargetMachine) ?*TargetData; extern fn LLVMCreateTargetDataLayout(T: *TargetMachine) ?*TargetData;
@ -165,9 +165,9 @@ extern fn LLVMCreateTargetDataLayout(T: *TargetMachine) ?*TargetData;
pub const CreateTargetMachine = ZigLLVMCreateTargetMachine; pub const CreateTargetMachine = ZigLLVMCreateTargetMachine;
extern fn ZigLLVMCreateTargetMachine( extern fn ZigLLVMCreateTargetMachine(
T: *Target, T: *Target,
Triple: [*]const u8, Triple: [*:0]const u8,
CPU: [*]const u8, CPU: [*:0]const u8,
Features: [*]const u8, Features: [*:0]const u8,
Level: CodeGenOptLevel, Level: CodeGenOptLevel,
Reloc: RelocMode, Reloc: RelocMode,
CodeModel: CodeModel, CodeModel: CodeModel,
@ -175,10 +175,10 @@ extern fn ZigLLVMCreateTargetMachine(
) ?*TargetMachine; ) ?*TargetMachine;
pub const GetHostCPUName = LLVMGetHostCPUName; pub const GetHostCPUName = LLVMGetHostCPUName;
extern fn LLVMGetHostCPUName() ?[*]u8; extern fn LLVMGetHostCPUName() ?[*:0]u8;
pub const GetNativeFeatures = ZigLLVMGetNativeFeatures; pub const GetNativeFeatures = ZigLLVMGetNativeFeatures;
extern fn ZigLLVMGetNativeFeatures() ?[*]u8; extern fn ZigLLVMGetNativeFeatures() ?[*:0]u8;
pub const GetElementType = LLVMGetElementType; pub const GetElementType = LLVMGetElementType;
extern fn LLVMGetElementType(Ty: *Type) *Type; extern fn LLVMGetElementType(Ty: *Type) *Type;
@ -190,16 +190,16 @@ pub const BuildStore = LLVMBuildStore;
extern fn LLVMBuildStore(arg0: *Builder, Val: *Value, Ptr: *Value) ?*Value; extern fn LLVMBuildStore(arg0: *Builder, Val: *Value, Ptr: *Value) ?*Value;
pub const BuildAlloca = LLVMBuildAlloca; pub const BuildAlloca = LLVMBuildAlloca;
extern fn LLVMBuildAlloca(arg0: *Builder, Ty: *Type, Name: ?[*]const u8) ?*Value; extern fn LLVMBuildAlloca(arg0: *Builder, Ty: *Type, Name: ?[*:0]const u8) ?*Value;
pub const ConstInBoundsGEP = LLVMConstInBoundsGEP; pub const ConstInBoundsGEP = LLVMConstInBoundsGEP;
pub extern fn LLVMConstInBoundsGEP(ConstantVal: *Value, ConstantIndices: [*]*Value, NumIndices: c_uint) ?*Value; pub extern fn LLVMConstInBoundsGEP(ConstantVal: *Value, ConstantIndices: [*]*Value, NumIndices: c_uint) ?*Value;
pub const GetTargetFromTriple = LLVMGetTargetFromTriple; pub const GetTargetFromTriple = LLVMGetTargetFromTriple;
extern fn LLVMGetTargetFromTriple(Triple: [*]const u8, T: **Target, ErrorMessage: ?*[*]u8) Bool; extern fn LLVMGetTargetFromTriple(Triple: [*:0]const u8, T: **Target, ErrorMessage: ?*[*:0]u8) Bool;
pub const VerifyModule = LLVMVerifyModule; pub const VerifyModule = LLVMVerifyModule;
extern fn LLVMVerifyModule(M: *Module, Action: VerifierFailureAction, OutMessage: *?[*]u8) Bool; extern fn LLVMVerifyModule(M: *Module, Action: VerifierFailureAction, OutMessage: *?[*:0]u8) Bool;
pub const GetInsertBlock = LLVMGetInsertBlock; pub const GetInsertBlock = LLVMGetInsertBlock;
extern fn LLVMGetInsertBlock(Builder: *Builder) *BasicBlock; extern fn LLVMGetInsertBlock(Builder: *Builder) *BasicBlock;
@ -216,7 +216,7 @@ pub const GetParam = LLVMGetParam;
extern fn LLVMGetParam(Fn: *Value, Index: c_uint) *Value; extern fn LLVMGetParam(Fn: *Value, Index: c_uint) *Value;
pub const AppendBasicBlockInContext = LLVMAppendBasicBlockInContext; pub const AppendBasicBlockInContext = LLVMAppendBasicBlockInContext;
extern fn LLVMAppendBasicBlockInContext(C: *Context, Fn: *Value, Name: [*]const u8) ?*BasicBlock; extern fn LLVMAppendBasicBlockInContext(C: *Context, Fn: *Value, Name: [*:0]const u8) ?*BasicBlock;
pub const PositionBuilderAtEnd = LLVMPositionBuilderAtEnd; pub const PositionBuilderAtEnd = LLVMPositionBuilderAtEnd;
extern fn LLVMPositionBuilderAtEnd(Builder: *Builder, Block: *BasicBlock) void; extern fn LLVMPositionBuilderAtEnd(Builder: *Builder, Block: *BasicBlock) void;
@ -278,14 +278,14 @@ pub const TargetMachineEmitToFile = ZigLLVMTargetMachineEmitToFile;
extern fn ZigLLVMTargetMachineEmitToFile( extern fn ZigLLVMTargetMachineEmitToFile(
targ_machine_ref: *TargetMachine, targ_machine_ref: *TargetMachine,
module_ref: *Module, module_ref: *Module,
filename: [*]const u8, filename: [*:0]const u8,
output_type: EmitOutputType, output_type: EmitOutputType,
error_message: *[*]u8, error_message: *[*:0]u8,
is_debug: bool, is_debug: bool,
is_small: bool, is_small: bool,
) bool; ) bool;
pub const BuildCall = ZigLLVMBuildCall; pub const BuildCall = ZigLLVMBuildCall;
extern fn ZigLLVMBuildCall(B: *Builder, Fn: *Value, Args: [*]*Value, NumArgs: c_uint, CC: c_uint, fn_inline: FnInline, Name: [*]const u8) ?*Value; extern fn ZigLLVMBuildCall(B: *Builder, Fn: *Value, Args: [*]*Value, NumArgs: c_uint, CC: c_uint, fn_inline: FnInline, Name: [*:0]const u8) ?*Value;
pub const PrivateLinkage = c.LLVMLinkage.LLVMPrivateLinkage; pub const PrivateLinkage = c.LLVMLinkage.LLVMPrivateLinkage;

View File

@ -144,7 +144,7 @@ export fn stage2_render_ast(tree: *ast.Tree, output_file: *FILE) Error {
// TODO: just use the actual self-hosted zig fmt. Until https://github.com/ziglang/zig/issues/2377, // TODO: just use the actual self-hosted zig fmt. Until https://github.com/ziglang/zig/issues/2377,
// we use a blocking implementation. // we use a blocking implementation.
export fn stage2_fmt(argc: c_int, argv: [*]const [*]const u8) c_int { export fn stage2_fmt(argc: c_int, argv: [*]const [*:0]const u8) c_int {
if (std.debug.runtime_safety) { if (std.debug.runtime_safety) {
fmtMain(argc, argv) catch unreachable; fmtMain(argc, argv) catch unreachable;
} else { } else {
@ -156,7 +156,7 @@ export fn stage2_fmt(argc: c_int, argv: [*]const [*]const u8) c_int {
return 0; return 0;
} }
fn fmtMain(argc: c_int, argv: [*]const [*]const u8) !void { fn fmtMain(argc: c_int, argv: [*]const [*:0]const u8) !void {
const allocator = std.heap.c_allocator; const allocator = std.heap.c_allocator;
var args_list = std.ArrayList([]const u8).init(allocator); var args_list = std.ArrayList([]const u8).init(allocator);
const argc_usize = @intCast(usize, argc); const argc_usize = @intCast(usize, argc);

View File

@ -113,7 +113,7 @@ const Context = struct {
} }
/// Convert a null-terminated C string to a slice allocated in the arena /// Convert a null-terminated C string to a slice allocated in the arena
fn str(c: *Context, s: [*]const u8) ![]u8 { fn str(c: *Context, s: [*:0]const u8) ![]u8 {
return std.mem.dupe(c.a(), u8, std.mem.toSliceConst(u8, s)); return std.mem.dupe(c.a(), u8, std.mem.toSliceConst(u8, s));
} }

View File

@ -172,9 +172,9 @@ pub fn getDarwinArchString(self: Target) []const u8 {
pub fn llvmTargetFromTriple(triple: std.Buffer) !*llvm.Target { pub fn llvmTargetFromTriple(triple: std.Buffer) !*llvm.Target {
var result: *llvm.Target = undefined; var result: *llvm.Target = undefined;
var err_msg: [*]u8 = undefined; var err_msg: [*:0]u8 = undefined;
if (llvm.GetTargetFromTriple(triple.ptr(), &result, &err_msg) != 0) { if (llvm.GetTargetFromTriple(triple.toSlice(), &result, &err_msg) != 0) {
std.debug.warn("triple: {s} error: {s}\n", triple.ptr(), err_msg); std.debug.warn("triple: {s} error: {s}\n", triple.toSlice(), err_msg);
return error.UnsupportedTarget; return error.UnsupportedTarget;
} }
return result; return result;

View File

@ -7792,7 +7792,8 @@ static void resolve_llvm_types_slice(CodeGen *g, ZigType *type, ResolveStatus wa
bool done = false; bool done = false;
if (ptr_type->data.pointer.is_const || ptr_type->data.pointer.is_volatile || if (ptr_type->data.pointer.is_const || ptr_type->data.pointer.is_volatile ||
ptr_type->data.pointer.explicit_alignment != 0 || ptr_type->data.pointer.allow_zero) ptr_type->data.pointer.explicit_alignment != 0 || ptr_type->data.pointer.allow_zero ||
ptr_type->data.pointer.sentinel != nullptr)
{ {
ZigType *peer_ptr_type = get_pointer_to_type_extra(g, child_type, false, false, ZigType *peer_ptr_type = get_pointer_to_type_extra(g, child_type, false, false,
PtrLenUnknown, 0, 0, 0, false); PtrLenUnknown, 0, 0, 0, false);
@ -7811,7 +7812,8 @@ static void resolve_llvm_types_slice(CodeGen *g, ZigType *type, ResolveStatus wa
ZigType *child_ptr_type = child_type->data.structure.fields[slice_ptr_index]->type_entry; ZigType *child_ptr_type = child_type->data.structure.fields[slice_ptr_index]->type_entry;
assert(child_ptr_type->id == ZigTypeIdPointer); assert(child_ptr_type->id == ZigTypeIdPointer);
if (child_ptr_type->data.pointer.is_const || child_ptr_type->data.pointer.is_volatile || if (child_ptr_type->data.pointer.is_const || child_ptr_type->data.pointer.is_volatile ||
child_ptr_type->data.pointer.explicit_alignment != 0 || child_ptr_type->data.pointer.allow_zero) child_ptr_type->data.pointer.explicit_alignment != 0 || child_ptr_type->data.pointer.allow_zero ||
child_ptr_type->data.pointer.sentinel != nullptr)
{ {
ZigType *grand_child_type = child_ptr_type->data.pointer.child_type; ZigType *grand_child_type = child_ptr_type->data.pointer.child_type;
ZigType *bland_child_ptr_type = get_pointer_to_type_extra(g, grand_child_type, false, false, ZigType *bland_child_ptr_type = get_pointer_to_type_extra(g, grand_child_type, false, false,

View File

@ -348,7 +348,7 @@ fn testCastPtrOfArrayToSliceAndPtr() void {
test "cast *[1][*]const u8 to [*]const ?[*]const u8" { test "cast *[1][*]const u8 to [*]const ?[*]const u8" {
const window_name = [1][*]const u8{"window name"}; const window_name = [1][*]const u8{"window name"};
const x: [*]const ?[*]const u8 = &window_name; const x: [*]const ?[*]const u8 = &window_name;
expect(mem.eql(u8, std.mem.toSliceConst(u8, x[0].?), "window name")); expect(mem.eql(u8, std.mem.toSliceConst(u8, @ptrCast([*:0]const u8, x[0].?)), "window name"));
} }
test "@intCast comptime_int" { test "@intCast comptime_int" {

View File

@ -207,7 +207,8 @@ test "null terminated pointer" {
var array_with_zero = [_:0]u8{'h', 'e', 'l', 'l', 'o'}; var array_with_zero = [_:0]u8{'h', 'e', 'l', 'l', 'o'};
var zero_ptr: [*:0]const u8 = @ptrCast([*:0]const u8, &array_with_zero); var zero_ptr: [*:0]const u8 = @ptrCast([*:0]const u8, &array_with_zero);
var no_zero_ptr: [*]const u8 = zero_ptr; var no_zero_ptr: [*]const u8 = zero_ptr;
expect(std.mem.eql(u8, std.mem.toSliceConst(u8, no_zero_ptr), "hello")); var zero_ptr_again = @ptrCast([*:0]const u8, no_zero_ptr);
expect(std.mem.eql(u8, std.mem.toSliceConst(u8, zero_ptr_again), "hello"));
} }
}; };
S.doTheTest(); S.doTheTest();