Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

revive C import support #1136

Merged
merged 1 commit into from
May 30, 2023
Merged

Conversation

Techatrix
Copy link
Member

Its time to bring back C import support. fixes #1080
Please feel free to try this out and report any issues.

error reporting currently doesn't work because of an issue on the zig side (I suspect)

@Techatrix Techatrix self-assigned this Apr 16, 2023
@Techatrix Techatrix force-pushed the revive-translateC branch 2 times, most recently from 35ba14c to 82f9199 Compare April 17, 2023 10:04
@silversquirl
Copy link

Doesn't seem to be working for me - translate-c errors out with UnexpectedEOF

Full zls log:

info : (config): Using zig executable '/home/silver/.local/bin/zig'
info : (config): Using zig lib path '/home/silver/.opt/zig/lib'
info : (config): Using build runner global cache path '/home/silver/.cache/zig'
info : (config): Detected runtime zig version: '0.11.0-dev.2614+7b908e173'
info : (server): client is 'kak-lsp-14.2.0'
info : (server): zls initializing
info : (server): Server.ClientCapabilities{ .supports_snippets = false, .supports_apply_edits = true, .supports_will_save = false, .supports_will_save_wait_until = false, .supports_publish_diagnostics = true, .supports_code_action_fixall = false, .hover_supports_md = true, .completion_doc_supports_md = true, .label_details_support = false, .supports_configuration = true, .supports_workspace_did_change_configuration_dynamic_registration = false }
info : (server): Using offset encoding: utf-8
warning: (store ): Document already open: file:///home/silver/code/pipit/src/CodeGen.zig
info : (server): Setting configuration...
info : (config): Using zig executable '/home/silver/.local/bin/zig'
info : (config): Using zig lib path '/home/silver/.opt/zig/lib'
info : (config): Using build runner global cache path '/home/silver/.cache/zig'
info : (config): Detected runtime zig version: '0.11.0-dev.2614+7b908e173'
info : (server): Setting configuration...
info : (config): Using zig executable '/home/silver/.local/bin/zig'
info : (config): Using zig lib path '/home/silver/.opt/zig/lib'
info : (config): Using build runner global cache path '/home/silver/.cache/zig'
info : (config): Detected runtime zig version: '0.11.0-dev.2614+7b908e173'
info : (server): Setting configuration...
info : (config): Using zig executable '/home/silver/.local/bin/zig'
info : (config): Using zig lib path '/home/silver/.opt/zig/lib'
info : (config): Using build runner global cache path '/home/silver/.cache/zig'
info : (config): Detected runtime zig version: '0.11.0-dev.2614+7b908e173'
info : (server): Setting configuration...
info : (config): Using zig executable '/home/silver/.local/bin/zig'
info : (config): Using zig lib path '/home/silver/.opt/zig/lib'
info : (config): Using build runner global cache path '/home/silver/.cache/zig'
info : (config): Detected runtime zig version: '0.11.0-dev.2614+7b908e173'
error: (translate_c): failed zig translate-c command:
/home/silver/.local/bin/zig translate-c --zig-lib-dir /home/silver/.opt/zig/lib --cache-dir /home/silver/.cache/zls -lc --listen=- -I /usr/local/include -I /usr/include/x86_64-linux-gnu -I /usr/include /home/silver/.cache/zls/cimport.h
error:error.UnexpectedEOF

error: (store ): failed to translate cimport: error.UnexpectedEOF

@Techatrix
Copy link
Member Author

Doesn't seem to be working for me - translate-c errors out with UnexpectedEOF

I didn't check on Windows, it should work now.

@silversquirl
Copy link

I'm on Linux, and using the latest commit in this branch :)

@Techatrix
Copy link
Member Author

I should have read that log more carefully, my bad. I thought I just fixed that error.

@silversquirl
Copy link

No error any more, but still no completions. It works when I @cImport(@cInclude("stdlib.h")) outside of a project though - seems like it's just not working with headers from packages?

@Techatrix
Copy link
Member Author

resolving include paths from build.zig may not work correctly anymore. I will check later if something regressed.

@recursiveGecko
Copy link

recursiveGecko commented May 1, 2023

I had to change a few things to make it build with Zig 0.11.0-dev.2935+ec6ffaa1e, but I'm now getting completions! 🚀

Screenshot

image

