Skip to content

Commit

Permalink
Adding workaround for libclang bug on mac
Browse files Browse the repository at this point in the history
On Mac OS X, libclang will fail to include the standard header search
paths that clang will. So we add the paths ourselves.

Fixes #303
  • Loading branch information
Valloric committed Mar 27, 2015
1 parent 03d18ea commit 11edf12
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
7 changes: 0 additions & 7 deletions cpp/ycm/.ycm_extra_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,6 @@
'-isystem',
'./tests/gmock/include',
'-isystem',
'/usr/include',
'-isystem',
'/usr/local/include',
'-isystem',
'/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1',
'-isystem',
'/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include',
]


Expand Down
33 changes: 28 additions & 5 deletions ycmd/completers/cpp/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import os
import inspect
from ycmd import extra_conf_store
from ycmd.utils import ToUtf8IfNeeded
from ycmd.utils import ToUtf8IfNeeded, OnMac
from ycmd.responses import NoExtraConfDetected

INCLUDE_FLAGS = [ '-isystem', '-I', '-iquote', '--sysroot=', '-isysroot',
Expand All @@ -35,6 +35,21 @@
# https://gcc.gnu.org/onlinedocs/gcc-4.9.0/gcc/Preprocessor-Options.html
FILE_FLAGS_TO_SKIP = set(['-MD', '-MMD', '-MF', '-MT', '-MQ', '-o'])

# These are the standard header search paths that clang will use on Mac BUT
# libclang won't, for unknown reasons. We add these paths when the user is on a
# Mac because if we don't, libclang would fail to find <vector> etc.
# This should be fixed upstream in libclang, but until it does, we need to help
# users out.
# See Valloric/YouCompleteMe#303 for details.
MAC_INCLUDE_PATHS = [
'/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1',
'/usr/local/include',
'/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include',
'/usr/include',
'/System/Library/Frameworks',
'/Library/Frameworks',
]


class Flags( object ):
"""Keeps track of the flags necessary to compile a file.
Expand All @@ -44,13 +59,13 @@ class Flags( object ):
def __init__( self ):
# It's caches all the way down...
self.flags_for_file = {}
self.special_clang_flags = _SpecialClangIncludes()
self.extra_clang_flags = _ExtraClangFlags()
self.no_extra_conf_file_warning_posted = False


def FlagsForFile( self,
filename,
add_special_clang_flags = True,
add_extra_clang_flags = True,
client_data = None ):
try:
return self.flags_for_file[ filename ]
Expand All @@ -73,8 +88,8 @@ def FlagsForFile( self,
if not flags:
return None

if add_special_clang_flags:
flags += self.special_clang_flags
if add_extra_clang_flags:
flags += self.extra_clang_flags
sanitized_flags = PrepareFlagsForClang( flags, filename )

if results[ 'do_cache' ]:
Expand Down Expand Up @@ -207,6 +222,14 @@ def _RemoveUnusedFlags( flags, filename ):
return new_flags


def _ExtraClangFlags():
flags = _SpecialClangIncludes()
if OnMac():
for path in MAC_INCLUDE_PATHS:
flags.extend( [ '-isystem', path ] )
return flags


def _SpecialClangIncludes():
libclang_dir = os.path.dirname( ycm_core.__file__ )
path_to_includes = os.path.join( libclang_dir, 'clang_includes' )
Expand Down
4 changes: 4 additions & 0 deletions ycmd/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ def OnCygwin():
return sys.platform == 'cygwin'


def OnMac():
return sys.platform == 'darwin'


# From here: http://stackoverflow.com/a/8536476/1672783
def TerminateProcess( pid ):
if OnWindows():
Expand Down

0 comments on commit 11edf12

Please sign in to comment.