Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to retain #include directives in preprocessor #75

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions cxxheaderparser/preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ class PreprocessorError(Exception):


class _CustomPreprocessor(Preprocessor):
def __init__(self, encoding: typing.Optional[str]):
def __init__(
self,
encoding: typing.Optional[str],
passthru_includes: typing.Optional["re.Pattern"],
):
Preprocessor.__init__(self)
self.errors: typing.List[str] = []
self.assume_encoding = encoding
self.passthru_includes = passthru_includes

def on_error(self, file, line, msg):
self.errors.append(f"{file}:{line} error: {msg}")
Expand Down Expand Up @@ -58,12 +63,15 @@ def make_pcpp_preprocessor(
include_paths: typing.List[str] = [],
retain_all_content: bool = False,
encoding: typing.Optional[str] = None,
passthru_includes: typing.Optional["re.Pattern"] = None,
) -> PreprocessorFunction:
"""
Creates a preprocessor function that uses pcpp (which must be installed
separately) to preprocess the input text.

:param encoding: If specified any include files are opened with this encoding
:param passthru_includes: If specified any #include directives that match the
compiled regex pattern will be part of the output.

.. code-block:: python

Expand All @@ -75,7 +83,7 @@ def make_pcpp_preprocessor(
"""

def _preprocess_file(filename: str, content: str) -> str:
pp = _CustomPreprocessor(encoding)
pp = _CustomPreprocessor(encoding, passthru_includes)
if include_paths:
for p in include_paths:
pp.add_path(p)
Expand Down
29 changes: 28 additions & 1 deletion tests/test_preprocessor.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import os
import pathlib
import re

from cxxheaderparser.options import ParserOptions
from cxxheaderparser.preprocessor import make_pcpp_preprocessor
from cxxheaderparser.simple import NamespaceScope, ParsedData, parse_file, parse_string
from cxxheaderparser.simple import (
NamespaceScope,
ParsedData,
parse_file,
parse_string,
Include,
)
from cxxheaderparser.types import (
FundamentalSpecifier,
NameSpecifier,
Expand Down Expand Up @@ -135,3 +142,23 @@ def test_preprocessor_encoding(tmp_path: pathlib.Path) -> None:
]
)
)


def test_preprocessor_passthru_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_includes=re.compile(".+"))
)
data = parse_file(tmp_path / "t1.h", options=options)

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