ycm-core / YouCompleteMe

A code-completion engine for Vim
http://ycm-core.github.io/YouCompleteMe/
GNU General Public License v3.0
25.46k stars 2.81k forks source link

An AST deserialization error for header files only #4049

Closed tearandfix closed 2 years ago

tearandfix commented 2 years ago

Issue Prelude

After system update I'm having ClangParseError: An AST deserialization error occurred while parsing the translation unit. for all .h/.hpp files. I'm using compile_commands.json for compilation flags. It seems like the problem is with flags for headers. There's addition '--' flag in the flags for the header and it causes this error. It seems like when the flags are deduced from .cpp file an additional '--' is included for some reason. If I manually place a record in compile_commands.json for the header just by copying it from the .cpp file there is no error.

Can you please point me to the place where compilation flags are formed for the header? I mean where they are copied from the corresponding .cpp file?

I tried it on a simple cmake example: https://github.com/bast/cmake-example I've also tried downgrading YouCompleteMe, ycmd, vim, python without success.

Please complete these steps and check these boxes (by putting an x inside the brackets) before filing your issue:

Thank you for adhering to this process! It ensures your issue is resolved quickly and that neither your nor our time is needlessly wasted.

Issue Details

Provide a clear description of the problem, including the following key questions:

  1. git clone https://github.com/bast/cmake-example.git
  2. cd cmake-example
  3. mkdir build
  4. cd build
  5. cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..
  6. cd ..
  7. cp build/compile_commands.json .
  8. vim -Nu ~/.vim/bundle/YouCompleteMe/vimrc_ycm_minimal src/example.h

I expect ycm to work with header file the same way as with the source file.

YCM reports error: ClangParseError: An AST deserialization error occurred while parsing the translation unit. :YcmDebugInfo shows additional '--' in the flags. Which I believe caused this error.

Diagnostic data

Output of vim --version

VIM - Vi IMproved 9.0 (2022 Jun 28, compiled Aug 21 2022 14:20:23)
Included patches: 1-236
Compiled by Arch Linux
Huge version with GTK3 GUI.  Features included (+) or not (-):
+acl               +file_in_path      +mouse_urxvt       -tag_any_white
+arabic            +find_in_path      +mouse_xterm       +tcl/dyn
+autocmd           +float             +multi_byte        +termguicolors
+autochdir         +folding           +multi_lang        +terminal
-autoservername    -footer            -mzscheme          +terminfo
+balloon_eval      +fork()            +netbeans_intg     +termresponse
+balloon_eval_term +gettext           +num64             +textobjects
+browse            -hangul_input      +packages          +textprop
++builtin_terms    +iconv             +path_extra        +timers
+byte_offset       +insert_expand     +perl/dyn          +title
+channel           +ipv6              +persistent_undo   +toolbar
+cindent           +job               +popupwin          +user_commands
+clientserver      +jumplist          +postscript        +vartabs
+clipboard         +keymap            +printer           +vertsplit
+cmdline_compl     +lambda            +profile           +vim9script
+cmdline_hist      +langmap           -python            +viminfo
+cmdline_info      +libcall           +python3/dyn       +virtualedit
+comments          +linebreak         +quickfix          +visual
+conceal           +lispindent        +reltime           +visualextra
+cryptv            +listcmds          +rightleft         +vreplace
+cscope            +localmap          +ruby/dyn          +wildignore
+cursorbind        +lua/dyn           +scrollbind        +wildmenu
+cursorshape       +menu              +signs             +windows
+dialog_con_gui    +mksession         +smartindent       +writebackup
+diff              +modify_fname      -sodium            +X11
+digraphs          +mouse             +sound             -xfontset
+dnd               +mouseshape        +spell             +xim
-ebcdic            +mouse_dec         +startuptime       -xpm
+emacs_tags        +mouse_gpm         +statusline        +xsmp_interact
+eval              -mouse_jsbterm     -sun_workshop      +xterm_clipboard
+ex_extra          +mouse_netterm     +syntax            -xterm_save
+extra_search      +mouse_sgr         +tag_binary
-farsi             -mouse_sysmouse    -tag_old_static
   system vimrc file: "/etc/vimrc"
     user vimrc file: "$HOME/.vimrc"
 2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
  system gvimrc file: "/etc/gvimrc"
    user gvimrc file: "$HOME/.gvimrc"
