nasa / trick

Trick Simulation Environment. Trick provides a common set of simulation capabilities and utilities to build simulations automatically.
Other
32 stars 16 forks source link

Trick Fails to Compile Sims with enum classes #1611

Closed shaferandrew closed 9 months ago

shaferandrew commented 9 months ago

PR #1603 (commit 763b52f), causes a syntax error when compiling a sim that uses c++ enum class.

To reproduce (using SIM_cannon_analytic as a base):

  1. Create "foo.hh" in "trick_sims/Cannon/models/cannon/gravity/include/" with the following contents:
    // @trick_parse{everything}
    enum class Foo {
       a,
       b,
       c
    };
  2. In SIM_cannon_analytic/S_define, add line 13 as ##include "cannon/gravity/include/foo.hh"
  3. Optionally, add line 20 as Foo bar{Foo::a}; (it fails to compile even without this line).
  4. Run trick-CP in the SIM_cannon_analytic directory and observe this output:
    Building with the following compilation flags:
    TRICK_CFLAGS   =  -I../models
    TRICK_CXXFLAGS =  -I../models
    SWIGing    build/home/ajshafer/dev/trick/trick_sims/Cannon/SIM_cannon_analytic/S_source_py.i
    build/home/ajshafer/dev/trick/trick_sims/Cannon/models/cannon/gravity/include/foo_py.i:20: Error: Syntax error in input(1).
    make: *** [build/Makefile_swig:55: build/home/ajshafer/dev/trick/trick_sims/Cannon/SIM_cannon_analytic/S_source_py.cpp] Error 1
  5. From "foo_py.i", starting at line 18:

    enum class Foo {
    #if SWIG_VERSION > 0x040000
    %pythoncode %{
       __setattr__ = _swig_setattr_nondynamic_instance_variable(object.__setattr__)
    %}
    #endif
    
       a,
       b,
       c
    };
    #define TRICK_SWIG_DEFINED_Foo
  6. Removing or commenting out lines 19 through 23 (from #if... to ...#endif) and re-running make will complete successfully.

I can't find any instances of "enum class" in the Trick testing setup.

shaferandrew commented 9 months ago

Interestingly, this does not seem to affect an enum class that is declared within an object.

  1. Create "bar.hh" using step 1. above with these contents:

    // @trick_parse{everything}
    class Bar {
       enum class Baz{
           a,
           b,
           c
       };
    
       Baz baz{Baz::a};
    };
  2. Update the S_define to include it (##include "cannon/gravity/include/bar.hh")
  3. Optionally, create a Bar object in the body (Bar bar{};)

Notice that this will compile. Lines 18-32 of "bar_py.i" are below:

   class Bar {
   #if SWIG_VERSION > 0x040000
   %pythoncode %{
       __setattr__ = _swig_setattr_nondynamic_instance_variable(object.__setattr__)
   %}
   #endif

       enum class Baz{
           a,
           b,
           c
       };

       Baz baz{Baz::a};
   };

This issue may only affect an enum class that is declared outside of an encapsulating class, e.g. just within a namespace.

alexlin0 commented 9 months ago

This is the result of #1288.

convert_swig does not understand an enum class and erroneously matches it as a regular class, adding the code above and causing the error.

sharmeye commented 9 months ago

Seems to be addressed in recent PR