standardese / cppast

Library to parse and work with the C++ AST
Other
1.7k stars 165 forks source link

GCC array bounds complain on libclang_parser.cpp #160

Closed CarlosNihelton closed 1 year ago

CarlosNihelton commented 1 year ago

Hi @foonathan ! Thank you very much for this great library!

I was very excited attempting to compile standardese with GCC 12.2 and got stuck on array bounds checking error message on line 123: https://github.com/foonathan/cppast/blob/7b255252328969435487cd8a06f569b0e4b6847a/src/libclang/libclang_parser.cpp#L118-L129 , thus couldn't compile. It's nice to work with such a high level of strictness. It seems to me that the dir string would never be empty, but the compiler is not so convinced, thus I guess we'd need to throw an exception before accessing dir operator[] in case it's empty. Or maybe you got a better idea of how to convince the compiler that string will never be emtpy (caching it in the class constructor, maybe?).

To move forward I just put a check-and-throw before that line and got it to compile till the end. I noticed the code is the same on the main branch.

Input flags: cmake -S. -Bbuild -GNinja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_CXX_FLAGS="-frecord-gcc-switches" -DLLVM_CONFIG_BINARY=/usr/lib/llvm-15/bin/llvm-config -DCMAKE_CXX_COMPILER=g++

Output:

cmake --build build --target standardese_tool
[44/83] Building CXX object external/cppast/src/CMakeFiles/cppast.dir/libclang/libclang_parser.cpp.o
FAILED: external/cppast/src/CMakeFiles/cppast.dir/libclang/libclang_parser.cpp.o
/usr/bin/g++ -DCPPAST_CLANG_BINARY=\"/usr/lib/llvm-15/bin/clang\" -DCPPAST_CLANG_VERSION_STRING=\"15.0.6\" -DCPPAST_VERSION_MAJOR=\"0\" -DCPPAST_VERSION_MINOR=\"0\" -DCPPAST_VERSION_STRING=\"0.0\" -DTYPE_SAFE_ARITHMETIC_POLICY=1 -DTYPE_SAFE_ENABLE_ASSERTIONS=0 -DTYPE_SAFE_ENABLE_PRECONDITION_CHECKS=1 -DTYPE_SAFE_ENABLE_WRAPPER=1 -Dcppast_EXPORTS -I/home/cnihelton/Dev/2_Tools/standardese/external/cppast/src/../include -I/home/cnihelton/Dev/2_Tools/standardese/external/cppast/src/SYSTEM -I/home/cnihelton/Dev/2_Tools/standardese/build/_deps/type_safe-src/include -I/home/cnihelton/Dev/2_Tools/standardese/build/_deps/type_safe-src/external/debug_assert -I/home/cnihelton/Dev/2_Tools/standardese/external/cppast/external/tpl -I/usr/lib/llvm-15/include -frecord-gcc-switches -O2 -g -DNDEBUG -fPIC -pedantic-errors -Werror -Wall -Wextra -Wconversion -Wsign-conversion -Wno-noexcept-type -MD -MT external/cppast/src/CMakeFiles/cppast.dir/libclang/libclang_parser.cpp.o -MF external/cppast/src/CMakeFiles/cppast.dir/libclang/libclang_parser.cpp.o.d -o external/cppast/src/CMakeFiles/cppast.dir/libclang/libclang_parser.cpp.o -c /home/cnihelton/Dev/2_Tools/standardese/external/cppast/src/libclang/libclang_parser.cpp
In file included from /home/cnihelton/Dev/2_Tools/standardese/external/cppast/src/libclang/cxtokenizer.hpp:14,
                 from /home/cnihelton/Dev/2_Tools/standardese/external/cppast/src/libclang/libclang_parser.cpp:14:
In member function ‘char cppast::detail::cxstring::operator[](std::size_t) const’,
    inlined from ‘std::string {anonymous}::get_full_path(const cppast::detail::cxstring&, const std::string&)’ at /home/cnihelton/Dev/2_Tools/standardese/external/cppast/src/libclang/libclang_parser.cpp:122:34:
/home/cnihelton/Dev/2_Tools/standardese/external/cppast/src/libclang/raii_wrapper.hpp:125:29: error: array subscript -1 is outside array bounds of ‘const char [1]’ [-Werror=array-bounds]
  125 |             return c_str()[i];
      |                             ^
cc1plus: all warnings being treated as errors
[53/83] Building CXX object src/CMakeFiles/standardese.dir/comment/config.cpp.o
ninja: build stopped: subcommand failed.