xavierd / clang_complete

Vim plugin that use clang for completing C/C++ code.
http://www.vim.org/scripts/script.php?script_id=3302
1.96k stars 308 forks source link

c++11 STL classes won't autocomplete, user-defined classes will #497

Closed tmsimont closed 8 years ago

tmsimont commented 8 years ago

Vim 7.4.2143

$ vim --version
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Aug  2 2016 05:20:25)
Included patches: 1-2143
Compiled by Arch Linux
Huge version without GUI.  Features included (+) or not (-):
+acl             +file_in_path    +mouse_sgr       +tag_old_static
+arabic          +find_in_path    -mouse_sysmouse  -tag_any_white
+autocmd         +float           +mouse_urxvt     +tcl/dyn
-balloon_eval    +folding         +mouse_xterm     +termguicolors
-browse          -footer          +multi_byte      +terminfo
++builtin_terms  +fork()          +multi_lang      +termresponse
+byte_offset     +gettext         -mzscheme        +textobjects
+channel         -hangul_input    +netbeans_intg   +timers
+cindent         +iconv           +num64           +title
-clientserver    +insert_expand   +packages        -toolbar
-clipboard       +job             +path_extra      +user_commands
+cmdline_compl   +jumplist        +perl/dyn        +vertsplit
+cmdline_hist    +keymap          +persistent_undo +virtualedit
+cmdline_info    +lambda          +postscript      +visual
+comments        +langmap         +printer         +visualextra
+conceal         +libcall         +profile         +viminfo
+cryptv          +linebreak       +python/dyn      +vreplace
+cscope          +lispindent      +python3/dyn     +wildignore
+cursorbind      +listcmds        +quickfix        +wildmenu
+cursorshape     +localmap        +reltime         +windows
+dialog_con      +lua/dyn         +rightleft       +writebackup
+diff            +menu            +ruby/dyn        -X11
+digraphs        +mksession       +scrollbind      -xfontset
-dnd             +modify_fname    +signs           -xim
-ebcdic          +mouse           +smartindent     -xsmp
+emacs_tags      -mouseshape      +startuptime     -xterm_clipboard
+eval            +mouse_dec       +statusline      -xterm_save
+ex_extra        +mouse_gpm       -sun_workshop    -xpm
+extra_search    -mouse_jsbterm   +syntax          
+farsi           +mouse_netterm   +tag_binary      
   system vimrc file: "/etc/vimrc"
     user vimrc file: "$HOME/.vimrc"
 2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
       defaults file: "$VIMRUNTIME/defaults.vim"
  fall-back for $VIM: "/usr/share/vim"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H   -D_FORTIFY_SOURCE=2  -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1      
Linking: gcc   -L. -Wl,-O1,--sort-common,--as-needed,-z,relro -fstack-protector -rdynamic -Wl,-export-dynamic -Wl,-E -Wl,-rpath,/usr/lib/perl5/core_perl/CORE  -Wl,-O1,--sort-common,--as-needed,-z,relro -L/usr/local/lib -Wl,--as-needed -o vim        -lm -lncurses -lelf -lnsl    -lacl -lattr -lgpm -ldl   -Wl,-E -Wl,-rpath,/usr/lib/perl5/core_perl/CORE -Wl,-O1,--sort-common,--as-needed,-z,relro -fstack-protector-strong -L/usr/local/lib  -L/usr/lib/perl5/core_perl/CORE -lperl -lpthread -lnsl -ldl -lm -lcrypt -lutil -lc   -L/usr/lib -ltclstub8.6 -ldl -lz -lpthread -lieee -lm    

I just pulled rip-rip/clang_complete from the master branch yesterday.

~/.vimrc

let g:clang_user_options="-std=c++11"
let g:clang_close_preview=1
let g:clang_auto_select=1
let g:clang_complete_copen=1

foo.cc

#include <vector>

class foo {
public:
    int bar;
}
int main() {
  foo f;
  std::vector<int> v;
  f.
} 

After typing the f. I see a popup with the following suggestions:

  operator= f foo & operator=(const foo &) 
  operator= f foo & operator=(foo &&)      
  ~foo      ~ void ~foo()                  
  bar       m int bar                                                                                                                                                                                            
  foo       t foo::  

This also works if I have foo.h and use .clang_complete to point to the header file.

However, this doesn't work with STL classes:

foo.cc

#include <vector>

class foo {
public:
    int bar;
}
int main() {
  foo f;
  std::vector<int> v;
  v.
} 

I then get: -- User defined completion (^U^N^P) Pattern not found

I tried using g:clang_use_library=1, too, but this has no effect. I also tried g:clang_use_library=0 but I get an error about not being able to use binary anymore.

I also tried using g:clang_library_path with no sucess.

As a side node, clang++ -std=c++11 foo.cc works without any issues to compile the project.

What can I do to fix?

tmsimont commented 8 years ago

also,

:echo has('python') returns 1

graywolf commented 8 years ago

As a side node, clang++ -std=c++11 foo.cc works without any issues to compile the project.

I don't believe you, I'm getting

$ clang++ -std=c++11 a.cpp
a.cpp:6:2: error: expected ';' after class
}
 ^
 ;
1 error generated.

I then get: -- User defined completion (^U^N^P) Pattern not found

Hm, that's weird, with your code

#include <vector>

class foo {
public:
    int bar;
}
int main() {
  foo f;
  std::vector<int> v;
  v.
} 

I get autocomplete as expected.

This is a long shot, but can you try gvim package instead of vim? I'm using Arch too, but my version string starts with

VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Aug  2 2016 05:21:06)
Included patches: 1-2143
Compiled by Arch Linux
Huge version with GTK2 GUI.  Features included (+) or not (-):

Can could you also post your clang version? Mine is:

$ clang --version
clang version 3.8.1 (tags/RELEASE_381/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

Relevant section of .vimrc:

let g:clang_use_library=1
let g:clang_complete_copen=1
let g:clang_snippets=1
let g:clang_snippets_engine='clang_complete'
let g:clang_complete_optional_args_in_snippets=1
let g:clang_close_preview=1
let g:clang_trailing_placeholder=1
let g:clang_complete_macros=1

Do you have any .clang_complete defined for this? It works out-of-the-box for me, so if yes, you can try to delete it and try it again.

As a last point, since we already cleared that your code doesn't actually compile while you claim it does, you obviously have something else on you disk. Could you tar -cvjf whole directory and send it to wolf@wolfsden.cz so we can determine if it's caused by the project or by system?

Since we both use Archlinux, system differences should be reasonably easy to figure out.

tmsimont commented 8 years ago

You're right -- I forgot the semicolon.

I just added it, and I still get the same error: -- User defined completion (^U^N^P) Pattern not found

However, I then removed .clang_complete which has this line: -include /Tree.h

The Tree.h file is in my project..

I changed it to -include Tree.h

And it worked!

So it seems that having a bad include path in .clang_complete was my problem.

graywolf commented 8 years ago

So it seems that having a bad include path in .clang_complete was my problem.

Bad include path is ok, what caused your problem was bad include file. Basically from clang's point of view you did #include "non-existent-file.h", which obviously broke the compilation.

May I ask what is the reasoning of using -include Tree.h instead of just putting #include "Tree.h" into the .cpp file?

tmsimont commented 8 years ago

I didn't know the latter would work :) I'm pretty new to this plugin.