antlr4 icon indicating copy to clipboard operation
antlr4 copied to clipboard

C++ runtime demo fails to build

Open felixn opened this issue 3 years ago • 14 comments

In the current state of the master branch (dd623ec), the C++ runtime demo fails to build:

$ cd <snip>/runtime/Cpp
$ mkdir build && mkdir run && cd build
$ cmake .. -DANTLR_JAR_LOCATION=<snip>/antlr-4.9.3-complete.jar -DWITH_DEMO=True
$ make
...
[ 97%] Building CXX object demo/CMakeFiles/antlr4-demo.dir/Linux/main.cpp.o
In file included from /home/felix/src/antlr4/runtime/Cpp/demo/Linux/main.cpp:16:
/home/felix/src/antlr4/runtime/Cpp/demo/generated/TLexer.h:57:43: error: ‘virtual const std::vector<std::__cxx11::basic_string<char> >& antlrcpptest::TLexer::getTokenNames() const’ marked ‘override’, but does not override
   57 |   virtual const std::vector<std::string>& getTokenNames() const override; // deprecated, use vocabulary instead
      |                                           ^~~~~~~~~~~~~
In file included from /home/felix/src/antlr4/runtime/Cpp/demo/Linux/main.cpp:17:
/home/felix/src/antlr4/runtime/Cpp/demo/generated/TParser.h:44:43: error: ‘virtual const std::vector<std::__cxx11::basic_string<char> >& antlrcpptest::TParser::getTokenNames() const’ marked ‘override’, but does not override
   44 |   virtual const std::vector<std::string>& getTokenNames() const override { return _tokenNames; }; // deprecated: use vocabulary instead.
      |                                           ^~~~~~~~~~~~~
make[2]: *** [demo/CMakeFiles/antlr4-demo.dir/build.make:97: demo/CMakeFiles/antlr4-demo.dir/Linux/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:414: demo/CMakeFiles/antlr4-demo.dir/all] Error 2
make: *** [Makefile:166: all] Error 2

There's also a new warning:

[ 12%] Building CXX object runtime/CMakeFiles/antlr4_shared.dir/src/Vocabulary.cpp.o
/home/felix/src/antlr4/runtime/Cpp/runtime/src/Vocabulary.cpp: In static member function ‘static void antlr4::dfa::Vocabulary::__static_initialization_and_destruction_0(int, int)’:
/home/felix/src/antlr4/runtime/Cpp/runtime/src/Vocabulary.cpp:12:18: warning: ‘antlr4::dfa::Vocabulary::EMPTY_VOCABULARY’ is deprecated: Use the default constructor of Vocabulary instead. [-Wdeprecated-declarations]
   12 | const Vocabulary Vocabulary::EMPTY_VOCABULARY;
      |                  ^~~~~~~~~~
/home/felix/src/antlr4/runtime/Cpp/runtime/src/Vocabulary.cpp:12:18: note: declared here

@jcking / @mike-lischke - I'm naively assuming this is related to one of your recent PRs. Could you please take a look? I haven't spent the time to narrow down when exactly the error was introduced, but I can if you want me to.

Versions:

-- The C compiler identification is GNU 11.2.1
-- The CXX compiler identification is GNU 11.2.1

$ gcc --version
gcc (GCC) 11.2.1 20211203 (Red Hat 11.2.1-7)

$ java --version
openjdk 11.0.13 2021-10-19
OpenJDK Runtime Environment 18.9 (build 11.0.13+8)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.13+8, mixed mode, sharing)

felixn avatar Dec 19 '21 15:12 felixn

Ahh, with a custom build of the antlr tool (antlr4-4.9.4-SNAPSHOT-complete.jar) it works again.

Still, a bit of an annoying situation for downstream projects, because by default the cmake files refers to the master branch, which currently doesnt't work with the released version of the antlr tool (antlr-4.9.3-complete.jar). https://github.com/antlr/antlr4/blob/dd623ecc2f02f4fa051496466b80c138c0191ae9/runtime/Cpp/cmake/ExternalAntlr4Cpp.cmake#L8-L12 Should that be changed to refer to the last released branch, matching the released version of the tool?

felixn avatar Dec 19 '21 15:12 felixn

Yes there was a clean up to remove deprecated stuff like token names. You probably should not mix the published tool jar and a runtime from the git repository. Instead there are also published runtime bundles.

I understand the trouble such changes can make, but at some point the master branch must change before a public release happens.

mike-lischke avatar Dec 19 '21 17:12 mike-lischke

I also encountered the same problem, using the ExternalAntlr4Cpp method. How can I solve this problem?

hsl4125 avatar Dec 20 '21 16:12 hsl4125

Set ANTLR4_TAG to e4c1a74c66bd5290364ea2b36c97cd724b247357, which is the latest stable release 4.9.3.

