選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 

110 行
3.3 KiB

  1. import os
  2. import ycm_core
  3. flags = [
  4. '-Wall',
  5. '-Wextra',
  6. '-Werror',
  7. '-x', 'c',
  8. '-Iinclude',
  9. ]
  10. # Set this to the absolute path to the folder (NOT the file!) containing the
  11. # compile_commands.json file to use that instead of 'flags'. See here for
  12. # more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html
  13. #
  14. # Most projects will NOT need to set this to anything; you can just change the
  15. # 'flags' list of compilation flags. Notice that YCM itself uses that approach.
  16. compilation_database_folder = 'obj'
  17. if os.path.exists( compilation_database_folder ):
  18. database = ycm_core.CompilationDatabase( compilation_database_folder )
  19. else:
  20. database = None
  21. SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ]
  22. def DirectoryOfThisScript():
  23. return os.path.dirname( os.path.abspath( __file__ ) )
  24. def MakeRelativePathsInFlagsAbsolute( flags, working_directory ):
  25. if not working_directory:
  26. return list( flags )
  27. new_flags = []
  28. make_next_absolute = False
  29. path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ]
  30. for flag in flags:
  31. new_flag = flag
  32. if make_next_absolute:
  33. make_next_absolute = False
  34. if not flag.startswith( '/' ):
  35. new_flag = os.path.join( working_directory, flag )
  36. for path_flag in path_flags:
  37. if flag == path_flag:
  38. make_next_absolute = True
  39. break
  40. if flag.startswith( path_flag ):
  41. path = flag[ len( path_flag ): ]
  42. new_flag = path_flag + os.path.join( working_directory, path )
  43. break
  44. if new_flag:
  45. new_flags.append( new_flag )
  46. return new_flags
  47. def IsHeaderFile( filename ):
  48. extension = os.path.splitext( filename )[ 1 ]
  49. return extension in [ '.h', '.hxx', '.hpp', '.hh' ]
  50. def GetCompilationInfoForFile( filename ):
  51. # The compilation_commands.json file generated by CMake does not have entries
  52. # for header files. So we do our best by asking the db for flags for a
  53. # corresponding source file, if any. If one exists, the flags for that file
  54. # should be good enough.
  55. if IsHeaderFile( filename ):
  56. basename = os.path.splitext( filename )[ 0 ]
  57. for extension in SOURCE_EXTENSIONS:
  58. replacement_file = basename + extension
  59. if os.path.exists( replacement_file ):
  60. compilation_info = database.GetCompilationInfoForFile(
  61. replacement_file )
  62. if compilation_info.compiler_flags_:
  63. return compilation_info
  64. return None
  65. return database.GetCompilationInfoForFile( filename )
  66. def FlagsForFile( filename, **kwargs ):
  67. if database:
  68. # Bear in mind that compilation_info.compiler_flags_ does NOT return a
  69. # python list, but a "list-like" StringVec object
  70. compilation_info = GetCompilationInfoForFile( filename )
  71. if not compilation_info:
  72. return None
  73. final_flags = MakeRelativePathsInFlagsAbsolute(
  74. compilation_info.compiler_flags_,
  75. compilation_info.compiler_working_dir_ )
  76. # NOTE: This is just for YouCompleteMe; it's highly likely that your project
  77. # does NOT need to remove the stdlib flag. DO NOT USE THIS IN YOUR
  78. # ycm_extra_conf IF YOU'RE NOT 100% SURE YOU NEED IT.
  79. try:
  80. final_flags.remove( '-stdlib=libc++' )
  81. except ValueError:
  82. pass
  83. else:
  84. relative_to = DirectoryOfThisScript()
  85. final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to )
  86. return {
  87. 'flags': final_flags,
  88. 'do_cache': True
  89. }