title: How do I get the mangled name of a NamedDecl in Clang?
body: <p>I am using Clang to parse some C++ code. I would like to print the name and mangled name for every <code>FunctionDecl</code> that I encounter. </p>
  
  <p>I can print the function name fairly easily by adding this to my <code>RecursiveASTVisitor</code>: </p>
  
  <pre><code>bool VisitFunctionDecl(FunctionDecl* f) {
  
    auto declName = f-&gt;getNameInfo().getName();
    auto functionName = declName.getAsString();
  
    std::cout &lt;&lt; functionName &lt;&lt; std::endl;
  
    return true;
  }
  </code></pre>
  
  <p>How can I also print the mangled name? </p>
  
  <hr>
  
  <p>Working code I produced after following Sebastian's pointers: </p>
  
  <pre><code>const auto getMangledName = [&amp;](FunctionDecl* decl) {
  
    auto mangleContext = context.createMangleContext();
  
    if (!mangleContext-&gt;shouldMangleDeclName(decl)) {
      return decl-&gt;getNameInfo().getName().getAsString();
    }
  
    std::string mangledName;
    llvm::raw_string_ostream ostream(mangledName);
  
    mangleContext-&gt;mangleName(decl, ostream);
  
    ostream.flush();
  
    delete mangleContext;
  
    return mangledName;
  };
  </code></pre>