2nd user gvimrc file: "~/.vim/gvimrc"
       defaults file: "$VIMRUNTIME/defaults.vim"
    system menu file: "$VIMRUNTIME/menu.vim"
  fall-back for $VIM: "/usr/share/vim"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H -DFEAT_GUI_GTK -I/usr/include/gtk-3.0 -I/usr/include/pango-1.0 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/sysprof-4 -I/usr/include/harfbuzz -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/fribidi -I/usr/include/cairo -I/usr/include/lzo -I/usr/include/pixman-1 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/gio-unix-2.0 -I/usr/include/cloudproviders -I/usr/include/atk-1.0 -I/usr/include/at-spi2-atk/2.0 -I/usr/include/dbus-1.0 -I/usr/lib/dbus-1.0/include -I/usr/include/at-spi-2.0 -pthread -march=x86-64 -mtune=generic -O2 -pipe -fno-plt -fexceptions -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection -g -ffile-prefix-map=/build/vim/src=/usr/src/debug -flto=auto -D_REENTRANT -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1
Linking: gcc -L. -Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now -fstack-protector-strong -rdynamic -Wl,-export-dynamic -Wl,--no-as-needed -Wl,-E -Wl,-rpath,/usr/lib/perl5/5.36/core_perl/CORE -Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now -flto=auto -L/usr/local/lib -o vim -lgtk-3 -lgdk-3 -lz -lpangocairo-1.0 -lpango-1.0 -lharfbuzz -latk-1.0 -lcairo-gobject -lcairo -lgdk_pixbuf-2.0 -lgio-2.0 -lgobject-2.0 -lglib-2.0 -lSM -lICE -lXt -lX11 -lXdmcp -lSM -lICE -lm -ltinfo -lelf -lcanberra -lacl -lattr -lgpm -Wl,-E -Wl,-rpath,/usr/lib/perl5/5.36/core_perl/CORE -Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now -flto -fstack-protector-strong -L/usr/local/lib -L/usr/lib/perl5/5.36/core_perl/CORE -lperl -lpthread -ldl -lm -lcrypt -lutil -lc -L/usr/lib -ltclstub8.6 -ldl -lz -lpthread -lm

Output of YcmDebugInfo

