Skip to content

Commit

Permalink
add --annotate-functions option
Browse files Browse the repository at this point in the history
  • Loading branch information
lyskov committed Nov 6, 2024
1 parent 0fd3d59 commit e0306de
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
22 changes: 15 additions & 7 deletions source/class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1019,8 +1019,16 @@ string bind_constructor(ConstructorBindingInfo const &CBI, uint args_to_bind, bo
// string function_qualified_name { F->getQualifiedNameAsString() };

string c;

if( O_annotate_functions ) {
clang::FunctionDecl const *F = CBI.T;
string const include = relevant_include(F);
c += "\t// function-signature: " + function_qualified_name(F) + "(" + function_arguments(F) + ") file:" + (include.size() ? include.substr(1, include.size() - 2) : "") + " line:" + line_number(F) + "\n";
}


if( args_to_bind == CBI.T->getNumParams() and not CBI.T->isVariadic() ) {
c = "\tcl.def( pybind11::init<{}>()"_format(function_arguments(CBI.T));
c += "\tcl.def( pybind11::init<{}>()"_format(function_arguments(CBI.T));

for( uint i = 0; i < CBI.T->getNumParams() and i < args_to_bind; ++i ) {
c += ", pybind11::arg(\"{}\")"_format(string(CBI.T->getParamDecl(i)->getName()));
Expand All @@ -1039,14 +1047,14 @@ string bind_constructor(ConstructorBindingInfo const &CBI, uint args_to_bind, bo

for( uint i = 0; i < CBI.T->getNumParams() and i < args_to_bind; ++i ) { args_helper += ", pybind11::arg(\"{}\")"_format(string(CBI.T->getParamDecl(i)->getName())); }

// if( CBI.T->isVariadic() ) c = fmt::format(constructor_lambda_template, params, args.second, constructor_types.first, constructor_types.second);
// else if( constructor_types.first.size() and constructor_types.second.size() ) c = fmt::format(constructor_lambda_template, params, args.second, constructor_types.first,
// constructor_types.second); else if( constructor_types.first.size() ) c = fmt::format(constructor_template, params, args.second, constructor_types.first); else c =
// if( CBI.T->isVariadic() ) c += fmt::format(constructor_lambda_template, params, args.second, constructor_types.first, constructor_types.second);
// else if( constructor_types.first.size() and constructor_types.second.size() ) c += fmt::format(constructor_lambda_template, params, args.second, constructor_types.first,
// constructor_types.second); else if( constructor_types.first.size() ) c += fmt::format(constructor_template, params, args.second, constructor_types.first); else c +=
// fmt::format(constructor_template, params, args.second, constructor_types.second);

if( CBI.C->isAbstract() ) c = fmt::format(constructor_template, params, args.second, CBI.trampoline_qualified_name);
else if( CBI.trampoline ) c = fmt::format(constructor_with_trampoline_template, params, args.second, CBI.class_qualified_name, CBI.trampoline_qualified_name);
else c = fmt::format(constructor_template_with_py_arg, params, args.second, CBI.class_qualified_name, args_helper);
if( CBI.C->isAbstract() ) c += fmt::format(constructor_template, params, args.second, CBI.trampoline_qualified_name);
else if( CBI.trampoline ) c += fmt::format(constructor_with_trampoline_template, params, args.second, CBI.class_qualified_name, CBI.trampoline_qualified_name);
else c += fmt::format(constructor_template_with_py_arg, params, args.second, CBI.class_qualified_name, args_helper);
}

return c;
Expand Down
6 changes: 6 additions & 0 deletions source/function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
/// @author Sergey Lyskov

#include <function.hpp>
#include <options.hpp>

#include <class.hpp>
#include <fmt/format.h>
Expand Down Expand Up @@ -451,6 +452,11 @@ string bind_function(string const &module, FunctionDecl const *F, Context &conte
{
string code;

if( O_annotate_functions ) {
string const include = relevant_include(F);
code += "\t// function-signature: " + function_qualified_name(F) + "(" + function_arguments(F) + ") file:" + (include.size() ? include.substr(1, include.size() - 2) : "") + " line:" + line_number(F) + "\n";
}

int num_params = F->getNumParams();

int args_to_bind = 0;
Expand Down
2 changes: 2 additions & 0 deletions source/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ llvm::cl::OptionCategory BinderToolCategory("Binder options");

cl::opt<bool> O_annotate_includes("annotate-includes", cl::desc("Annotate each includes in generated code with type name that trigger it inclusion"), cl::init(false), cl::cat(BinderToolCategory));

cl::opt<bool> O_annotate_functions("annotate-functions", cl::desc("Annotate each function bindings with full function signature"), cl::init(false), cl::cat(BinderToolCategory));

cl::opt<bool> O_single_file("single-file", cl::desc("Concatenate all binder output into single file with name: root-module-name + '.cpp'. Use this for a small projects and for testing."),
cl::init(false), cl::cat(BinderToolCategory));

Expand Down
1 change: 1 addition & 0 deletions source/options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
extern llvm::cl::OptionCategory BinderToolCategory;

extern llvm::cl::opt<bool> O_annotate_includes;
extern llvm::cl::opt<bool> O_annotate_functions;
extern llvm::cl::opt<bool> O_single_file;
extern llvm::cl::opt<bool> O_trace;
extern llvm::cl::opt<bool> O_verbose;
Expand Down
7 changes: 5 additions & 2 deletions test/self-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,11 @@ def run_test(test_path, build_dir, pyenv):
python = pyenv.python
python_includes = '-I'+pyenv.python_include_dir #'-I/usr/include/python2.7'

command_line = '{binder} --bind "" --skip-line-number --root-module {root_module} --prefix {build_dir} --single-file --annotate-includes {config}{cli} {source} -- -x c++ -std=c++11 -I {source_dir} -I {source_dir}/.. -isystem {pybind11} {python_includes}' \
extras = '--annotate-functions' if Options.annotate else ''

command_line = '{binder} --bind "" --skip-line-number --root-module {root_module} --prefix {build_dir} --single-file --annotate-includes {extras} {config}{cli} {source} -- -x c++ -std=c++11 -I {source_dir} -I {source_dir}/.. -isystem {pybind11} {python_includes}' \
.format(binder=Options.binder, root_module=root_module, build_dir=build_dir, source_dir=source_dir, cli=cli, source=source_include,
config='--config {}'.format(config) if config else '', pybind11=Options.pybind11, python_includes=python_includes)
config='--config {}'.format(config) if config else '', pybind11=Options.pybind11, python_includes=python_includes, extras=extras)

execute('{} Running test...'.format(test), command_line);

Expand Down Expand Up @@ -150,6 +152,7 @@ def main():
parser.add_argument('--pybind11', default='', help='Path to pybind11 source tree')

parser.add_argument("--accept", action="store_true", help="Run tests and accept new tests results as reference")
parser.add_argument("--annotate", action="store_true", help="Run Binder with extra annotation options")

parser.add_argument('args', nargs=argparse.REMAINDER, help='Optional: list of tests to run')

Expand Down

0 comments on commit e0306de

Please sign in to comment.