Skip to content

Commit

Permalink
Automatically use libclang if available
Browse files Browse the repository at this point in the history
If the user did not request anything specific, we first try to use
libclang and only fall back to the clang executable if libclang could
not be found. Error messages are only printed in debugging mode or
if the user explicitly requested libclang.

This patch allows us to use libclang.so as soon as it can be found in
the system library path. No further configuration is required in
.vimrc.
  • Loading branch information
tobiasgrosser committed Jan 13, 2013
1 parent 2f5f59b commit 22ada62
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
26 changes: 11 additions & 15 deletions plugin/clang_complete.vim
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,6 @@ function! s:ClangCompleteInit()
let g:clang_compilation_database = ''
endif

" Only use libclang if the user clearly show intent to do so for now
if !exists('g:clang_use_library')
let g:clang_use_library = (has('python') && exists('g:clang_library_path'))
endif

if !exists('g:clang_library_path')
let g:clang_library_path = ''
endif
Expand Down Expand Up @@ -165,11 +160,10 @@ function! s:ClangCompleteInit()
augroup end
endif

if g:clang_use_library == 1
let l:initialized = s:initClangCompletePython()
if l:initialized == 0
let g:clang_use_library = 0
endif
if !exists('g:clang_use_library') || g:clang_use_library == 1
" Try to use libclang. On failure, we fall back to the clang executable.
let l:initialized = s:initClangCompletePython(exists('g:clang_use_library'))
let g:clang_use_library = l:initialized
endif
endfunction

Expand Down Expand Up @@ -249,11 +243,13 @@ function! s:parsePathOption()
endfor
endfunction

function! s:initClangCompletePython()
function! s:initClangCompletePython(user_requested)
if !has('python')
echoe 'clang_complete: No python support available.'
echoe 'Cannot use clang library, using executable'
echoe 'Compile vim with python support to use libclang'
if a:user_requested || g:clang_debug
echoe 'clang_complete: No python support available.'
echoe 'Cannot use clang library, using executable'
echoe 'Compile vim with python support to use libclang'
endif
return 0
endif

Expand All @@ -263,7 +259,7 @@ function! s:initClangCompletePython()

exe 'python sys.path = ["' . s:plugin_path . '"] + sys.path'
exe 'pyfile ' . s:plugin_path . '/libclang.py'
py vim.command('let l:res = ' + str(initClangComplete(vim.eval('g:clang_complete_lib_flags'), vim.eval('g:clang_compilation_database'), vim.eval('g:clang_library_path'))))
py vim.command('let l:res = ' + str(initClangComplete(vim.eval('g:clang_complete_lib_flags'), vim.eval('g:clang_compilation_database'), vim.eval('g:clang_library_path'), vim.eval('a:user_requested'))))
if l:res == 0
return 0
endif
Expand Down
20 changes: 13 additions & 7 deletions plugin/libclang.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ def getBuiltinHeaderPath(library_path):
return path
return None

def initClangComplete(clang_complete_flags, clang_compilation_database, library_path):
def initClangComplete(clang_complete_flags, clang_compilation_database, \
library_path, user_requested):
global index

debug = int(vim.eval("g:clang_debug")) == 1
printWarnings = (user_requested != "0") or debug

if library_path != "":
Config.set_library_path(library_path)

Expand All @@ -46,19 +51,20 @@ def initClangComplete(clang_complete_flags, clang_compilation_database, library_
try:
index = Index.create()
except Exception, e:
print "Loading libclang failed, falling back to clang executable. ",
if library_path == "":
print "Consider setting g:clang_library_path"
else:
print "Are you sure '%s' contains libclang?" % library_path
if printWarnings:
print "Loading libclang failed, falling back to clang executable. ",
if library_path == "":
print "Consider setting g:clang_library_path"
else:
print "Are you sure '%s' contains libclang?" % library_path
return 0

global builtinHeaderPath
builtinHeaderPath = None
if not canFindBuiltinHeaders(index):
builtinHeaderPath = getBuiltinHeaderPath(library_path)

if not builtinHeaderPath:
if not builtinHeaderPath and printWarnings:
print "WARNING: libclang can not find the builtin includes."
print " This will cause slow code completion."
print " Please report the problem."
Expand Down

0 comments on commit 22ada62

Please sign in to comment.