+SOURCE_EXTENSIONS = [
+ '.cpp',
+ '.cxx',
+ '.cc',
+ '.c',
+ '.m',
+ '.mm'
+]
+
+HEADER_EXTENSIONS = [
+ '.h',
+ '.hxx',
+ '.hpp',
+ '.hh'
+]
+
+
+# Implementation taken from
+# https://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#Python
+def levenshtein(s, t):
+ ''' From Wikipedia article; Iterative with two matrix rows. '''
+ if s == t: return 0
+ elif len(s) == 0: return len(t)
+ elif len(t) == 0: return len(s)
+ v0 = [None] * (len(t) + 1)
+ v1 = [None] * (len(t) + 1)
+ for i in range(len(v0)):
+ v0[i] = i
+ for i in range(len(s)):
+ v1[0] = i + 1
+ for j in range(len(t)):
+ cost = 0 if s[i] == t[j] else 1
+ v1[j + 1] = min(v1[j] + 1, v0[j + 1] + 1, v0[j] + cost)
+ for j in range(len(v0)):
+ v0[j] = v1[j]
+
+ return v1[len(t)]
+
+def generate_qt_flags():
+ flags = ['-isystem', '/usr/include/qt/']
+ for p in glob('/usr/include/qt/*/'):
+ flags += ['-isystem', p]
+ return flags
+
+
+def find_similar_file_in_database(dbpath, filename):
+ import json
+ logging.info("Trying to find some file close to: " + filename)
+ db = json.load(open(dbpath))
+ best_filename = ''
+ best_distance = 1 << 31
+ for entry in db:
+ entry_filename = os.path.normpath(
+ os.path.join(entry["directory"], entry["file"]))
+ distance = levenshtein(str(filename), str(entry_filename))
+ if distance < best_distance:
+ best_filename = entry_filename
+ best_distance = distance
+ return best_filename
+
+def ok_compilation_info(info):
+ return bool(info.compiler_flags_)
+
+def get_compilation_info_for_file(dbpath, database, filename):
+ info = database.GetCompilationInfoForFile(filename)
+ if ok_compilation_info(info):
+ logging.info("Flags for file where found in database: " + filename)
+ return info
+ else:
+ logging.info("Flags for file not found in database: " + filename)
+ basename = os.path.splitext(filename)[0]
+ for extension in SOURCE_EXTENSIONS:
+ replacement_file = basename + extension
+ logging.info("Trying to replace extension with: " + extension)
+ info = database.GetCompilationInfoForFile(replacement_file)
+ if ok_compilation_info(info):
+ logging.info("Replacing header with: " + replacement_file)
+ return info
+ replacement_file = find_similar_file_in_database(dbpath, filename)
+ logging.info("Replacing header with: " + replacement_file)
+ return database.GetCompilationInfoForFile(replacement_file)
+
+
+def find_nearest_compilation_database(root='.'):
+ dirs = glob(root + '/*/compile_commands.json', recursive=True)
+
+ if len(dirs) == 1:
+ return dirs[0]
+ elif len(dirs) > 1:
+ logging.info("Multiple compilation databases found!")
+ logging.info(dirs)
+ logging.info("Selecting first: %s" % (dir))
+ return dirs[0]