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

Make generated file use valid C++ identifiers #31

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
python-version: ${{ matrix.python-version }}

- name: Install package
run: python -m pip install .[test]
run: python -m pip install .[test] "clang==14.0.6"

- name: Test package
run: python -m pytest --forked
Expand Down
27 changes: 13 additions & 14 deletions pybind11_mkdoc/mkdoc_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def sanitize_name(name):
name = re.sub('<.*>', '', name)
name = ''.join([ch if ch.isalnum() else '_' for ch in name])
name = re.sub('_$', '', re.sub('_+', '_', name))
return '__doc_' + name
return 'mkd_doc_' + name


def process_comment(comment):
Expand Down Expand Up @@ -389,19 +389,18 @@ def write_header(comments, out_file=sys.stdout):
Do not edit! They were automatically extracted by pybind11_mkdoc.
*/

#define __EXPAND(x) x
#define __COUNT(_1, _2, _3, _4, _5, _6, _7, COUNT, ...) COUNT
#define __VA_SIZE(...) __EXPAND(__COUNT(__VA_ARGS__, 7, 6, 5, 4, 3, 2, 1, 0))
#define __CAT1(a, b) a ## b
#define __CAT2(a, b) __CAT1(a, b)
#define __DOC1(n1) __doc_##n1
#define __DOC2(n1, n2) __doc_##n1##_##n2
#define __DOC3(n1, n2, n3) __doc_##n1##_##n2##_##n3
#define __DOC4(n1, n2, n3, n4) __doc_##n1##_##n2##_##n3##_##n4
#define __DOC5(n1, n2, n3, n4, n5) __doc_##n1##_##n2##_##n3##_##n4##_##n5
#define __DOC6(n1, n2, n3, n4, n5, n6) __doc_##n1##_##n2##_##n3##_##n4##_##n5##_##n6
#define __DOC7(n1, n2, n3, n4, n5, n6, n7) __doc_##n1##_##n2##_##n3##_##n4##_##n5##_##n6##_##n7
#define DOC(...) __EXPAND(__EXPAND(__CAT2(__DOC, __VA_SIZE(__VA_ARGS__)))(__VA_ARGS__))
#define MKD_EXPAND(x) x
#define MKD_COUNT(_1, _2, _3, _4, _5, _6, _7, COUNT, ...) COUNT
#define MKD_VA_SIZE(...) MKD_EXPAND(MKD_COUNT(__VA_ARGS__, 7, 6, 5, 4, 3, 2, 1, 0))
#define MKD_CAT1(a, b) a ## b
#define MKD_CAT2(a, b) MKD_CAT1(a, b)
#define MKD_DOC1(n1) mkd_doc_##n1
#define MKD_DOC2(n1, n2) mkd_doc_##n1##_##n2
#define MKD_DOC3(n1, n2, n3) mkd_doc_##n1##_##n2##_##n3
#define MKD_DOC4(n1, n2, n3, n4) mkd_doc_##n1##_##n2##_##n3##_##n4
#define MKD_DOC5(n1, n2, n3, n4, n5) mkd_doc_##n1##_##n2##_##n3##_##n4##_##n5
#define MKD_DOC7(n1, n2, n3, n4, n5, n6, n7) mkd_doc_##n1##_##n2##_##n3##_##n4##_##n5##_##n6##_##n7
#define DOC(...) MKD_EXPAND(MKD_EXPAND(MKD_CAT2(MKD_DOC, MKD_VA_SIZE(__VA_ARGS__)))(__VA_ARGS__))

#if defined(__GNUG__)
#pragma GCC diagnostic push
Expand Down
49 changes: 24 additions & 25 deletions tests/sample_header_test.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
import collections
import os
import time
import sysconfig
import sys

import pytest

import pybind11_mkdoc

DIR = os.path.abspath(os.path.dirname(__file__))


