Skip to content

Commit 20a0959

Browse files
mgeplfhenryiii
andauthored
Make generated file use valid C++ identifiers (#31)
* Make generated file use valid C++ identifiers * according to https://en.cppreference.com/w/cpp/language/identifiers * identifiers with a double underscore anywhere; * identifiers that begin with an underscore followed by an uppercase letter; * in the global namespace, identifiers that begin with an underscore. * clang w/ `-Weverything` is including `-Wreserved-id-macro`, which fails with the current macro naming scheme. * cleaned up tests a bit * check `comments` early * Pin clang==14.0.6 * Apply suggestion from @henryiii --------- Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
1 parent a390d93 commit 20a0959

File tree

2 files changed

+37
-39
lines changed

2 files changed

+37
-39
lines changed

pybind11_mkdoc/mkdoc_lib.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def sanitize_name(name):
8282
name = re.sub('<.*>', '', name)
8383
name = ''.join([ch if ch.isalnum() else '_' for ch in name])
8484
name = re.sub('_$', '', re.sub('_+', '_', name))
85-
return '__doc_' + name
85+
return 'mkd_doc_' + name
8686

8787

8888
def process_comment(comment):
@@ -392,19 +392,18 @@ def write_header(comments, out_file=sys.stdout):
392392
Do not edit! They were automatically extracted by pybind11_mkdoc.
393393
*/
394394
395-
#define __EXPAND(x) x
396-
#define __COUNT(_1, _2, _3, _4, _5, _6, _7, COUNT, ...) COUNT
397-
#define __VA_SIZE(...) __EXPAND(__COUNT(__VA_ARGS__, 7, 6, 5, 4, 3, 2, 1, 0))
398-
#define __CAT1(a, b) a ## b
399-
#define __CAT2(a, b) __CAT1(a, b)
400-
#define __DOC1(n1) __doc_##n1
401-
#define __DOC2(n1, n2) __doc_##n1##_##n2
402-
#define __DOC3(n1, n2, n3) __doc_##n1##_##n2##_##n3
403-
#define __DOC4(n1, n2, n3, n4) __doc_##n1##_##n2##_##n3##_##n4
404-
#define __DOC5(n1, n2, n3, n4, n5) __doc_##n1##_##n2##_##n3##_##n4##_##n5
405-
#define __DOC6(n1, n2, n3, n4, n5, n6) __doc_##n1##_##n2##_##n3##_##n4##_##n5##_##n6
406-
#define __DOC7(n1, n2, n3, n4, n5, n6, n7) __doc_##n1##_##n2##_##n3##_##n4##_##n5##_##n6##_##n7
407-
#define DOC(...) __EXPAND(__EXPAND(__CAT2(__DOC, __VA_SIZE(__VA_ARGS__)))(__VA_ARGS__))
395+
#define MKD_EXPAND(x) x
396+
#define MKD_COUNT(_1, _2, _3, _4, _5, _6, _7, COUNT, ...) COUNT
397+
#define MKD_VA_SIZE(...) MKD_EXPAND(MKD_COUNT(__VA_ARGS__, 7, 6, 5, 4, 3, 2, 1, 0))
398+
#define MKD_CAT1(a, b) a ## b
399+
#define MKD_CAT2(a, b) MKD_CAT1(a, b)
400+
#define MKD_DOC1(n1) mkd_doc_##n1
401+
#define MKD_DOC2(n1, n2) mkd_doc_##n1##_##n2
402+
#define MKD_DOC3(n1, n2, n3) mkd_doc_##n1##_##n2##_##n3
403+
#define MKD_DOC4(n1, n2, n3, n4) mkd_doc_##n1##_##n2##_##n3##_##n4
404+
#define MKD_DOC5(n1, n2, n3, n4, n5) mkd_doc_##n1##_##n2##_##n3##_##n4##_##n5
405+
#define MKD_DOC7(n1, n2, n3, n4, n5, n6, n7) mkd_doc_##n1##_##n2##_##n3##_##n4##_##n5##_##n6##_##n7
406+
#define DOC(...) MKD_EXPAND(MKD_EXPAND(MKD_CAT2(MKD_DOC, MKD_VA_SIZE(__VA_ARGS__)))(__VA_ARGS__))
408407
409408
#if defined(__GNUG__)
410409
#pragma GCC diagnostic push

tests/sample_header_test.py

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@
1-
import collections
21
import os
3-
import time
4-
import sysconfig
52
import sys
63

7-
import pytest
8-
94
import pybind11_mkdoc
105

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

138

14-
def test_generate_headers(capsys):
15-
comments = pybind11_mkdoc.mkdoc_lib.extract_all([os.path.join(DIR, "sample_header_docs", "sample_header.h")])
16-
pybind11_mkdoc.mkdoc_lib.write_header(comments, sys.stdout)
9+
def test_generate_headers(capsys, tmp_path):
10+
comments = pybind11_mkdoc.mkdoc_lib.extract_all(
11+
[os.path.join(DIR, "sample_header_docs", "sample_header.h")]
12+
)
13+
assert ['mkd_doc_RootLevelSymbol', 'mkd_doc_drake_MidLevelSymbol'] == [c[0] for c in comments]
14+
15+
output = tmp_path / "docs.h"
16+
with output.open("w") as fd:
17+
pybind11_mkdoc.mkdoc_lib.write_header(comments, fd)
1718

1819
res = capsys.readouterr()
1920

2021
assert "warning" not in res.err
2122
assert "error" not in res.err
22-
assert res.out == """\
23+
assert output.read_text() == """\
2324
/*
2425
This file contains docstrings for use in the Python bindings.
2526
Do not edit! They were automatically extracted by pybind11_mkdoc.
2627
*/
2728
28-
#define __EXPAND(x) x
29-
#define __COUNT(_1, _2, _3, _4, _5, _6, _7, COUNT, ...) COUNT
30-
#define __VA_SIZE(...) __EXPAND(__COUNT(__VA_ARGS__, 7, 6, 5, 4, 3, 2, 1, 0))
31-
#define __CAT1(a, b) a ## b
32-
#define __CAT2(a, b) __CAT1(a, b)
33-
#define __DOC1(n1) __doc_##n1
34-
#define __DOC2(n1, n2) __doc_##n1##_##n2
35-
#define __DOC3(n1, n2, n3) __doc_##n1##_##n2##_##n3
36-
#define __DOC4(n1, n2, n3, n4) __doc_##n1##_##n2##_##n3##_##n4
37-
#define __DOC5(n1, n2, n3, n4, n5) __doc_##n1##_##n2##_##n3##_##n4##_##n5
38-
#define __DOC6(n1, n2, n3, n4, n5, n6) __doc_##n1##_##n2##_##n3##_##n4##_##n5##_##n6
39-
#define __DOC7(n1, n2, n3, n4, n5, n6, n7) __doc_##n1##_##n2##_##n3##_##n4##_##n5##_##n6##_##n7
40-
#define DOC(...) __EXPAND(__EXPAND(__CAT2(__DOC, __VA_SIZE(__VA_ARGS__)))(__VA_ARGS__))
29+
#define MKD_EXPAND(x) x
30+
#define MKD_COUNT(_1, _2, _3, _4, _5, _6, _7, COUNT, ...) COUNT
31+
#define MKD_VA_SIZE(...) MKD_EXPAND(MKD_COUNT(__VA_ARGS__, 7, 6, 5, 4, 3, 2, 1, 0))
32+
#define MKD_CAT1(a, b) a ## b
33+
#define MKD_CAT2(a, b) MKD_CAT1(a, b)
34+
#define MKD_DOC1(n1) mkd_doc_##n1
35+
#define MKD_DOC2(n1, n2) mkd_doc_##n1##_##n2
36+
#define MKD_DOC3(n1, n2, n3) mkd_doc_##n1##_##n2##_##n3
37+
#define MKD_DOC4(n1, n2, n3, n4) mkd_doc_##n1##_##n2##_##n3##_##n4
38+
#define MKD_DOC5(n1, n2, n3, n4, n5) mkd_doc_##n1##_##n2##_##n3##_##n4##_##n5
39+
#define MKD_DOC7(n1, n2, n3, n4, n5, n6, n7) mkd_doc_##n1##_##n2##_##n3##_##n4##_##n5##_##n6##_##n7
40+
#define DOC(...) MKD_EXPAND(MKD_EXPAND(MKD_CAT2(MKD_DOC, MKD_VA_SIZE(__VA_ARGS__)))(__VA_ARGS__))
4141
4242
#if defined(__GNUG__)
4343
#pragma GCC diagnostic push
4444
#pragma GCC diagnostic ignored "-Wunused-variable"
4545
#endif
4646
4747
48-
static const char *__doc_RootLevelSymbol =
48+
static const char *mkd_doc_RootLevelSymbol =
4949
R"doc(Root-level symbol. Magna fermentum iaculis eu non diam phasellus
5050
vestibulum.)doc";
5151
52-
static const char *__doc_drake_MidLevelSymbol =
52+
static const char *mkd_doc_drake_MidLevelSymbol =
5353
R"doc(1. Begin first ordered list element. Rutrum quisque non tellus orci ac
5454
auctor. End first ordered list element. 2. Begin second ordered list
5555
element. Ipsum faucibus vitae aliquet nec. Ligula ullamcorper
@@ -66,4 +66,3 @@ def test_generate_headers(capsys):
6666
#endif
6767
6868
"""
69-

0 commit comments

Comments
 (0)