clang11 Segfaulting on name mangling

Viewed 41

I have a clang-tooling application that traverses an AST and prints it out with mangled names. It works fine in clang10, but after the (simple) port to clang11 it is segfaulting and I can't pinpoint the issue. From the backtrace, it seems that I've somehow managed to get an AST node that isn't castable.

#0  getKind () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/clang/include/clang/AST/DeclBase.h:433
#1  classof () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/clang/include/clang/AST/Decl.h:4242
#2  doit () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/llvm/include/llvm/Support/Casting.h:58
#3  doit () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/llvm/include/llvm/Support/Casting.h:105
#4  doit () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/llvm/include/llvm/Support/Casting.h:131
#5  doit () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/llvm/include/llvm/Support/Casting.h:121
#6  isa<clang::BlockDecl, clang::Decl const*> () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/llvm/include/llvm/Support/Casting.h:142
#7  dyn_cast<clang::BlockDecl, clang::Decl const> () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/llvm/include/llvm/Support/Casting.h:345
#8  getEffectiveDeclContext () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/clang/lib/AST/ItaniumMangle.cpp:59
#9  0x00007fb7ea08865a in mangleNameWithAbiTags () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/clang/lib/AST/ItaniumMangle.cpp:867
#10 0x00007fb7ea087b1b in mangleName () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/clang/lib/AST/ItaniumMangle.cpp:855
#11 0x00007fb7ea084374 in mangleCXXName () at /build/llvm-toolchain-11-11.0.1~++20201218093139+43ff75f2c3fe/clang/lib/AST/ItaniumMangle.cpp:5144
...my application code...

The code that I'm running is

        mangleContext_->mangleName(decl, llvm::errs());

where decl is a const NamedDecl* that corresponds to the following union:

union U { int x; int y; };

and I'm effectively trying to print the mangling of U (I'm expecting _Z1U).

I am constructing the mangle context using the following code.

ClangPrinter::ClangPrinter(clang::CompilerInstance *compiler,
                           clang::ASTContext *context)
    : compiler_(compiler), context_(context) {
    mangleContext_ = ItaniumMangleContext::create(*context, compiler->getDiagnostics());
}

Does anyone have any suggestions as to where to look to debug this issue? My guess is that I'm somehow violating an invariant of the underlying system, but since I'm only consuming the AST in a read-only manner, I don't understand how I could end up with an uncastable object.

0 Answers
Related