c++clangllvmlibclang

Using libclang, is there a way to know if a closure type was introduced in the context of the initialization of a inline variable?


Given the following C++ code:

inline auto v = []{ return 1; }();

The context of the closure type is the initializer of an inline variable. This leads to a special mangling rule in the Itanium ABI (mentioned here).

When libclang visit the CXXRecordDecl corresponding to this closure type, is there a way to know if we are in this special context?


Solution

  • Turns out libtooling has a special function CXXRecordDecl::getLambdaContextDecl, which can be used to solve the problem:

    if (decl->isLambda()) {
        const clang::Decl *context = getLambdaContextDecl(decl);
        if (const auto *varDecl =
                llvm::dyn_cast_or_null<const clang::VarDecl>(context))
        {
            if (varDecl->isInline()) [...]