Blame .ycm_extra_conf.py

Packit 79ec17
import os
Packit 79ec17
import ycm_core
Packit 79ec17
Packit 79ec17
flags = [
Packit 79ec17
'-Wall',
Packit 79ec17
'-Wextra',
Packit 79ec17
'-Werror',
Packit 79ec17
'-x', 'c',
Packit 79ec17
'-Iinclude',
Packit 79ec17
]
Packit 79ec17
Packit 79ec17
# Set this to the absolute path to the folder (NOT the file!) containing the
Packit 79ec17
# compile_commands.json file to use that instead of 'flags'. See here for
Packit 79ec17
# more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html
Packit 79ec17
#
Packit 79ec17
# Most projects will NOT need to set this to anything; you can just change the
Packit 79ec17
# 'flags' list of compilation flags. Notice that YCM itself uses that approach.
Packit 79ec17
compilation_database_folder = 'obj'
Packit 79ec17
Packit 79ec17
if os.path.exists( compilation_database_folder ):
Packit 79ec17
  database = ycm_core.CompilationDatabase( compilation_database_folder )
Packit 79ec17
else:
Packit 79ec17
  database = None
Packit 79ec17
Packit 79ec17
SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ]
Packit 79ec17
Packit 79ec17
def DirectoryOfThisScript():
Packit 79ec17
  return os.path.dirname( os.path.abspath( __file__ ) )
Packit 79ec17
Packit 79ec17
Packit 79ec17
def MakeRelativePathsInFlagsAbsolute( flags, working_directory ):
Packit 79ec17
  if not working_directory:
Packit 79ec17
    return list( flags )
Packit 79ec17
  new_flags = []
Packit 79ec17
  make_next_absolute = False
Packit 79ec17
  path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ]
Packit 79ec17
  for flag in flags:
Packit 79ec17
    new_flag = flag
Packit 79ec17
Packit 79ec17
    if make_next_absolute:
Packit 79ec17
      make_next_absolute = False
Packit 79ec17
      if not flag.startswith( '/' ):
Packit 79ec17
        new_flag = os.path.join( working_directory, flag )
Packit 79ec17
Packit 79ec17
    for path_flag in path_flags:
Packit 79ec17
      if flag == path_flag:
Packit 79ec17
        make_next_absolute = True
Packit 79ec17
        break
Packit 79ec17
Packit 79ec17
      if flag.startswith( path_flag ):
Packit 79ec17
        path = flag[ len( path_flag ): ]
Packit 79ec17
        new_flag = path_flag + os.path.join( working_directory, path )
Packit 79ec17
        break
Packit 79ec17
Packit 79ec17
    if new_flag:
Packit 79ec17
      new_flags.append( new_flag )
Packit 79ec17
  return new_flags
Packit 79ec17
Packit 79ec17
Packit 79ec17
def IsHeaderFile( filename ):
Packit 79ec17
  extension = os.path.splitext( filename )[ 1 ]
Packit 79ec17
  return extension in [ '.h', '.hxx', '.hpp', '.hh' ]
Packit 79ec17
Packit 79ec17
Packit 79ec17
def GetCompilationInfoForFile( filename ):
Packit 79ec17
  # The compilation_commands.json file generated by CMake does not have entries
Packit 79ec17
  # for header files. So we do our best by asking the db for flags for a
Packit 79ec17
  # corresponding source file, if any. If one exists, the flags for that file
Packit 79ec17
  # should be good enough.
Packit 79ec17
  if IsHeaderFile( filename ):
Packit 79ec17
    basename = os.path.splitext( filename )[ 0 ]
Packit 79ec17
    for extension in SOURCE_EXTENSIONS:
Packit 79ec17
      replacement_file = basename + extension
Packit 79ec17
      if os.path.exists( replacement_file ):
Packit 79ec17
        compilation_info = database.GetCompilationInfoForFile(
Packit 79ec17
          replacement_file )
Packit 79ec17
        if compilation_info.compiler_flags_:
Packit 79ec17
          return compilation_info
Packit 79ec17
    return None
Packit 79ec17
  return database.GetCompilationInfoForFile( filename )
Packit 79ec17
Packit 79ec17
Packit 79ec17
def FlagsForFile( filename, **kwargs ):
Packit 79ec17
  if database:
Packit 79ec17
    # Bear in mind that compilation_info.compiler_flags_ does NOT return a
Packit 79ec17
    # python list, but a "list-like" StringVec object
Packit 79ec17
    compilation_info = GetCompilationInfoForFile( filename )
Packit 79ec17
    if not compilation_info:
Packit 79ec17
      return None
Packit 79ec17
Packit 79ec17
    final_flags = MakeRelativePathsInFlagsAbsolute(
Packit 79ec17
      compilation_info.compiler_flags_,
Packit 79ec17
      compilation_info.compiler_working_dir_ )
Packit 79ec17
Packit 79ec17
    # NOTE: This is just for YouCompleteMe; it's highly likely that your project
Packit 79ec17
    # does NOT need to remove the stdlib flag. DO NOT USE THIS IN YOUR
Packit 79ec17
    # ycm_extra_conf IF YOU'RE NOT 100% SURE YOU NEED IT.
Packit 79ec17
    try:
Packit 79ec17
      final_flags.remove( '-stdlib=libc++' )
Packit 79ec17
    except ValueError:
Packit 79ec17
      pass
Packit 79ec17
  else:
Packit 79ec17
    relative_to = DirectoryOfThisScript()
Packit 79ec17
    final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to )
Packit 79ec17
Packit 79ec17
  return {
Packit 79ec17
    'flags': final_flags,
Packit 79ec17
    'do_cache': True
Packit 79ec17
  }