diff --git a/cpp/ycm/.ycm_extra_conf.py b/cpp/ycm/.ycm_extra_conf.py index 9f091e39..c3a25c03 100644 --- a/cpp/ycm/.ycm_extra_conf.py +++ b/cpp/ycm/.ycm_extra_conf.py @@ -83,6 +83,7 @@ flags = [ './tests/gmock/include' ] + # Set this to the absolute path to the folder (NOT the file!) containing the # compile_commands.json file to use that instead of 'flags'. See here for # more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html @@ -96,6 +97,7 @@ if compilation_database_folder: else: database = None +SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ] def DirectoryOfThisScript(): return os.path.dirname( os.path.abspath( __file__ ) ) @@ -130,11 +132,37 @@ def MakeRelativePathsInFlagsAbsolute( flags, working_directory ): return new_flags +def IsHeaderFile( filename ): + extension = os.path.splitext( filename )[ 1 ] + return extension in [ '.h', '.hxx', '.hpp', '.hh' ] + + +def GetCompilationInfoForFile( filename ): + # The compilation_commands.json file generated by CMake does not have entries + # for header files. So we do our best by asking the db for flags for a + # corresponding source file, if any. If one exists, the flags for that file + # should be good enough. + if IsHeaderFile( filename ): + basename = os.path.splitext( filename )[ 0 ] + for extension in SOURCE_EXTENSIONS: + replacement_file = basename + extension + if os.path.exists( replacement_file ): + compilation_info = database.GetCompilationInfoForFile( + replacement_file ) + if compilation_info.compiler_flags_: + return compilation_info + return None + return database.GetCompilationInfoForFile( filename ) + + def FlagsForFile( filename, **kwargs ): if database: # Bear in mind that compilation_info.compiler_flags_ does NOT return a # python list, but a "list-like" StringVec object - compilation_info = database.GetCompilationInfoForFile( filename ) + compilation_info = GetCompilationInfoForFile( filename ) + if not compilation_info: + return None + final_flags = MakeRelativePathsInFlagsAbsolute( compilation_info.compiler_flags_, compilation_info.compiler_working_dir_ ) diff --git a/python/ycm/completers/cpp/flags.py b/python/ycm/completers/cpp/flags.py index 50901e6c..2f9b5a1e 100644 --- a/python/ycm/completers/cpp/flags.py +++ b/python/ycm/completers/cpp/flags.py @@ -60,7 +60,7 @@ class Flags( object ): filename, client_data ) - if not results.get( 'flags_ready', True ): + if not results or not results.get( 'flags_ready', True ): return None flags = list( results[ 'flags' ] )