-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathycmd_global_conf.py
301 lines (220 loc) · 8.63 KB
/
ycmd_global_conf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import os
from os import path
import ycm_core
import subprocess
import logging
import json
import re
from itertools import dropwhile, takewhile
# These are the compilation flags that will be used in case there's no
# compilation database found
flags = [
"-fexceptions",
"-std=c++2a",
"-x",
"c++",
# "-Wno-unused-parameter",
# "-Wall",
# "-Wextra",
# "-Wpedantic",
"-Wno-attributes",
"-I",
".",
"-I",
"../",
"-I",
"../../",
]
# Flags that always get added
# -fspell-checking gives better fixits and has no downside
extra_flags = ["-fspell-checking"]
SOURCE_EXTENSIONS = [".x.cpp", ".cpp", ".cxx", ".cc", ".c", ".m", ".mm", ".t.cpp"]
HEADER_EXTENSIONS = {".h", ".hxx", ".hpp", ".hh"}
l = logging.getLogger()
def load_system_includes():
l.info(
"Calling clang++ at {}".format(subprocess.check_output(["which", "clang++"]))
)
l.info(
"clang++ version: {}".format(subprocess.check_output(["clang++", "--version"]))
)
process = subprocess.Popen(
["clang++", "-v", "-E", "-x", "c++", "-"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
process_out, process_err = process.communicate("")
output = process_err.decode("utf8").split("\n")
def not_start(s):
return s != "#include <...> search starts here:"
def not_end(s):
return s != "End of search list."
from_start = dropwhile(not_start, output)
next(from_start)
return ["-isystem" + x[1:] for x in takewhile(not_end, from_start)]
def find_in_parent_dir(original_file, target):
d = os.path.abspath(original_file)
while not os.path.ismount(d):
d = os.path.dirname(d)
if os.path.exists(os.path.join(d, target)):
return d
return None
def pairwise(iterable):
it = iter(iterable)
previous = next(it)
for x in it:
yield (previous, x)
previous = x
def make_paths_absolute(flags, working_directory):
path_flags = ["-isystem", "-I", "-iquote", "--sysroot="]
if not working_directory:
return list(flags)
new_flags = []
def make_path_absolute(path):
if os.path.isabs(path):
return path
return os.path.join(working_directory, path)
pair_iter = pairwise(flags)
for flag, next_flag in pair_iter:
path_flag = next((p for p in path_flags if flag.startswith(p)), None)
if not path_flag:
new_flags.append(flag)
continue
if flag == path_flag:
new_flags.extend([flag, make_path_absolute(next_flag)])
try:
next(pair_iter)
except StopIteration:
break
continue
path = flag[len(path_flag) :]
new_flags.append(path_flag + make_path_absolute(path))
return new_flags
def header_heuristic_source_file(header_file, database):
l.info("Begin corresponding source file heuristic for {}".format(header_file))
basename = os.path.splitext(header_file)[0]
for extension in SOURCE_EXTENSIONS:
replacement_file = basename + extension
if os.path.exists(replacement_file):
compilation_info = database.GetCompilationInfoForFile(replacement_file)
l.info("Found corresponding source file {}".format(replacement_file))
if compilation_info.compiler_flags_:
return compilation_info
l.warn("Did not find corresponding source file in database!")
return None
def heuristic_same_dir(filename, database):
file_dir = os.path.dirname(filename)
dir_files = os.listdir(file_dir)
extension = path.splitext(filename)[-1]
# Prioritize same extension over different, helps with test files
dir_files = [x for x in dir_files if x.endswith(extension)] + [
x for x in dir_files if not x.endswith(extension)
]
for f in dir_files:
if any(f.endswith(i) for i in SOURCE_EXTENSIONS):
compilation_info = database.GetCompilationInfoForFile(
os.path.join(file_dir, f)
)
if compilation_info.compiler_flags_:
l.info("Found samedir file for flags: {}".format(f))
return compilation_info
return None
def heuristic_closest_in_db(filename, database):
l.info("Entering closest file in db heuristic")
database_dir = find_in_parent_dir(filename, "compile_commands.json")
with open(path.join(database_dir, "compile_commands.json")) as f:
json_db = json.load(f)
db_filenames = (path.join(x["directory"], x["file"]) for x in json_db)
extension = path.splitext(filename)[-1]
def key_value(db_fn):
distance = path.relpath(filename, db_fn).count("/")
# Prioritize same extension; this can help with test files
return distance - (0.5 if db_fn.endswith(extension) else 0)
best_match = min(db_filenames, key=key_value)
l.info("Best match found: {}".format(best_match))
l.info("With value".format(key_value(best_match)))
compilation_info = database.GetCompilationInfoForFile(best_match)
if compilation_info.compiler_flags_:
return compilation_info
return None
def exact(filename, database):
compilation_info = database.GetCompilationInfoForFile(filename)
if compilation_info.compiler_flags_:
return compilation_info
return None
# files which do not follow an easy pattern
def header_exceptions(filename, database):
l.info("Checking for exceptions")
token = "include/zen"
if token in filename:
idx = filename.index(token)
outfile = filename[:idx] + "tests/mktaccess/level_book.t.cpp"
compilation_info = database.GetCompilationInfoForFile(outfile)
if compilation_info.compiler_flags_:
return compilation_info
return None
def heuristic_change_include_to_src_dir(filename, database):
# we try two combinations
# change include/foo/name to src/name
# and change include/foo/name to src/foo/name
def regex_apply(reg):
regexed = re.sub(reg, "src", filename)
file_dir = os.path.dirname(regexed)
if not os.path.exists(file_dir):
l.info("{} does not exist".format(file_dir))
return None
dir_files = os.listdir(file_dir)
for f in dir_files:
if any(f.endswith(i) for i in SOURCE_EXTENSIONS):
fn = os.path.join(file_dir, f)
compilation_info = database.GetCompilationInfoForFile(fn)
if compilation_info.compiler_flags_:
l.info("Found samedir src file for flags: {}".format(f))
return compilation_info
result = regex_apply(r"include/\w+")
if result:
return result
result = regex_apply(r"include")
return result
def apply_heuristics(filename, database, heuristics):
for h in heuristics:
compilation_info = h(filename, database)
if compilation_info is not None:
return compilation_info
HEADER_HEURISTICS = [
header_exceptions,
heuristic_change_include_to_src_dir,
header_heuristic_source_file,
heuristic_same_dir
# heuristic_closest_in_db
]
SOURCE_HEURISTICS = [exact, heuristic_same_dir, heuristic_closest_in_db]
def get_flags_from_database(filename, database_dir):
database = ycm_core.CompilationDatabase(database_dir)
if not os.path.splitext(filename)[1] in HEADER_EXTENSIONS:
l.info("Source file, looking up flags in database")
compilation_info = apply_heuristics(filename, database, SOURCE_HEURISTICS)
else:
l.info("Header file, starting heuristics")
compilation_info = apply_heuristics(filename, database, HEADER_HEURISTICS)
if compilation_info is None:
return None
final_flags = make_paths_absolute(
compilation_info.compiler_flags_, compilation_info.compiler_working_dir_
)
final_flags = final_flags + load_system_includes() + flags
return final_flags
def FlagsForFile(filename, **kwargs):
l.info("Finding flags for file {}".format(filename))
database_dir = find_in_parent_dir(filename, "compile_commands.json")
final_flags = None
if database_dir is not None:
l.info("Database found at directory {}".format(database_dir))
# Bear in mind that compilation_info.compiler_flags_ does NOT return a
# python list, but a "list-like" StringVec object
final_flags = get_flags_from_database(filename, database_dir)
if final_flags is None:
relative_to = os.path.dirname(os.path.abspath(filename))
final_flags = make_paths_absolute(flags, relative_to) + load_system_includes()
return {"flags": final_flags + extra_flags, "do_cache": True}