Skip to content
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
16 changes: 15 additions & 1 deletion pybind11_mkdoc/mkdoc_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,9 +552,23 @@ def extract(filename, node, prefix, output, file_cache):
return None


def _report_diagnostics(filename, tu):
"""Print clang diagnostics to stderr and fail if any of them is an error."""
errors = 0
for diagnostic in tu.diagnostics:
sys.stderr.write(diagnostic.format() + "\n")
if diagnostic.severity >= cindex.Diagnostic.Error:
errors += 1
if errors:
msg = f"Clang reported {errors} error(s) while parsing {filename}"
raise RuntimeError(msg)


def _extract_file(filename, parameters):
index = cindex.Index(cindex.conf.lib.clang_createIndex(False, True))
# Diagnostics are printed by _report_diagnostics, not by libclang itself.
index = cindex.Index(cindex.conf.lib.clang_createIndex(False, False))
tu = index.parse(filename, parameters)
_report_diagnostics(filename, tu)
output = []
extract(filename, tu.cursor, "", output, {})
return output
Expand Down
15 changes: 15 additions & 0 deletions tests/cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,18 @@ def test_parse_failure_sets_exit_code(tmp_path: Path) -> None:

assert result.returncode != 0
assert not tf.exists()


def test_missing_include_reports_diagnostics(tmp_path: Path) -> None:
header = tmp_path / "bad_header.h"
header.write_text('#include "does_not_exist.h"\n', encoding="utf-8")
tf = tmp_path / "tmp.h"
result = subprocess.run(
[sys.executable, "-m", "pybind11_mkdoc", "-o", tf, header],
check=False,
capture_output=True,
)

assert result.returncode != 0
assert not tf.exists()
assert "does_not_exist.h" in result.stderr.decode()