MCPcopy Create free account
hub / github.com/Graphify-Labs/graphify / _cpp_conditional_groups

Function _cpp_conditional_groups

graphify/extract.py:3927–3980  ·  view source on GitHub ↗

Read conditional ranges only; never evaluate macros or open includes.

(source: bytes)

Source from the content-addressed store, hash-verified

3925
3926
3927def _cpp_conditional_groups(source: bytes) -> tuple[list[dict], str | None]:
3928 """Read conditional ranges only; never evaluate macros or open includes."""
3929 groups: list[dict] = []
3930 stack: list[dict] = []
3931 for match, _ in _cpp_conditional_tokens(source):
3932 word = match.group(2)
3933 if source[max(0, match.start() - 3):match.start()].endswith((b"\\\n", b"\\\r\n")):
3934 continue # A physical line after a splice is not a directive line.
3935 if word not in (b"if", b"ifdef", b"ifndef", b"elif", b"else", b"endif"):
3936 continue
3937 text = match.group().strip()
3938 argument = re.sub(rb"/\*[\s\S]*?\*/|//[^\r\n]*", b"", text)
3939 argument = re.sub(rb"^#[ \t]*[A-Za-z_]+", b"", argument).strip()
3940 if ((word in (b"if", b"elif") and not argument)
3941 or (word in (b"ifdef", b"ifndef")
3942 and not re.fullmatch(rb"[A-Za-z_][A-Za-z_0-9]*", argument))
3943 or (word in (b"else", b"endif") and argument)):
3944 return groups, "malformed conditional directive"
3945 line = source.count(b"\n", 0, match.start()) + 1
3946 branch = {"directive": text.decode("utf-8", errors="replace"),
3947 "directive_line": line, "start_byte": match.end(),
3948 "start_line": source.count(b"\n", 0, match.end()) + 2}
3949 if word in (b"if", b"ifdef", b"ifndef"):
3950 if len(groups) >= 128:
3951 return groups, "conditional-group limit exceeded"
3952 group = {"line": line, "start_byte": match.start(), "branches": [branch],
3953 "directives": [(match.start(), match.end())]}
3954 if stack:
3955 group["parent"] = groups.index(stack[-1])
3956 group["parent_branch"] = len(stack[-1]["branches"]) - 1
3957 groups.append(group)
3958 stack.append(group)
3959 continue
3960 if not stack:
3961 return groups, "unbalanced conditional directives"
3962 group = stack[-1]
3963 if group.get("has_else"):
3964 if word != b"endif":
3965 return groups, "conditional branch after else"
3966 previous = group["branches"][-1]
3967 previous.update(end_byte=match.start(), end_line=line - 1)
3968 group["directives"].append((match.start(), match.end()))
3969 if word in (b"elif", b"else"):
3970 group["branches"].append(branch)
3971 group["has_else"] = word == b"else"
3972 else:
3973 if not group.get("has_else"):
3974 group["branches"].append({"directive": "#else (implicit)",
3975 "directive_line": line, "start_byte": match.start(),
3976 "end_byte": match.start(), "start_line": line, "end_line": line - 1,
3977 "implicit": True})
3978 group["end_byte"] = match.end()
3979 stack.pop()
3980 return groups, "unbalanced conditional directives" if stack else None
3981
3982
3983def _cpp_expression_conditional_groups(source: bytes, groups: list[dict]) -> set[int]:

Callers 1

Calls 4

_cpp_conditional_tokensFunction · 0.85
updateMethod · 0.80
startMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected