Skip to content

Commit f45e0ed

Browse files
authored
Merge pull request #17 from Tolc-Software/docs
Docs
2 parents e5cb75f + cf828ef commit f45e0ed

23 files changed

+385
-42
lines changed

CMakeLists.txt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.15)
22

33
project(
44
Frontend.py
5-
VERSION 0.4.1
5+
VERSION 0.5.0
66
LANGUAGES CXX)
77

88
configure_file(docs/ReleaseNotes/version.in
@@ -37,23 +37,21 @@ include(FetchContent)
3737
FetchContent_Declare(
3838
IRSpecification
3939
GIT_REPOSITORY https://github.com/Tolc-Software/IntermediateRepresentation.git
40-
GIT_TAG v0.11.0)
40+
GIT_TAG v0.12.0)
4141

4242
FetchContent_MakeAvailable(IRSpecification)
4343

4444
add_library(
4545
Frontend.py
46+
src/Frontend/Python/frontend.cpp
4647
src/Pybind/Builders/attributeBuilder.cpp
4748
src/Pybind/Builders/classBuilder.cpp
4849
src/Pybind/Builders/enumBuilder.cpp
4950
src/Pybind/Builders/functionBuilder.cpp
5051
src/Pybind/Builders/moduleBuilder.cpp
5152
src/Pybind/Builders/moduleFileBuilder.cpp
5253
src/Pybind/Builders/typeToStringBuilder.cpp
53-
src/Frontend/Python/frontend.cpp
54-
src/Pybind/checkType.cpp
55-
src/Pybind/returnValuePolicy.cpp
56-
src/Pybind/getOverloadedFunctions.cpp
54+
src/Pybind/Helpers/getDocumentationParameter.cpp
5755
src/Pybind/Helpers/split.cpp
5856
src/Pybind/Helpers/string.cpp
5957
src/Pybind/Helpers/types.cpp
@@ -62,7 +60,10 @@ add_library(
6260
src/Pybind/Proxy/enum.cpp
6361
src/Pybind/Proxy/function.cpp
6462
src/Pybind/Proxy/module.cpp
65-
src/Pybind/Proxy/moduleFile.cpp)
63+
src/Pybind/Proxy/moduleFile.cpp
64+
src/Pybind/checkType.cpp
65+
src/Pybind/getOverloadedFunctions.cpp
66+
src/Pybind/returnValuePolicy.cpp)
6667

6768
add_warnings(TARGET Frontend.py)
6869
add_options(TARGET Frontend.py)

docs/ReleaseNotes/v0.5.0.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# News #
2+
3+
## Bindings ##
4+
5+
* Add support for transferring documentation from the C++ to pybind11
6+
* Add tests for all officially supported documentation styles
7+
* Supported for:
8+
* Classes
9+
* Member variables
10+
* Enums
11+
* Functions
12+
13+
Example of documentation string styles:
14+
15+
```cpp
16+
// One line comment
17+
class OneLiner {};
18+
19+
/** Single multi line comment */
20+
class SingleMulti {};
21+
22+
/**
23+
* Multi
24+
* line
25+
* comment
26+
*/
27+
class Multi {};
28+
29+
/**
30+
Bare multi
31+
Another line
32+
*/
33+
class BareMulti {};
34+
35+
/*!
36+
* Qt style
37+
*/
38+
class QtStyle {};
39+
40+
/*******************************************************************************
41+
* JavaDoc Style
42+
* is
43+
* boxy
44+
******************************************************************************/
45+
class JavaDoc {};
46+
47+
///
48+
/// Triplets is a commitment
49+
///
50+
class Triplets {};
51+
52+
//!
53+
//! This is one of the doxy styles
54+
//!
55+
class DoxyBang {};
56+
```
57+

docs/public/examples.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ public:
3636

3737
static int const i = 5;
3838

39+
// This class has a readwrite variable
40+
int readwrite = 10;
41+
3942
std::string getS() { return m_s; }
4043
std::string_view getSView() { return m_s; }
4144