Printing YouCompleteMe debug information...
-- Resolve completions: Up front
-- Client logfile: /tmp/ycm_1bakw1bh.log
-- Server Python interpreter: /usr/bin/python3
-- Server Python version: 3.10.6
-- Server has Clang support compiled in: True
-- Clang version: clang version 14.0.0 (https://github.com/ycm-core/llvm 38140b7637346fd0c62e5133c01acdc74797f2dc)
-- No extra configuration file found
-- C-family completer debug information:
--   Compilation database path: /home/yura/src/cmake-example
--   Flags: ['/usr/bin/c++', '-x', 'c++', '--driver-mode=g++', '-I/home/yura/src/cmake-example/src', '-Wall', '-Wno-unknown-pragmas', '-Wno-sign-compare', '-Woverloaded-virtual', '-Wwrite-st
rings', '-Wno-unused', '-fprofile-arcs', '-ftest-coverage', '-O3', '-x', 'c++-header', '--', '-resource-dir=/home/yura/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/clang/lib/clang/
14.0.0', '-fspell-checking']
--   Translation unit: /home/yura/src/cmake-example/src/example.h
-- Server running at: http://127.0.0.1:48555
-- Server process ID: 383391
-- Server logfiles:
--   /tmp/ycmd_48555_stdout_uo3auo4v.log
--   /tmp/ycmd_48555_stderr_ipji391c.log
-- Semantic highlighting supported: True
-- Virtual text supported: True
-- Popup windows supported: True

Output of YcmDiags

No warnings or errors detected.

Output of git rev-parse HEAD in YouCompleteMe installation directory

0dc8989e394ce02adb1c4a0d4a7981017e212996

Contents of YCM, ycmd and completion engine logfiles

Reproduce the issue with vim -Nu /path/to/YCM/vimrc_ycm_minimal, which enabled debug logging and other useful diagnostics. Include a link to a gist containing all of the log files listed by :YcmToggleLogs. https://gist.github.com/tearandfix/411a91741367e1749b72ab47632307dd https://gist.github.com/tearandfix/adf400a41de8f34412e604ca42610907 https://gist.github.com/tearandfix/9b5250f921d643b8b45f74ec656621ea

OS version, distribution, etc.

Arch Linux up to date

Output of build/install commands

Include link to a gist containing the invocation and entire output of install.py if reporting an installation issue.

puremourning commented 2 years ago

Thanks for the report. I suspect a recent change to the flags code broke this. Apologies for that.

But given that you have a compile_commands.json, I would strongly recommend you switch to using clangd (rather than the now-legacy libclang completer) and remove your .ycm_extra_conf.py entirely. This will give you a much better experience, with many more features, faster and a complete index.

puremourning commented 2 years ago

I can't build your repro case:

ben@BeniMac2020 cmake-example % cmake -S . -B build -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
-- The Fortran compiler identification is unknown
CMake Error at CMakeLists.txt:5 (project):
  No CMAKE_Fortran_COMPILER could be found.

  Tell CMake where to find the compiler by setting either the environment
  variable "FC" or the CMake cache entry CMAKE_Fortran_COMPILER to the full
  path to the compiler, or to the compiler name if it is in the PATH.

-- Configuring incomplete, errors occurred!
See also "/Users/ben/Development/YouCompleteMe/cmake-example/build/CMakeFiles/CMakeOutput.log".
See also "/Users/ben/Development/YouCompleteMe/cmake-example/build/CMakeFiles/CMakeError.log".

I see there is no .ycm_extra_conf.py in there, but I still recommend switching to clangd ./install.py --clangd-completer.

puremourning commented 2 years ago

OK I installed a fortran compiler and it works.

Hmm interesting, it doesn't reproduce on macOS (which is OK because we have different flags manipulations for macOS):

-- C-family completer debug information:
--   Compilation database path: /Users/ben/Development/YouCompleteMe/cmake-example
--   Flags: ['/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++', '-x', 'c++', '--driver-mode=g++', '-I/Users/ben/Development/YouCompleteMe/cmake
-example/src', '-O3', '-DNDEBUG', '-isysroot', '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk', '-resource-dir=/Users/ben/.vim/bundle
/YouCompleteMe/third_party/ycmd/third_party/clang/lib/clang/14.0.0', '-isystem', '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/i
nclude/c++/v1', '-isystem', '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/local/include', '-isystem', '/usr/local/include', '-is
ystem', '/Users/ben/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/clang/lib/clang/14.0.0/include', '-isystem', '/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefaul
t.xctoolchain/usr/include', '-isystem', '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include', '-iframework', '/Applications/Xc
ode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/System/Library/Frameworks', '-iframework', '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX
.platform/Developer/SDKs/MacOSX12.3.sdk/Library/Frameworks', '-fspell-checking']
--   Translation unit: /Users/ben/Development/YouCompleteMe/cmake-example/src/example.h

Can you share the generated compile_commands.json ?

tearandfix commented 2 years ago

I've switched to --clangd-completer and it solved the issue. Thanks.

I can't build your repro case

Probably some environment issue. But I was able to reproduce it on any cmake project with a header file. Cmake project needs to generate 'compile_commands.json`

tearandfix commented 2 years ago

Can you share the generated compile_commands.json ?

https://gist.github.com/tearandfix/168acd485a2af235a137bb23afd9268e#file-compile_commands-json

puremourning commented 2 years ago

This was broken by the upgrade to clang 14.0. The libclang compilation database now returns the extra -- flag. We'll need to filter it out.