Recover expression directives with separate, bounded syntactic branch views. shortcut: branch-wise views do not enumerate configurations, use a compilation database and a real configured frontend when configuration semantics are needed.
(path: Path, source: bytes, original: dict)
| 4020 | |
| 4021 | |
| 4022 | def _recover_cpp_conditional_initializers(path: Path, source: bytes, original: dict) -> dict: |
| 4023 | """Recover expression directives with separate, bounded syntactic branch views. |
| 4024 | |
| 4025 | shortcut: branch-wise views do not enumerate configurations, use a compilation |
| 4026 | database and a real configured frontend when configuration semantics are needed. |
| 4027 | """ |
| 4028 | if not original.get("parse_errors"): |
| 4029 | return original |
| 4030 | groups, problem = _cpp_conditional_groups(source) |
| 4031 | if not groups and not problem: |
| 4032 | return original |
| 4033 | coverage = {"schema": 1, "status": "unrecovered", "coverage": "branch-wise syntax", |
| 4034 | "conditions_evaluated": False, "combinations_enumerated": False, |
| 4035 | "recovered_from": original["parse_errors"]} |
| 4036 | |
| 4037 | def annotate(result: dict) -> dict: |
| 4038 | file_node = next((n for n in result.get("nodes", []) |
| 4039 | if n.get("id") == _make_id(str(path))), None) |
| 4040 | if file_node is not None: |
| 4041 | file_node.setdefault("metadata", {})["cpp_conditional_recovery"] = coverage |
| 4042 | return result |
| 4043 | |
| 4044 | if problem: |
| 4045 | coverage["reason"] = problem |
| 4046 | return annotate(original) |
| 4047 | selected = _cpp_expression_conditional_groups(source, groups) |
| 4048 | if not selected: |
| 4049 | return original |
| 4050 | coverage["groups"] = [{k: v for k, v in g.items() if k not in ("directives", "has_else")} |
| 4051 | | {"recovered": i in selected} for i, g in enumerate(groups)] |
| 4052 | choices = [tuple(0 for _ in groups)] |
| 4053 | for index in sorted(selected): |
| 4054 | for branch in range(len(groups[index]["branches"])): |
| 4055 | choice = list(choices[0]) |
| 4056 | choice[index] = branch |
| 4057 | current = groups[index] |
| 4058 | while "parent" in current: |
| 4059 | parent = current["parent"] |
| 4060 | choice[parent] = current["parent_branch"] |
| 4061 | current = groups[parent] |
| 4062 | value = tuple(choice) |
| 4063 | if value not in choices: |
| 4064 | choices.append(value) |
| 4065 | if len(choices) > _CPP_CONDITIONAL_VARIANT_LIMIT: |
| 4066 | coverage["reason"] = "branch-view limit exceeded" |
| 4067 | return annotate(original) |
| 4068 | coverage["variant_count"] = len(choices) |
| 4069 | |
| 4070 | def active(index: int, choice: tuple[int, ...]) -> bool: |
| 4071 | current = groups[index] |
| 4072 | while "parent" in current: |
| 4073 | parent = current["parent"] |
| 4074 | if parent in selected and choice[parent] != current["parent_branch"]: |
| 4075 | return False |
| 4076 | current = groups[parent] |
| 4077 | return True |
| 4078 | |
| 4079 | coverage["views"] = [[{"group_line": groups[index]["line"], "branch": choice[index]} |
no test coverage detected