def test_generate_headers(capsys):
comments = pybind11_mkdoc.mkdoc_lib.extract_all([os.path.join(DIR, "sample_header_docs", "sample_header.h")])
pybind11_mkdoc.mkdoc_lib.write_header(comments, sys.stdout)
def test_generate_headers(capsys, tmp_path):
comments = pybind11_mkdoc.mkdoc_lib.extract_all(
[os.path.join(DIR, "sample_header_docs", "sample_header.h")]
)
assert ['mkd_doc_RootLevelSymbol', 'mkd_doc_drake_MidLevelSymbol'] == [c[0] for c in comments]

output = tmp_path / "docs.h"
with output.open("w") as fd:
pybind11_mkdoc.mkdoc_lib.write_header(comments, fd)

res = capsys.readouterr()

assert "warning" not in res.err
assert "error" not in res.err
assert res.out == """\
assert output.read_text() == """\
/*
This file contains docstrings for use in the Python bindings.
Do not edit! They were automatically extracted by pybind11_mkdoc.
*/

#define __EXPAND(x) x
#define __COUNT(_1, _2, _3, _4, _5, _6, _7, COUNT, ...) COUNT
#define __VA_SIZE(...) __EXPAND(__COUNT(__VA_ARGS__, 7, 6, 5, 4, 3, 2, 1, 0))
#define __CAT1(a, b) a ## b
#define __CAT2(a, b) __CAT1(a, b)
#define __DOC1(n1) __doc_##n1
#define __DOC2(n1, n2) __doc_##n1##_##n2
#define __DOC3(n1, n2, n3) __doc_##n1##_##n2##_##n3
#define __DOC4(n1, n2, n3, n4) __doc_##n1##_##n2##_##n3##_##n4
#define __DOC5(n1, n2, n3, n4, n5) __doc_##n1##_##n2##_##n3##_##n4##_##n5
#define __DOC6(n1, n2, n3, n4, n5, n6) __doc_##n1##_##n2##_##n3##_##n4##_##n5##_##n6
#define __DOC7(n1, n2, n3, n4, n5, n6, n7) __doc_##n1##_##n2##_##n3##_##n4##_##n5##_##n6##_##n7
#define DOC(...) __EXPAND(__EXPAND(__CAT2(__DOC, __VA_SIZE(__VA_ARGS__)))(__VA_ARGS__))
#define MKD_EXPAND(x) x
#define MKD_COUNT(_1, _2, _3, _4, _5, _6, _7, COUNT, ...) COUNT
#define MKD_VA_SIZE(...) MKD_EXPAND(MKD_COUNT(__VA_ARGS__, 7, 6, 5, 4, 3, 2, 1, 0))
#define MKD_CAT1(a, b) a ## b
#define MKD_CAT2(a, b) MKD_CAT1(a, b)
#define MKD_DOC1(n1) mkd_doc_##n1
#define MKD_DOC2(n1, n2) mkd_doc_##n1##_##n2
#define MKD_DOC3(n1, n2, n3) mkd_doc_##n1##_##n2##_##n3
#define MKD_DOC4(n1, n2, n3, n4) mkd_doc_##n1##_##n2##_##n3##_##n4
#define MKD_DOC5(n1, n2, n3, n4, n5) mkd_doc_##n1##_##n2##_##n3##_##n4##_##n5
#define MKD_DOC7(n1, n2, n3, n4, n5, n6, n7) mkd_doc_##n1##_##n2##_##n3##_##n4##_##n5##_##n6##_##n7
#define DOC(...) MKD_EXPAND(MKD_EXPAND(MKD_CAT2(MKD_DOC, MKD_VA_SIZE(__VA_ARGS__)))(__VA_ARGS__))

#if defined(__GNUG__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
#endif


static const char *__doc_RootLevelSymbol =
static const char *mkd_doc_RootLevelSymbol =
R"doc(Root-level symbol. Magna fermentum iaculis eu non diam phasellus
vestibulum.)doc";

static const char *__doc_drake_MidLevelSymbol =
static const char *mkd_doc_drake_MidLevelSymbol =
R"doc(1. Begin first ordered list element. Rutrum quisque non tellus orci ac
auctor. End first ordered list element. 2. Begin second ordered list
element. Ipsum faucibus vitae aliquet nec. Ligula ullamcorper
Expand All @@ -66,4 +66,3 @@ def test_generate_headers(capsys):
#endif

"""