YouCompleteMe not work properly for C headers

1.4k Views Asked by At

I was writing a small c program for learning system calls, and found out that YouCompleteMe in my vim can not give correct completion for functions like sigaction in C headers. This is the code header I included.

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <signal.h>

and when I type siga , the completion panel is like this: enter image description here
It does not give the correct completion for sigaction function, I guess it's something wrong about my flags setting in .ycm_extra_conf.py file. This is .ycm_extra_conf.py file for my C program:

import os
import ycm_core

flags = [
'-Wall',
'-Wextra',
'-Werror',
'-Wc++98-compat',
'-Wno-long-long',
'-Wno-variadic-macros',
'-fexceptions',
'-DNDEBUG',
# THIS IS IMPORTANT! Without a "-std=<something>" flag, clang won't know which
# language to use when compiling headers. So it will guess. Badly. So C++
# headers will be compiled as C headers. You don't want that so ALWAYS specify
# a "-std=<something>".
# For a C project, you would set this to something like 'c99' instead of
# 'c++11'.
'-std=c99',
# ...and the same thing goes for the magic -x option which specifies the
# language that the files to be compiled are written in. This is mostly
# relevant for c++ headers.
# For a C project, you would set this to 'c' instead of 'c++'.
'-x', 'c',
'-isystem', '/usr/local/include',
'-isystem', '/usr/include/x86_64-linux-gnu',
'-isystem', '/usr/include'
]

This is YcmDebugInfo:

Printing YouCompleteMe debug information...
-- Resolve completions: Never
-- Client logfile: /tmp/ycm_3yj6gesj.log
-- Server Python interpreter: /usr/bin/python3
-- Server Python version: 3.8.2
-- Server has Clang support compiled in: False
-- Clang version: None
-- Extra configuration file found and loaded
-- Extra configuration path: /home/zxy/understanding-unix-linux-programming/ch7/.ycm_extra_conf.py
-- C-family completer debug information:
--   Clangd running
--   Clangd process ID: 177623
--   Clangd executable: ['/home/zxy/.vim/plugged/YouCompleteMe/third_party/ycmd/third_party/clangd/output/bin/clangd', '-header-insertion-decorators=0', '-resource-dir=/home/zxy/.vim/plugged/YouCompleteMe/third_
party/ycmd/third_party/clang/lib/clang/10.0.0', '-limit-results=500']
--   Clangd logfiles:
--     /tmp/clangd_stderrtwgnq048.log
--   Clangd Server State: Initialized
--   Clangd Project Directory: /home/zxy/understanding-unix-linux-programming/ch7
--   Clangd Settings: {}
--   Clangd Compilation Command: ['clang-tool', '-Wall', '-Wextra', '-Werror', '-fexceptions', '-DNDEBUG', '-std=c99', '-x', 'c', '-isystem', '/usr/local/include', '-isystem', '/usr/include/x86_64-linux-gnu', '-
isystem', '/usr/include', '/home/zxy/understanding-unix-linux-programming/ch7/sigactdemo.c']
-- Server running at: http://127.0.0.1:55187
-- Server process ID: 177581
-- Server logfiles:
--   /tmp/ycmd_55187_stdout_235pm2ty.log
--   /tmp/ycmd_55187_stderr_pd5sgbpp.log

And this is from one of YcmToggleLogs files:

I[17:12:54.491] clangd version 10.0.0 (https://github.com/ycm-core/llvm 038587147cf2f97d1c3e677042f69560c65b5bea)
I[17:12:54.491] PID: 177542
I[17:12:54.491] Working directory: /home/zxy/understanding-unix-linux-programming/ch7
I[17:12:54.491] argv[0]: /home/zxy/.vim/plugged/YouCompleteMe/third_party/ycmd/third_party/clangd/output/bin/clangd
I[17:12:54.491] argv[1]: -header-insertion-decorators=0
I[17:12:54.491] argv[2]: -resource-dir=/home/zxy/.vim/plugged/YouCompleteMe/third_party/ycmd/third_party/clang/lib/clang/10.0.0
I[17:12:54.491] argv[3]: -limit-results=500
I[17:12:54.491] Starting LSP over stdin/stdout
I[17:12:54.497] <-- initialize(1)
I[17:12:54.497] --> reply:initialize(1) 0 ms
I[17:12:54.498] <-- initialized
I[17:12:54.498] unhandled notification initialized
I[17:12:54.498] <-- workspace/didChangeConfiguration
I[17:12:54.499] <-- workspace/didChangeConfiguration
I[17:12:54.499] Failed to find compilation database for /home/zxy/understanding-unix-linux-programming/ch7/sigactdemo.c
I[17:12:54.499] <-- textDocument/didOpen
I[17:12:54.500] Updating file /home/zxy/understanding-unix-linux-programming/ch7/sigactdemo.c with command
[/home/zxy/understanding-unix-linux-programming/ch7]
clang-tool -Wall -Wextra -Werror -fexceptions -DNDEBUG -std=c99 -x c -isystem /usr/local/include -isystem /usr/include/x86_64-linux-gnu -isystem /usr/include /home/zxy/understanding-unix-linux-programming/ch7/sigactdemo.c -fsyntax-only -resource-dir=/home/zxy/.vim/plugged/YouCompleteMe/third_party/ycmd/third_party/clang/lib/clang/10.0.0
I[17:12:54.501] Enqueueing 1 commands for indexing
E[17:12:54.501] Tried to create storage for empty directory!
I[17:12:54.559] --> textDocument/publishDiagnostics
I[17:12:54.592] Indexed /home/zxy/understanding-unix-linux-programming/ch7/sigactdemo.c (601 symbols, 1916 refs, 43 files)
I[17:12:54.592] Failed to compile /home/zxy/understanding-unix-linux-programming/ch7/sigactdemo.c, index may be incomplete
E[17:12:54.593] Failed to write background-index shard for file /usr/include/features.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.594] Failed to write background-index shard for file /home/zxy/.vim/plugged/YouCompleteMe/third_party/ycmd/third_party/clang/lib/clang/10.0.0/include/stdbool.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.594] Failed to write background-index shard for file /home/zxy/.vim/plugged/YouCompleteMe/third_party/ycmd/third_party/clang/lib/clang/10.0.0/include/stddef.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.594] Failed to write background-index shard for file /usr/include/x86_64-linux-gnu/bits/types/clockid_t.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.594] Failed to write background-index shard for file /usr/include/stdlib.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.595] Failed to write background-index shard for file /usr/include/x86_64-linux-gnu/bits/types/__fpos64_t.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.595] Failed to write background-index shard for file /usr/include/x86_64-linux-gnu/bits/types/timer_t.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.595] Failed to write background-index shard for file /usr/include/stdc-predef.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.595] Failed to write background-index shard for file /usr/include/x86_64-linux-gnu/gnu/stubs-64.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.596] Failed to write background-index shard for file /usr/include/x86_64-linux-gnu/sys/types.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.596] Failed to write background-index shard for file /usr/include/x86_64-linux-gnu/bits/sys_errlist.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.596] Failed to write background-index shard for file /usr/include/x86_64-linux-gnu/bits/floatn.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.596] Failed to write background-index shard for file /usr/include/unistd.h: atomic_write_error: failed_to_create_uniq_file                                                                              E[17:12:54.597] Failed to write background-index shard for file /usr/include/x86_64-linux-gnu/bits/types/FILE.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.597] Failed to write background-index shard for file /usr/include/x86_64-linux-gnu/bits/unistd_ext.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.597] Failed to write background-index shard for file /usr/include/x86_64-linux-gnu/gnu/stubs.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.597] Failed to write background-index shard for file /usr/include/x86_64-linux-gnu/bits/timesize.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.598] Failed to write background-index shard for file /usr/include/x86_64-linux-gnu/bits/libc-header-start.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.598] Failed to write background-index shard for file /usr/include/signal.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.598] Failed to write background-index shard for file /usr/include/x86_64-linux-gnu/bits/types/__fpos_t.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.598] Failed to write background-index shard for file /usr/include/x86_64-linux-gnu/bits/stdio_lim.h: atomic_write_error: failed_to_create_uniq_file                                                     E[17:12:54.599] Failed to write background-index shard for file /usr/include/x86_64-linux-gnu/bits/signal_ext.h: atomic_write_error: failed_to_create_uniq_file
E[17:12:54.599] Failed to write background-index shard for file /usr/include/stdio.h: atomic_write_error: failed_to_create_uniq_file                                                                               E[17:12:54.599] Failed to write background-index shard for file /usr/include/x86_64-linux-gnu/bits/typesizes.h: atomic_write_error: failed_to_create_uniq_file

