diff --git a/source/class.cpp b/source/class.cpp index 55c36af7..2a16bfe5 100644 --- a/source/class.cpp +++ b/source/class.cpp @@ -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())); @@ -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; diff --git a/source/function.cpp b/source/function.cpp index cf2f54e1..62b46c5d 100644 --- a/source/function.cpp +++ b/source/function.cpp @@ -11,6 +11,7 @@ /// @author Sergey Lyskov #include +#include #include #include @@ -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; diff --git a/source/options.cpp b/source/options.cpp index 147a62d9..703782f1 100644 --- a/source/options.cpp +++ b/source/options.cpp @@ -20,6 +20,8 @@ llvm::cl::OptionCategory BinderToolCategory("Binder options"); cl::opt 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 O_annotate_functions("annotate-functions", cl::desc("Annotate each function bindings with full function signature"), cl::init(false), cl::cat(BinderToolCategory)); + cl::opt 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)); diff --git a/source/options.hpp b/source/options.hpp index 85be3c23..78df6f7e 100644 --- a/source/options.hpp +++ b/source/options.hpp @@ -20,6 +20,7 @@ extern llvm::cl::OptionCategory BinderToolCategory; extern llvm::cl::opt O_annotate_includes; +extern llvm::cl::opt O_annotate_functions; extern llvm::cl::opt O_single_file; extern llvm::cl::opt O_trace; extern llvm::cl::opt O_verbose; diff --git a/test/self-test.py b/test/self-test.py index cfa9c09d..e6dc7c63 100755 --- a/test/self-test.py +++ b/test/self-test.py @@ -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); @@ -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')