51
51
52
52
53
53
def DirectoryOfThisScript ():
54
+ """
55
+ Return the absolute path of the parent directory containing this
56
+ script.
57
+ """
54
58
return os .path .dirname (os .path .abspath (__file__ ))
55
59
56
60
57
61
def GuessBuildDirectory ():
62
+ """
63
+ Guess the build directory using the following heuristics:
64
+
65
+ 1. Returns the current directory of this script plus 'build'
66
+ subdirectory in absolute path if this subdirectory exists.
67
+
68
+ 2. Otherwise, probes whether there exists any directory
69
+ containing '.ninja_log' file two levels above the current directory;
70
+ returns this single directory only if there is one candidate.
71
+ """
58
72
result = os .path .join (DirectoryOfThisScript (), "build" )
59
73
60
74
if os .path .exists (result ):
@@ -73,6 +87,16 @@ def GuessBuildDirectory():
73
87
74
88
75
89
def TraverseByDepth (root , include_extensions ):
90
+ """
91
+ Return a set of child directories of the 'root' containing file
92
+ extensions specified in 'include_extensions'.
93
+
94
+ NOTE:
95
+ 1. The 'root' directory itself is excluded from the result set.
96
+ 2. No subdirectories would be excluded if 'include_extensions' is left
97
+ to 'None'.
98
+ 3. Each entry in 'include_extensions' must begin with string '.'.
99
+ """
76
100
is_root = True
77
101
result = set ()
78
102
# Perform a depth first top down traverse of the given directory tree.
@@ -132,6 +156,11 @@ def TraverseByDepth(root, include_extensions):
132
156
133
157
134
158
def MakeRelativePathsInFlagsAbsolute (flags , working_directory ):
159
+ """
160
+ Iterate through 'flags' and replace the relative paths prefixed by
161
+ '-isystem', '-I', '-iquote', '--sysroot=' with absolute paths
162
+ start with 'working_directory'.
163
+ """
135
164
if not working_directory :
136
165
return list (flags )
137
166
new_flags = []
@@ -161,11 +190,17 @@ def MakeRelativePathsInFlagsAbsolute(flags, working_directory):
161
190
162
191
163
192
def IsHeaderFile (filename ):
193
+ """
194
+ Check whether 'filename' is considered as a header file.
195
+ """
164
196
extension = os .path .splitext (filename )[1 ]
165
197
return extension in HEADER_EXTENSIONS
166
198
167
199
168
200
def GetCompilationInfoForFile (filename ):
201
+ """
202
+ Helper function to look up compilation info of 'filename' in the 'database'.
203
+ """
169
204
# The compilation_commands.json file generated by CMake does not have
170
205
# entries for header files. So we do our best by asking the db for flags for
171
206
# a corresponding source file, if any. If one exists, the flags for that
@@ -187,6 +222,13 @@ def GetCompilationInfoForFile(filename):
187
222
188
223
189
224
def FlagsForFile (filename , ** kwargs ):
225
+ """
226
+ Callback function to be invoked by YouCompleteMe in order to get the
227
+ information necessary to compile 'filename'.
228
+
229
+ It returns a dictionary with a single element 'flags'. This element is a
230
+ list of compiler flags to pass to libclang for the file 'filename'.
231
+ """
190
232
if database :
191
233
# Bear in mind that compilation_info.compiler_flags_ does NOT return a
192
234
# python list, but a "list-like" StringVec object
0 commit comments