Skip to content

Commit

Permalink
Add passthru_all_includes as a param to make_pcpp_preprocessor()
Browse files Browse the repository at this point in the history
  • Loading branch information
seladb committed Oct 5, 2023
1 parent 9f84384 commit 9fe4713
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
11 changes: 8 additions & 3 deletions cxxheaderparser/preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ class PreprocessorError(Exception):


class _CustomPreprocessor(Preprocessor):
def __init__(self, encoding: typing.Optional[str]):
def __init__(
self, encoding: typing.Optional[str], passthru_all_includes: bool = False
):
Preprocessor.__init__(self)
self.errors: typing.List[str] = []
self.assume_encoding = encoding
self.passthru_includes = re.compile(r".")
if passthru_all_includes:
self.passthru_includes = re.compile(r".")

def on_error(self, file, line, msg):
self.errors.append(f"{file}:{line} error: {msg}")
Expand Down Expand Up @@ -58,12 +61,14 @@ def make_pcpp_preprocessor(
defines: typing.List[str] = [],
include_paths: typing.List[str] = [],
retain_all_content: bool = False,
passthru_all_includes: bool = False,
encoding: typing.Optional[str] = None,
) -> PreprocessorFunction:
"""
Creates a preprocessor function that uses pcpp (which must be installed
separately) to preprocess the input text.
:param passthru_all_includes: If set to `True`, the preprocessor will not expand any #include
:param encoding: If specified any include files are opened with this encoding
.. code-block:: python
Expand All @@ -76,7 +81,7 @@ def make_pcpp_preprocessor(
"""

def _preprocess_file(filename: str, content: str) -> str:
pp = _CustomPreprocessor(encoding)
pp = _CustomPreprocessor(encoding, passthru_all_includes)
if include_paths:
for p in include_paths:
pp.add_path(p)
Expand Down
29 changes: 23 additions & 6 deletions tests/test_preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ def test_preprocessor_omit_content(tmp_path: pathlib.Path) -> None:
value=Value(tokens=[Token(value="2")]),
)
]
),
includes=[Include(filename='"t2.h"')],
)
)


Expand Down Expand Up @@ -110,8 +109,7 @@ def test_preprocessor_omit_content2(tmp_path: pathlib.Path) -> None:
value=Value(tokens=[Token(value="2")]),
)
]
),
includes=[Include(filename='"t2.h"')],
)
)


Expand Down Expand Up @@ -141,6 +139,25 @@ def test_preprocessor_encoding(tmp_path: pathlib.Path) -> None:
value=Value(tokens=[Token(value="3")]),
)
]
),
includes=[Include(filename='"t2.h"')],
)
)


def test_preprocessor_passthru_all_includes(tmp_path: pathlib.Path) -> None:
"""Ensure that all #include pass through"""
h_content = '#include "t2.h"\n'

with open(tmp_path / "t1.h", "w") as fp:
fp.write(h_content)

with open(tmp_path / "t2.h", "w") as fp:
fp.write("")

options = ParserOptions(
preprocessor=make_pcpp_preprocessor(passthru_all_includes=True)
)
data = parse_file(tmp_path / "t1.h", options=options)

assert data == ParsedData(
namespace=NamespaceScope(), includes=[Include(filename='"t2.h"')]
)

0 comments on commit 9fe4713

Please sign in to comment.