Backends¶
Parser backends convert C/C++ source code into the autopxd IR.
Backend Registry¶
backends
¶
Parser backends for autopxd.
This package contains parser backend implementations that convert C/C++ source code into the autopxd IR (Intermediate Representation).
Available Backends¶
pycparser Pure Python C99 parser. Default backend with no external dependencies. Requires preprocessed input (CPP/clang -E output).
libclang
LLVM clang-based parser with full C++ support. Requires system
libclang library and matching clang2 Python package.
Example¶
::
from autopxd.backends import get_backend, list_backends
# Get the default backend
backend = get_backend()
# Get a specific backend
backend = get_backend("libclang")
# List available backends
for name in list_backends():
print(name)
get_backend(name=None)
¶
Get a parser backend instance.
Returns a new instance of the requested backend. If no name is provided, returns the default backend (pycparser).
::
from autopxd.backends import get_backend
# Get default backend
backend = get_backend()
# Get libclang backend
clang = get_backend("libclang")
# Parse a header
header = backend.parse(code, "myheader.h")
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
Backend name (e.g., |
None
|
Returns:
| Type | Description |
|---|---|
ParserBackend
|
New instance of the requested backend. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the requested backend is not available. Example ------- |
list_backends()
¶
List names of all registered backends.
::
from autopxd.backends import list_backends
for name in list_backends():
print(f"Available: {name}")
Returns:
| Type | Description |
|---|---|
list[str]
|
List of backend names that can be passed to :func: |
register_backend(name, backend_class, is_default=False)
¶
Register a parser backend.
Called by backend modules during import to add themselves to the registry.
The first registered backend becomes the default unless is_default is
explicitly set on a later registration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Unique name for the backend (e.g., |
required |
backend_class
|
type[ParserBackend]
|
Class implementing the :class: |
required |
is_default
|
bool
|
If True, this becomes the default backend for :func: |
False
|
pycparser Backend¶
pycparser_backend
¶
pycparser-based parser backend.
This backend uses pycparser (pure Python C99 parser) to parse C header files. It's the default backend since it requires no external dependencies beyond the pycparser package itself.
Limitations¶
- C99 only - no C++ support (use libclang for C++)
- Cannot extract
#definemacro values (processed by preprocessor)
Example¶
::
from autopxd.backends.pycparser_backend import PycparserBackend
backend = PycparserBackend()
header = backend.parse(code, "myheader.h")
PycparserBackend
¶
Parser backend using pycparser.
The default autopxd parser backend, using the pure-Python pycparser library. This backend has no external dependencies but requires preprocessed C code as input.
Properties¶
name : str
Returns "pycparser".
supports_macros : bool
Returns False - macros are consumed by the preprocessor.
supports_cpp : bool
Returns False - pycparser only supports C99.
Example¶
::
from autopxd.backends.pycparser_backend import PycparserBackend
backend = PycparserBackend()
# Parse preprocessed code
preprocessed = run_cpp("myheader.h")
header = backend.parse(preprocessed, "myheader.h")
for decl in header.declarations:
print(decl)
Source code in autopxd/backends/pycparser_backend.py
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 | |
parse(code, filename, include_dirs=None, extra_args=None)
¶
Parse C code using pycparser.
The code is automatically preprocessed using the system's C preprocessor
(cpp on Unix/Mac, clang -E on macOS, or cl.exe /E on Windows).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
C source code to parse. |
required |
filename
|
str
|
Source filename for error messages and location tracking. |
required |
include_dirs
|
list[str] | None
|
Additional include directories for the preprocessor. |
None
|
extra_args
|
list[str] | None
|
Extra arguments to pass to the preprocessor. |
None
|
Returns:
| Type | Description |
|---|---|
Header
|
:class: |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If preprocessing fails. |
pycparser.plyparser.ParseError
|
If the code has syntax errors. |
Source code in autopxd/backends/pycparser_backend.py
libclang Backend¶
libclang_backend
¶
libclang-based parser backend.
This backend uses libclang (LLVM's C/C++ parser) to parse header files. It provides full C/C++ support including templates, namespaces, and classes.
Requirements¶
- System libclang library must be installed
- Python clang2 bindings version must match system libclang version
(e.g.,
clang2==18.*for LLVM 18)
If system libclang is not available, autopxd2 automatically falls back to the pycparser backend (C99 only).
Advantages over pycparser¶
- Full C++ support (classes, templates, namespaces)
- Handles complex preprocessor constructs
- Uses the same parser as production compilers
- Better error messages with source locations
Limitations¶
- Macro extraction is limited due to Python bindings constraints
- Requires system libclang installation
Example¶
::
from autopxd.backends.libclang_backend import LibclangBackend
backend = LibclangBackend()
header = backend.parse(code, "myheader.hpp", extra_args=["-std=c++17"])
LibclangBackend
¶
Parser backend using libclang.
Uses LLVM's libclang to parse C and C++ code. This backend supports the full C++ language including templates, classes, and namespaces.
Properties¶
name : str
Returns "libclang".
supports_macros : bool
Returns False - macro extraction is limited in Python bindings.
supports_cpp : bool
Returns True - full C++ support.
Example¶
::
from autopxd.backends.libclang_backend import LibclangBackend
backend = LibclangBackend()
# Parse C++ code with specific standard
header = backend.parse(
code,
"myheader.hpp",
extra_args=["-std=c++17", "-DDEBUG=1"]
)
Source code in autopxd/backends/libclang_backend.py
1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 | |
parse(code, filename, include_dirs=None, extra_args=None, use_default_includes=True, recursive_includes=True, max_depth=10, project_prefixes=None)
¶
Parse C/C++ code using libclang.
Unlike the pycparser backend, this method handles raw (unpreprocessed) code and performs preprocessing internally.
Umbrella header support: If the header has few/no declarations but many includes (umbrella header pattern), this method can recursively parse the included headers and combine their declarations.
::
# Basic usage
header = backend.parse(
code,
"myheader.hpp",
include_dirs=["/usr/local/include"],
extra_args=["-std=c++17", "-DNDEBUG"]
)
# Umbrella header (all-includes) pattern
header = backend.parse(
code,
"LibraryAll.h",
include_dirs=["./include"],
recursive_includes=True # Auto-detect and expand includes
)
# Umbrella header in system location
header = backend.parse(
code,
"sodium.h",
include_dirs=["/opt/homebrew/include"],
project_prefixes=("/opt/homebrew/include/sodium",) # Whitelist sodium/*
)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
C/C++ source code to parse (raw, not preprocessed). |
required |
filename
|
str
|
Source filename for error messages and location tracking. |
required |
include_dirs
|
list[str] | None
|
Additional include directories (converted to |
None
|
extra_args
|
list[str] | None
|
Additional compiler arguments (e.g., |
None
|
use_default_includes
|
bool
|
If True (default), automatically detect and add system include directories by querying the system clang compiler. Set to False to disable this behavior. |
True
|
recursive_includes
|
bool
|
If True (default), detect umbrella headers and recursively parse included project headers. System headers are always skipped. Set to False to only parse the main file. |
True
|
max_depth
|
int
|
Maximum recursion depth for include processing (default 10). Prevents infinite recursion from circular includes. |
10
|
project_prefixes
|
tuple[str, ...] | None
|
Optional tuple of path prefixes to treat as project headers (not system). Use this for umbrella headers of libraries installed in system locations (e.g., |
None
|
Returns:
| Type | Description |
|---|---|
Header
|
:class: |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If parsing fails with errors. Example ------- |
Source code in autopxd/backends/libclang_backend.py
1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 | |