but if I configure .ycm_extra_conf.py file for cpp,

import os
import ycm_core

flags = [
'-Wall',
'-Wextra',
'-Werror',
'-fexceptions',
'-DNDEBUG',
# THIS IS IMPORTANT! Without a "-std=<something>" flag, clang won't know which
# language to use when compiling headers. So it will guess. Badly. So C++
# headers will be compiled as C headers. You don't want that so ALWAYS specify
# a "-std=<something>".
# For a C project, you would set this to something like 'c99' instead of
# 'c++11'.
'-std=c++11',
# ...and the same thing goes for the magic -x option which specifies the
# language that the files to be compiled are written in. This is mostly
# relevant for c++ headers.
# For a C project, you would set this to 'c' instead of 'c++'.
'-x', 'c++',
'-isystem/usr/include/c++/9',
'-isystem/usr/include/x86_64-linux-gnu',
'-isystem/usr/local/include',
'-isystem/usr/include'
]

the completion works ok without changing my code :enter image description here

can someone help me on this? I've been searching whole day, still no solultion...

1

There are 1 best solutions below

0
On

This is an old question but I would like to provide my suggestion for future reference. I had similar problems with youcompleteme as well. After carefully reading the project README, I learned that the preferred way to configure youcompleteme for c family of languages is to use the compilation database. You can generate the compile_commands.json (compilation database in json format) using your build tool and place it in the root directory of your project. youcompleteme will pickup the compilation database automatically when you open vim on the project root directory. Avoid using .ycm_extra_conf.py in general and resort to using it only as last option.