Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
gen740 committed Mar 4, 2023
1 parent 43f012f commit 87f5c1e
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 26 deletions.
12 changes: 10 additions & 2 deletions cppygen/component.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Dict, List, Tuple
from typing import Dict, List, Tuple, cast


class Function(object):
Expand Down Expand Up @@ -77,6 +77,10 @@ def to_decl_string(self):
f'{{ {self._return_type} {self._name}({", ".join(args)}); }}'
)

def signature(self) -> str:
args = [f"{i[1]} {i[0]}" for i in self._arguments]
return f'{"::".join(self._namespace)}::{self._name}({", ".join(args)}) -> {self._return_type}'

def __eq__(self, obj):
if isinstance(obj, Function):
return self._full_name == obj._full_name
Expand Down Expand Up @@ -175,6 +179,9 @@ def to_pybind_string(self):
+ ";"
)

def signature(self) -> str:
return f"{self._full_name}"


class Submodule:
"""
Expand Down Expand Up @@ -218,6 +225,7 @@ def to_pybind_string(self):

def __eq__(self, obj):
if isinstance(obj, Submodule):
return self._name == obj._name
return self.cpp_name == obj.cpp_name
else:
return False

12 changes: 6 additions & 6 deletions cppygen/cppygen_parser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import copy
import os
import re
import sys
from typing import List

from clang.cindex import AccessSpecifier, Config, Cursor, CursorKind, TranslationUnit
Expand Down Expand Up @@ -83,6 +84,7 @@ def _extract_functions(self, cu: Cursor, namespace: List[str], module_name: str)
j: Cursor
if j.kind == CursorKind.PARM_DECL: # type: ignore
func.add_argument_type((j.spelling, j.type.spelling))
print("\t| Function | " + func.signature())
self._funcitons.append(func)

def _extract_struct_and_class(
Expand Down Expand Up @@ -121,6 +123,7 @@ def _extract_struct_and_class(
j.brief_comment or "",
j.access_specifier == AccessSpecifier.PRIVATE, # type: ignore
)
print("\t| Class | " + struct_or_class.signature())
self._structs_and_classes.append(struct_or_class)

def add_hpp_includes(self, hpp: str):
Expand All @@ -143,10 +146,6 @@ def parse(
logger.error(
f"{diag.location.file}:{diag.location.line}:{diag.location.column}: error: {diag.spelling} [{diag.option}]"
)
if diag.severity == diag.Warning:
logger.warn(
f"{diag.location.file}:{diag.location.line}:{diag.location.column}: warining: {diag.spelling} [{diag.option}]"
)
if has_error:
exit(1)
root: Cursor = tu.cursor
Expand All @@ -161,13 +160,14 @@ def visit(x: Cursor, namespace: List[str], module_name: str):
self._extract_struct_and_class(x, namespace, module_name)
for i in list(x.get_children()):
i: Cursor
namespace_in = copy.copy(namespace)
namespace_in = copy.deepcopy(namespace)
if i.kind == CursorKind.NAMESPACE: # type: ignore
submod = Submodule()
submod.set_name(i.spelling)
submod.set_description(i.brief_comment or "")
submod.set_parent(copy.copy(namespace_in))
submod.set_parent(copy.deepcopy(namespace_in))
if not submod in self._submodules:
print(f"\t| Submodule | {submod.cpp_name}")
self._submodules.append(submod)
namespace_in.append(i.spelling)
visit(i, namespace_in, submod.cpp_name)
Expand Down
3 changes: 2 additions & 1 deletion cppygen/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def create_default_formatter() -> colorlog.ColoredFormatter:
return colorlog.ColoredFormatter(
"%(log_color)s%(message)s",
no_color=False if _color_supported() else True,
stream=sys.stdout
)


Expand All @@ -29,7 +30,7 @@ def _color_supported() -> bool:


def get_logger(name: str) -> Logger:
handler = colorlog.StreamHandler()
handler = colorlog.StreamHandler(sys.stdout)
handler.setFormatter(create_default_formatter())
logger = colorlog.getLogger(name)
logger.addHandler(handler)
Expand Down
1 change: 1 addition & 0 deletions example/.clang_format
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
style: Google
22 changes: 8 additions & 14 deletions example/shell/piyo.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
#include "hoge.hpp"
#include "piyo.hpp"
#include "hoge.hpp"
#include <vector>

namespace Shell
{
Piyoyo make_piyoyo()
{
Piyoyo tmp;
tmp.setValue(-5);
return tmp;
namespace Shell::piyo {
Piyoyo make_piyoyo() {
Piyoyo tmp;
tmp.setValue(-5);
return tmp;
}
std::vector<double> fugafuga()
{
return {3, 4, 5, 6};
}

std::vector<double> fugafuga() { return {3, 4, 5, 6}; }

} // namespace Shell
} // namespace Shell::piyo
4 changes: 1 addition & 3 deletions example/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@

void main_impl([[maybe_unused]] int argc, [[maybe_unused]] char *argv[]) {}

PYBIND11_MODULE(pyshell, m) {
CPPyGen::CPPyGenExport(m);
}
PYBIND11_MODULE(pyshell, m) { CPPyGen::CPPyGenExport(m); }

0 comments on commit 87f5c1e

Please sign in to comment.