libclang crash in libcxx
My KDevelop is unusable for a long time with any C++ project. I finally found time to dive in and here's what I found.
Things seems to start being wrong on
#4 0x0000000826e511d3 in clang::FieldDecl::getBitWidthValue (this=<optimized out>, Ctx=...) at /wrkdirs/usr/ports/devel/llvm15/work-default/llvm-project-15.0.7.src/clang/lib/AST/Decl.cpp:4283
4283 return getBitWidth()->EvaluateKnownConstInt(Ctx).getZExtValue();
(gdb) print Ctx
$27 = (const clang::ASTContext &) <error reading variable: Cannot access memory at address 0x76098f8>
Going up one frame I get
#5 0x00000008261aea78 in (anonymous namespace)::Visitor::setDeclData<(CXCursorKind)6> (this=0x7fffdbbde0b0, cursor=..., decl=0x84d614930) at /wrkdirs/usr/ports/devel/kdevelop/work/kdevelop-23.08.1/plugins/clang/duchain/builder.cpp:1157
1157 decl->setBitWidth(clang_getFieldDeclBitWidth(cursor));
(gdb) print decl->toString()
$28 = "<notype> __cap_"
The <notype>
looks strange. Maybe this is what cause a crash?
Dumping the libclang's CXCursor
reveals the part it chokes on:
(gdb) print ((char*)(*(&clang_getCursorPrettyPrinted))(cursor, 0))
$18 = 0x84d626780 "std::basic_string::size_type __cap_ : sizeof(std::basic_string::size_type) * 8 - 1"
It corresponds to the following part of the <string>
libc++ header: https://github.com/llvm/llvm-project/blob/release/15.x/libcxx/include/string#L743
The important thing about my setup is that KDevelop uses libclang that comes from LLVM 15 while the system ships libc++ headers from LLVM 16. While this also may cause some subtle issues, the code I linked seems to be the same for both 15 and 16 versions.
If anyone have an idea how to debug this further it'd be really appreciated.