1

I am writing a llvm pass where I need the base name of a function, for example,

mangled name is ZSt3minIfERKT_S2_S2

demangled name is float const& std::min(float const&, float const&)

but I need the base name which is min

I have seen some llvm API defined in -

#include "llvm/Demangle/Demangle.h"
#include <cxxabi.h>
#include "llvm/Demangle/ItaniumDemangle.h"

but I was not able to properly use that. Any help is appreciated

  • 1
    Does this answer your question? [Unmangling the result of std::type\_info::name](https://stackoverflow.com/questions/281818/unmangling-the-result-of-stdtype-infoname). That specific question is about type names, but it contains a lot of info that should make it possible to do the same for a function. – AVH Mar 25 '21 at 16:17
  • 1
    Also, have you tried the `__func__` or `__FUNCTION__` macros? Are they not sufficient? – AVH Mar 25 '21 at 16:19
  • I saw that already, didnt find anything helpful – middle-mood Mar 25 '21 at 17:06

1 Answers1

2

Clang's ItaniumPartialDemangler is capable of doing this assuming your name was mangled using the Itanium ABI. https://llvm.org/doxygen/Demangle_8h.html

The ItaniumPartialMangler is able to retrieve properties from a mangled name, similar to an AST node.

I believe the function you're looking for in particular is https://llvm.org/doxygen/structllvm_1_1ItaniumPartialDemangler.html#a2effd15853bfffbeec4d08451f1fa81c

An example snippet demonstrating usage of this function:

#include "llvm/Demangle/Demangle.h"
#include "llvm/Support/raw_ostream.h"

int main() {
    const char* mangled = "_ZSt3minRKfS0_";
    size_t Size = 1;
    char *Buf = static_cast<char *>(std::malloc(Size));

    llvm::ItaniumPartialDemangler Mangler;
    if (Mangler.partialDemangle(mangled)) {
        llvm::errs() << "Failed to demangle name";
        return 1;
    }

    char* Result = Mangler.getFunctionBaseName(Buf, &Size);

    llvm::outs() << "Result: " << Result << "\n";
}
Mats Jun
  • 351
  • 2
  • 8
  • yes, that's the function, but I was not able to call it properly, also didn't find any reference use in llvm. Do you have any sample code that uses that getFunctionBaseName function? – middle-mood Mar 25 '21 at 17:04
  • I have updated the answer with a code example. – Mats Jun Mar 25 '21 at 17:52