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

Generate C++ code docs #85

Merged
merged 7 commits into from
Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
3 changes: 3 additions & 0 deletions tool/src/cpp/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,12 @@ pub fn gen_rust_to_cpp<W: Write>(
}
}

/// Meta information about a [ast::TypeName::Reference].
#[derive(Eq, PartialEq)]
pub struct ReferenceMeta {
/// Whether or not the reference is owned.
owned: bool,
/// Whether or not the reference is mutable.
mutable: bool,
}

Expand Down
24 changes: 23 additions & 1 deletion tool/src/cpp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ mod types;
mod structs;
use structs::*;

use crate::cpp::util::gen_comment_block;

mod conversions;

pub mod docs;
Expand Down Expand Up @@ -66,9 +68,11 @@ pub fn gen_bindings(

ast::CustomType::Enum(enm) => {
writeln!(out)?;
gen_comment_block(out, &enm.doc_lines)?;
writeln!(out, "enum struct {} {{", enm.name)?;
let mut enm_indent = indented(out).with_str(" ");
for (name, discriminant, _) in enm.variants.iter() {
for (name, discriminant, doc_lines) in enm.variants.iter() {
gen_comment_block(&mut enm_indent, doc_lines)?;
writeln!(&mut enm_indent, "{} = {},", name, discriminant)?;
}
writeln!(out, "}};")?;
Expand Down Expand Up @@ -358,4 +362,22 @@ mod tests {
}
}
}

#[test]
fn test_enum_documentation() {
test_file! {
#[diplomat::bridge]
mod ffi {
/// Documentation for MyEnum.
enum MyEnum {
/// All about A.
A,
/// All about B.
B,
/// All about C.
C
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ namespace capi {

struct MyStruct;

/**
* A destruction policy for using Foo with std::unique_ptr.
*/
struct FooDeleter {
void operator()(capi::Foo* l) const noexcept {
capi::Foo_destroy(l);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ namespace capi {

#include "MyEnum.hpp"

/**
* A destruction policy for using MyStruct with std::unique_ptr.
*/
struct MyStructDeleter {
void operator()(capi::MyStruct* l) const noexcept {
capi::MyStruct_destroy(l);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ namespace capi {

class MyStruct;

/**
* A destruction policy for using MyStruct with std::unique_ptr.
*/
struct MyStructDeleter {
void operator()(capi::MyStruct* l) const noexcept {
capi::MyStruct_destroy(l);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ namespace capi {
}


/**
* A destruction policy for using MyStruct with std::unique_ptr.
*/
struct MyStructDeleter {
void operator()(capi::MyStruct* l) const noexcept {
capi::MyStruct_destroy(l);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ namespace capi {

struct MyStruct;

/**
* A destruction policy for using MyStruct with std::unique_ptr.
*/
struct MyStructDeleter {
void operator()(capi::MyStruct* l) const noexcept {
capi::MyStruct_destroy(l);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ namespace capi {

class MyStruct;

/**
* A destruction policy for using MyStruct with std::unique_ptr.
*/
struct MyStructDeleter {
void operator()(capi::MyStruct* l) const noexcept {
capi::MyStruct_destroy(l);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
source: tool/src/cpp/structs.rs
expression: out_texts.get(out).unwrap()

---
#ifndef Foo_HPP
#define Foo_HPP
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <algorithm>
#include <memory>
#include <optional>
#include <variant>
#include "diplomat_runtime.hpp"

namespace capi {
#include "Foo.h"
}


/**
* A destruction policy for using Foo with std::unique_ptr.
*/
struct FooDeleter {
void operator()(capi::Foo* l) const noexcept {
capi::Foo_destroy(l);
}
};

/**
* Documentation for Foo.
* Second line.
*/
struct Foo {
public:

/**
* Documentation for x.
*/
uint8_t x;

/**
* Documentation for get_x.
*/
uint8_t get_x() const;
};


inline uint8_t Foo::get_x() const {
return capi::Foo_get_x((capi::Foo*) &this);
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ namespace capi {

struct Foo;

/**
* A destruction policy for using Bar with std::unique_ptr.
*/
struct BarDeleter {
void operator()(capi::Bar* l) const noexcept {
capi::Bar_destroy(l);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ namespace capi {

#include "Bar.hpp"

/**
* A destruction policy for using Foo with std::unique_ptr.
*/
struct FooDeleter {
void operator()(capi::Foo* l) const noexcept {
capi::Foo_destroy(l);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ namespace capi {

class Foo;

/**
* A destruction policy for using Bar with std::unique_ptr.
*/
struct BarDeleter {
void operator()(capi::Bar* l) const noexcept {
capi::Bar_destroy(l);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ namespace capi {

struct Bar;

/**
* A destruction policy for using Foo with std::unique_ptr.
*/
struct FooDeleter {
void operator()(capi::Foo* l) const noexcept {
capi::Foo_destroy(l);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
source: tool/src/cpp/mod.rs
expression: out_texts.get(out).unwrap()

---
#ifndef MyEnum_HPP
#define MyEnum_HPP
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <algorithm>
#include <memory>
#include <optional>
#include <variant>
#include "diplomat_runtime.hpp"

namespace capi {
#include "MyEnum.h"
}



/**
* Documentation for MyEnum.
*/
enum struct MyEnum {

/**
* All about A.
*/
A = 0,

/**
* All about B.
*/
B = 1,

/**
* All about C.
*/
C = 2,
};

#endif

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ namespace capi {
}


/**
* A destruction policy for using MyOpaqueStruct with std::unique_ptr.
*/
struct MyOpaqueStructDeleter {
void operator()(capi::MyOpaqueStruct* l) const noexcept {
capi::MyOpaqueStruct_destroy(l);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ namespace capi {

class MyOpaqueStruct;

/**
* A destruction policy for using MyStruct with std::unique_ptr.
*/
struct MyStructDeleter {
void operator()(capi::MyStruct* l) const noexcept {
capi::MyStruct_destroy(l);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ namespace capi {
}


/**
* A destruction policy for using MyOpaqueStruct with std::unique_ptr.
*/
struct MyOpaqueStructDeleter {
void operator()(capi::MyOpaqueStruct* l) const noexcept {
capi::MyOpaqueStruct_destroy(l);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ namespace capi {
class MyOpaqueStruct;
struct MyStruct;

/**
* A destruction policy for using MyStruct with std::unique_ptr.
*/
struct MyStructDeleter {
void operator()(capi::MyStruct* l) const noexcept {
capi::MyStruct_destroy(l);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ namespace capi {
}


/**
* A destruction policy for using MyOpaqueStruct with std::unique_ptr.
*/
struct MyOpaqueStructDeleter {
void operator()(capi::MyOpaqueStruct* l) const noexcept {
capi::MyOpaqueStruct_destroy(l);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ namespace capi {
class MyOpaqueStruct;
struct MyStruct;

/**
* A destruction policy for using MyStruct with std::unique_ptr.
*/
struct MyStructDeleter {
void operator()(capi::MyStruct* l) const noexcept {
capi::MyStruct_destroy(l);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ namespace capi {

struct MyStruct;

/**
* A destruction policy for using MyStruct with std::unique_ptr.
*/
struct MyStructDeleter {
void operator()(capi::MyStruct* l) const noexcept {
capi::MyStruct_destroy(l);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ namespace capi {
}


/**
* A destruction policy for using MyStruct with std::unique_ptr.
*/
struct MyStructDeleter {
void operator()(capi::MyStruct* l) const noexcept {
capi::MyStruct_destroy(l);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ namespace capi {
}


/**
* A destruction policy for using MyStruct with std::unique_ptr.
*/
struct MyStructDeleter {
void operator()(capi::MyStruct* l) const noexcept {
capi::MyStruct_destroy(l);
Expand Down
Loading