From e6509df362a3d70399d7f2b2aa170fbf46807217 Mon Sep 17 00:00:00 2001 From: Derek Bankieris Date: Fri, 10 May 2019 13:31:03 -0500 Subject: [PATCH 1/2] Improve scoping of SWIG %template directives Refs #768 & Refs #769 --- libexec/trick/convert_swig | 24 ++++- test/SIM_swig_template_scoping/S_define | 2 + test/SIM_swig_template_scoping/S_overrides.mk | 3 + .../models/Classes.hh | 93 +++++++++++++++++++ 4 files changed, 118 insertions(+), 4 deletions(-) create mode 100644 test/SIM_swig_template_scoping/S_define create mode 100644 test/SIM_swig_template_scoping/S_overrides.mk create mode 100644 test/SIM_swig_template_scoping/models/Classes.hh diff --git a/libexec/trick/convert_swig b/libexec/trick/convert_swig index 80c5c5e7..94815fa9 100755 --- a/libexec/trick/convert_swig +++ b/libexec/trick/convert_swig @@ -60,6 +60,7 @@ my %sim ; my %out_of_date ; my ($version, $thread, $year) ; my %processed_templates ; +my $global_template_typedefs ; my $typedef_def = qr/typedef\s+ # the word typedef (?:[_A-Za-z][\s\w]*\s*) # resolved type @@ -336,6 +337,7 @@ sub process_file() { } print OUT "\n$new_contents" ; print OUT "$contents\n" ; + print OUT $global_template_typedefs ; # Add a trick_cast_as macro line for each class parsed in the file. These lines must appear at the bottom of the # file to ensure they are not in a namespace directive and they are after the #define statements they depend on. undef %class_typemap_printed ; @@ -638,13 +640,27 @@ sub process_class($$$$$) { my ($template_type_no_sp) = $template_full_type ; $template_type_no_sp =~ s/\s//g ; + + # If the type is qualified, assume it's fully qualified and put the + # %template directive in the global namespace. + # See https://github.com/nasa/trick/issues/768 + my $qualified = $template_type_no_sp =~ /^\w+(::)\w+ + +namespace a { + template class A { + private: + void operator=(const A&); + }; +} + +namespace b { + + template class A {}; + template class B {}; + + class C { + public: + std::vector v; + a::A a; + a::A a2; + A a3; + A a4; + B b; + B b2; + b::B b3; + b::B b4; + B > ba; + B > ba2; + b::B > ba3; + b::B > ba4; + B > ba5; + B > ba6; + + // These don't work. Because the type is qualified, convert_swig assumes + // it's fully qualified and puts the %template directive in the global + // namespace. However, there is no A in the global namespace, so the wrapper + // code fails to compile. Bonus points if you can fix this without breaking + // something else. + //b::B > ba7; + //b::B > ba8; + }; + + // This class is the same as the previous. Seems weird, but it reveals scoping + // errors under SWIG 2. The replication is not necessary in SWIG 4, which has + // better error detection. + class D { + public: + std::vector v; + a::A a; + a::A a2; + A a3; + A a4; + B b; + B b2; + b::B b3; + b::B b4; + B > ba; + B > ba2; + b::B > ba3; + b::B > ba4; + B > ba5; + B > ba6; + //b::B > ba7; + //b::B > ba8; + }; +} + +class E { + public: + std::vector v; + a::A a; + a::A a2; + b::B b3; + b::B b4; + b::B > ba3; + b::B > ba4; +}; + +namespace a { + class B { + public: + std::vector v; + a::A a; + a::A a2; + A a3; + A a4; + b::B b3; + b::B b4; + b::B > ba3; + b::B > ba4; + //b::B > ba7; + //b::B > ba8; + }; +} From 3e3bc02cd2c3fa1b3bd8a30042d718c33ef12c4c Mon Sep 17 00:00:00 2001 From: Derek Bankieris Date: Fri, 10 May 2019 15:21:28 -0500 Subject: [PATCH 2/2] Correct TRICK_CONVERT_SWIG_FLAGS in Makefile_swig Fixes #770 --- libexec/trick/make_makefile_swig | 4 ++-- share/trick/makefiles/Makefile.common | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/libexec/trick/make_makefile_swig b/libexec/trick/make_makefile_swig index fefa81d3..5c95abb4 100755 --- a/libexec/trick/make_makefile_swig +++ b/libexec/trick/make_makefile_swig @@ -244,8 +244,8 @@ SWIG_I_CUSTOM_OUTDIR =" ; define create_convert_swig_rule build/%_py.i: /%.\$1 -\t\@echo \${TRICK_HOME}/\$(LIBEXEC)/trick/convert_swig \${TRICK_CONVERT_SWIG_FLAGS} \$\$< >> \$(MAKE_OUT) -\t\$(ECHO_CMD)\${TRICK_HOME}/\$(LIBEXEC)/trick/convert_swig \${TRICK_CONVERT_SWIG_FLAGS} \$\$< 2>&1 | \$(TEE) -a \$(MAKE_OUT) ; exit \$\${PIPESTATUS[0]} +\t\@echo \${TRICK_HOME}/\$(LIBEXEC)/trick/convert_swig \$\${TRICK_CONVERT_SWIG_FLAGS} \$\$< >> \$(MAKE_OUT) +\t\$(ECHO_CMD)\${TRICK_HOME}/\$(LIBEXEC)/trick/convert_swig \$\${TRICK_CONVERT_SWIG_FLAGS} \$\$< 2>&1 | \$(TEE) -a \$(MAKE_OUT) ; exit \$\${PIPESTATUS[0]} endef EXTENSIONS := H h hh hxx h++ hpp diff --git a/share/trick/makefiles/Makefile.common b/share/trick/makefiles/Makefile.common index 5c763ecd..5e7b426a 100644 --- a/share/trick/makefiles/Makefile.common +++ b/share/trick/makefiles/Makefile.common @@ -56,6 +56,7 @@ export TRICK_LIBS := ${RPATH} -L${TRICK_LIB_DIR} -ltrick -ltrick_pyip -ltrick_co export TRICK_SYSTEM_LDFLAGS := $(TRICK_SYSTEM_LDFLAGS) export TRICK_SWIG_FLAGS := $(TRICK_SWIG_FLAGS) export TRICK_SWIG_CFLAGS := $(TRICK_SWIG_CFLAGS) +export TRICK_CONVERT_SWIG_FLAGS := $(TRICK_CONVERT_SWIG_FLAGS) IO_SRC_DIR := io_src/ OBJ_DIR := object_${TRICK_HOST_CPU}