Handle pcpp relative path quirk
This commit is contained in:
parent
e6908386ea
commit
ab99f2fa72
@ -3,6 +3,7 @@ Contains optional preprocessor support via pcpp
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import io
|
import io
|
||||||
|
import re
|
||||||
import os
|
import os
|
||||||
from os.path import relpath
|
from os.path import relpath
|
||||||
import typing
|
import typing
|
||||||
@ -36,7 +37,6 @@ def _filter_self(fname: str, fp: typing.TextIO) -> str:
|
|||||||
# isn't what a typical user of cxxheaderparser would want, so we strip out
|
# isn't what a typical user of cxxheaderparser would want, so we strip out
|
||||||
# the line directives and any content that isn't in our original file
|
# the line directives and any content that isn't in our original file
|
||||||
|
|
||||||
# pcpp always emits line directives that match whatever is passed in to it
|
|
||||||
line_ending = f'{fname}"\n'
|
line_ending = f'{fname}"\n'
|
||||||
|
|
||||||
new_output = io.StringIO()
|
new_output = io.StringIO()
|
||||||
@ -100,6 +100,18 @@ def make_pcpp_preprocessor(
|
|||||||
if retain_all_content:
|
if retain_all_content:
|
||||||
return fp.read()
|
return fp.read()
|
||||||
else:
|
else:
|
||||||
|
# pcpp emits the #line directive using the filename you pass in
|
||||||
|
# but will rewrite it if it's on the include path it uses. This
|
||||||
|
# is copied from pcpp:
|
||||||
|
abssource = os.path.abspath(filename)
|
||||||
|
for rewrite in pp.rewrite_paths:
|
||||||
|
temp = re.sub(rewrite[0], rewrite[1], abssource)
|
||||||
|
if temp != abssource:
|
||||||
|
filename = temp
|
||||||
|
if os.sep != "/":
|
||||||
|
filename = filename.replace(os.sep, "/")
|
||||||
|
break
|
||||||
|
|
||||||
return _filter_self(filename, fp)
|
return _filter_self(filename, fp)
|
||||||
|
|
||||||
return _preprocess_file
|
return _preprocess_file
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import os
|
||||||
import pathlib
|
import pathlib
|
||||||
|
|
||||||
from cxxheaderparser.options import ParserOptions
|
from cxxheaderparser.options import ParserOptions
|
||||||
@ -66,6 +67,46 @@ def test_preprocessor_omit_content(tmp_path: pathlib.Path) -> None:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_preprocessor_omit_content2(tmp_path: pathlib.Path) -> None:
|
||||||
|
"""
|
||||||
|
Ensure that content in other headers is omitted while handling pcpp
|
||||||
|
relative path quirk
|
||||||
|
"""
|
||||||
|
h_content = '#include "t2.h"' "\n" "int x = X;\n"
|
||||||
|
h2_content = "#define X 2\n" "int omitted = 1;\n"
|
||||||
|
|
||||||
|
tmp_path2 = tmp_path / "l1"
|
||||||
|
tmp_path2.mkdir()
|
||||||
|
|
||||||
|
with open(tmp_path2 / "t1.h", "w") as fp:
|
||||||
|
fp.write(h_content)
|
||||||
|
|
||||||
|
with open(tmp_path2 / "t2.h", "w") as fp:
|
||||||
|
fp.write(h2_content)
|
||||||
|
|
||||||
|
options = ParserOptions(
|
||||||
|
preprocessor=make_pcpp_preprocessor(include_paths=[str(tmp_path)])
|
||||||
|
)
|
||||||
|
|
||||||
|
# Weirdness happens here
|
||||||
|
os.chdir(tmp_path)
|
||||||
|
data = parse_file(tmp_path2 / "t1.h", options=options)
|
||||||
|
|
||||||
|
assert data == ParsedData(
|
||||||
|
namespace=NamespaceScope(
|
||||||
|
variables=[
|
||||||
|
Variable(
|
||||||
|
name=PQName(segments=[NameSpecifier(name="x")]),
|
||||||
|
type=Type(
|
||||||
|
typename=PQName(segments=[FundamentalSpecifier(name="int")])
|
||||||
|
),
|
||||||
|
value=Value(tokens=[Token(value="2")]),
|
||||||
|
)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_preprocessor_encoding(tmp_path: pathlib.Path) -> None:
|
def test_preprocessor_encoding(tmp_path: pathlib.Path) -> None:
|
||||||
"""Ensure we can handle alternate encodings"""
|
"""Ensure we can handle alternate encodings"""
|
||||||
h_content = b"// \xa9 2023 someone\n" b'#include "t2.h"' b"\n" b"int x = X;\n"
|
h_content = b"// \xa9 2023 someone\n" b'#include "t2.h"' b"\n" b"int x = X;\n"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user