Youcompleteme: Only local completion for C

Created on 22 Sep 2013  路  5Comments  路  Source: ycm-core/YouCompleteMe

Hi!

I'm using vim 7.4 and clang 3.3 on ArchLinux. I installed ycm through

./install --clang-completer --system-libclang

I also tried

./install --clang-completer

but it didn't change anything.

I am using the following .ycm_extra_conf.py file to work on a C project:

# This file is NOT licensed under the GPLv3, which is the license for the rest
# of YouCompleteMe.
#
# Here's the license text for this file:
#
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# For more information, please refer to <http://unlicense.org/>
import os
import ycm_core

flags = [
    '-Wall',
    '-Wextra',
    '-DUSE_CLANG_COMPLETER',
    '-std=c99',
    '-x',
    'c',
    '-isystem',
    '/usr/include/'
]
compilation_database_folder = ''

if compilation_database_folder:
    database = ycm_core.CompilationDatabase(compilation_database_folder)
else:
    database = None

def DirectoryOfThisScript():
    return os.path.dirname(os.path.abspath(__file__))

def MakeRelativePathsInFlagsAbsolute(flags, working_directory):
    if not working_directory:
        return list(flags)

    new_flags = []
    make_next_absolute = False
    path_flags = ['-isystem', '-I', '-iquote', '--sysroot=']

    for flag in flags:
        new_flag = flag
        if make_next_absolute:
            make_next_absolute = False
            if not flag.startswith('/'):
                new_flag = os.path.join(working_directory, flag)
        for path_flag in path_flags:
            if flag == path_flag:
                make_next_absolute = True
                break
            if flag.startswith(path_flag):
                path = flag[len(path_flag):]
                new_flag = path_flag + os.path.join(working_directory, path)
                break
        if new_flag:
            new_flags.append(new_flag)
    return new_flags

def FlagsForFile(filename):
    if database:
        compilation_info = database.GetCompilationInfoForFile(filename)
        final_flags = MakeRelativePathsInFlagsAbsolute(compilation_info.compiler_flags_, compilation_info.compiler_working_dir_)
    else:
        final_flags = MakeRelativePathsInFlagsAbsolute(flags, DirectoryOfThisScript())
    return {
        'flags': final_flags,
        'do_cache': True
    }

When coding i only get completion for local variables and functions. Functions from stdlib.h or stdio.h for example won't work. This really bothers me as I can compile my files with with clang and the parameters specified in the file above.

:YcmDebugInfo

shows me that the parameters are loaded properly and that clang support is compiled in.

:YcmDiags

says that there are no errors compiling my project. At this moment my project is a HelloWorld-program to get c-completion to work:

#include <stdio.h>
#include <stdlib.h>

int main() {
    printf("Hello World!\n");
    return EXIT_SUCCESS;
}

What am I missing? As you can see I also forced /usr/include (place where stdlib.h, stdio.h ar) to be an include path. But if that would have been the problem, clang would have complained about it.

Thaks in advance

Most helpful comment

If you search in the issues you will find something similar if not identical to your request, and the answer is: you don't. In the GIF on the front page the code is C++ that have more "semantic trigger", so we can ask for completion only when is really needed. Calling the clang backend at every keystroke is too expensive (it should recompile the project for generate the AST at every keystroke which is bad) so you can't do that.

All 5 comments

You mean that if you are inside the main() funciton and press Ctrl + Space nothing is showed?

Ctrl + Space shows me the correct completion. I expected YCM to do this automatically when typing (like in the gif on the front page). Now how do I get this to work?

If you search in the issues you will find something similar if not identical to your request, and the answer is: you don't. In the GIF on the front page the code is C++ that have more "semantic trigger", so we can ask for completion only when is really needed. Calling the clang backend at every keystroke is too expensive (it should recompile the project for generate the AST at every keystroke which is bad) so you can't do that.

Ok. Thanks a lot, that sound reasonable. I guess this issue can be closed. :)

Hi, all!

I have a workaround for this, two steps:
1. set the following in vimrc:

let g:ycm_collect_identifiers_from_tags_files = 1

2. call Ctags with the '--fields=+l' option. For example, run the following command in project folder:

find -L . -regex ".*\.\(h\|c\|hpp\|cpp\)" | xargs gcc -M | sed -e 's/[\\ ]/\n/g' | sed -e '/^$/d' -e '/\.o:[ \t]*$/d' | ctags -L - -I __attribute__,__attribute_deprecated__,__attribute_format_arg__,__attribute_format_strfmon__,__attribute_malloc__,__attribute_noinline__,__attribute_pure__,__attribute_used__,__attribute_warn_unused_result__,__wur,__THROW,__nonnull+ --file-scope=yes --langmap=c:+.h --languages=c,c++ --links=yes --c-kinds=+p --c++-kinds=+p --fields=+liaS --extra=+q

Refer to the following link for generating tags file as small as needed:
http://www.topbug.net/blog/2012/03/17/generate-ctags-files-for-c-slash-c-plus-plus-source-files-and-all-of-their-included-header-files/

WARNING: if tags file is too big, computer may stick for a long time.

Was this page helpful?
0 / 5 - 0 ratings