-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcppfileparser.py
355 lines (317 loc) · 13.1 KB
/
cppfileparser.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import logging
import os
import re
from dataclasses import dataclass
from typing import Any, Dict, Generator, List, Optional, Set, Tuple
from helpers import resolvePath
from build import BuildTarget
from bazel import BazelCCImport
def findAllHeaderFiles(current_dir: str) -> Generator[str, None, None]:
for dirpath, dirname, files in os.walk(current_dir):
for f in files:
if f.endswith(".h") or f.endswith(".hpp"):
yield (f"{dirpath}/{f}")
def parseIncludes(includes: str) -> List[str]:
ret = []
matches = re.findall(r"-I([^ ](?:[^ ]|(?: (?!(?:-I)|(?:-isystem)|$)))+)", includes)
for m in matches:
if m not in ret:
ret.append(m)
return ret
seen = set()
@dataclass
class CPPIncludes:
foundHeaders: Set[Tuple[str, Optional[str]]]
notFoundHeaders: Set[str]
neededImports: Set[BuildTarget]
neededGeneratedFiles: Set[Tuple[str, Optional[str]]]
def __add__(self, other: "CPPIncludes") -> "CPPIncludes":
return CPPIncludes(
self.foundHeaders.union(other.foundHeaders),
self.notFoundHeaders.union(other.notFoundHeaders),
self.neededImports.union(other.neededImports),
self.neededGeneratedFiles.union(other.neededGeneratedFiles),
)
cache: Dict[str, CPPIncludes] = {}
def _findCPPIncludeForFile(
file: str,
includes_dirs: List[str],
current_dir: str,
cc_imports: List[BuildTarget],
compilerIncludes: List[str],
generatedFiles: Dict[str, Any],
generatedDir: Optional[str],
) -> Tuple[bool, CPPIncludes]:
found = False
ret = CPPIncludes(set(), set(), set(), set())
check = False
logging.info(f"_findCPPIncludeForFile: {file}")
for d in includes_dirs:
use_generated_dir = False
if d == "/generated":
full_file_name = file
use_generated_dir = True
elif d.startswith("/generated"):
full_file_name = f"{d.replace('/generated', '')}/{file}"
use_generated_dir = True
elif d.startswith("/"):
full_file_name = f"{d}/{file}"
else:
full_file_name = f"{current_dir}/{d}/{file}"
if use_generated_dir and full_file_name in generatedFiles:
# The search header is a generated one that whose path match the includes
# There might be something to do remove prefixes
ret.neededGeneratedFiles.add((full_file_name, d))
found = True
if not full_file_name.endswith(".pb.h"):
check = True
generatedFileFullName = full_file_name
tempDir = generatedFiles[full_file_name][1]
full_file_name = f"{tempDir}/{full_file_name}"
logging.debug(f"Found generated {file} in the includes variable")
break
# Search in the compiler include, depending on how things were done in the Ninja file
# the include path might have it or not ...
foundCCImport = False
for cdir in compilerIncludes:
full_file_name2 = resolvePath(f"{cdir}/{file}")
if not os.path.exists(full_file_name2) or os.path.isdir(full_file_name2):
continue
# File might be in the standard include path of the compiler but still coming from
# an external packate that we need to depends on
for imp in cc_imports:
assert isinstance(imp.opaque, BazelCCImport)
if full_file_name2 in imp.opaque.hdrs:
foundCCImport = True
logging.debug(f"Found {full_file_name} in {imp}")
ret.neededImports.add(imp)
break
found = True
break
if found and os.path.exists(full_file_name2):
if not foundCCImport:
logging.debug(f"Found {file} in the compiler include path: {cdir} skipping")
break
if not os.path.exists(full_file_name) or os.path.isdir(full_file_name):
continue
logging.debug(f"Found {file} in the includes variable using {d}")
# Check if the file is part of the cc_imports as we don't want to recurse for headers there
# We have to do it twice because now we are not looking at a file in the standard include paths
for imp in cc_imports:
assert isinstance(imp.opaque, BazelCCImport)
if full_file_name in imp.opaque.hdrs:
logging.info(f"Found {full_file_name} in cc_import {imp}")
ret.neededImports.add(imp)
found = True
break
if found:
break
full_file_name = resolvePath(full_file_name)
if not use_generated_dir:
# If generated dir is True it means that the header was found using a generated dir include
# we don't want to add it as is to the list of headers otherwise we will have a "/tmp" and it won't be great
if generatedDir and d == generatedDir:
ret.foundHeaders.add((full_file_name.replace(f"{generatedDir}/", ""), "/generated"))
else:
logging.info(f"Found {file} {full_file_name} in the includes variable using {d}")
ret.foundHeaders.add((full_file_name, d))
else:
ret.neededGeneratedFiles.add(("/generated" + generatedFileFullName, d))
found = True
check = True
break
if check:
cppIncludes = findCPPIncludes(
full_file_name,
includes_dirs,
compilerIncludes,
cc_imports,
generatedFiles,
use_generated_dir,
generatedDir,
)
if use_generated_dir:
newfoundHeaders = set()
for e in cppIncludes.foundHeaders:
# The list of header might include headers with the same temporary folder used by the current file
# the reason for that is that current file a.h might have #include "b.h" and b.h is generated
# so we end-up with returning /tmp/tmpxxbbcc/subfolder1/subfolder2/b.h
if e[0].startswith(tempDir):
cppIncludes.neededGeneratedFiles.add(
(e[0].replace(tempDir, "/generated"), e[1])
)
else:
newfoundHeaders.add((e[0], e[1]))
cppIncludes.foundHeaders = newfoundHeaders
ret += cppIncludes
return found, ret
def _findCPPIncludeForFileSameDir(
name: str,
file: str,
includes_dirs: List[str],
current_dir: str,
cc_imports: List[BuildTarget],
compilerIncludes: List[str],
generatedFiles: Dict[str, Any],
generatedDir: Optional[str],
generated: bool = False,
) -> Tuple[bool, CPPIncludes]:
ret = CPPIncludes(set(), set(), set(), set())
found = False
full_file_name = f"{current_dir}/{file}"
if not (os.path.exists(full_file_name) and not os.path.isdir(full_file_name)):
return False, ret
found = True
logging.debug(
f"Found {file} in the same directory as the looked file generated {generated}"
)
# We need a way of dealing with path with ..
full_file_name = resolvePath(full_file_name)
# Current file is generated so we are in some /tmp/tmpxxbbcc path and
# in this path we find `file` so it's safe to return "/generated"
if generated:
# full_file_name will have the same base folder (ie. /tmp/tmpxxbbcc) as the current file
# it's ok we cppIncludes will take care of it
# So this is tricky we don't know the generic name here only the resolved one and removing the folder of the file where we
# found this include won't help because it's most probably not the one used in generatedFiles dict.
# So we will need to iterate on the dict look for the values
foundGenerated = False
for k, v in generatedFiles.items():
#logging.info(f"Checking {k} against {name}")
if name.endswith(k):
foundGenerated = True
genericGeneratedHeader = f"{name.replace(v[1]+'/','').replace(os.path.basename(k), file)}"
ret.neededGeneratedFiles.add(
(genericGeneratedHeader, "/generated")
)
break
if not foundGenerated:
# We can't find the relative path because we only know the filename and the current directory
# So let's try to use the generatedDir that was passed as an argument to hopefully remove the prefix
assert generatedDir is not None
tmp = full_file_name.replace(f"{generatedDir}/","")
if tmp.startswith("/"):
logging.error(f"Could not find the relative path for {file} in the generated files")
ret.neededGeneratedFiles.add(
(tmp, "/generated")
)
return False, ret
else:
ret.foundHeaders.add((full_file_name, None))
cppIncludes = findCPPIncludes(
full_file_name,
includes_dirs,
compilerIncludes,
cc_imports,
generatedFiles,
generated,
generatedDir,
)
ret += cppIncludes
return found, ret
def findCPPIncludes(
name: str,
includes_dirs: List[str],
compilerIncludes: List[str],
cc_imports: List[BuildTarget],
generatedFiles: Dict[str, Any],
generated: bool = False,
generatedDir: Optional[str] = None,
) -> CPPIncludes:
key = f"{name}"
seenkey = f"{name} {includes_dirs}"
ret = CPPIncludes(set(), set(), set(), set())
# There is sometimes loop, as we don't really implement the #pragma once
# deal with it
if key in cache:
return cache[key]
if seenkey in seen:
return ret
seen.add(seenkey)
current_dir = os.path.dirname(os.path.abspath(name))
logging.debug(f"Handling findCPPIncludes {name}")
with open(name, "r") as f:
content = f.readlines()
for line in content:
found = False
match = re.match(r'\s*#\s*include ((?:<|").*(?:>|"))', line)
if not match:
continue
current_include = match.group(1)
file = current_include[1:-1]
if current_include.startswith('"'):
found, cppIncludes = _findCPPIncludeForFileSameDir(
name,
file,
includes_dirs,
current_dir,
cc_imports,
compilerIncludes,
generatedFiles,
generatedDir,
generated,
)
if not found:
if len(includes_dirs) == 0:
empty = CPPIncludes(set(), set(), set(), set())
return empty
found, cppIncludes = _findCPPIncludeForFile(
file,
includes_dirs,
current_dir,
cc_imports,
compilerIncludes,
generatedFiles,
generatedDir,
)
ret += cppIncludes
else:
ret += cppIncludes
else:
if len(includes_dirs) == 0:
empty = CPPIncludes(set(), set(), set(), set())
return empty
found, cppIncludes = _findCPPIncludeForFile(
file,
includes_dirs,
current_dir,
cc_imports,
compilerIncludes,
generatedFiles,
generatedDir,
)
ret += cppIncludes
if not found:
logging.warning(f"Could not find {file} as used by {name}, include dirs {includes_dirs}")
# We don't include compiler includes in the list of includes
if not found:
for d in compilerIncludes:
full_file_name = f"{d}/{file}"
if not os.path.exists(full_file_name) or os.path.isdir(full_file_name):
continue
logging.debug(f"Found {file} in the compiler includes")
found = True
break
if file in generatedFiles:
logging.info(f"Found missing header {file} in the generated files")
(found, cppIncludes) = _findCPPIncludeForFile(
file,
includes_dirs,
current_dir,
cc_imports,
compilerIncludes,
generatedFiles,
generatedDir
)
ret += cppIncludes
found = True
if not found:
logging.info(
f"Not found {file} in the compiler includes for {name} wih includes {includes_dirs}"
)
ret.notFoundHeaders.add(file)
if len(ret.notFoundHeaders) > 0:
ret.notFoundHeaders = set(filter(lambda x: not x.endswith(".pb.h"), ret.notFoundHeaders))
logging.debug(f"Could not find {set(ret.notFoundHeaders)} in {name}")
cache[key] = ret
return ret