2012-08-05 17:14:31 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright (C) 2011, 2012 Strahinja Val Markovic <val@markovic.io>
|
|
|
|
#
|
|
|
|
# This file is part of YouCompleteMe.
|
|
|
|
#
|
|
|
|
# YouCompleteMe is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# YouCompleteMe is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import ycm_core
|
2013-04-12 15:06:52 -04:00
|
|
|
import os
|
2013-10-26 19:22:43 -04:00
|
|
|
import inspect
|
2013-05-19 22:44:42 -04:00
|
|
|
from ycm import extra_conf_store
|
2013-09-27 17:59:00 -04:00
|
|
|
from ycm.utils import ToUtf8IfNeeded
|
2012-08-05 17:14:31 -04:00
|
|
|
|
2013-09-06 02:43:14 -04:00
|
|
|
NO_EXTRA_CONF_FILENAME_MESSAGE = ( 'No {0} file detected, so no compile flags '
|
2013-07-01 14:32:50 -04:00
|
|
|
'are available. Thus no semantic support for C/C++/ObjC/ObjC++. Go READ THE '
|
2013-09-06 02:43:14 -04:00
|
|
|
'DOCS *NOW*, DON\'T file a bug report.' ).format(
|
2013-07-01 14:32:50 -04:00
|
|
|
extra_conf_store.YCM_EXTRA_CONF_FILENAME )
|
2012-08-05 17:14:31 -04:00
|
|
|
|
2013-12-03 03:43:42 -05:00
|
|
|
INCLUDE_FLAGS = [ '-isystem', '-I', '-iquote', '--sysroot=', '-isysroot',
|
|
|
|
'-include' ]
|
2013-05-19 17:20:13 -04:00
|
|
|
|
2012-08-05 17:14:31 -04:00
|
|
|
class Flags( object ):
|
2013-02-25 04:58:04 -05:00
|
|
|
"""Keeps track of the flags necessary to compile a file.
|
2013-02-26 21:28:58 -05:00
|
|
|
The flags are loaded from user-created python files (hereafter referred to as
|
|
|
|
'modules') that contain a method FlagsForFile( filename )."""
|
|
|
|
|
2012-08-05 17:14:31 -04:00
|
|
|
def __init__( self ):
|
|
|
|
# It's caches all the way down...
|
|
|
|
self.flags_for_file = {}
|
2013-01-17 20:08:28 -05:00
|
|
|
self.special_clang_flags = _SpecialClangIncludes()
|
2013-10-08 19:21:43 -04:00
|
|
|
self.no_extra_conf_file_warning_posted = False
|
2012-08-05 17:14:31 -04:00
|
|
|
|
|
|
|
|
2013-10-26 19:22:43 -04:00
|
|
|
def FlagsForFile( self,
|
|
|
|
filename,
|
|
|
|
add_special_clang_flags = True,
|
|
|
|
client_data = None ):
|
2012-08-05 17:14:31 -04:00
|
|
|
try:
|
|
|
|
return self.flags_for_file[ filename ]
|
|
|
|
except KeyError:
|
2013-04-12 15:06:52 -04:00
|
|
|
module = extra_conf_store.ModuleForSourceFile( filename )
|
|
|
|
if not module:
|
2013-10-08 19:21:43 -04:00
|
|
|
if not self.no_extra_conf_file_warning_posted:
|
|
|
|
self.no_extra_conf_file_warning_posted = True
|
|
|
|
raise RuntimeError( NO_EXTRA_CONF_FILENAME_MESSAGE )
|
|
|
|
return None
|
2012-08-05 17:14:31 -04:00
|
|
|
|
2013-10-26 19:22:43 -04:00
|
|
|
results = _CallExtraConfFlagsForFile( module,
|
|
|
|
filename,
|
|
|
|
client_data )
|
2013-01-23 20:23:51 -05:00
|
|
|
|
2013-11-17 17:17:20 -05:00
|
|
|
if not results or not results.get( 'flags_ready', True ):
|
2013-01-23 20:23:51 -05:00
|
|
|
return None
|
|
|
|
|
2013-05-22 18:47:30 -04:00
|
|
|
flags = list( results[ 'flags' ] )
|
2013-11-17 16:32:43 -05:00
|
|
|
if not flags:
|
|
|
|
return None
|
|
|
|
|
2013-04-25 01:51:53 -04:00
|
|
|
if add_special_clang_flags:
|
2013-05-22 18:47:30 -04:00
|
|
|
flags += self.special_clang_flags
|
2013-09-27 17:59:00 -04:00
|
|
|
sanitized_flags = PrepareFlagsForClang( flags, filename )
|
2012-08-05 17:14:31 -04:00
|
|
|
|
|
|
|
if results[ 'do_cache' ]:
|
|
|
|
self.flags_for_file[ filename ] = sanitized_flags
|
|
|
|
return sanitized_flags
|
|
|
|
|
|
|
|
|
2013-04-25 01:51:53 -04:00
|
|
|
def UserIncludePaths( self, filename ):
|
|
|
|
flags = self.FlagsForFile( filename, False )
|
|
|
|
if not flags:
|
|
|
|
return []
|
|
|
|
|
|
|
|
include_paths = []
|
|
|
|
path_flags = [ '-isystem', '-I', '-iquote' ]
|
|
|
|
|
|
|
|
next_flag_is_include_path = False
|
|
|
|
for flag in flags:
|
|
|
|
if next_flag_is_include_path:
|
|
|
|
next_flag_is_include_path = False
|
2013-04-27 01:32:22 -04:00
|
|
|
include_paths.append( flag )
|
2013-04-25 01:51:53 -04:00
|
|
|
|
|
|
|
for path_flag in path_flags:
|
|
|
|
if flag == path_flag:
|
|
|
|
next_flag_is_include_path = True
|
|
|
|
break
|
|
|
|
|
|
|
|
if flag.startswith( path_flag ):
|
|
|
|
path = flag[ len( path_flag ): ]
|
2013-04-27 01:32:22 -04:00
|
|
|
include_paths.append( path )
|
|
|
|
return [ x for x in include_paths if x ]
|
2013-04-25 01:51:53 -04:00
|
|
|
|
|
|
|
|
2013-05-24 17:48:46 -04:00
|
|
|
def Clear( self ):
|
|
|
|
self.flags_for_file.clear()
|
|
|
|
|
|
|
|
|
2013-10-26 19:22:43 -04:00
|
|
|
def _CallExtraConfFlagsForFile( module, filename, client_data ):
|
|
|
|
# For the sake of backwards compatibility, we need to first check whether the
|
|
|
|
# FlagsForFile function in the extra conf module even allows keyword args.
|
|
|
|
if inspect.getargspec( module.FlagsForFile ).keywords:
|
|
|
|
return module.FlagsForFile( filename, client_data = client_data )
|
|
|
|
else:
|
|
|
|
return module.FlagsForFile( filename )
|
|
|
|
|
|
|
|
|
2013-09-27 17:59:00 -04:00
|
|
|
def PrepareFlagsForClang( flags, filename ):
|
2013-05-19 13:48:23 -04:00
|
|
|
flags = _RemoveUnusedFlags( flags, filename )
|
|
|
|
flags = _SanitizeFlags( flags )
|
|
|
|
return flags
|
|
|
|
|
2013-04-25 01:51:53 -04:00
|
|
|
|
2012-08-05 17:14:31 -04:00
|
|
|
def _SanitizeFlags( flags ):
|
2012-08-05 17:32:11 -04:00
|
|
|
"""Drops unsafe flags. Currently these are only -arch flags; they tend to
|
|
|
|
crash libclang."""
|
|
|
|
|
2012-08-05 17:14:31 -04:00
|
|
|
sanitized_flags = []
|
|
|
|
saw_arch = False
|
|
|
|
for i, flag in enumerate( flags ):
|
|
|
|
if flag == '-arch':
|
|
|
|
saw_arch = True
|
|
|
|
continue
|
|
|
|
elif flag.startswith( '-arch' ):
|
|
|
|
continue
|
|
|
|
elif saw_arch:
|
|
|
|
saw_arch = False
|
|
|
|
continue
|
|
|
|
|
|
|
|
sanitized_flags.append( flag )
|
|
|
|
|
|
|
|
vector = ycm_core.StringVec()
|
|
|
|
for flag in sanitized_flags:
|
2013-09-27 17:59:00 -04:00
|
|
|
vector.append( ToUtf8IfNeeded( flag ) )
|
2012-08-05 17:14:31 -04:00
|
|
|
return vector
|
2013-01-17 20:08:28 -05:00
|
|
|
|
|
|
|
|
2013-05-19 13:48:23 -04:00
|
|
|
def _RemoveUnusedFlags( flags, filename ):
|
|
|
|
"""Given an iterable object that produces strings (flags for Clang), removes
|
|
|
|
the '-c' and '-o' options that Clang does not like to see when it's producing
|
2013-11-17 16:10:16 -05:00
|
|
|
completions for a file.
|
|
|
|
|
|
|
|
Also removes the first flag in the list if it does not
|
|
|
|
start with a '-' (it's highly likely to be the compiler name/path).
|
|
|
|
|
|
|
|
We also try to remove any stray filenames in the flags that aren't include
|
|
|
|
dirs."""
|
2013-05-19 13:48:23 -04:00
|
|
|
|
|
|
|
new_flags = []
|
2013-05-19 17:20:13 -04:00
|
|
|
|
|
|
|
# When flags come from the compile_commands.json file, the first flag is
|
|
|
|
# usually the path to the compiler that should be invoked. We want to strip
|
|
|
|
# that.
|
|
|
|
if not flags[ 0 ].startswith( '-' ):
|
|
|
|
flags = flags[ 1: ]
|
|
|
|
|
2013-11-17 16:10:16 -05:00
|
|
|
skip_next = False
|
|
|
|
previous_flag_is_include = False
|
|
|
|
previous_flag_starts_with_dash = False
|
|
|
|
current_flag_starts_with_dash = False
|
2013-05-19 13:48:23 -04:00
|
|
|
for flag in flags:
|
2013-11-17 16:10:16 -05:00
|
|
|
previous_flag_starts_with_dash = current_flag_starts_with_dash
|
|
|
|
current_flag_starts_with_dash = flag.startswith( '-' )
|
|
|
|
if skip_next:
|
|
|
|
skip_next = False
|
2013-05-19 13:48:23 -04:00
|
|
|
continue
|
|
|
|
|
|
|
|
if flag == '-c':
|
|
|
|
continue
|
|
|
|
|
|
|
|
if flag == '-o':
|
2013-11-17 16:10:16 -05:00
|
|
|
skip_next = True;
|
2013-05-19 13:48:23 -04:00
|
|
|
continue
|
|
|
|
|
2013-05-19 17:20:13 -04:00
|
|
|
if flag == filename or os.path.realpath( flag ) == filename:
|
2013-05-19 13:48:23 -04:00
|
|
|
continue
|
|
|
|
|
2013-11-17 16:10:16 -05:00
|
|
|
# We want to make sure that we don't have any stray filenames in our flags;
|
|
|
|
# filenames that are part of include flags are ok, but others are not. This
|
|
|
|
# solves the case where we ask the compilation database for flags for
|
|
|
|
# "foo.cpp" when we are compiling "foo.h" because the comp db doesn't have
|
|
|
|
# flags for headers. The returned flags include "foo.cpp" and we need to
|
|
|
|
# remove that.
|
|
|
|
if ( not current_flag_starts_with_dash and
|
|
|
|
( not previous_flag_starts_with_dash or
|
|
|
|
( not previous_flag_is_include and '/' in flag ) ) ):
|
|
|
|
continue
|
|
|
|
|
2013-05-19 13:48:23 -04:00
|
|
|
new_flags.append( flag )
|
2013-11-17 16:10:16 -05:00
|
|
|
previous_flag_is_include = flag in INCLUDE_FLAGS
|
2013-05-19 13:48:23 -04:00
|
|
|
return new_flags
|
|
|
|
|
|
|
|
|
2013-01-17 20:08:28 -05:00
|
|
|
def _SpecialClangIncludes():
|
|
|
|
libclang_dir = os.path.dirname( ycm_core.__file__ )
|
|
|
|
path_to_includes = os.path.join( libclang_dir, 'clang_includes' )
|
|
|
|
return [ '-I', path_to_includes ]
|
2013-01-19 21:53:30 -05:00
|
|
|
|
|
|
|
|