@@ -66,6 +69,15 @@ namespace MyLib {
6669
};
6770
}
6871

72+
/** Documentation carries over */
73+
struct Documentation {};
74+
75+
/***********************************************************
76+
* JavaDoc Style
77+
* is
78+
* boxy
79+
**********************************************************/
80+
struct JavaDoc {};
6981

7082
```
7183
@@ -79,6 +91,9 @@ self.assertEqual(m.WithConstructor.i, 5)
7991
with_constructor = m.WithConstructor("Hello")
8092
self.assertEqual(with_constructor.getS(), "Hello")
8193
94+
# Documentation for variables carries over aswell
95+
self.assertIn("This class has a readwrite variable", m.WithConstructor.readwrite.__doc__)
96+
8297
# Named arguments in constructors
8398
with_constructor = m.WithConstructor(s="named argument")
8499
self.assertEqual(with_constructor.getS(), "named argument")
@@ -105,6 +120,87 @@ self.assertEqual(
105120
nested = m.MyLib.Nested()
106121
self.assertEqual(nested.divideByTwo(10), 5)
107122
123+
# Different styles of documentation on classes carries over
124+
self.assertIn("Documentation carries over", m.Documentation.__doc__)
125+
self.assertIn("JavaDoc Style", m.JavaDoc.__doc__)
126+
127+
128+
```
129+
130+
131+
## Documentation Styles ##
132+
133+
134+
```cpp
135+
136+
// One line comment
137+
class OneLiner {};
138+
139+
/** Single multi line comment */
140+
class SingleMulti {};
141+
142+
/**
143+
* Multi
144+
* line
145+
* comment
146+
*/
147+
class Multi {};
148+
149+
/**
150+
Bare multi
151+
Another line
152+
*/
153+
class BareMulti {};
154+
155+
/*!
156+
* Qt style
157+
*/
158+
class QtStyle {};
159+
160+
/*****************************
161+
* JavaDoc Style
162+
* is
163+
* boxy
164+
****************************/
165+
class JavaDoc {};
166+
167+
///
168+
/// Triplets is a commitment
169+
///
170+
class Triplets {};
171+
172+
//!
173+
//! This is one of the doxy styles
174+
//!
175+
class DoxyBang {};
176+
177+
```
178+
179+
180+
```python
181+
182+
# These types of documentations are supported for:
183+
# Classes
184+
# Member variables
185+
# Enums
186+
# Functions
187+
188+
self.assertIn("One line comment", m.OneLiner.__doc__)
189+
190+
self.assertIn("Single multi line", m.SingleMulti.__doc__)
191+
192+
self.assertIn("Multi", m.Multi.__doc__)
193+
194+
self.assertIn("Bare multi", m.BareMulti.__doc__)
195+
196+
self.assertIn("Qt style", m.QtStyle.__doc__)
197+
198+
self.assertIn("JavaDoc Style", m.JavaDoc.__doc__)
199+
200+
self.assertIn("Triplets", m.Triplets.__doc__)
201+
202+
self.assertIn("one of the doxy styles", m.DoxyBang.__doc__)
203+
108204
```
109205

110206

@@ -135,6 +231,7 @@ Unscoped f(Unscoped u) {
135231
}
136232

