-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_module_scan.py
More file actions
679 lines (551 loc) · 21.4 KB
/
cpp_module_scan.py
File metadata and controls
679 lines (551 loc) · 21.4 KB
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
from __future__ import annotations
from dataclasses import dataclass
from typing import Any
from cpp_index_model import Diagnostic, SourceRange, StructuralEvent, Token
from cpp_index_utils import normalize_signature_spacing, source_text_range
from cpp_lexer import tokenize_lines, tokens_to_text
@dataclass(slots=True)
class ModuleScanResult:
module: dict[str, Any]
imports: list[dict[str, Any]]
includes: list[dict[str, Any]]
exports: list[dict[str, Any]]
structural_events: list[StructuralEvent]
diagnostics: list[Diagnostic]
# ---------------------------------------------------------------------------
# Statement stream
# ---------------------------------------------------------------------------
def iter_semicolon_statements(tokens: list[Token]):
start = 0
for index, token in enumerate(tokens):
if token.value != ";":
continue
statement = tokens[start : index + 1]
if statement:
yield statement
start = index + 1
def statement_values(statement: list[Token]) -> list[str]:
return [token.value for token in statement]
def statement_range(statement: list[Token]) -> SourceRange:
first = statement[0]
last = statement[-1]
return SourceRange(
start_line=first.line,
end_line=last.line,
start_col0=first.col0,
end_col0=last.col0,
)
def statement_text(lines: list[str], statement: list[Token]) -> str:
first = statement[0]
last = statement[-1]
return source_text_range(
lines,
first.line,
last.line,
last.col0 + len(last.value),
)
# ---------------------------------------------------------------------------
# Token-level parsing helpers
# ---------------------------------------------------------------------------
def parse_dotted_name(tokens: list[Token], index: int) -> tuple[str | None, int]:
parts: list[str] = []
if index >= len(tokens) or tokens[index].kind != "identifier":
return None, index
parts.append(tokens[index].value)
index += 1
while index + 1 < len(tokens):
if tokens[index].value != ".":
break
if tokens[index + 1].kind != "identifier":
break
parts.append(tokens[index + 1].value)
index += 2
return ".".join(parts), index
def parse_module_declaration(statement: list[Token]) -> dict[str, Any] | None:
values = statement_values(statement)
if not values:
return None
index = 0
exported = False
if values[index] == "export":
exported = True
index += 1
if index >= len(statement) or statement[index].value != "module":
return None
module_token = statement[index]
index += 1
# global module fragment: module;
if index < len(statement) and statement[index].value == ";":
return {
"kind": "global_module_fragment",
"exported": False,
"moduleName": None,
"partitionName": None,
"fullModuleName": None,
"startLine": module_token.line,
"endLine": statement[-1].line,
"startCol0": module_token.col0,
"endCol0": statement[-1].col0,
}
# private module fragment: module : private;
if (
index + 2 < len(statement)
and statement[index].value == ":"
and statement[index + 1].value == "private"
and statement[index + 2].value == ";"
):
return {
"kind": "private_module_fragment",
"exported": False,
"moduleName": None,
"partitionName": None,
"fullModuleName": None,
"startLine": module_token.line,
"endLine": statement[-1].line,
"startCol0": module_token.col0,
"endCol0": statement[-1].col0,
}
module_name, index = parse_dotted_name(statement, index)
if module_name is None:
return None
partition_name = None
if index < len(statement) and statement[index].value == ":":
partition_name, index = parse_dotted_name(statement, index + 1)
if partition_name is None:
return None
if index >= len(statement) or statement[index].value != ";":
return None
full_module_name = module_name
if partition_name:
full_module_name = f"{module_name}:{partition_name}"
return {
"kind": "module_declaration",
"exported": exported,
"moduleName": module_name,
"partitionName": partition_name,
"fullModuleName": full_module_name,
"startLine": module_token.line,
"endLine": statement[-1].line,
"startCol0": module_token.col0,
"endCol0": statement[-1].col0,
}
def parse_import_declaration(statement: list[Token]) -> dict[str, Any] | None:
values = statement_values(statement)
if not values:
return None
index = 0
exported = False
if values[index] == "export":
exported = True
index += 1
if index >= len(statement) or statement[index].value != "import":
return None
import_token = statement[index]
index += 1
if index >= len(statement):
return None
raw_tokens: list[Token] = []
while index < len(statement) and statement[index].value != ";":
raw_tokens.append(statement[index])
index += 1
if index >= len(statement) or statement[index].value != ";":
return None
if not raw_tokens:
return None
raw_module = import_target_text(raw_tokens)
return {
"kind": "import_declaration",
"exported": exported,
"rawModule": raw_module,
"startLine": import_token.line,
"endLine": statement[-1].line,
"startCol0": import_token.col0,
"endCol0": statement[-1].col0,
}
def import_target_text(tokens: list[Token]) -> str:
if not tokens:
return ""
# Partition import:
# import :ElementImpl;
# must become:
# :ElementImpl
if tokens[0].value == ":":
return ":" + tokens_to_text(tokens[1:]).lstrip()
# Header unit:
# import <vector>;
if tokens[0].value == "<":
return tokens_to_text(tokens).replace("< ", "<").replace(" >", ">")
# Normal module name:
# import Foo.Bar;
return tokens_to_text(tokens).replace(" . ", ".")
def classify_module_unit_kind(
*,
is_module_unit: bool,
exported: bool,
partition_name: str | None,
) -> str:
if not is_module_unit:
return "non_module"
if exported and partition_name:
return "module_partition_interface"
if exported:
return "module_interface"
if partition_name:
return "module_partition_implementation"
return "module_implementation"
def classify_import_kind(raw_module: str) -> str:
text = raw_module.strip()
if text.startswith("<") and text.endswith(">"):
return "header_unit_import"
if text == "std" or text.startswith("std."):
return "std_import"
if text.startswith(":"):
return "module_partition_import"
# Stream parser already tokenized this; this final check is intentionally
# simple and only classifies the normalized token text.
if text and all(part and (part[0].isalpha() or part[0] == "_") for part in text.split(".")):
return "module_import"
return "unknown_import"
def resolve_import_module(
*,
raw_module: str,
import_kind: str,
module_name: str | None,
) -> str | None:
raw_module = raw_module.replace(": ", ":")
if import_kind == "module_partition_import":
if module_name is None:
return None
return f"{module_name}{raw_module}"
return raw_module
def determine_fragment_for_line(line_no: int, module_info: dict[str, Any]) -> str:
global_fragment = module_info["globalModuleFragment"]
private_fragment = module_info["privateModuleFragment"]
if (
global_fragment["present"]
and global_fragment["startLine"] <= line_no <= global_fragment["endLine"]
):
return "global_module_fragment"
if (
private_fragment["present"]
and private_fragment["startLine"] <= line_no <= private_fragment["endLine"]
):
return "private_module_fragment"
if module_info["isModuleUnit"]:
return "module_purview"
return "unknown"
# ---------------------------------------------------------------------------
# JSON/event builders
# ---------------------------------------------------------------------------
def make_module_structural_event(
*,
kind: str,
start_line: int,
end_line: int,
start_col0: int,
end_col0: int,
signature: str,
order: int,
exported: bool,
fragment: str,
) -> StructuralEvent:
return StructuralEvent(
kind=kind,
name="",
qualified_name="",
start_line=start_line,
end_line=end_line,
start_col0=start_col0,
end_col0=end_col0,
open_brace_line=None,
open_brace_col0=None,
close_line=None,
signature=signature,
order=order,
inline=False,
exported=exported,
fragment=fragment,
template=None,
)
def make_module_info(
*,
total_lines: int,
global_fragment_decl: dict[str, Any] | None,
module_decl: dict[str, Any] | None,
private_fragment_decl: dict[str, Any] | None,
module_decl_text: str | None,
) -> dict[str, Any]:
is_module_unit = module_decl is not None
module_name = module_decl["moduleName"] if module_decl else None
partition_name = module_decl["partitionName"] if module_decl else None
full_module_name = module_decl["fullModuleName"] if module_decl else None
exported = bool(module_decl and module_decl["exported"])
if global_fragment_decl is not None:
if module_decl is not None:
global_end = max(global_fragment_decl["startLine"], module_decl["startLine"] - 1)
else:
global_end = total_lines
global_fragment = {
"present": True,
"startLine": global_fragment_decl["startLine"],
"endLine": global_end,
"declarationLine": global_fragment_decl["startLine"],
}
else:
global_fragment = {
"present": False,
"startLine": None,
"endLine": None,
"declarationLine": None,
}
if private_fragment_decl is not None:
private_fragment = {
"present": True,
"startLine": private_fragment_decl["startLine"],
"endLine": total_lines,
"declarationLine": private_fragment_decl["startLine"],
}
else:
private_fragment = {
"present": False,
"startLine": None,
"endLine": None,
"declarationLine": None,
}
module_declaration = None
if module_decl is not None:
module_declaration = {
"startLine": module_decl["startLine"],
"endLine": module_decl["endLine"],
"text": module_decl_text or "",
}
return {
"isModuleUnit": is_module_unit,
"unitKind": classify_module_unit_kind(
is_module_unit=is_module_unit,
exported=exported,
partition_name=partition_name,
),
"moduleName": module_name,
"partitionName": partition_name,
"fullModuleName": full_module_name,
"isExportedModuleDeclaration": exported,
"globalModuleFragment": global_fragment,
"moduleDeclaration": module_declaration,
"privateModuleFragment": private_fragment,
}
# ---------------------------------------------------------------------------
# Public API
# ---------------------------------------------------------------------------
def scan_module_facts(lines: list[str]) -> ModuleScanResult:
tokens = tokenize_lines(lines)
total_lines = len(lines)
diagnostics: list[Diagnostic] = []
structural_events: list[StructuralEvent] = []
global_fragment_decl: dict[str, Any] | None = None
private_fragment_decl: dict[str, Any] | None = None
module_decl: dict[str, Any] | None = None
module_decl_text: str | None = None
parsed_imports: list[dict[str, Any]] = []
order = 0
for statement in iter_semicolon_statements(tokens):
module_parse = parse_module_declaration(statement)
if module_parse is not None:
signature = statement_text(lines, statement)
if module_parse["kind"] == "global_module_fragment":
if global_fragment_decl is None:
global_fragment_decl = module_parse
else:
diagnostics.append(
Diagnostic(
severity="warning",
code="unknown_module_declaration",
message="Duplicate global module fragment declaration.",
start_line=module_parse["startLine"],
end_line=module_parse["endLine"],
)
)
structural_events.append(
make_module_structural_event(
kind="global_module_fragment",
start_line=module_parse["startLine"],
end_line=module_parse["endLine"],
start_col0=module_parse["startCol0"],
end_col0=module_parse["endCol0"],
signature=signature,
order=order,
exported=False,
fragment="global_module_fragment",
)
)
order += 1
continue
if module_parse["kind"] == "private_module_fragment":
if private_fragment_decl is None:
private_fragment_decl = module_parse
else:
diagnostics.append(
Diagnostic(
severity="warning",
code="unexpected_private_module_fragment",
message="Duplicate private module fragment declaration.",
start_line=module_parse["startLine"],
end_line=module_parse["endLine"],
)
)
# Fragment is resolved again after module_info is known.
structural_events.append(
make_module_structural_event(
kind="private_module_fragment",
start_line=module_parse["startLine"],
end_line=module_parse["endLine"],
start_col0=module_parse["startCol0"],
end_col0=module_parse["endCol0"],
signature=signature,
order=order,
exported=False,
fragment="private_module_fragment",
)
)
order += 1
continue
if module_parse["kind"] == "module_declaration":
if module_decl is None:
module_decl = module_parse
module_decl_text = signature
else:
diagnostics.append(
Diagnostic(
severity="warning",
code="duplicate_module_declaration",
message="Duplicate module declaration detected; keeping the first one.",
start_line=module_parse["startLine"],
end_line=module_parse["endLine"],
)
)
structural_events.append(
make_module_structural_event(
kind="module_declaration",
start_line=module_parse["startLine"],
end_line=module_parse["endLine"],
start_col0=module_parse["startCol0"],
end_col0=module_parse["endCol0"],
signature=signature,
order=order,
exported=module_parse["exported"],
fragment="module_purview",
)
)
order += 1
continue
import_parse = parse_import_declaration(statement)
if import_parse is not None:
parsed_imports.append(
{
**import_parse,
"text": statement_text(lines, statement),
}
)
structural_events.append(
make_module_structural_event(
kind="import_declaration",
start_line=import_parse["startLine"],
end_line=import_parse["endLine"],
start_col0=import_parse["startCol0"],
end_col0=import_parse["endCol0"],
signature=statement_text(lines, statement),
order=order,
exported=import_parse["exported"],
fragment="unknown",
)
)
order += 1
continue
module_info = make_module_info(
total_lines=total_lines,
global_fragment_decl=global_fragment_decl,
module_decl=module_decl,
private_fragment_decl=private_fragment_decl,
module_decl_text=module_decl_text,
)
# Now that module_info exists, fill import/export fragment data.
imports: list[dict[str, Any]] = []
exports: list[dict[str, Any]] = []
if module_decl is not None and module_decl["exported"]:
exports.append(
{
"kind": "export_module_declaration",
"range": SourceRange(
start_line=module_decl["startLine"],
end_line=module_decl["endLine"],
start_col0=module_decl["startCol0"],
end_col0=module_decl["endCol0"],
).to_json(),
"signature": module_decl_text or "",
"fragment": determine_fragment_for_line(module_decl["startLine"], module_info),
}
)
for item in parsed_imports:
import_kind = classify_import_kind(item["rawModule"])
resolved_module = resolve_import_module(
raw_module=item["rawModule"],
import_kind=import_kind,
module_name=module_info["moduleName"],
)
if import_kind == "module_partition_import" and resolved_module is None:
diagnostics.append(
Diagnostic(
severity="warning",
code="unresolved_partition_import",
message=f"Could not resolve partition import {item['rawModule']} without current module name.",
start_line=item["startLine"],
end_line=item["endLine"],
)
)
fragment = determine_fragment_for_line(item["startLine"], module_info)
imports.append(
{
"kind": import_kind,
"module": item["rawModule"],
"resolvedModule": resolved_module,
"isExported": item["exported"],
"range": SourceRange(
start_line=item["startLine"],
end_line=item["endLine"],
start_col0=item["startCol0"],
end_col0=item["endCol0"],
).to_json(),
"text": item["text"],
"fragment": fragment,
}
)
if item["exported"]:
exports.append(
{
"kind": "export_import",
"range": SourceRange(
start_line=item["startLine"],
end_line=item["endLine"],
start_col0=item["startCol0"],
end_col0=item["endCol0"],
).to_json(),
"signature": item["text"],
"fragment": fragment,
}
)
# #include directives are line-based preprocessor metadata. They are
# extracted in cpp_file_index where source paths are available for
# best-effort project-relative resolution.
includes: list[dict[str, Any]] = []
# Update unknown event fragments now that module_info is known.
for event in structural_events:
if event.fragment == "unknown":
event.fragment = determine_fragment_for_line(event.start_line, module_info)
return ModuleScanResult(
module=module_info,
imports=imports,
includes=includes,
exports=exports,
structural_events=structural_events,
diagnostics=diagnostics,
)