Author Topic: Clang command line support for codecompletion  (Read 70136 times)

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5905
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Clang command line support for codecompletion
« on: October 25, 2010, 09:04:29 am »
I just find a useful file (the file can be enable codecompletion for emace)

http://stackoverflow.com/questions/2487931/llvm-c-ide-for-windows
and
https://llvm.org/svn/llvm-project/cfe/trunk/utils/clang-completion-mode.el

http://mike.struct.cn/blogs/entry/15/

I have never used emace.

Can some emace guys give some comments on this?

So that we can use the same command line?
« Last Edit: October 25, 2010, 09:27:19 am by ollydbg »
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Clang command line support for codecompletion
« Reply #1 on: October 25, 2010, 10:11:50 am »
ollydbg: if you plan to integrate clang, please, don't do it with creating another process and communicating with some IPC mechanism.
The proper way is to link directly to clangs libs. It will save you many problems.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5905
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Clang command line support for codecompletion
« Reply #2 on: October 25, 2010, 10:17:20 am »
ollydbg: if you plan to integrate clang, please, don't do it with creating another process and communicating with some IPC mechanism.
The proper way is to link directly to clangs libs. It will save you many problems.
thanks for the hint.

currently, I'm checking Clang's features (command line mode) listed in

Examples of using Clang section of http://clang.llvm.org/get_started.html

Tested by this package
http://llvm.org/releases/2.8/llvm-gcc4.2-2.8-x86-mingw32.tar.bz2

It seems I can't get the result from
Code
$ clang -cc1 ~/t.c -ast-print

I have the error like:
Code
F:\cb\test_code\test_clang>llvm-c++ main.cpp
llvm-c++: CreateProcess: No such file or directory

F:\cb\test_code\test_clang>llvm-c++ main.cpp -E
# 1 "main.cpp"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "main.cpp"
int main()
{

}

F:\cb\test_code\test_clang>llvm-c++ main.cpp -cc1 -ast-print
llvm-c++: unrecognized option '-cc1'
cc1plus.exe: error: unrecognized command line option "-ast-print"

F:\cb\test_code\test_clang>llvm-c++ -cc1 main.cpp -ast-print
llvm-c++: unrecognized option '-cc1'
cc1plus.exe: error: unrecognized command line option "-ast-print"

So, I have to firstly figure it out. :D
« Last Edit: October 25, 2010, 10:19:24 am by ollydbg »
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline eranif

  • Regular
  • ***
  • Posts: 256
Re: Clang command line support for codecompletion
« Reply #3 on: October 25, 2010, 11:02:02 am »
You might find this useful:

http://codelite.svn.sourceforge.net/viewvc/codelite/trunk/LiteEditor/clang_code_completion.h?revision=4207&view=markup
http://codelite.svn.sourceforge.net/viewvc/codelite/trunk/LiteEditor/clang_code_completion.cpp?revision=4207&view=markup

It is working (currently disabled)
Although it uses the command line invocation of clang executable.

Note that clang itself provides a lib named 'libIndex' to perform what I am doing in the linked code. However, they too are invoking clang from the command line.

Eran

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5905
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Clang command line support for codecompletion
« Reply #4 on: October 26, 2010, 02:33:46 am »
thanks eranif.
I will look into your links.

btw: did you know some Clang binaries which works under windows.
http://llvm.org/releases/2.8/llvm-gcc4.2-2.8-x86-mingw32.tar.bz2 seems is not a full package

thanks.
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5905
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Clang command line support for codecompletion
« Reply #5 on: October 26, 2010, 10:45:15 am »
updated:

Today, I have tried the method listed in
http://clang.llvm.org/get_started.html