137233
namespace NS {
234+
// Documentation describing the enum
138235
enum class Deep {
139236
Double,
140237
Down,
@@ -161,6 +258,9 @@ self.assertEqual(u, unscoped)
161258
deep = m.NS.Deep.Down
162259
self.assertNotEqual(deep, m.NS.Deep.Double)
163260
261+
# Documentation carries over from C++
262+
self.assertIn("Documentation describing the enum", m.NS.Deep.__doc__)
263+
164264
```
165265

166266

@@ -184,10 +284,14 @@ void addYourOwn(std::string content) {
184284
f.close();
185285
}
186286

287+
/**
288+
* Documentation carries over
289+
*/
187290
int calculate() {
188291
return 5;
189292
}
190293

294+
// Different documentation styles are supported
191295
int missingArgumentsNaming(int, int i) {
192296
return i;
193297
}
@@ -216,10 +320,13 @@ with open("hello.txt", "r") as f:
216320
217321
result = m.calculate()
218322
self.assertEqual(result, 5)
323+
self.assertIn("Documentation carries over", m.calculate.__doc__)
219324
220325
# Without naming variables is fine
221326
result = m.missingArgumentsNaming(2, 5)
222327
self.assertEqual(result, 5)
328+
self.assertIn("Different documentation styles are supported", \
329+
m.missingArgumentsNaming.__doc__)
223330
224331
# Not possible to name any variables unless they are all known
225332
with self.assertRaises(TypeError) as error_context:

src/Pybind/Builders/classBuilder.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ buildClass(IR::Struct const& cppClass, Pybind::Proxy::TypeInfo& typeInfo) {
3737
getTemplateParameterString(cppClass.m_templateArguments),
3838
cppClass.m_representation);
3939

40+
pyClass.setDocumentation(cppClass.m_documentation);
4041
auto overloadedFunctions =
4142
Pybind::getOverloadedFunctions(cppClass.m_public.m_functions);
4243
// Ignore private functions
@@ -79,6 +80,7 @@ buildClass(IR::Struct const& cppClass, Pybind::Proxy::TypeInfo& typeInfo) {
7980
for (auto const& variable : cppClass.m_public.m_memberVariables) {
8081
Pybind::checkType(variable.m_type, typeInfo);
8182
pyClass.addMemberVariable(variable.m_name,
83+
variable.m_documentation,
8284
variable.m_type.m_isConst,
8385
variable.m_type.m_isStatic);
8486
}

src/Pybind/Builders/enumBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Pybind::Proxy::Enum buildEnum(IR::Enum const& e) {
77
Pybind::Proxy::Enum proxyEnum(e.m_name, e.m_representation);
88

99
proxyEnum.setScoped(e.m_isScoped);
10+
proxyEnum.setDocumentation(e.m_documentation);
1011

1112
for (auto const& value : e.m_values) {
1213
proxyEnum.addValue(value);

src/Pybind/Builders/functionBuilder.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ buildFunction(IR::Function const& cppFunction,
1515
Pybind::Helpers::removeCppTemplate(cppFunction.m_name),
1616
cppFunction.m_representation);
1717

18-
std::set<std::string> includes;
19-
2018
for (auto const& arg : cppFunction.m_arguments) {
2119
if (!Pybind::Helpers::isContainerType(arg.m_type,
2220
IR::ContainerType::UniquePtr)) {
@@ -33,6 +31,7 @@ buildFunction(IR::Function const& cppFunction,
3331

3432
Pybind::checkType(cppFunction.m_returnType, typeInfo);
3533
pyFunction.setReturnType(cppFunction.m_returnType.m_representation);
34+
pyFunction.setDocumentation(cppFunction.m_documentation);
3635

3736
if (cppFunction.m_returnType.m_numPointers > 0) {
3837
pyFunction.setReturnValuePolicy(
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "Pybind/Helpers/getDocumentationParameter.hpp"
2+
#include <string>
3+
#include <fmt/format.h>
4+
5+
namespace Pybind::Helpers {
6+
std::string getDocumentationParameter(std::string const& documentation) {
7+
return documentation.empty() ?
8+
"\"\"" :
9+
fmt::format("R\"_tolc_docs({})_tolc_docs\"", documentation);
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
#include <string>
3+
4+
namespace Pybind::Helpers {
5+
/**
6+
* Return a valid parameter to pass to the creation of a pybind11 function
7+
* E.g.
8+
* m.def("f", &f, {getDocumentationParameter('My function')})
9+
*/
10+
std::string getDocumentationParameter(std::string const& documentation);
11+
}

0 commit comments

Comments
 (0)