This repository was archived by the owner on Oct 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathycmd.py
317 lines (295 loc) · 12.5 KB
/
ycmd.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# global_conf.py --- ycmd global configuration file for Spacemacs
#
# Copyright (c) 2012-2017 Sylvain Benner & Contributors
#
# Author: Thomas de Beauchene <thomas.de.beauchene@gmail.com>
# URL: https://github.com/syl20bnr/spacemacs
#
# This file is not part of GNU Emacs.
#
# License: GPLv3
#
# This script tries to get the compilation flags for a translation unit using
# the following logic:
#
# 1) If there is a compile_commands.json in a parent directory:
# a) If the file is a header file:
# - search for the header file itself in db
# - search for a sibling source file in the same directory (i.e. a source
# file with the same name but different extension)
# - search for a source file that includes our header's path
# - search for the nearest source file in db
#
# b) If the file is a source file:
# - search for the source file itself
# - search for the nearest source file in db
#
# 2) If no compile_commands.json, search for a .clang_complete:
# - get flags from .clang_complete
#
# 3) Always try to add extra flags from a .ycm_extra_flags file in a parent
# directory. (like --sysroot="/path/to/your/toolchain/libc" if you are cross-compiling)
#
# Thanks to Jonas Devlieghere and Gabor Marton for their work on which this code is based.
# https://jonasdevlieghere.com/a-better-youcompleteme-config/
# https://github.com/martong/ycm_extra_conf.jsondb
import itertools
import json
import logging
import os
import os.path
import re
import ycm_core
from subprocess import Popen, PIPE
def prefix_isystem_includes(byte_str):
for x in byte_str.decode("utf-8").split('\n'):
if x.startswith(' /'):
yield '-isystem'
yield x.strip()
def get_includes():
clang=Popen('clang -v -E -x c++ -',
stdin=PIPE,
stdout=PIPE,
stderr=PIPE,
shell=True)
_,err = clang.communicate()
clang.stdin.close()
includes = prefix_isystem_includes(err)
return list(includes)
SOURCE_EXTENSIONS = ['.cpp', '.cxx', '.cc', '.C', '.c', '.m', '.mm']
HEADER_EXTENSIONS = ['.h', '.hxx', '.hpp', '.hh' ]
# This function is called by ycmd.
def FlagsForFile(filename):
logging.info("%s: Getting flags for %s" % (__file__, filename))
root = os.path.realpath(filename)
flags = FlagsFromCompilationDatabase(root, filename)
if not flags:
flags = FlagsFromClangComplete(root, filename)
extra_flags = GetUserExtraFlags(filename)
if extra_flags:
if flags:
flags += extra_flags
else:
flags = extra_flags
if flags:
flags = [ flag for flag in flags if not flag.startswith("-m") ] # strip -m flags
logging.info("%s: Flags = [\n\t\t%s\n]"
% (os.path.basename(filename), "\n\t\t".join(flags)))
else:
logging.error("%s: No flags were found !" % (os.path.basename(filename)))
logging.warn("using default flags")
extension = os.path.splitext(filename)[1]
flags = [
'-Wall', '-Wextra', '-Wpedantic', '-pedantic', '-pedantic-errors', '-march=native'
]
if extension in ['.cpp', '.cxx', '.cc', '.C', '.hpp']:
flags = flags + [
'-x', 'c++',
'-std=c++1z'
]
elif extension in [ '.c', '.h' ]:
flags = flags + [
'-x', 'c',
'-std=c11'
]
flags = flags + get_includes()
return { 'flags': flags, 'do_cache': True }
def FlagsFromClangComplete(root, filename):
try:
clang_complete_path = FindNearest(root, '.clang_complete', filename)
clang_complete_flags = open(clang_complete_path, 'r').read().splitlines()
logging.info("%s: Using %s" % (os.path.basename(filename), clang_complete_path))
return MakeRelativePathsInFlagsAbsolute(clang_complete_flags,
os.path.dirname(clang_complete_path))
except:
return None
def FlagsFromCompilationDatabase(root, filename):
try:
database_path = FindNearest(root, 'compile_commands.json', filename)
database = ycm_core.CompilationDatabase(os.path.dirname(database_path))
if not database:
logging.info("%s: Compilation database file found but unable to load"
% os.path.basename(filename))
return None
extension = os.path.splitext(filename)[1]
if extension in HEADER_EXTENSIONS:
flags = GetFlagsForHeader(database_path, database, filename)
else:
flags = GetFlagsForSourceFile(database_path, database, filename)
if not flags:
logging.info("%s: No compilation info for %s in compilation database"
% (os.path.basename(filename), filename))
return None
return MakeRelativePathsInFlagsAbsolute(flags.compiler_flags_,
flags.compiler_working_dir_)
except Exception as e:
logging.info("%s: Could not get compilation flags from db: %s"
% (os.path.basename(filename), e))
return None
def GetFlagsForHeader(database_path, database, filename):
flags = FindFileInDb(database, filename)
if flags:
return flags
flags = FindSiblingFileForHeader(database, filename)
if flags:
return flags
flags = SearchForTranslationUnitWhichIncludesPath(database_path,
database,
os.path.dirname(filename),
filename)
if flags:
return flags
return FindNearestSourceFileInDb(database_path, database, filename)
def GetFlagsForSourceFile (database_path, database, filename):
flags = FindFileInDb(database, filename)
if flags:
return flags
return FindNearestSourceFileInDb(database_path, database, filename)
def FindNearest(path, target, filename):
candidate = os.path.join(path, target)
if(os.path.isfile(candidate) or os.path.isdir(candidate)):
logging.info("%s: Found nearest %s at %s"
% (os.path.basename(filename), target, candidate))
return candidate
else:
parent = os.path.dirname(os.path.abspath(path))
if(parent == path):
raise RuntimeError("could not find %s" % target)
return FindNearest(parent, target, filename)
def FindFileInDb(database, filename):
logging.info("%s: Trying to find file in database..."
% (os.path.basename(filename)))
flags = database.GetCompilationInfoForFile(filename)
if flags.compiler_flags_:
logging.info("%s: Found file in database."
% (os.path.basename(filename)))
return flags
logging.info("%s: File not found in compilation db."
% (os.path.basename(filename)))
return None
def FindSiblingFileForHeader(database, filename):
logging.info("%s: Trying to find a sibling source file for that header in database..."
% (os.path.basename(filename)))
basename = os.path.splitext(filename)[0]
for extension in SOURCE_EXTENSIONS:
replacement_file = basename + extension
if os.path.exists(replacement_file):
flags = database.GetCompilationInfoForFile(replacement_file)
if flags.compiler_flags_:
logging.info("%s: Found sibling source file: %s"
% (os.path.basename(filename), replacement_file))
return flags
logging.info("%s: Did not find sibling source file."
% (os.path.basename(filename)))
return None
def FindNearestSourceFileInDb(database_path, database, srcfile):
logging.info("%s: Trying to find nearest source file in database..."
% (srcfile))
filename, flags = DoFindNearestSourceFileInDb(database_path, database, srcfile, None)
if flags:
logging.info("%s: Found nearest source file from %s: %s"
% (os.path.basename(srcfile), srcfile, filename))
return flags
logging.info("%s: Could not find nearest source file from %s in compilation db."
% (srcfile, srcfile))
return None
# Search subdirectories recursively, then do the same recursively for parent
# directories until a file was found or we have searched the database's directory
def DoFindNearestSourceFileInDb(database_path, database, directory, skip):
for root, dirnames, filenames in os.walk(directory):
if os.path.basename(skip) in dirnames:
dirnames.remove(os.path.basename(skip))
for filename in filenames:
if filename.endswith(tuple(SOURCE_EXTENSIONS)):
flags = database.GetCompilationInfoForFile(os.path.join(root, filename))
if flags.compiler_flags_:
return os.path.join(root, filename), flags
if database_path == directory or os.path.dirname(directory) == directory:
return None, None
return DoFindNearestSourceFileInDb(database_path, database, os.path.dirname(directory), directory)
def Pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = itertools.tee(iterable)
next(b, None)
return itertools.izip(a, b)
def RemoveClosingSlash(path):
if path.endswith('/'):
path = path[:-1]
return path
def SearchForTranslationUnitWhichIncludesPath(database_path, database, path, filename):
logging.info("%s: Trying to find a translation unit that includes our header's path..."
% (os.path.basename(filename)))
with open(database_path, 'r') as f:
jsonDb = json.load(f)
path = RemoveClosingSlash(os.path.abspath(path))
found = []
for translationUnit in jsonDb:
buildDir = translationUnit["directory"]
switches = translationUnit["command"].split()
for currentSwitch, nextSwitch in Pairwise(switches):
matchObj = re.match(r'(-I|-isystem)(.*)', currentSwitch)
includeDir = ""
isIncFlag = False
if currentSwitch == "-I" or currentSwitch == "-isystem":
includeDir = nextSwitch
isIncFlag = True
elif matchObj:
includeDir = matchObj.group(2)
isIncFlag = True
if not isIncFlag:
continue
includeDir = RemoveClosingSlash(os.path.abspath(os.path.join(buildDir, includeDir)))
# Check all the parent dirs in path
pathCopy = path
distance = 0
while pathCopy != os.path.abspath(os.sep):
if includeDir == pathCopy:
found.append((distance, str(translationUnit["file"])))
distance += 1
pathCopy, tail = os.path.split(pathCopy)
found.sort()
if len(found) == 0:
logging.info("%s: Did not find translation unit which includes path %s"
% (os.path.basename(filename), path))
return None
else:
result = found[0][1]
logging.info("%s: Found best source file which includes path: %s"
% (os.path.basename(filename), result))
return database.GetCompilationInfoForFile(result)
def GetUserExtraFlags(filename):
try:
extra_flags_file = FindNearest(os.path.dirname(filename), ".ycm_extra_flags", filename)
except:
logging.info("%s: No extra flags."
% (os.path.basename(filename)))
return None
with open(extra_flags_file, 'r') as f:
lines = f.readlines()
lines = [ line[0:line.find("#")].split() for line in lines ]
lines = list(itertools.chain.from_iterable(lines))
logging.info("%s: Extra flags = [\n\t\t%s\n]"
% (os.path.basename(filename), "\n\t\t".join(lines)))
return lines
def MakeRelativePathsInFlagsAbsolute(flags, working_directory):
if not working_directory:
return list(flags)
new_flags = []
make_next_absolute = False
for flag in flags:
new_flag = flag
if make_next_absolute:
make_next_absolute = False
if not flag.startswith('/'):
new_flag = os.path.join(working_directory, flag)
for path_flag in [ '-isystem', '-I', '-iquote', '--sysroot=' ]:
if flag == path_flag:
make_next_absolute = True
break
if flag.startswith(path_flag):
path = flag[ len(path_flag): ]
new_flag = path_flag + os.path.join(working_directory, path)
break
if new_flag:
new_flags.append(new_flag)
return new_flags