to build a clang.exe, but no luck, all failed  :( :( :(.

1, one failed method.
under Msys mingw, check out the llvm and clang svn code, and configure. make. but the error occurred when I run the make,  such as:
Quote
$ make
Makefile:130: /Makefile.rules: No such file or directory
make: *** No rule to make target `/Makefile.rules'.  Stop.



2. failed method two:

I just use the cmake, then generate the makefile for mingw, then just run the make command.
when at build stage 97%, I get these message:
Quote
Linking CXX executable ..\..\..\..\bin\c-index-test.exe
CMakeFiles\c-index-test.dir\c-index-test.c.obj:c-index-test.c:(.text+0x47a4): un
defined reference to `_imp__pthread_create'
CMakeFiles\c-index-test.dir\c-index-test.c.obj:c-index-test.c:(.text+0x47e0): un
defined reference to `_imp__pthread_join'
collect2: ld returned 1 exit status
make[2]: *** [bin/c-index-test.exe] Error 1
make[1]: *** [tools/clang/tools/c-index-test/CMakeFiles/c-index-test.dir/all] Er
ror 2
make: *** [all] Error 2

E:\clang_build\build\tools\clang>
I have struggleed with the pthread for several hour,( copy some libs or shared libs...), and finally I still get stuck. :(


I hope some brave guy can help to build a clang distribution (windows). thanks.
currently, I can't find any clang binaries for windows.

thanks.
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Clang command line support for codecompletion
« Reply #6 on: October 26, 2010, 12:27:54 pm »
Probably you should post on their mailing list :)
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5905
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Clang command line support for codecompletion
« Reply #7 on: October 26, 2010, 03:08:16 pm »
Probably you should post on their mailing list :)
I asked there, but no replies. seems the devs are not interested in windows build.

my friend xunxun has build on for me (under mingw 4.5.1), he just some customized configure.

Code
./configure --prefix=/llvm --enable-optimized --enable-targets=host

then manually change the file
Makefile.config

Quote
-O2

to

Code
-pipe -O2 -DPTW32_STATIC_LIB

But the built clang seems can't work correctly, it even crashed in a quite dummy test code

a.c
Code
main()
{
}

then I run:
Code
clang.exe a.c -E

then clang crashed....Too bad. :(

I don't want to test any more until it becomes stable and useful enough. :shock:
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline eranif

  • Regular
  • ***
  • Posts: 256
Re: Clang command line support for codecompletion
« Reply #8 on: October 26, 2010, 03:21:47 pm »
thanks eranif.
I will look into your links.

btw: did you know some Clang binaries which works under windows.
http://llvm.org/releases/2.8/llvm-gcc4.2-2.8-x86-mingw32.tar.bz2 seems is not a full package

thanks.
I compiled clang myself on my machine using SVN / MinGW - so I am not using those.

Eran

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5905
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Clang command line support for codecompletion
« Reply #9 on: October 26, 2010, 03:29:57 pm »
thanks eranif.
I will look into your links.

btw: did you know some Clang binaries which works under windows.
http://llvm.org/releases/2.8/llvm-gcc4.2-2.8-x86-mingw32.tar.bz2 seems is not a full package

thanks.
I compiled clang myself on my machine using SVN / MinGW - so I am not using those.

Eran

Hi, eran. thanks.
 
could you tell me how to build the clang svn under mingw? I have tried this nearly half of the day, but no success.  :(
what the plan if clang becomes more powerful? I guess it could replace the codecompletion part of codeblocks or codelite (if clang becomes stable)

if possible, could you upload the whole package of the clang to some site?

Currently, I can't find any clang binaries on the internet.

thanks.
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline eranif

  • Regular
  • ***
  • Posts: 256
Re: Clang command line support for codecompletion
« Reply #10 on: October 26, 2010, 03:38:46 pm »
I already integrated this code into codelite and it seems to work very well.
The problem is that it is too slow.

Most of the time the parsing is done using an in-complete source file so you will need to provide a string buffer containing the input string up to the point where the completion should appear.

The problem is that, I tried to use the completion using codelite's project itself on the file frame.cpp (one of the largest files in codelite project) it took 4 seconds for the completion to show (or even more, can't remember now it was 3-4 months ago)

It also lacks some crucial information (which I could not seem to get an access to, like an exact position of the match), I can probably obtain more information if I will link directly with the clang libraries - but for the POC (prove of concept) it seems too much for me.

I had several ideas of how to improve the responsiveness of the code-completion by using a pre-compiled header, but it seems like that PCH is not yet supported for C++ (at least it was not available couple of months ago)

You can search their mailing list and see the discussion I had with DGregor (he is the person that knows most about code-completion in clang)


About the binaries, I remembers that enabling optimization causes a crash or ICE using MinGW. Also, make sure you build clang in release mode (which is NOT the default) it will boost its performance a lot.

I will have a look at home of how I built clang and will see if I can upload the binaries to codelite's site

Eran
 

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Clang command line support for codecompletion
« Reply #11 on: October 26, 2010, 05:37:33 pm »
@ollydbg:

you can try this one: http://apt.jenslody.de/downloads/clang_win32.7z

Cross-build on linux, at least simple hello world works on my win-xp machine.
Pretty printing also seems to work.

You will most likely find a broken clang++.exe in the bin subfolder, this is just a symlink to clang.exe on linux, but neither 7z, nor windows can handle (linux-)symlinks.
To use clang++.exe, you can just copy clang.exe to clang++.exe .

Offline ironhead

  • Almost regular
  • **
  • Posts: 210
Re: Clang command line support for codecompletion
« Reply #12 on: October 26, 2010, 06:43:02 pm »
You will most likely find a broken clang++.exe in the bin subfolder, this is just a symlink to clang.exe on linux, but neither 7z, nor windows can handle (linux-)symlinks.
To use clang++.exe, you can just copy clang.exe to clang++.exe .

Not that it matters, but as a quick FYI, you can use tar to de-reference symlinks:
Code
-h, --dereference            follow symlinks; archive and dump the files they point to
    --hard-dereference       follow hard links; archive and dump the files they refer to

I use these options when preparing MinGW packages that I've compiled under Cygwin.

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: Clang command line support for codecompletion
« Reply #13 on: October 26, 2010, 07:05:27 pm »
Not that it matters, but as a quick FYI, you can use tar to de-reference symlinks:

I know that, but I cros-compiled it on linux, and windows can not follow symlinks (at least not linux-symlinks, and the windows links are not real links, just link-files).

By the way, 7z seems to work correctly with symlinks (just access control and ownership is not copied without tar).

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5905
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Clang command line support for codecompletion
« Reply #14 on: October 27, 2010, 02:41:21 am »
I already integrated this code into codelite and it seems to work very well.
The problem is that it is too slow.

Most of the time the parsing is done using an in-complete source file so you will need to provide a string buffer containing the input string up to the point where the completion should appear.

The problem is that, I tried to use the completion using codelite's project itself on the file frame.cpp (one of the largest files in codelite project) it took 4 seconds for the completion to show (or even more, can't remember now it was 3-4 months ago)

It also lacks some crucial information (which I could not seem to get an access to, like an exact position of the match), I can probably obtain more information if I will link directly with the clang libraries - but for the POC (prove of concept) it seems too much for me.

I had several ideas of how to improve the responsiveness of the code-completion by using a pre-compiled header, but it seems like that PCH is not yet supported for C++ (at least it was not available couple of months ago)

You can search their mailing list and see the discussion I had with DGregor (he is the person that knows most about code-completion in clang)


About the binaries, I remembers that enabling optimization causes a crash or ICE using MinGW. Also, make sure you build clang in release mode (which is NOT the default) it will boost its performance a lot.

I will have a look at home of how I built clang and will see if I can upload the binaries to codelite's site

Eran
 

thanks eran for the explanation and help.
I just do a simple search and found that clang now support PCH, see:  Precompiled Headers (PCH)

Quote
Using Precompiled Headers with clang

The Clang compiler frontend, clang -cc1, supports two command line options for generating and using PCH files.

To generate PCH files using clang -cc1, use the option -emit-pch:

 $ clang -cc1 test.h -emit-pch -o test.h.pch

This option is transparently used by clang when generating PCH files. The resulting PCH file contains the serialized form of the compiler's internal representation after it has completed parsing and semantic analysis. The PCH file can then be used as a prefix header with the -include-pch option:

  $ clang -cc1 -include-pch test.h.pch test.c -o test.s


@jens:
thanks for your time and effort to build the win32 version of clang for me, it works quite well.

for the symlink issue, under windows, I use 7-zip to extract the files, but some files like clang++.exe was less than 10k. So, as you suggest, I need to copy and renamed the clang.exe.

I will do more test on clang today.
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.