Advertisement
Guest User

Untitled

a guest
Sep 15th, 2017
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.43 KB | None | 0 0
  1. # This file is NOT licensed under the GPLv3, which is the license for the rest
  2. # of YouCompleteMe.
  3. #
  4. # Here's the license text for this file:
  5. #
  6. # This is free and unencumbered software released into the public domain.
  7. #
  8. # Anyone is free to copy, modify, publish, use, compile, sell, or
  9. # distribute this software, either in source code form or as a compiled
  10. # binary, for any purpose, commercial or non-commercial, and by any
  11. # means.
  12. #
  13. # In jurisdictions that recognize copyright laws, the author or authors
  14. # of this software dedicate any and all copyright interest in the
  15. # software to the public domain. We make this dedication for the benefit
  16. # of the public at large and to the detriment of our heirs and
  17. # successors. We intend this dedication to be an overt act of
  18. # relinquishment in perpetuity of all present and future rights to this
  19. # software under copyright law.
  20. #
  21. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  24. # IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  25. # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  26. # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  27. # OTHER DEALINGS IN THE SOFTWARE.
  28. #
  29. # For more information, please refer to <http://unlicense.org/>
  30.  
  31. import os
  32. import ycm_core
  33.  
  34. flags = [
  35.     # INSERT FLAGS HERE
  36. ]
  37.  
  38.  
  39. # Set this to the absolute path to the folder (NOT the file!) containing the
  40. # compile_commands.json file to use that instead of 'flags'. See here for
  41. # more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html
  42. #
  43. # You can get CMake to generate this file for you by adding:
  44. #   set( CMAKE_EXPORT_COMPILE_COMMANDS 1 )
  45. # to your CMakeLists.txt file.
  46. #
  47. # Most projects will NOT need to set this to anything; you can just change the
  48. # 'flags' list of compilation flags. Notice that YCM itself uses that approach.
  49. compilation_database_folder = ''
  50.  
  51. if os.path.exists( compilation_database_folder ):
  52.   database = ycm_core.CompilationDatabase( compilation_database_folder )
  53. else:
  54.   database = None
  55.  
  56. SOURCE_EXTENSIONS = [ '.C', '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ]
  57.  
  58. def DirectoryOfThisScript():
  59.   return os.path.dirname( os.path.abspath( __file__ ) )
  60.  
  61.  
  62. def MakeRelativePathsInFlagsAbsolute( flags, working_directory ):
  63.   if not working_directory:
  64.     return list( flags )
  65.   new_flags = []
  66.   make_next_absolute = False
  67.   path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ]
  68.   for flag in flags:
  69.     new_flag = flag
  70.  
  71.     if make_next_absolute:
  72.       make_next_absolute = False
  73.       if not flag.startswith( '/' ):
  74.         new_flag = os.path.join( working_directory, flag )
  75.  
  76.     for path_flag in path_flags:
  77.       if flag == path_flag:
  78.         make_next_absolute = True
  79.         break
  80.  
  81.       if flag.startswith( path_flag ):
  82.         path = flag[ len( path_flag ): ]
  83.         new_flag = path_flag + os.path.join( working_directory, path )
  84.         break
  85.  
  86.     if new_flag:
  87.       new_flags.append( new_flag )
  88.   return new_flags
  89.  
  90.  
  91. def IsHeaderFile( filename ):
  92.   extension = os.path.splitext( filename )[ 1 ]
  93.   return extension in [ '.H', '.h', '.hxx', '.hpp', '.hh' ]
  94.  
  95. def filter_out_cxx_std( flags ):
  96.   return [f for f in flags if not (f[:5] == '-std=' and f[5:].find('++') != -1)]
  97.  
  98. def filter_out_c_std( flags ):
  99.   return [f for f in flags if not (f[:5] == '-std=' and f[5:].find('++') == -1)]
  100.  
  101. def LangFlags( filename, flags ):
  102.   extension = os.path.splitext( filename )[ 1 ]
  103.   langmap = {
  104.     'c++': [ '.hh', '.hpp', '.cc', '.cpp', '.cxx' ],
  105.     'c': [ '.h', '.c' ]
  106.   }
  107.   for lang, extlist in langmap.iteritems():
  108.     if extension in extlist:
  109.       if lang == 'c++':
  110.         fflags = filter_out_c_std(flags)
  111.       elif lang == 'c':
  112.         fflags = filter_out_cxx_std(flags)
  113.       else:
  114.         fflags = flags
  115.       return [ '-x', lang ] + fflags
  116.   return flags
  117.  
  118.  
  119. def GetCompilationInfoForFile( filename ):
  120.   # The compilation_commands.json file generated by CMake does not have entries
  121.   # for header files. So we do our best by asking the db for flags for a
  122.   # corresponding source file, if any. If one exists, the flags for that file
  123.   # should be good enough.
  124.   if IsHeaderFile( filename ):
  125.     basename = os.path.splitext( filename )[ 0 ]
  126.     for extension in SOURCE_EXTENSIONS:
  127.       replacement_file = basename + extension
  128.       if os.path.exists( replacement_file ):
  129.         compilation_info = database.GetCompilationInfoForFile(
  130.           replacement_file )
  131.         if compilation_info.compiler_flags_:
  132.           return compilation_info
  133.     return None
  134.   return database.GetCompilationInfoForFile( filename )
  135.  
  136.  
  137. def FlagsForFile( filename, **kwargs ):
  138.   if database:
  139.     # Bear in mind that compilation_info.compiler_flags_ does NOT return a
  140.     # python list, but a "list-like" StringVec object
  141.     compilation_info = GetCompilationInfoForFile( filename )
  142.     if not compilation_info:
  143.       return None
  144.  
  145.     final_flags = MakeRelativePathsInFlagsAbsolute(
  146.       compilation_info.compiler_flags_,
  147.       compilation_info.compiler_working_dir_ )
  148.  
  149.   else:
  150.     relative_to = DirectoryOfThisScript()
  151.     final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to )
  152.  
  153.   if '-x' not in final_flags:
  154.     final_flags = LangFlags( filename, final_flags )
  155.  
  156.   return {
  157.     'flags': final_flags,
  158.     'do_cache': True
  159.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement