Skip to content
Closed
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
10 changes: 5 additions & 5 deletions source/binder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ namespace binder {

bool IncludeSet::add_decl(clang::NamedDecl const *D, int level)
{
auto it_inserted = stack_.insert( {D, level} );
auto & it = it_inserted.first;
auto & inserted = it_inserted.second;
if(inserted) return true;
auto it_inserted = stack_.insert({D, level});
auto &it = it_inserted.first;
auto &inserted = it_inserted.second;
if( inserted ) return true;
else {
if( it->second <= level ) return false;
else {
it->second = level;
//it.value() = level;
// it.value() = level;
return true;
}
}
Expand Down
24 changes: 16 additions & 8 deletions source/binder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
#include <clang/AST/Decl.h>
#include <clang/AST/DeclCXX.h>

#include <llvm/Support/raw_ostream.h>
#include <llvm/ADT/DenseMap.h>
//#include <tsl/robin_map.h>
#include <llvm/Support/raw_ostream.h>
// #include <tsl/robin_map.h>

#include <string>
#include <unordered_map>
Expand Down Expand Up @@ -48,9 +48,9 @@ class IncludeSet
private:
std::vector<std::string> includes_;

//using StackType = std::unordered_map<clang::NamedDecl const *, int>;
// using StackType = std::unordered_map<clang::NamedDecl const *, int>;
using StackType = llvm::DenseMap<clang::NamedDecl const *, int>;
//using StackType = tsl::robin_map<clang::NamedDecl const *, int>;
// using StackType = tsl::robin_map<clang::NamedDecl const *, int>;

StackType stack_;

Expand All @@ -59,10 +59,18 @@ class IncludeSet


enum RequestFlags : int8_t {
none=0, skipping = 1, binding = 2,
none = 0,
skipping = 1,
binding = 2,
};
inline RequestFlags operator|(RequestFlags a, RequestFlags b) { return static_cast<RequestFlags>(static_cast<int>(a) | static_cast<int>(b)); }
inline RequestFlags operator&(RequestFlags a, RequestFlags b) { return static_cast<RequestFlags>(static_cast<int>(a) & static_cast<int>(b)); }
inline RequestFlags operator|(RequestFlags a, RequestFlags b)
{
return static_cast<RequestFlags>(static_cast<int>(a) | static_cast<int>(b));
}
inline RequestFlags operator&(RequestFlags a, RequestFlags b)
{
return static_cast<RequestFlags>(static_cast<int>(a) & static_cast<int>(b));
}

/// Bindings Generator - represent object that can generate binding info for function, class, enum or data variable
class Binder
Expand Down Expand Up @@ -92,7 +100,7 @@ class Binder


/// check if user supplied config requested binding for the given declaration and if so request it
virtual void request_bindings_and_skipping(Config const &, RequestFlags flags = RequestFlags::skipping | RequestFlags::binding) = 0;
virtual void request_bindings_and_skipping(Config const &, RequestFlags flags = RequestFlags::skipping | RequestFlags::binding) = 0;

/// extract include needed for this generator and add it to includes vector
virtual void add_relevant_includes(IncludeSet &) const = 0;
Expand Down
121 changes: 52 additions & 69 deletions source/class.cpp

Large diffs are not rendered by default.

45 changes: 19 additions & 26 deletions source/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

#include <llvm/Support/raw_ostream.h>

#include <sstream>
#include <fstream>
#include <sstream>
#include <stdexcept>

using namespace llvm;
Expand Down Expand Up @@ -102,7 +102,7 @@ void Config::read(string const &file_name)
string line;

while( std::getline(f, line) ) {
if( line.empty() or line[0] == '#' ) continue;
if( line.empty() or line[0] == '#' ) continue;

if( line.back() == '\r' ) {
line.pop_back();
Expand Down Expand Up @@ -139,7 +139,7 @@ void Config::read(string const &file_name)
}
else if( token == _python_builtin_ ) {

if (bind) python_builtins.insert(name_without_spaces);
if( bind ) python_builtins.insert(name_without_spaces);
else not_python_builtins.insert(name_without_spaces);
}
else if( token == _function_ ) {
Expand Down Expand Up @@ -173,14 +173,10 @@ void Config::read(string const &file_name)
}
}
else if( token == _buffer_protocol_ ) {
if(bind) {
buffer_protocols.push_back(name_without_spaces);
}
if( bind ) { buffer_protocols.push_back(name_without_spaces); }
}
else if( token == _module_local_namespace_) {
if(bind) {
module_local_namespaces_to_add.push_back(name_without_spaces);
}
else if( token == _module_local_namespace_ ) {
if( bind ) { module_local_namespaces_to_add.push_back(name_without_spaces); }
else {
module_local_namespaces_to_skip.push_back(name_without_spaces);
}
Expand Down Expand Up @@ -212,18 +208,15 @@ void Config::read(string const &file_name)
auto binder_function = split_in_two(name, "Invalid line for add_on_binder_for_namespace specification! Must be: name_of_type + <space or tab> + name_of_binder. Got: " + line);
add_on_binder_for_namespaces_[binder_function.first] = trim(binder_function.second);
}
} else if ( token == _field_ ) {
}
else if( token == _field_ ) {

if (!bind) {
fields_to_skip.push_back(name_without_spaces);
}
if( !bind ) { fields_to_skip.push_back(name_without_spaces); }
}
else if( token == _custom_shared_ ) holder_type_ = name_without_spaces;

else if( token == _smart_holder_ ) {
if(bind) {
smart_held_classes.push_back(name_without_spaces);
}
if( bind ) { smart_held_classes.push_back(name_without_spaces); }
}

else if( token == _pybind11_include_file_ ) {
Expand All @@ -243,7 +236,8 @@ void Config::read(string const &file_name)

else if( token == _trampoline_member_function_binder_ ) {
if( bind ) {
auto member_function_name_and_function_name = split_in_two(name, "Invalid line for trampoline_member_function_binder specification! Must be: qualified_class_name::member_funtion_name + <space or tab> + name_of_function. Got: " + line);
auto member_function_name_and_function_name = split_in_two(
name, "Invalid line for trampoline_member_function_binder specification! Must be: qualified_class_name::member_funtion_name + <space or tab> + name_of_function. Got: " + line);
custom_trampoline_functions_[member_function_name_and_function_name.first] = member_function_name_and_function_name.second;
}
}
Expand Down Expand Up @@ -440,17 +434,16 @@ bool Config::is_module_local_requested(string const &namespace_) const
auto module_local_all = std::find(module_local_namespaces_to_add.begin(), module_local_namespaces_to_add.end(), namespace_all);
if( module_local_all != module_local_namespaces_to_add.end() ) {
auto module_local_to_skip = std::find(module_local_namespaces_to_skip.begin(), module_local_namespaces_to_skip.end(), namespace_);
if( module_local_to_skip != module_local_namespaces_to_skip.end()) {
return false;
}
if( module_local_to_skip != module_local_namespaces_to_skip.end() ) { return false; }
return true;
}

auto module_local_to_add = std::find(module_local_namespaces_to_add.begin(), module_local_namespaces_to_add.end(), namespace_);
if( module_local_to_add != module_local_namespaces_to_add.end()) {
if( module_local_to_add != module_local_namespaces_to_add.end() ) {
auto module_local_to_skip = std::find(module_local_namespaces_to_skip.begin(), module_local_namespaces_to_skip.end(), namespace_);
if( module_local_to_skip != module_local_namespaces_to_skip.end()) {
throw std::runtime_error("Could not determent if namespace '" + namespace_ + "' should use module_local or not... please resolve the conlficting options +module_local_namespace and -module_local_namespace!!!");
if( module_local_to_skip != module_local_namespaces_to_skip.end() ) {
throw std::runtime_error("Could not determent if namespace '" + namespace_ +
"' should use module_local or not... please resolve the conlficting options +module_local_namespace and -module_local_namespace!!!");
}
return true;
}
Expand Down Expand Up @@ -486,8 +479,8 @@ string Config::includes_code() const
{
std::ostringstream s;
for( auto &i : includes_to_add ) s << "#include " << i << "\n";
if (O_include_pybind11_stl) s << "#include <pybind11/stl.h>\n";
if (s.tellp() != std::streampos(0)) s << '\n';
if( O_include_pybind11_stl ) s << "#include <pybind11/stl.h>\n";
if( s.tellp() != std::streampos(0) ) s << '\n';
return s.str();
}

Expand Down
8 changes: 5 additions & 3 deletions source/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
#ifndef _INCLUDED_config_hpp_
#define _INCLUDED_config_hpp_

#include <set>
#include <map>
#include <set>
#include <string>
#include <vector>

Expand All @@ -32,8 +32,10 @@ class Config

Config() {}

Config(string const &root_module_, std::vector<string> namespaces_to_bind_, std::vector<string> namespaces_to_skip_, string const &prefix_, std::size_t maximum_file_length_, bool skip_line_number_)
: root_module(root_module_), namespaces_to_bind(namespaces_to_bind_), namespaces_to_skip(namespaces_to_skip_), prefix(prefix_), maximum_file_length(maximum_file_length_), skip_line_number(skip_line_number_)
Config(string const &root_module_, std::vector<string> namespaces_to_bind_, std::vector<string> namespaces_to_skip_, string const &prefix_, std::size_t maximum_file_length_,
bool skip_line_number_)
: root_module(root_module_), namespaces_to_bind(namespaces_to_bind_), namespaces_to_skip(namespaces_to_skip_), prefix(prefix_), maximum_file_length(maximum_file_length_),
skip_line_number(skip_line_number_)
{
}

Expand Down
7 changes: 4 additions & 3 deletions source/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,11 +437,12 @@ void Context::generate(Config const &config)

string const holder_type = Config::get().holder_type();

string shared_declare = "PYBIND11_DECLARE_HOLDER_TYPE(T, "+holder_type+"<T>, false)";
string shared_make_opaque = "PYBIND11_MAKE_OPAQUE("+holder_type+"<void>)";
string shared_declare = "PYBIND11_DECLARE_HOLDER_TYPE(T, " + holder_type + "<T>, false)";
string shared_make_opaque = "PYBIND11_MAKE_OPAQUE(" + holder_type + "<void>)";

string const pybind11_include = "#include <" + Config::get().pybind11_include_file() + ">";
code = generate_include_directives(includes) + fmt::format(module_header, pybind11_include, config.includes_code(), shared_declare, shared_make_opaque) + prefix_code + "void " + function_name + module_function_suffix + "\n{\n" + code + "}\n";
code = generate_include_directives(includes) + fmt::format(module_header, pybind11_include, config.includes_code(), shared_declare, shared_make_opaque) + prefix_code + "void " +
function_name + module_function_suffix + "\n{\n" + code + "}\n";

if( O_single_file ) root_module_file_handle << "// File: " << file_name << '\n' << code << "\n\n";
else update_source_file(config.prefix, file_name, code);
Expand Down
13 changes: 6 additions & 7 deletions source/enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ bool is_bindable(EnumDecl const *E)
/// check if user requested binding for the given declaration
bool is_binding_requested(clang::EnumDecl const *E, Config const &config)
{
if( config.is_enum_binding_requested( E->getQualifiedNameAsString() ) ) return true;
if( config.is_enum_binding_requested(E->getQualifiedNameAsString()) ) return true;

bool bind = config.is_namespace_binding_requested(namespace_from_named_decl(E));
//for( auto &t : get_type_dependencies(E) ) bind &= !is_skipping_requested(t, config);
// for( auto &t : get_type_dependencies(E) ) bind &= !is_skipping_requested(t, config);
return bind;
}

Expand All @@ -76,7 +76,7 @@ bool is_skipping_requested(clang::EnumDecl const *E, Config const &config)

bool skip = config.is_namespace_skipping_requested(namespace_from_named_decl(E));

//for( auto &t : get_type_dependencies(E) ) skip |= is_skipping_requested(t, config);
// for( auto &t : get_type_dependencies(E) ) skip |= is_skipping_requested(t, config);

return skip;
}
Expand Down Expand Up @@ -123,8 +123,7 @@ std::string bind_enum(std::string const &module, EnumDecl const *E)

// Add module local if requested for the namespace
std::string module_local_annotation = "";
if (Config::get().is_module_local_requested(namespace_from_named_decl(E)))
module_local_annotation = ", pybind11::module_local()";
if( Config::get().is_module_local_requested(namespace_from_named_decl(E)) ) module_local_annotation = ", pybind11::module_local()";

string r = "\tpybind11::enum_<{}>({}, \"{}\"{}, \"{}\"{})\n"_format(qualified_name, module, name, maybe_arithmetic, generate_documentation_string_for_declaration(E), module_local_annotation);

Expand Down Expand Up @@ -160,8 +159,8 @@ bool EnumBinder::bindable() const
/// check if user requested binding for the given declaration
void EnumBinder::request_bindings_and_skipping(Config const &config, RequestFlags flags)
{
if( (flags&RequestFlags::skipping) and is_skipping_requested(E, config) ) Binder::request_skipping();
else if( (flags&RequestFlags::binding) and is_binding_requested(E, config) ) Binder::request_bindings();
if( (flags & RequestFlags::skipping) and is_skipping_requested(E, config) ) Binder::request_skipping();
else if( (flags & RequestFlags::binding) and is_binding_requested(E, config) ) Binder::request_bindings();
}


Expand Down
Loading
Loading