Skip to content

Commit 2a17b27

Browse files
authored
Merge pull request #75 from seladb/do-not-skip-headers-in-preprocessor
Add option to retain #include directives in preprocessor
2 parents 26da918 + 312f6fb commit 2a17b27

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

cxxheaderparser/preprocessor.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@ class PreprocessorError(Exception):
1616

1717

1818
class _CustomPreprocessor(Preprocessor):
19-
def __init__(self, encoding: typing.Optional[str]):
19+
def __init__(
20+
self,
21+
encoding: typing.Optional[str],
22+
passthru_includes: typing.Optional["re.Pattern"],
23+
):
2024
Preprocessor.__init__(self)
2125
self.errors: typing.List[str] = []
2226
self.assume_encoding = encoding
27+
self.passthru_includes = passthru_includes
2328

2429
def on_error(self, file, line, msg):
2530
self.errors.append(f"{file}:{line} error: {msg}")
@@ -58,12 +63,15 @@ def make_pcpp_preprocessor(
5863
include_paths: typing.List[str] = [],
5964
retain_all_content: bool = False,
6065
encoding: typing.Optional[str] = None,
66+
passthru_includes: typing.Optional["re.Pattern"] = None,
6167
) -> PreprocessorFunction:
6268
"""
6369
Creates a preprocessor function that uses pcpp (which must be installed
6470
separately) to preprocess the input text.
6571
6672
:param encoding: If specified any include files are opened with this encoding
73+
:param passthru_includes: If specified any #include directives that match the
74+
compiled regex pattern will be part of the output.
6775
6876
.. code-block:: python
6977
@@ -75,7 +83,7 @@ def make_pcpp_preprocessor(
7583
"""
7684

7785
def _preprocess_file(filename: str, content: str) -> str:
78-
pp = _CustomPreprocessor(encoding)
86+
pp = _CustomPreprocessor(encoding, passthru_includes)
7987
if include_paths:
8088
for p in include_paths:
8189
pp.add_path(p)

tests/test_preprocessor.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import os
22
import pathlib
3+
import re
34

45
from cxxheaderparser.options import ParserOptions
56
from cxxheaderparser.preprocessor import make_pcpp_preprocessor
6-
from cxxheaderparser.simple import NamespaceScope, ParsedData, parse_file, parse_string
7+
from cxxheaderparser.simple import (
8+
NamespaceScope,
9+
ParsedData,
10+
parse_file,
11+
parse_string,
12+
Include,
13+
)
714
from cxxheaderparser.types import (
815
FundamentalSpecifier,
916
NameSpecifier,
@@ -135,3 +142,23 @@ def test_preprocessor_encoding(tmp_path: pathlib.Path) -> None:
135142
]
136143
)
137144
)
145+
146+
147+
def test_preprocessor_passthru_includes(tmp_path: pathlib.Path) -> None:
148+
"""Ensure that all #include pass through"""
149+
h_content = '#include "t2.h"\n'
150+
151+
with open(tmp_path / "t1.h", "w") as fp:
152+
fp.write(h_content)
153+
154+
with open(tmp_path / "t2.h", "w") as fp:
155+
fp.write("")
156+
157+
options = ParserOptions(
158+
preprocessor=make_pcpp_preprocessor(passthru_includes=re.compile(".+"))
159+
)
160+
data = parse_file(tmp_path / "t1.h", options=options)
161+
162+
assert data == ParsedData(
163+
namespace=NamespaceScope(), includes=[Include(filename='"t2.h"')]
164+
)

0 commit comments

Comments
 (0)