-def FlagsForFile( filename, **kwargs ):
- client_data = kwargs['client_data']
- cwd = client_data['getcwd()']
- # These are the compilation flags that will be used in case there's no
- # compilation database set (by default, one is not set).
- flags = [
- '-Wall',
- # '-Wextra',
- # '-Wshadow',
- # '-Werror',
- # '-Wc++98-compat',
- # '-Wno-long-long',
- # '-Wno-variadic-macros',
- # '-fexceptions',
- # '-DNDEBUG',
- '-std=c++11',
- '-stdlib=libstdc++',
- '-x', 'c++',
- '-I', '.',
- '-I', './include',
- ]
- flags += rospack()
- relative_to = cwd
- final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to )
-
- return {
- 'flags': final_flags,
- 'do_cache': True
- }
-
-# This function makes it easy to pull in additional flags from rospack
-def rospack():
- cmd = ['rospack', 'cflags-only-I']
- try:
- out = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE).stdout
- except:
- return []
- line = out.readline()[:-1].split(" ")
- includes = []
- for include in line:
- if include.startswith(os.path.expanduser('~')):
- includes += ['-I', include]
+BASE_FLAGS = [
+ '-Wall',
+ '-std=c++1z',
+ '-x', 'c++',
+ '-isystem', '/usr/include',
+ '-isystem', '/usr/local/include',
+]
+
+EXTRA_FLAGS = [
+ '-Wall',
+ '-Wextra',
+ # '-Wshadow',
+ # '-Werror',
+ # '-Wc++98-compat',
+ # '-Wno-long-long',
+ # '-Wno-variadic-macros',
+ # '-fexceptions',
+ # '-DNDEBUG',
+]
+
+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