This forum has been archived. Please start a new discussion on GitHub.

slice2cpp produces bad code

slice2cpp generates bad C++ code while compiling the following Slice:
module Foo
    class while


  • You don't show the bad code--what exactly do you think is wrong?

    Because while is a C++ reserved word, it is mapped to _cpp_while. Is that what you are referring to? If so, that is expected; see the client-side C++ mapping in the manual.


  • I mean that if I compile such Slice with slice2cpp and then compile the generated
    C++ code with some C++ compiler (gcc4 in my case) it won't compile due to errors.
  • matthew
    matthew NL, Canada
    It would help to provide the exact compiler version, operating system, and finally compilation errors that you are getting.
  • When I compile Sheff's slice code (after adding a semicolon in the last line) with slice2cpp 3.3.0 and try to compile the generated C++ code with g++ 4.2.4 on GNU/Linux (Kubuntu 8.04), the compiler reports the following error in line 381 of the generated header file:
    In file included from sl.cpp:13:
    ./sl.h:381: Fehler: »_cpp_while__staticInit« in Namensbereich »Foo« bezeichnet keinen Typ
    The associated class definition in line 374 is called while__staticInit and not _cpp_while__staticInit.

    (BTW, this looks suspiciously like the bug reported in post 14617 two years ago. ;) )
  • This is a bug in the code generator. Thanks for reporting this. We'll include a fix in the next release. As a work-around in the mean time, you can either rename the Slice symbol so it is no longer a C++ identifier, or you can edit the generated code. The line reading:
    static ::Foo::_cpp_while__staticInit _while_init;

    should instead be
    static ::Foo::while__staticInit _while_init;


  • By the way, C++ identifier in module name also produces bad code if
    that module contain classes.
  • Here is a patch that fixes the class name problem:
    diff --git a/cpp/src/slice2cpp/Gen.cpp b/cpp/src/slice2cpp/Gen.cpp
    index 51ea19f..22a0334 100644
    --- a/cpp/src/slice2cpp/Gen.cpp
    +++ b/cpp/src/slice2cpp/Gen.cpp
    @@ -3857,7 +3857,7 @@ Slice::Gen::ObjectVisitor::visitClassDefEnd(const ClassDefPtr& p)
             H << sp << nl << scoped << " _init;";
             H << eb << ';';
             _doneStaticSymbol = true;
    -        H << sp << nl << "static " << scoped << "__staticInit _" << p->name() << "_init;";
    +        H << sp << nl << "static " << p->name() << "__staticInit _" << p->name() << "_init;";

    I'll have a look at the module name issue as well, thanks!


  • Thanks for the bug report. Here is a patch that fixes it:
    diff --git a/cpp/src/slice2cpp/Gen.cpp b/cpp/src/slice2cpp/Gen.cpp
    index 22a0334..f990ccd 100644
    --- a/cpp/src/slice2cpp/Gen.cpp
    +++ b/cpp/src/slice2cpp/Gen.cpp
    @@ -4460,7 +4460,8 @@ Slice::Gen::ObjectVisitor::emitOneShotConstructor(const ClassDefPtr& p)
    -        C << sp << nl << p->scoped().substr(2) << "::" << fixKwd(p->name()) << spar << allParamDecls << epar << " :";
    +        C << sp << nl << fixKwd(p->scoped()).substr(2) << "::" << fixKwd(p->name())
    +         << spar << allParamDecls << epar << " :";
             DataMemberList dataMembers = p->dataMembers();