| 1 |
import os |
| 2 |
import pathlib |
| 3 |
import pytest |
| 4 |
import re |
| 5 |
import shutil |
| 6 |
import subprocess |
| 7 |
import typing |
| 8 |
|
| 9 |
from cxxheaderparser.options import ParserOptions, PreprocessorFunction |
| 10 |
from cxxheaderparser import preprocessor |
| 11 |
from cxxheaderparser.simple import ( |
| 12 |
NamespaceScope, |
| 13 |
ParsedData, |
| 14 |
parse_file, |
| 15 |
parse_string, |
| 16 |
Include, |
| 17 |
) |
| 18 |
from cxxheaderparser.types import ( |
| 19 |
FundamentalSpecifier, |
| 20 |
NameSpecifier, |
| 21 |
PQName, |
| 22 |
Token, |
| 23 |
Type, |
| 24 |
Value, |
| 25 |
Variable, |
| 26 |
) |
| 27 |
|
| 28 |
|
| 29 |
@pytest.fixture(params=["gcc", "msvc", "pcpp"]) |
| 30 |
def make_pp(request) -> typing.Callable[..., PreprocessorFunction]: |
| 31 |
param = request.param |
| 32 |
if param == "gcc": |
| 33 |
gcc_path = shutil.which("g++") |
| 34 |
if not gcc_path: |
| 35 |
pytest.skip("g++ not found") |
| 36 |
|
| 37 |
subprocess.run([gcc_path, "--version"]) |
| 38 |
return preprocessor.make_gcc_preprocessor |
| 39 |
elif param == "msvc": |
| 40 |
gcc_path = shutil.which("cl.exe") |
| 41 |
if not gcc_path: |
| 42 |
pytest.skip("cl.exe not found") |
| 43 |
|
| 44 |
return preprocessor.make_msvc_preprocessor |
| 45 |
elif param == "pcpp": |
| 46 |
if preprocessor.pcpp is None: |
| 47 |
pytest.skip("pcpp not installed") |
| 48 |
return preprocessor.make_pcpp_preprocessor |
| 49 |
else: |
| 50 |
assert False |
| 51 |
|
| 52 |
|
| 53 |
def test_basic_preprocessor( |
| 54 |
make_pp: typing.Callable[..., PreprocessorFunction] |
| 55 |
) -> None: |
| 56 |
content = """ |
| 57 |
#define X 1 |
| 58 |
int x = X; |
| 59 |
""" |
| 60 |
|
| 61 |
options = ParserOptions(preprocessor=make_pp()) |
| 62 |
data = parse_string(content, cleandoc=True, options=options) |
| 63 |
|
| 64 |
assert data == ParsedData( |
| 65 |
namespace=NamespaceScope( |
| 66 |
variables=[ |
| 67 |
Variable( |
| 68 |
name=PQName(segments=[NameSpecifier(name="x")]), |
| 69 |
type=Type( |
| 70 |
typename=PQName(segments=[FundamentalSpecifier(name="int")]) |
| 71 |
), |
| 72 |
value=Value(tokens=[Token(value="1")]), |
| 73 |
) |
| 74 |
] |
| 75 |
) |
| 76 |
) |
| 77 |
|
| 78 |
|
| 79 |
def test_preprocessor_omit_content( |
| 80 |
make_pp: typing.Callable[..., PreprocessorFunction], |
| 81 |
tmp_path: pathlib.Path, |
| 82 |
) -> None: |
| 83 |
"""Ensure that content in other headers is omitted""" |
| 84 |
h_content = '#include "t2.h"' "\n" "int x = X;\n" |
| 85 |
h2_content = "#define X 2\n" "int omitted = 1;\n" |
| 86 |
|
| 87 |
with open(tmp_path / "t1.h", "w") as fp: |
| 88 |
fp.write(h_content) |
| 89 |
|
| 90 |
with open(tmp_path / "t2.h", "w") as fp: |
| 91 |
fp.write(h2_content) |
| 92 |
|
| 93 |
options = ParserOptions(preprocessor=make_pp()) |
| 94 |
data = parse_file(tmp_path / "t1.h", options=options) |
| 95 |
|
| 96 |
assert data == ParsedData( |
| 97 |
namespace=NamespaceScope( |
| 98 |
variables=[ |
| 99 |
Variable( |
| 100 |
name=PQName(segments=[NameSpecifier(name="x")]), |
| 101 |
type=Type( |
| 102 |
typename=PQName(segments=[FundamentalSpecifier(name="int")]) |
| 103 |
), |
| 104 |
value=Value(tokens=[Token(value="2")]), |
| 105 |
) |
| 106 |
] |
| 107 |
) |
| 108 |
) |
| 109 |
|
| 110 |
|
| 111 |
def test_preprocessor_omit_content2( |
| 112 |
make_pp: typing.Callable[..., PreprocessorFunction], |
| 113 |
tmp_path: pathlib.Path, |
| 114 |
) -> None: |
| 115 |
""" |
| 116 |
Ensure that content in other headers is omitted while handling pcpp |
| 117 |
relative path quirk |
| 118 |
""" |
| 119 |
h_content = '#include "t2.h"' "\n" "int x = X;\n" |
| 120 |
h2_content = "#define X 2\n" "int omitted = 1;\n" |
| 121 |
|
| 122 |
tmp_path2 = tmp_path / "l1" |
| 123 |
tmp_path2.mkdir() |
| 124 |
|
| 125 |
with open(tmp_path2 / "t1.h", "w") as fp: |
| 126 |
fp.write(h_content) |
| 127 |
|
| 128 |
with open(tmp_path2 / "t2.h", "w") as fp: |
| 129 |
fp.write(h2_content) |
| 130 |
|
| 131 |
options = ParserOptions(preprocessor=make_pp(include_paths=[str(tmp_path)])) |
| 132 |
|
| 133 |
# Weirdness happens here |
| 134 |
os.chdir(tmp_path) |
| 135 |
data = parse_file(tmp_path2 / "t1.h", options=options) |
| 136 |
|
| 137 |
assert data == ParsedData( |
| 138 |
namespace=NamespaceScope( |
| 139 |
variables=[ |
| 140 |
Variable( |
| 141 |
name=PQName(segments=[NameSpecifier(name="x")]), |
| 142 |
type=Type( |
| 143 |
typename=PQName(segments=[FundamentalSpecifier(name="int")]) |
| 144 |
), |
| 145 |
value=Value(tokens=[Token(value="2")]), |
| 146 |
) |
| 147 |
] |
| 148 |
) |
| 149 |
) |
| 150 |
|
| 151 |
|
| 152 |
def test_preprocessor_encoding( |
| 153 |
make_pp: typing.Callable[..., PreprocessorFunction], tmp_path: pathlib.Path |
| 154 |
) -> None: |
| 155 |
"""Ensure we can handle alternate encodings""" |
| 156 |
h_content = b"// \xa9 2023 someone\n" b'#include "t2.h"' b"\n" b"int x = X;\n" |
| 157 |
|
| 158 |
h2_content = b"// \xa9 2023 someone\n" b"#define X 3\n" b"int omitted = 1;\n" |
| 159 |
|
| 160 |
with open(tmp_path / "t1.h", "wb") as fp: |
| 161 |
fp.write(h_content) |
| 162 |
|
| 163 |
with open(tmp_path / "t2.h", "wb") as fp: |
| 164 |
fp.write(h2_content) |
| 165 |
|
| 166 |
options = ParserOptions(preprocessor=make_pp(encoding="cp1252")) |
| 167 |
data = parse_file(tmp_path / "t1.h", options=options, encoding="cp1252") |
| 168 |
|
| 169 |
assert data == ParsedData( |
| 170 |
namespace=NamespaceScope( |
| 171 |
variables=[ |
| 172 |
Variable( |
| 173 |
name=PQName(segments=[NameSpecifier(name="x")]), |
| 174 |
type=Type( |
| 175 |
typename=PQName(segments=[FundamentalSpecifier(name="int")]) |
| 176 |
), |
| 177 |
value=Value(tokens=[Token(value="3")]), |
| 178 |
) |
| 179 |
] |
| 180 |
) |
| 181 |
) |
| 182 |
|
| 183 |
|
| 184 |
@pytest.mark.skipif(preprocessor.pcpp is None, reason="pcpp not installed") |
| 185 |
def test_preprocessor_passthru_includes(tmp_path: pathlib.Path) -> None: |
| 186 |
"""Ensure that all #include pass through""" |
| 187 |
h_content = '#include "t2.h"\n' |
| 188 |
|
| 189 |
with open(tmp_path / "t1.h", "w") as fp: |
| 190 |
fp.write(h_content) |
| 191 |
|
| 192 |
with open(tmp_path / "t2.h", "w") as fp: |
| 193 |
fp.write("") |
| 194 |
|
| 195 |
options = ParserOptions( |
| 196 |
preprocessor=preprocessor.make_pcpp_preprocessor( |
| 197 |
passthru_includes=re.compile(".+") |
| 198 |
) |
| 199 |
) |
| 200 |
data = parse_file(tmp_path / "t1.h", options=options) |
| 201 |
|
| 202 |
assert data == ParsedData( |
| 203 |
namespace=NamespaceScope(), includes=[Include(filename='"t2.h"')] |
| 204 |
) |