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

Function _normalize_cpp_export_macros

graphify/extract.py:3785–3848  ·  view source on GitHub ↗

Blank an export macro between ``class``/``struct`` and the type name, and one leading a function or variable declaration (#5550). Matches inside comments and string, char, or raw string literals are left unchanged. Single ALL-CAPS identifier macros (and stacked ones) are handled. No

(source: bytes)

Source from the content-addressed store, hash-verified

3783
3784
3785def _normalize_cpp_export_macros(source: bytes) -> bytes:
3786 """Blank an export macro between ``class``/``struct`` and the type name,
3787 and one leading a function or variable declaration (#5550). Matches inside
3788 comments and string, char, or raw string literals are left unchanged.
3789
3790 Single ALL-CAPS identifier macros (and stacked ones) are handled.
3791 Note: paren-attribute macros like ``__declspec(dllexport)`` and
3792 ``__attribute__((visibility("default")))`` are currently unsupported,
3793 as well as template specializations (e.g. ``template<> class MODULE_API Widget<int>``).
3794 """
3795 if not (
3796 _CPP_EXPORT_MACRO_RE.search(source)
3797 or _CPP_EXPORT_DECL_MACRO_RE.search(source)
3798 ):
3799 return source
3800
3801 excluded_spans = _cpp_comment_and_literal_spans(source)
3802 excluded_starts = [start for start, _ in excluded_spans]
3803
3804 def is_excluded(start: int) -> bool:
3805 index = bisect_right(excluded_starts, start) - 1
3806 return index >= 0 and start < excluded_spans[index][1]
3807
3808 def repl(m: re.Match[bytes]) -> bytes:
3809 start = m.start()
3810 if is_excluded(start):
3811 return m.group(0)
3812
3813 # Check if preceded by `(` (e.g. range-based for loop `for(class MACRO var: items)`)
3814 idx = start - 1
3815 while idx >= 0 and source[idx] in b" \t\r\n":
3816 idx -= 1
3817 if idx >= 0 and source[idx] == ord(b"("):
3818 return m.group(0)
3819
3820 # Check for brace initialization like `var{1}`
3821 if m.group(4) == b"{":
3822 end = m.end()
3823 idx_after = end
3824 while idx_after < len(source) and source[idx_after] in b" \t\r\n":
3825 idx_after += 1
3826 if idx_after < len(source):
3827 next_char = source[idx_after : idx_after + 1]
3828 if next_char in b"0123456789\"&#x27;-":
3829 return m.group(0)
3830
3831 # Blank the macro with spaces to preserve offsets, but KEEP NEWLINES!
3832 macro_text = m.group(2)
3833 blanked_macro = bytearray(macro_text)
3834 for i in range(len(blanked_macro)):
3835 if blanked_macro[i] not in b"\r\n":
3836 blanked_macro[i] = ord(b" ")
3837
3838 return m.group(1) + bytes(blanked_macro) + m.group(3) + m.group(4)
3839
3840 # Both rewrites preserve byte lengths, so the original token spans stay valid.
3841 source = _CPP_EXPORT_MACRO_RE.sub(repl, source)
3842

Calls 1