Sublime Forum

Clang (C/C++/Objective-C/Objective-C++) autocomplete

#1

I started a plugin for clang autocompletion. It’s pretty functional already, but does need more work. Check it out if you’re coding in C/C++
github.com/quarnster/SublimeClang

0 Likes

#2

Hey, this is really cool!

It seems a little bit buggy still with the completions (it listed several symbols that weren’t valid), but still it’s a great idea and a good start for sure.

I mostly code in objective-C and objective-C++ when I use the C family of languages, so I modified line 116 in sublimeclang.py to include it as an allowed language:

if language != "c++" and language != "c" and language != "objc":

I was happy to find that that seemed to at least make auto-completion work in my .m file. It didn’t seem to pick up unused variables and stuff like that though which worked for regular .c files.

Could you take a look at adding objective-C support? Perhaps the -ObjC flag needs to be passed to clang.

Also, does this hook into the clang static analyzer, or just scan for standard warnings?

Anyway, I’m really looking forward to see where this goes. It’s great to be able to get some of the power xcode has from clang integration in sublime text :smile:

0 Likes

#3

I’m not too familiar with Objective-C/Objective-C++, but I’ve just submitted a shot at adding basic support.

As for listing symbols that aren’t available, I only list the completions provided by clang + some filtering. If you have any suggestions for how to better filter or any options that should be provided to clang to suggest better completions, I’d be happy to change.

It’s not hooked up to the static analyzer, just the regular compiler. The static analyzer looks cool though, maybe I’ll dig into that as time allows.

0 Likes

#4

Wow, this bundle works much better than I expected.

I have to admit, I expected it to fail on a complex project. I just tried it out on the source code of Postgresql. After pointing it to the correct include dir in clang.sublime-settings, it completes function calls (showing the correct parameter names and types) and structures (showing member names and types), and it does so very quickly!

Very nice plugin indeed!

I had only one problem: with very long C files it would show errors where there are none. I found the following line in sublimeclang.py to cause the problem:

unsaved_files = (view.file_name(), view.substr(Region(0, 65536)))]

I do not know why you introduced the artificial length limit. If I remove it, everything works beautifully for me. Specifically, I replaced the above line with

unsaved_files = (view.file_name(), view.substr(Region(0, view.size())))]

Greetings, and keep up the great work!
Julian

0 Likes

#5

Only reason for the 65536 would be not being too familiar with the plugin api. Thanks for the fix and the visual marker changes :smile:

0 Likes

#6

Hey guys,

I wanted to give this plugin a try with ST2 (x86 version) on Windows 7 64-bits and plugin version 5c43973 (latest today).

I get the following python error:

Traceback (most recent call last): File ".\sublimeclang.py", line 190, in recompile File ".\clang\cindex.py", line 208, in location ValueError: Procedure called with not enough arguments (4 bytes missing) or wrong calling convention
I believe it has something to do with the communication between Clang and the python plugin, but since my python skills are quite weak, I’m not sure how to fix it.

Cheers

0 Likes

#7

Hmm. I get the same thing on my Windows installation, but nothing I tried seemed to fix it. I’m starting to think that the dll itself is broken, I used the precompiled 2.9 from the llvm website.

Is anyone using this plugin successfully on windows? If I get no replies I’ll just hack it to look like the following until someone can provide a proper fix:

    def location(self):
        import platform
        if platform.system() == "Windows":
            class hack:
                def __init__(self):
                    self.file = None
                    self.line = 0
                    self.column = 0
            return hack()

        return _clang_getDiagnosticLocation(self)
0 Likes

#8

FWIW, the hack seems to work on my machine as well. I’ll try to figure out why I still have include problems, though the -I parameter seems to point to the proper directory.

Thanks

0 Likes

#9

Great, thanks :smile:

I’m seeing a bit more of it working now. It does highlighting and stuff in my objective-C files. Unfortunately, it seems it still needs some flags set because clang is giving me some errors where there shouldn’t be. I’ll try to take a look and see if I can figure out what the problem is and maybe submit a patch or something.

[quote]
As for listing symbols that aren’t available, I only list the completions provided by clang + some filtering. If you have any suggestions for how to better filter or any options that should be provided to clang to suggest better completions, I’d be happy to change.[/quote]

Well, I just tried it again and I didn’t seem to notice the same behavior as before. But if it comes up again I’ll try to investigate a bit more.

0 Likes

#10

FYI just got a chance to test it on Linux and it’s not working there due to sublimetext.userecho.com/topic/8 … -in-linux/ so upvote that one if you’d like to see Linux support.

0 Likes

#11

Compiling libclang using Visual C++ was trivial so with the latest version in git getting the location of errors and warnings should be fine on Windows.