Patch
diff --git a/src/analyser/InternPool.zig b/src/analyser/InternPool.zig
index 42f3afb..5a68ee2 100644
--- a/src/analyser/InternPool.zig
+++ b/src/analyser/InternPool.zig
@@ -3315,15 +3315,15 @@ test "bytes value" {

     var str1: [43]u8 = "https://www.youtube.com/watch?v=dQw4w9WgXcQ".*;
     const bytes_value1 = try ip.get(gpa, .{ .bytes = &str1 });
-    @memset(&str1, 0, str1.len);
+    @memset(&str1, 0);

     var str2: [43]u8 = "https://www.youtube.com/watch?v=dQw4w9WgXcQ".*;
     const bytes_value2 = try ip.get(gpa, .{ .bytes = &str2 });
-    @memset(&str2, 0, str2.len);
+    @memset(&str2, 0);

     var str3: [26]u8 = "https://www.duckduckgo.com".*;
     const bytes_value3 = try ip.get(gpa, .{ .bytes = &str3 });
-    @memset(&str3, 0, str3.len);
+    @memset(&str3, 0);

     try expect(bytes_value1 == bytes_value2);
     try expect(bytes_value2 != bytes_value3);
diff --git a/src/main.zig b/src/main.zig
index b3e6251..6636e61 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -345,7 +345,7 @@ const stack_frames = switch (zig_builtin.mode) {

 pub fn main() !void {
     var gpa_state = std.heap.GeneralPurposeAllocator(.{ .stack_trace_frames = stack_frames }){};
-    defer std.debug.assert(!gpa_state.deinit());
+    defer std.debug.assert(gpa_state.deinit() == .ok);

     var tracy_state = if (tracy.enable_allocation) tracy.tracyAllocator(gpa_state.allocator()) else void{};
     const inner_allocator: std.mem.Allocator = if (tracy.enable_allocation) tracy_state.allocator() else gpa_state.allocator();

@neurocyte
Copy link

This works well for me when rebased to zls master.

@leecannon leecannon force-pushed the revive-translateC branch from 106a864 to ea6c626 Compare May 2, 2023 16:54
@leecannon leecannon added the pr:fuzz Attach to a PR to start fuzzing / continually fuzz (please do this before merging!) label May 2, 2023
@Techatrix Techatrix force-pushed the revive-translateC branch from ea6c626 to b8d7a71 Compare May 9, 2023 18:00
@Ultra-Code
Copy link

@Techatrix thanks for your work on this feature. Completions currently work but hover for identifier information doesn't work.

In neovim 0.9.0 I get

Error executing vim.schedule lua callback: /usr/share/nvim/runtime/lua/vim/lsp/util.lua:1682: 'width' key must be a positive Integer
stack traceback:
	[C]: in function 'nvim_open_win'
	/usr/share/nvim/runtime/lua/vim/lsp/util.lua:1682: in function 'handler'
	/usr/share/nvim/runtime/lua/vim/lsp.lua:1394: in function ''
	vim/_editor.lua: in function <vim/_editor.lua:0>

on hover

@Techatrix
Copy link
Member Author

Can you provide me with an example and steps you took to get this error? Information on your neovim setup would also be appreciated because I don't have much experience dealing with neovim.

@Ultra-Code
Copy link

Steps to reproduce the error

  1. fetch and switch to this pr branch
  2. compile with zig build -Doptimize=ReleaseFast
  3. go into a zig project with c files example
  4. on line 23 move to cnet.bind with you cursor on bind in normal mode
  5. type :=vim.lsp.buf.hover() .ie command for lsp hover
  6. the specified error is returned.
  7. when all ui plugins are disabled(.ie comment out content in lua/plugins/ui.lua of my config) you get No information available
  8. But if you move to a zig identifier or function you get hover information

NOTE: may neovim configuration

@Techatrix Techatrix force-pushed the revive-translateC branch from b8d7a71 to beda996 Compare May 29, 2023 22:04
@Techatrix Techatrix marked this pull request as ready for review May 29, 2023 23:31
@Techatrix Techatrix force-pushed the revive-translateC branch from beda996 to b623f14 Compare May 29, 2023 23:59
@leecannon
Copy link
Member

Works on all my projects that use cimport.

Including a non-existant file results in an error in the log but zls keeps going 👍

Error
debug: (server): Took 1ms to process method textDocument/didChange
error: (translate_c): failed zig translate-c command:
/home/lee/bin/zig translate-c --zig-lib-dir /home/lee/zig/0.11.0-dev.3322+82632aff2/files/lib --cache-dir /home/lee/.cache/zls -lc --listen=- -I /usr/local/include -I /usr/include/x86_64-linux-gnu -I /usr/include -I /home/lee/src/zig-libgit2/../libgit2/include /home/lee/.cache/zls/cimport.h
error:error.Timeout

error: (store ): failed to translate cimport: error.Timeout
debug: (server): Took 29ms to process method textDocument/completion

@leecannon leecannon merged commit 54f4768 into zigtools:master May 30, 2023
@Techatrix Techatrix deleted the revive-translateC branch May 30, 2023 17:51
@Techatrix
Copy link
Member Author

Completions currently work but hover for identifier information doesn't work.

@Ultra-Code #1232 should have fixed your hover issue. (nice nvim config btw)

@Ultra-Code
Copy link

@Techatrix thanks for the fix, Indeed the hover works now 😄 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr:fuzz Attach to a PR to start fuzzing / continually fuzz (please do this before merging!)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cimport crashes due to Zig not respecting --enable-cache
6 participants