revive translateC support

This commit is contained in:
Techatrix
2023-05-30 01:59:46 +02:00
parent b958b258a3
commit b623f141ea
4 changed files with 315 additions and 29 deletions

View File

@@ -8,6 +8,24 @@ const translate_c = zls.translate_c;
const allocator: std.mem.Allocator = std.testing.allocator;
test "zig compile server - translate c" {
var result1 = try testTranslate(
\\void foo(int);
\\void bar(float*);
);
defer result1.deinit(allocator);
try std.testing.expect(result1 == .success);
// TODO the zig compiler doesn't seem to report error bundles for translate-c
// Hopefully I can fix that once llvm-16 finished compiling :)
var result2 = testTranslate(
\\#include <this_file_doesnt_exist>
);
defer if (result2) |*r| r.deinit(allocator) else |_| {};
try std.testing.expectError(error.Timeout, result2);
}
test "convertCInclude - empty" {
try testConvertCInclude("@cImport()", "");
try testConvertCInclude("@cImport({})", "");
@@ -29,7 +47,7 @@ test "convertCInclude - cInclude" {
);
}
test "translate_c - cDefine" {
test "convertCInclude - cDefine" {
try testConvertCInclude(
\\@cImport(@cDefine("FOO", "BAR"))
,
@@ -42,7 +60,7 @@ test "translate_c - cDefine" {
);
}
test "translate_c - cUndef" {
test "convertCInclude - cUndef" {
try testConvertCInclude(
\\@cImport(@cUndef("FOO"))
,
@@ -85,3 +103,33 @@ fn testConvertCInclude(cimport_source: []const u8, expected: []const u8) !void {
try std.testing.expectEqualStrings(expected, trimmed_output);
}
fn testTranslate(c_source: []const u8) !translate_c.Result {
if (!std.process.can_spawn) return error.SkipZigTest;
var config: zls.Config = .{};
defer std.json.parseFree(zls.Config, allocator, config);
var runtime_zig_version: ?zls.ZigVersionWrapper = null;
defer if (runtime_zig_version) |*v| v.free();
try zls.configuration.configChanged(&config, &runtime_zig_version, allocator, null);
if (config.global_cache_path == null or
config.zig_exe_path == null or
config.zig_lib_path == null) return error.SkipZigTest;
const result = (try translate_c.translate(allocator, config, &.{}, c_source)).?;
switch (result) {
.success => |uri| {
const path = try zls.URI.parse(allocator, uri);
defer allocator.free(path);
try std.testing.expect(std.fs.path.isAbsolute(path));
},
.failure => |message| {
try std.testing.expect(message.len != 0);
},
}
return result;
}