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 imp
|
|
|
|
import os
|
|
|
|
import ycm_core
|
|
|
|
import random
|
|
|
|
import string
|
2013-01-19 21:53:30 -05:00
|
|
|
import sys
|
2013-01-22 20:13:14 -05:00
|
|
|
import vimsupport
|
2013-02-24 18:21:34 -05:00
|
|
|
from fnmatch import fnmatch
|
2012-08-05 17:14:31 -04:00
|
|
|
|
2013-01-24 12:59:21 -05:00
|
|
|
YCM_EXTRA_CONF_FILENAME = '.ycm_extra_conf.py'
|
2013-02-05 22:03:38 -05:00
|
|
|
NO_EXTRA_CONF_FILENAME_MESSAGE = ('No {0} file detected, so no compile flags '
|
2013-02-20 16:14:16 -05:00
|
|
|
'are available. Thus no semantic support for C/C++/ObjC/ObjC++. See the '
|
|
|
|
'docs for details.').format( YCM_EXTRA_CONF_FILENAME )
|
2013-02-25 04:58:04 -05:00
|
|
|
CONFIRM_CONF_FILE_MESSAGE = 'Found {0}. Load?'
|
2013-02-20 16:14:16 -05:00
|
|
|
GLOBAL_YCM_EXTRA_CONF_FILE = os.path.expanduser(
|
2013-02-18 00:58:22 -05:00
|
|
|
vimsupport.GetVariableValue( "g:ycm_global_ycm_extra_conf" )
|
|
|
|
)
|
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-02-25 04:58:04 -05:00
|
|
|
self.module_for_file = {}
|
|
|
|
self.modules = FlagsModules()
|
2013-01-17 20:08:28 -05:00
|
|
|
self.special_clang_flags = _SpecialClangIncludes()
|
2013-02-05 22:03:38 -05:00
|
|
|
self.no_extra_conf_file_warning_posted = False
|
2012-08-05 17:14:31 -04:00
|
|
|
|
2013-02-25 04:58:04 -05:00
|
|
|
def ModuleForFile( self, filename ):
|
|
|
|
"""This will try all files returned by _FlagsModuleSourceFilesForFile in
|
|
|
|
order and return the filename of the first module that was allowed to load.
|
|
|
|
If no module was found or allowed to load, None is returned."""
|
|
|
|
|
|
|
|
if not self.module_for_file.has_key( filename ):
|
|
|
|
for flags_module_file in _FlagsModuleSourceFilesForFile( filename ):
|
|
|
|
if self.modules.Load( flags_module_file ):
|
|
|
|
self.module_for_file[ filename ] = flags_module_file
|
|
|
|
break
|
|
|
|
|
|
|
|
return self.module_for_file.setdefault( filename )
|
2012-08-05 17:14:31 -04:00
|
|
|
|
|
|
|
def FlagsForFile( self, filename ):
|
|
|
|
try:
|
|
|
|
return self.flags_for_file[ filename ]
|
|
|
|
except KeyError:
|
2013-02-25 04:58:04 -05:00
|
|
|
module_file = self.ModuleForFile( filename )
|
|
|
|
if not module_file:
|
2013-02-05 22:03:38 -05:00
|
|
|
if not self.no_extra_conf_file_warning_posted:
|
|
|
|
vimsupport.PostVimMessage( NO_EXTRA_CONF_FILENAME_MESSAGE )
|
|
|
|
self.no_extra_conf_file_warning_posted = True
|
2013-01-23 20:23:51 -05:00
|
|
|
return None
|
2012-08-05 17:14:31 -04:00
|
|
|
|
2013-02-25 04:58:04 -05:00
|
|
|
results = self.modules[ module_file ].FlagsForFile( filename )
|
2013-01-23 20:23:51 -05:00
|
|
|
|
|
|
|
if not results.get( 'flags_ready', True ):
|
|
|
|
return None
|
|
|
|
|
2013-01-17 20:08:28 -05:00
|
|
|
results[ 'flags' ] += self.special_clang_flags
|
2012-08-05 17:14:31 -04:00
|
|
|
sanitized_flags = _SanitizeFlags( results[ 'flags' ] )
|
|
|
|
|
|
|
|
if results[ 'do_cache' ]:
|
|
|
|
self.flags_for_file[ filename ] = sanitized_flags
|
|
|
|
return sanitized_flags
|
|
|
|
|
2013-02-25 04:58:04 -05:00
|
|
|
def ReloadModule( self, module_file ):
|
|
|
|
"""Reloads a module file cleaning the flags cache for all files
|
|
|
|
associated with that module. Returns False if reloading failed
|
|
|
|
(for example due to the model not being loaded in the first place)."""
|
2013-02-26 21:28:58 -05:00
|
|
|
|
2013-02-25 04:58:04 -05:00
|
|
|
module_file = os.path.abspath(module_file)
|
|
|
|
if self.modules.Reload( module_file ):
|
|
|
|
for filename, module in self.module_for_file.iteritems():
|
|
|
|
if module == module_file:
|
|
|
|
del self.flags_for_file[ filename ]
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
class FlagsModules( object ):
|
|
|
|
"""Keeps track of modules.
|
|
|
|
Modules are loaded on-demand and cached in self.modules for quick access."""
|
2013-02-26 21:28:58 -05:00
|
|
|
|
2013-02-25 04:58:04 -05:00
|
|
|
def __init__( self ):
|
|
|
|
self.modules = {}
|
|
|
|
|
|
|
|
def Disable( self, module_file ):
|
|
|
|
"""Disables the loading of a module for the current session."""
|
|
|
|
self.modules[ module_file ] = None
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def ShouldLoad( module_file ):
|
2013-02-24 18:21:34 -05:00
|
|
|
"""Checks if a module is safe to be loaded. By default this will try to
|
|
|
|
decide using a white-/blacklist and ask the user for confirmation as a
|
|
|
|
fallback."""
|
2013-02-26 21:28:58 -05:00
|
|
|
|
2013-02-24 18:21:34 -05:00
|
|
|
if ( module_file == GLOBAL_YCM_EXTRA_CONF_FILE or
|
|
|
|
not vimsupport.GetBoolValue( 'g:ycm_confirm_extra_conf' ) ):
|
2013-02-25 04:58:04 -05:00
|
|
|
return True
|
2013-02-24 18:21:34 -05:00
|
|
|
|
|
|
|
globlist = vimsupport.GetVariableValue( 'g:ycm_extra_conf_globlist' )
|
|
|
|
for glob in globlist:
|
|
|
|
is_blacklisted = glob[0] == '!'
|
|
|
|
if MatchesGlobPattern( module_file, glob.lstrip('!') ):
|
|
|
|
return not is_blacklisted
|
|
|
|
|
2013-02-28 12:54:22 -05:00
|
|
|
return vimsupport.Confirm( CONFIRM_CONF_FILE_MESSAGE.format( module_file ) )
|
2013-02-25 04:58:04 -05:00
|
|
|
|
|
|
|
def Load( self, module_file, force = False ):
|
|
|
|
"""Load and return the module contained in a file.
|
|
|
|
Using force = True the module will be loaded regardless
|
|
|
|
of the criteria in ShouldLoad.
|
|
|
|
This will return None if the module was not allowed to be loaded."""
|
2013-02-26 21:28:58 -05:00
|
|
|
|
2013-02-25 04:58:04 -05:00
|
|
|
if not force:
|
|
|
|
if self.modules.has_key( module_file ):
|
|
|
|
return self.modules[ module_file ]
|
|
|
|
|
|
|
|
if not self.ShouldLoad( module_file ):
|
|
|
|
return self.Disable( module_file )
|
|
|
|
|
|
|
|
sys.path.insert( 0, _DirectoryOfThisScript() )
|
|
|
|
module = imp.load_source( _RandomName(), module_file )
|
|
|
|
del sys.path[ 0 ]
|
|
|
|
|
|
|
|
self.modules[ module_file ] = module
|
|
|
|
return module
|
|
|
|
|
|
|
|
def Reload( self, module_file ):
|
|
|
|
"""Reloads the given module. If it has not been loaded yet does nothing.
|
|
|
|
Note that the module will not be subject to the loading criteria again."""
|
2013-02-26 21:28:58 -05:00
|
|
|
|
2013-02-25 04:58:04 -05:00
|
|
|
if self.modules.get( module_file ):
|
|
|
|
return self.Load( module_file, force = True )
|
|
|
|
|
|
|
|
def __getitem__( self, key ):
|
|
|
|
return self.Load( key )
|
|
|
|
|
|
|
|
|
|
|
|
def _FlagsModuleSourceFilesForFile( filename ):
|
|
|
|
"""For a given filename, search all parent folders for YCM_EXTRA_CONF_FILENAME
|
|
|
|
files that will compute the flags necessary to compile the file.
|
|
|
|
If GLOBAL_YCM_EXTRA_CONF_FILE exists it is returned as a fallback."""
|
2013-02-26 21:28:58 -05:00
|
|
|
|
2013-02-25 04:58:04 -05:00
|
|
|
for folder in _PathsToAllParentFolders( filename ):
|
|
|
|
candidate = os.path.join( folder, YCM_EXTRA_CONF_FILENAME )
|
|
|
|
if os.path.exists( candidate ):
|
|
|
|
yield candidate
|
|
|
|
if ( GLOBAL_YCM_EXTRA_CONF_FILE
|
|
|
|
and os.path.exists( GLOBAL_YCM_EXTRA_CONF_FILE ) ):
|
|
|
|
yield GLOBAL_YCM_EXTRA_CONF_FILE
|
|
|
|
|
|
|
|
|
|
|
|
def _PathsToAllParentFolders( filename ):
|
|
|
|
"""Build a list of all parent folders of a file.
|
|
|
|
The neares files will be returned first.
|
|
|
|
Example: _PathsToAllParentFolders( '/home/user/projects/test.c' )
|
|
|
|
[ '/home/user/projects', '/home/user', '/home', '/' ]"""
|
2013-02-26 21:28:58 -05:00
|
|
|
|
2013-02-25 04:58:04 -05:00
|
|
|
parent_folders = os.path.abspath(
|
|
|
|
os.path.dirname( filename ) ).split( os.path.sep )
|
|
|
|
if not parent_folders[0]:
|
|
|
|
parent_folders[0] = os.path.sep
|
|
|
|
parent_folders = [ os.path.join( *parent_folders[:i + 1] )
|
|
|
|
for i in xrange( len( parent_folders ) ) ]
|
|
|
|
return reversed( parent_folders )
|
2012-08-05 17:14:31 -04:00
|
|
|
|
|
|
|
|
|
|
|
def _RandomName():
|
2012-08-05 17:32:11 -04:00
|
|
|
"""Generates a random module name."""
|
2012-08-05 17:14:31 -04:00
|
|
|
return ''.join( random.choice( string.ascii_lowercase ) for x in range( 15 ) )
|
|
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
vector.append( flag )
|
|
|
|
return vector
|
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
|
|
|
|
|
|
|
|
|
|
|
def _DirectoryOfThisScript():
|
|
|
|
return os.path.dirname( os.path.abspath( __file__ ) )
|
2013-02-24 18:21:34 -05:00
|
|
|
|
|
|
|
|
|
|
|
def MatchesGlobPattern( filename, glob ):
|
|
|
|
"""Returns true if a filename matches a given pattern. A '~' in glob will be
|
|
|
|
expanded to the home directory and checking will be performed using absolute
|
|
|
|
paths. See the documentation of fnmatch for the supported patterns."""
|
|
|
|
|
|
|
|
abspath = os.path.abspath( filename )
|
|
|
|
return fnmatch( abspath, os.path.abspath( os.path.expanduser( glob ) ) )
|