jcking avatar Dec 20 '21 16:12 jcking

I'm trying to update the CI build for grammars-v4 and have hit this problem as well. Note, the CMake instructions need to be updated for the current tip of "master". The code cannot compile under GNU C++ -std=c++11. @jcking This line is now wrong as well as the preceding comment. It must be at least set(CMAKE_CXX_STANDARD 17). string_view was introduced in C++17.

kaby76 avatar Dec 22 '21 21:12 kaby76

@jcking Since I don't know much about cmake, I didn't look after the cmake files. Can you file a new PR with updates to all of them, to give users an easy start?

mike-lischke avatar Dec 23 '21 18:12 mike-lischke

When will this be fixed? I'm attempting to build a do-nothing main with the files generated from parsing the Java lex and grammar files provided by the grammars-v4 repository. Attempting to use set(CMAKE_CXX_STANDARD 17) results in:

'error: 'getTokenNames' marked 'override' but does not override any member functions virtual const std::vectorstd::string& getTokenNames() const override; // deprecated, use vocabulary instead'

And:

'error: no viable conversion from 'std::string_view' (aka 'basic_string_view') to 'std::string' (aka 'basic_string')' std::string name = _vocabulary.getLiteralName(i); ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Is this related? Do I need to build e4c1a74?

jklappenbach avatar Jan 17 '22 06:01 jklappenbach

@jklappenbach The problems you are seeing are caused by two things: (1) Antlr4 4.9.3 targeting C++ now requires C++17 or newer, and (2) CMake picking up changes in the Antlr4 runtime post 4.9.3 that you don't want. You will need to adjust CMakeList.txt for both. See this line for ANTLR4_TAG in my template code for C++ CMakeLists.txt and this line for C++17. The code is part of my driver generator trgen, which is used for CI testing in the grammars-v4/ repo. Note, only two of the Java grammars are ported to C++ (java/ and java8). I haven't had time to fix java9/, so it is "skipped" in testing.

kaby76 avatar Jan 17 '22 10:01 kaby76

Probably not related, but I got this error when running with the CMakeLists.txt in the README:

terminate called after throwing an instance of 'std::system_error'
  what():  Unknown error -1
Aborted

I had to add:

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
...
target_link_libraries(demo Threads::Threads)

jepperaskdk avatar Apr 16 '22 15:04 jepperaskdk

@jepperaskdk Thanks for the info. I have a ton of those errors while updating the build for 4.10 in github.com/antlr/grammars-v4.

kaby76 avatar Apr 16 '22 15:04 kaby76

@jepperaskdk Thanks for the info. I have a ton of those errors while updating the build for 4.10 in github.com/antlr/grammars-v4.

I was only able to find that because I enabled debugging with set(CMAKE_CXX_FLAGS "-g") and stepped through the code. So I guess that's also useful to include - but this is also my first CPP project.

jepperaskdk avatar Apr 16 '22 15:04 jepperaskdk

That was it. Not in the demo, not in the readme. It needs to, or someone figure out why it should not.

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
...
target_link_libraries(Test antlr4_static Threads::Threads)

Is the demo actually built in CI?

Also, I'm noticing this coming out of the CMake build: -- Found ANTLR: /tmp/antlr4-4.10-complete.jar (found version "4.1"), while for 4.9.3, I saw -- Found ANTLR: /tmp/antlr-4.9.3-complete.jar (found version "4.9.3"). Something messed up, but likely something I did wrong.

kaby76 avatar Apr 16 '22 16:04 kaby76

Ahh, with a custom build of the antlr tool (antlr4-4.9.4-SNAPSHOT-complete.jar) it works again.

Hi @felixn , I came across the exact error when running a parser. May I ask how to get the antlr4-4.9.4-SNAPSHOT-complete.jar? Thanks.

lemonci avatar May 16 '22 00:05 lemonci

Ahh, with a custom build of the antlr tool (antlr4-4.9.4-SNAPSHOT-complete.jar) it works again.

Hi @felixn , I came across the exact error when running a parser. May I ask how to get the antlr4-4.9.4-SNAPSHOT-complete.jar? Thanks.

This explains building Antlr, which produces the file: https://github.com/antlr/antlr4/blob/master/doc/building-antlr.md

Then checkout the version you wish to build after cloning. It doesn't look like there's a 4.9.4 tag though, only 4.9.3 or 4.10.

jepperaskdk avatar May 16 '22 06:05 jepperaskdk

Probably not related, but I got this error when running with the CMakeLists.txt in the README:

terminate called after throwing an instance of 'std::system_error'
  what():  Unknown error -1
Aborted

I had to add:

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
...
target_link_libraries(demo Threads::Threads)

thank you very much

t34shen avatar Mar 02 '23 13:03 t34shen