As for it not picking up your include paths, try modifying the user settings rather than the default settings. You can access those by ctrl+shift+P and typing “SublimeClang”.

0 Likes

#12

Regarding Linux errors

I’m running Ubuntu 11.10 64bit. I was able to get this plugin working by symlinking /usr/lib/python2.6/ to sublime2’s lib directory, and recompiling libclang.so to target a 64bit architecture.

1.) Install python2.6 via apt-get or synpactic.
2.) Backup python26.zip in sublime2’s lib directory (just for safe measure). I did mv “python26.zip backup.python26.zip”
3.) Symlink /usr/lib/python2.6/ to sublime2’s lib directory.
4.) Download and compile clang. (You can probably get away with installing clang from the ubuntu repos)
5.) Copy libclang.so from the installed location into /.config/sublime-text-2/Packages/SublimeClang/

I’m still left with the issue of Clang not being able to find “stdarg.h”, but I believe that’s a clang bug and has nothing to do with this plugin.

Note to the the author of this plugin: It’s probably best not to include the compiled library for linux in your repository, given that each architecture requires its own build. It may be a better idea just to have a text file instructing linux users to build and copy libclang themselves. Thanks for this awesome plugin!

0 Likes

#13

Thanks for the tip, I’ve deleted the default libclang.so and added your work around to the readme at github.com/quarnster/SublimeClang/.

0 Likes

#14

Hi, I tried installing SublimeClang on Windows 7 x64 (x64 sublime 2), however it did not work. During install through Package manager I get:

Reloading plugin C:\Users\radix\AppData\Roaming\Sublime Text 2\Packages\SublimeClang\sublimeclang.py
Traceback (most recent call last):
  File ".\sublime_plugin.py", line 57, in reload_plugin
  File ".\sublimeclang.py", line 31, in <module>
    from clang import cindex
  File ".\clang\cindex.py", line 86, in <module>
    lib = get_cindex_library()
  File ".\clang\cindex.py", line 76, in get_cindex_library
    return cdll.LoadLibrary('libclang.dll')
  File ".\ctypes\__init__.py", line 431, in LoadLibrary
  File ".\ctypes\__init__.py", line 353, in __init__
WindowsError: [Error 193] %1 is not a valid Win32 application

Thanks.

0 Likes

#15

Until someone contributes a 64-bit libclang.dll, you’ll have to make do with using the 32 bit Sublime Text 2 if you want to use this plugin.

0 Likes

#16

Yeah I thought this was the case. I did manage to build libclang in 64 mode for windows. However, now I get these when it’s trying to autocomplete on -> or ::

Perhaps there’s an issue with how I built it…

Traceback (most recent call last):
  File ".\sublime_plugin.py", line 175, in on_query_completions
  File ".\sublimeclang.py", line 596, in on_query_completions
  File ".\clang\cindex.py", line 1568, in codeComplete
WindowsError: exception: access violation writing 0x00000000000C0000
Traceback (most recent call last):
  File ".\sublimeclang.py", line 175, in worker
  File ".\clang\cindex.py", line 1546, in reparse
WindowsError: exception: access violation writing 0x0000000004EC0000

Here’s my libclang (x64, compiled using vc2010).
ttvd.org/temp/libclang_x64_vc2010.rar

Thanks!

0 Likes

#17

Are you working with huge files or is there any other reason you must use the 64-bit executable of Sublime Text 2?

0 Likes

#18

Hi,
I got it working on Arch Linux.
You have to add the header search path /usr/lib/clang/3.0/include to SublimeClang’s config. If your distribution doesn’t have clang 3.0 yet, you can find those headers in clang-3.0.src/lib/Headers of llvm.org/releases/3.0/clang-3.0.tar.gz

Thanks for your work, quarnster!

0 Likes

#19

Wow… this is utterly mind blowing even in its early state. My vote for best plugin…ever?

I would have killed for the equivalent of visual studio’s f12, and now i have it, plus really good code completion that just works (still a bit rough, but its great still)… props to you quarnster.

Edit: To help other newbies, to get the most out of it I had to change my options and add them to the User version of SublimeClang.sublime-settings like so: (on mac os lion, will try win 7 64 bit eventually)

"options":
	    
	        "-Wall",
	        "-std=c++11",
	        "-I/usr/lib/clang/3.0/include/",
	        "-I/Developer/usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin11/4.2.1",
	        "-I/Users/alanandersen/dev"
	    ]

Also, it seems once you install Xcode 4.2, all the gcc stuff is moved from /usr/ into /Developer, took a bit to figure that out…

0 Likes

#20

Cheers for the props guys :smile:

Please let me know if you have any specific requests or suggestions for making it better. Pull requests with new features are welcome.

0 Likes