r260858 - [index] Factor libclang's functionality to determing the mangled name of symbols into the clangIndex library.

Argyrios Kyrtzidis via cfe-commits cfe-commits at lists.llvm.org
Sun Feb 14 14:30:16 PST 2016


Author: akirtzidis
Date: Sun Feb 14 16:30:14 2016
New Revision: 260858

URL: http://llvm.org/viewvc/llvm-project?rev=260858&view=rev
Log:
[index] Factor libclang's functionality to determing the mangled name of symbols into the clangIndex library.

Added:
    cfe/trunk/include/clang/Index/CodegenNameGenerator.h
    cfe/trunk/lib/Index/CodegenNameGenerator.cpp
Modified:
    cfe/trunk/include/clang/AST/Mangle.h
    cfe/trunk/include/clang/Index/IndexDataConsumer.h
    cfe/trunk/lib/AST/Mangle.cpp
    cfe/trunk/lib/Index/CMakeLists.txt
    cfe/trunk/lib/Index/IndexingAction.cpp
    cfe/trunk/test/Index/Core/index-source.m
    cfe/trunk/tools/c-index-test/core_main.cpp
    cfe/trunk/tools/libclang/CIndex.cpp

Modified: cfe/trunk/include/clang/AST/Mangle.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Mangle.h?rev=260858&r1=260857&r2=260858&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Mangle.h (original)
+++ cfe/trunk/include/clang/AST/Mangle.h Sun Feb 14 16:30:14 2016
@@ -124,6 +124,7 @@ public:
   void mangleBlock(const DeclContext *DC, const BlockDecl *BD,
                    raw_ostream &Out);
 
+  void mangleObjCMethodNameWithoutSize(const ObjCMethodDecl *MD, raw_ostream &);
   void mangleObjCMethodName(const ObjCMethodDecl *MD, raw_ostream &);
 
   virtual void mangleStaticGuardVariable(const VarDecl *D, raw_ostream &) = 0;

Added: cfe/trunk/include/clang/Index/CodegenNameGenerator.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Index/CodegenNameGenerator.h?rev=260858&view=auto
==============================================================================
--- cfe/trunk/include/clang/Index/CodegenNameGenerator.h (added)
+++ cfe/trunk/include/clang/Index/CodegenNameGenerator.h Sun Feb 14 16:30:14 2016
@@ -0,0 +1,52 @@
+//===- CodegenNameGenerator.h - Codegen name generation -------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Determines the name that the symbol will get for code generation.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_INDEX_CODEGENNAMEGENERATOR_H
+#define LLVM_CLANG_INDEX_CODEGENNAMEGENERATOR_H
+
+#include "clang/Basic/LLVM.h"
+#include <memory>
+#include <string>
+#include <vector>
+
+namespace clang {
+  class ASTContext;
+  class Decl;
+
+namespace index {
+
+class CodegenNameGenerator {
+public:
+  explicit CodegenNameGenerator(ASTContext &Ctx);
+  ~CodegenNameGenerator();
+
+  /// \returns true on failure to produce a name for the given decl, false on
+  /// success.
+  bool writeName(const Decl *D, raw_ostream &OS);
+
+  /// Version of \c writeName function that returns a string.
+  std::string getName(const Decl *D);
+
+  /// This can return multiple mangled names when applicable, e.g. for C++
+  /// constructors/destructors.
+  std::vector<std::string> getAllManglings(const Decl *D);
+
+private:
+  struct Implementation;
+  std::unique_ptr<Implementation> Impl;
+};
+
+} // namespace index
+} // namespace clang
+
+#endif // LLVM_CLANG_INDEX_CODEGENNAMEGENERATOR_H

Modified: cfe/trunk/include/clang/Index/IndexDataConsumer.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Index/IndexDataConsumer.h?rev=260858&r1=260857&r2=260858&view=diff
==============================================================================
--- cfe/trunk/include/clang/Index/IndexDataConsumer.h (original)
+++ cfe/trunk/include/clang/Index/IndexDataConsumer.h Sun Feb 14 16:30:14 2016
@@ -13,6 +13,7 @@
 #include "clang/Index/IndexSymbol.h"
 
 namespace clang {
+  class ASTContext;
   class DeclContext;
   class Expr;
   class FileID;
@@ -33,6 +34,8 @@ public:
 
   virtual ~IndexDataConsumer() {}
 
+  virtual void initialize(ASTContext &Ctx) {}
+
   /// \returns true to continue indexing, or false to abort.
   virtual bool handleDeclOccurence(const Decl *D, SymbolRoleSet Roles,
                                    ArrayRef<SymbolRelation> Relations,

Modified: cfe/trunk/lib/AST/Mangle.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Mangle.cpp?rev=260858&r1=260857&r2=260858&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Mangle.cpp (original)
+++ cfe/trunk/lib/AST/Mangle.cpp Sun Feb 14 16:30:14 2016
@@ -254,11 +254,8 @@ void MangleContext::mangleBlock(const De
   mangleFunctionBlock(*this, Buffer, BD, Out);
 }
 
-void MangleContext::mangleObjCMethodName(const ObjCMethodDecl *MD,
-                                         raw_ostream &Out) {
-  SmallString<64> Name;
-  llvm::raw_svector_ostream OS(Name);
-  
+void MangleContext::mangleObjCMethodNameWithoutSize(const ObjCMethodDecl *MD,
+                                                    raw_ostream &OS) {
   const ObjCContainerDecl *CD =
   dyn_cast<ObjCContainerDecl>(MD->getDeclContext());
   assert (CD && "Missing container decl in GetNameForMethod");
@@ -268,6 +265,13 @@ void MangleContext::mangleObjCMethodName
   OS << ' ';
   MD->getSelector().print(OS);
   OS << ']';
-  
+}
+
+void MangleContext::mangleObjCMethodName(const ObjCMethodDecl *MD,
+                                         raw_ostream &Out) {
+  SmallString<64> Name;
+  llvm::raw_svector_ostream OS(Name);
+
+  mangleObjCMethodNameWithoutSize(MD, OS);
   Out << OS.str().size() << OS.str();
 }

Modified: cfe/trunk/lib/Index/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Index/CMakeLists.txt?rev=260858&r1=260857&r2=260858&view=diff
==============================================================================
--- cfe/trunk/lib/Index/CMakeLists.txt (original)
+++ cfe/trunk/lib/Index/CMakeLists.txt Sun Feb 14 16:30:14 2016
@@ -3,14 +3,15 @@ set(LLVM_LINK_COMPONENTS
   )
 
 add_clang_library(clangIndex
+  CodegenNameGenerator.cpp
   CommentToXML.cpp
-  USRGeneration.cpp
   IndexBody.cpp
   IndexDecl.cpp
   IndexingAction.cpp
   IndexingContext.cpp
   IndexSymbol.cpp
   IndexTypeSourceInfo.cpp
+  USRGeneration.cpp
 
   ADDITIONAL_HEADERS
   IndexingContext.h

Added: cfe/trunk/lib/Index/CodegenNameGenerator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Index/CodegenNameGenerator.cpp?rev=260858&view=auto
==============================================================================
--- cfe/trunk/lib/Index/CodegenNameGenerator.cpp (added)
+++ cfe/trunk/lib/Index/CodegenNameGenerator.cpp Sun Feb 14 16:30:14 2016
@@ -0,0 +1,197 @@
+//===- CodegenNameGenerator.cpp - Codegen name generation -----------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Determines the name that the symbol will get for code generation.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Index/CodegenNameGenerator.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/AST/DeclCXX.h"
+#include "clang/AST/DeclObjC.h"
+#include "clang/AST/Mangle.h"
+#include "clang/AST/VTableBuilder.h"
+#include "clang/Basic/TargetInfo.h"
+#include "llvm/IR/DataLayout.h"
+#include "llvm/IR/Mangler.h"
+#include "llvm/Support/raw_ostream.h"
+
+using namespace clang;
+using namespace clang::index;
+
+struct CodegenNameGenerator::Implementation {
+  std::unique_ptr<MangleContext> MC;
+  llvm::DataLayout DL;
+
+  Implementation(ASTContext &Ctx)
+    : MC(Ctx.createMangleContext()),
+      DL(Ctx.getTargetInfo().getDataLayoutString()) {}
+
+  bool writeName(const Decl *D, raw_ostream &OS) {
+    // First apply frontend mangling.
+    SmallString<128> FrontendBuf;
+    llvm::raw_svector_ostream FrontendBufOS(FrontendBuf);
+    if (auto *FD = dyn_cast<FunctionDecl>(D)) {
+      if (FD->isDependentContext())
+        return true;
+      if (writeFuncOrVarName(FD, FrontendBufOS))
+        return true;
+    } else if (auto *VD = dyn_cast<VarDecl>(D)) {
+      if (writeFuncOrVarName(VD, FrontendBufOS))
+        return true;
+    } else if (auto *MD = dyn_cast<ObjCMethodDecl>(D)) {
+      MC->mangleObjCMethodNameWithoutSize(MD, OS);
+      return false;
+    } else if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
+      writeObjCClassName(ID, FrontendBufOS);
+    } else {
+      return true;
+    }
+
+    // Now apply backend mangling.
+    llvm::Mangler::getNameWithPrefix(OS, FrontendBufOS.str(), DL);
+    return false;
+  }
+
+  std::string getName(const Decl *D) {
+    std::string Name;
+    {
+      llvm::raw_string_ostream OS(Name);
+      writeName(D, OS);
+    }
+    return Name;
+  }
+
+  std::vector<std::string> getAllManglings(const Decl *D) {
+    if (!(isa<CXXRecordDecl>(D) || isa<CXXMethodDecl>(D)))
+      return {};
+
+    const NamedDecl *ND = cast<NamedDecl>(D);
+
+    ASTContext &Ctx = ND->getASTContext();
+    std::unique_ptr<MangleContext> M(Ctx.createMangleContext());
+    std::unique_ptr<llvm::DataLayout> DL(
+        new llvm::DataLayout(Ctx.getTargetInfo().getDataLayoutString()));
+
+    std::vector<std::string> Manglings;
+
+    auto hasDefaultCXXMethodCC = [](ASTContext &C, const CXXMethodDecl *MD) {
+      auto DefaultCC = C.getDefaultCallingConvention(/*IsVariadic=*/false,
+                                                     /*IsCSSMethod=*/true);
+      auto CC = MD->getType()->getAs<FunctionProtoType>()->getCallConv();
+      return CC == DefaultCC;
+    };
+
+    if (const auto *CD = dyn_cast_or_null<CXXConstructorDecl>(ND)) {
+      Manglings.emplace_back(getMangledStructor(CD, Ctor_Base));
+
+      if (Ctx.getTargetInfo().getCXXABI().isItaniumFamily())
+        if (!CD->getParent()->isAbstract())
+          Manglings.emplace_back(getMangledStructor(CD, Ctor_Complete));
+
+      if (Ctx.getTargetInfo().getCXXABI().isMicrosoft())
+        if (CD->hasAttr<DLLExportAttr>() && CD->isDefaultConstructor())
+          if (!(hasDefaultCXXMethodCC(Ctx, CD) && CD->getNumParams() == 0))
+            Manglings.emplace_back(getMangledStructor(CD, Ctor_DefaultClosure));
+    } else if (const auto *DD = dyn_cast_or_null<CXXDestructorDecl>(ND)) {
+      Manglings.emplace_back(getMangledStructor(DD, Dtor_Base));
+      if (Ctx.getTargetInfo().getCXXABI().isItaniumFamily()) {
+        Manglings.emplace_back(getMangledStructor(DD, Dtor_Complete));
+        if (DD->isVirtual())
+          Manglings.emplace_back(getMangledStructor(DD, Dtor_Deleting));
+      }
+    } else if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(ND)) {
+      Manglings.emplace_back(getName(ND));
+      if (MD->isVirtual())
+        if (const auto *TIV = Ctx.getVTableContext()->getThunkInfo(MD))
+          for (const auto &T : *TIV)
+            Manglings.emplace_back(getMangledThunk(MD, T));
+    }
+
+    return Manglings;
+  }
+
+private:
+  bool writeFuncOrVarName(const NamedDecl *D, raw_ostream &OS) {
+    if (MC->shouldMangleDeclName(D)) {
+      if (const auto *CtorD = dyn_cast<CXXConstructorDecl>(D))
+        MC->mangleCXXCtor(CtorD, Ctor_Complete, OS);
+      else if (const auto *DtorD = dyn_cast<CXXDestructorDecl>(D))
+        MC->mangleCXXDtor(DtorD, Dtor_Complete, OS);
+      else
+        MC->mangleName(D, OS);
+      return false;
+    } else {
+      IdentifierInfo *II = D->getIdentifier();
+      if (!II)
+        return true;
+      OS << II->getName();
+      return false;
+    }
+  }
+
+  void writeObjCClassName(const ObjCInterfaceDecl *D, raw_ostream &OS) {
+    OS << getClassSymbolPrefix();
+    OS << D->getObjCRuntimeNameAsString();
+  }
+
+  static StringRef getClassSymbolPrefix() {
+    return "OBJC_CLASS_$_";
+  }
+
+  std::string getMangledStructor(const NamedDecl *ND, unsigned StructorType) {
+    std::string FrontendBuf;
+    llvm::raw_string_ostream FOS(FrontendBuf);
+
+    if (const auto *CD = dyn_cast_or_null<CXXConstructorDecl>(ND))
+      MC->mangleCXXCtor(CD, static_cast<CXXCtorType>(StructorType), FOS);
+    else if (const auto *DD = dyn_cast_or_null<CXXDestructorDecl>(ND))
+      MC->mangleCXXDtor(DD, static_cast<CXXDtorType>(StructorType), FOS);
+
+    std::string BackendBuf;
+    llvm::raw_string_ostream BOS(BackendBuf);
+
+    llvm::Mangler::getNameWithPrefix(BOS, FOS.str(), DL);
+
+    return BOS.str();
+  }
+
+  std::string getMangledThunk(const CXXMethodDecl *MD, const ThunkInfo &T) {
+    std::string FrontendBuf;
+    llvm::raw_string_ostream FOS(FrontendBuf);
+
+    MC->mangleThunk(MD, T, FOS);
+
+    std::string BackendBuf;
+    llvm::raw_string_ostream BOS(BackendBuf);
+
+    llvm::Mangler::getNameWithPrefix(BOS, FOS.str(), DL);
+
+    return BOS.str();
+  }
+};
+
+CodegenNameGenerator::CodegenNameGenerator(ASTContext &Ctx)
+  : Impl(new Implementation(Ctx)) {
+}
+
+CodegenNameGenerator::~CodegenNameGenerator() {
+}
+
+bool CodegenNameGenerator::writeName(const Decl *D, raw_ostream &OS) {
+  return Impl->writeName(D, OS);
+}
+
+std::string CodegenNameGenerator::getName(const Decl *D) {
+  return Impl->getName(D);
+}
+
+std::vector<std::string> CodegenNameGenerator::getAllManglings(const Decl *D) {
+  return Impl->getAllManglings(D);
+}

Modified: cfe/trunk/lib/Index/IndexingAction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Index/IndexingAction.cpp?rev=260858&r1=260857&r2=260858&view=diff
==============================================================================
--- cfe/trunk/lib/Index/IndexingAction.cpp (original)
+++ cfe/trunk/lib/Index/IndexingAction.cpp Sun Feb 14 16:30:14 2016
@@ -50,6 +50,7 @@ public:
 protected:
   void Initialize(ASTContext &Context) override {
     IndexCtx.setASTContext(Context);
+    IndexCtx.getDataConsumer().initialize(Context);
   }
 
   bool HandleTopLevelDecl(DeclGroupRef DG) override {
@@ -170,5 +171,6 @@ void index::indexASTUnit(ASTUnit &Unit,
                          IndexingOptions Opts) {
   IndexingContext IndexCtx(Opts, *DataConsumer);
   IndexCtx.setASTContext(Unit.getASTContext());
+  DataConsumer->initialize(Unit.getASTContext());
   indexTranslationUnit(Unit, IndexCtx);
 }

Modified: cfe/trunk/test/Index/Core/index-source.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/Core/index-source.m?rev=260858&r1=260857&r2=260858&view=diff
==============================================================================
--- cfe/trunk/test/Index/Core/index-source.m (original)
+++ cfe/trunk/test/Index/Core/index-source.m Sun Feb 14 16:30:14 2016
@@ -1,8 +1,8 @@
 // RUN: c-index-test core -print-source-symbols -- %s | FileCheck %s
 
 @interface Base
-// CHECK: [[@LINE-1]]:12 | objc-class/ObjC | Base | c:objc(cs)Base | Decl | rel: 0
+// CHECK: [[@LINE-1]]:12 | objc-class/ObjC | Base | c:objc(cs)Base | _OBJC_CLASS_$_Base | Decl | rel: 0
 -(void)meth;
-// CHECK: [[@LINE-1]]:1 | objc-instance-method/ObjC | meth | c:objc(cs)Base(im)meth | Decl/Dyn/RelChild | rel: 1
+// CHECK: [[@LINE-1]]:1 | objc-instance-method/ObjC | meth | c:objc(cs)Base(im)meth | -[Base meth] | Decl/Dyn/RelChild | rel: 1
 // CHECK-NEXT: RelChild | Base | c:objc(cs)Base
 @end

Modified: cfe/trunk/tools/c-index-test/core_main.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/c-index-test/core_main.cpp?rev=260858&r1=260857&r2=260858&view=diff
==============================================================================
--- cfe/trunk/tools/c-index-test/core_main.cpp (original)
+++ cfe/trunk/tools/c-index-test/core_main.cpp Sun Feb 14 16:30:14 2016
@@ -14,6 +14,7 @@
 #include "clang/Index/IndexingAction.h"
 #include "clang/Index/IndexDataConsumer.h"
 #include "clang/Index/USRGeneration.h"
+#include "clang/Index/CodegenNameGenerator.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Signals.h"
 #include "llvm/Support/raw_ostream.h"
@@ -60,11 +61,16 @@ namespace {
 
 class PrintIndexDataConsumer : public IndexDataConsumer {
   raw_ostream &OS;
+  std::unique_ptr<CodegenNameGenerator> CGNameGen;
 
 public:
   PrintIndexDataConsumer(raw_ostream &OS) : OS(OS) {
   }
 
+  void initialize(ASTContext &Ctx) override {
+    CGNameGen.reset(new CodegenNameGenerator(Ctx));
+  }
+
   bool handleDeclOccurence(const Decl *D, SymbolRoleSet Roles,
                            ArrayRef<SymbolRelation> Relations,
                            FileID FID, unsigned Offset,
@@ -82,6 +88,10 @@ public:
     printSymbolNameAndUSR(D, Ctx, OS);
     OS << " | ";
 
+    if (CGNameGen->writeName(D, OS))
+      OS << "<no-cgname>";
+    OS << " | ";
+
     printSymbolRoles(Roles, OS);
     OS << " | ";
 

Modified: cfe/trunk/tools/libclang/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndex.cpp?rev=260858&r1=260857&r2=260858&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndex.cpp (original)
+++ cfe/trunk/tools/libclang/CIndex.cpp Sun Feb 14 16:30:14 2016
@@ -22,17 +22,15 @@
 #include "CXType.h"
 #include "CursorVisitor.h"
 #include "clang/AST/Attr.h"
-#include "clang/AST/Mangle.h"
 #include "clang/AST/StmtVisitor.h"
-#include "clang/AST/VTableBuilder.h"
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/DiagnosticCategories.h"
 #include "clang/Basic/DiagnosticIDs.h"
-#include "clang/Basic/TargetInfo.h"
 #include "clang/Basic/Version.h"
 #include "clang/Frontend/ASTUnit.h"
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Frontend/FrontendDiagnostic.h"
+#include "clang/Index/CodegenNameGenerator.h"
 #include "clang/Index/CommentToXML.h"
 #include "clang/Lex/HeaderSearch.h"
 #include "clang/Lex/Lexer.h"
@@ -43,8 +41,6 @@
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/StringSwitch.h"
 #include "llvm/Config/llvm-config.h"
-#include "llvm/IR/DataLayout.h"
-#include "llvm/IR/Mangler.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/CrashRecoveryContext.h"
 #include "llvm/Support/Format.h"
@@ -3976,59 +3972,6 @@ static SourceLocation getLocationFromExp
   return E->getLocStart();
 }
 
-static std::string getMangledStructor(std::unique_ptr<MangleContext> &M,
-                                      std::unique_ptr<llvm::DataLayout> &DL,
-                                      const NamedDecl *ND,
-                                      unsigned StructorType) {
-  std::string FrontendBuf;
-  llvm::raw_string_ostream FOS(FrontendBuf);
-
-  if (const auto *CD = dyn_cast_or_null<CXXConstructorDecl>(ND))
-    M->mangleCXXCtor(CD, static_cast<CXXCtorType>(StructorType), FOS);
-  else if (const auto *DD = dyn_cast_or_null<CXXDestructorDecl>(ND))
-    M->mangleCXXDtor(DD, static_cast<CXXDtorType>(StructorType), FOS);
-
-  std::string BackendBuf;
-  llvm::raw_string_ostream BOS(BackendBuf);
-
-  llvm::Mangler::getNameWithPrefix(BOS, llvm::Twine(FOS.str()), *DL);
-
-  return BOS.str();
-}
-
-static std::string getMangledName(std::unique_ptr<MangleContext> &M,
-                                  std::unique_ptr<llvm::DataLayout> &DL,
-                                  const NamedDecl *ND) {
-  std::string FrontendBuf;
-  llvm::raw_string_ostream FOS(FrontendBuf);
-
-  M->mangleName(ND, FOS);
-
-  std::string BackendBuf;
-  llvm::raw_string_ostream BOS(BackendBuf);
-
-  llvm::Mangler::getNameWithPrefix(BOS, llvm::Twine(FOS.str()), *DL);
-
-  return BOS.str();
-}
-
-static std::string getMangledThunk(std::unique_ptr<MangleContext> &M,
-                                   std::unique_ptr<llvm::DataLayout> &DL,
-                                   const CXXMethodDecl *MD,
-                                   const ThunkInfo &T) {
-  std::string FrontendBuf;
-  llvm::raw_string_ostream FOS(FrontendBuf);
-
-  M->mangleThunk(MD, T, FOS);
-
-  std::string BackendBuf;
-  llvm::raw_string_ostream BOS(BackendBuf);
-
-  llvm::Mangler::getNameWithPrefix(BOS, llvm::Twine(FOS.str()), *DL);
-
-  return BOS.str();
-}
-
 extern "C" {
 
 unsigned clang_visitChildren(CXCursor parent,
@@ -4377,29 +4320,9 @@ CXString clang_Cursor_getMangling(CXCurs
   if (!D || !(isa<FunctionDecl>(D) || isa<VarDecl>(D)))
     return cxstring::createEmpty();
 
-  // First apply frontend mangling.
-  const NamedDecl *ND = cast<NamedDecl>(D);
-  ASTContext &Ctx = ND->getASTContext();
-  std::unique_ptr<MangleContext> MC(Ctx.createMangleContext());
-
-  std::string FrontendBuf;
-  llvm::raw_string_ostream FrontendBufOS(FrontendBuf);
-  if (MC->shouldMangleDeclName(ND)) {
-    MC->mangleName(ND, FrontendBufOS);
-  } else {
-    ND->printName(FrontendBufOS);
-  }
-
-  // Now apply backend mangling.
-  std::unique_ptr<llvm::DataLayout> DL(
-      new llvm::DataLayout(Ctx.getTargetInfo().getDataLayoutString()));
-
-  std::string FinalBuf;
-  llvm::raw_string_ostream FinalBufOS(FinalBuf);
-  llvm::Mangler::getNameWithPrefix(FinalBufOS, llvm::Twine(FrontendBufOS.str()),
-                                   *DL);
-
-  return cxstring::createDup(FinalBufOS.str());
+  ASTContext &Ctx = D->getASTContext();
+  index::CodegenNameGenerator CGNameGen(Ctx);
+  return cxstring::createDup(CGNameGen.getName(D));
 }
 
 CXStringSet *clang_Cursor_getCXXManglings(CXCursor C) {
@@ -4410,49 +4333,9 @@ CXStringSet *clang_Cursor_getCXXMangling
   if (!(isa<CXXRecordDecl>(D) || isa<CXXMethodDecl>(D)))
     return nullptr;
 
-  const NamedDecl *ND = cast<NamedDecl>(D);
-
-  ASTContext &Ctx = ND->getASTContext();
-  std::unique_ptr<MangleContext> M(Ctx.createMangleContext());
-  std::unique_ptr<llvm::DataLayout> DL(
-      new llvm::DataLayout(Ctx.getTargetInfo().getDataLayoutString()));
-
-  std::vector<std::string> Manglings;
-
-  auto hasDefaultCXXMethodCC = [](ASTContext &C, const CXXMethodDecl *MD) {
-    auto DefaultCC = C.getDefaultCallingConvention(/*IsVariadic=*/false,
-                                                   /*IsCSSMethod=*/true);
-    auto CC = MD->getType()->getAs<FunctionProtoType>()->getCallConv();
-    return CC == DefaultCC;
-  };
-
-  if (const auto *CD = dyn_cast_or_null<CXXConstructorDecl>(ND)) {
-    Manglings.emplace_back(getMangledStructor(M, DL, CD, Ctor_Base));
-
-    if (Ctx.getTargetInfo().getCXXABI().isItaniumFamily())
-      if (!CD->getParent()->isAbstract())
-        Manglings.emplace_back(getMangledStructor(M, DL, CD, Ctor_Complete));
-
-    if (Ctx.getTargetInfo().getCXXABI().isMicrosoft())
-      if (CD->hasAttr<DLLExportAttr>() && CD->isDefaultConstructor())
-        if (!(hasDefaultCXXMethodCC(Ctx, CD) && CD->getNumParams() == 0))
-          Manglings.emplace_back(getMangledStructor(M, DL, CD,
-                                                    Ctor_DefaultClosure));
-  } else if (const auto *DD = dyn_cast_or_null<CXXDestructorDecl>(ND)) {
-    Manglings.emplace_back(getMangledStructor(M, DL, DD, Dtor_Base));
-    if (Ctx.getTargetInfo().getCXXABI().isItaniumFamily()) {
-      Manglings.emplace_back(getMangledStructor(M, DL, DD, Dtor_Complete));
-      if (DD->isVirtual())
-        Manglings.emplace_back(getMangledStructor(M, DL, DD, Dtor_Deleting));
-    }
-  } else if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(ND)) {
-    Manglings.emplace_back(getMangledName(M, DL, ND));
-    if (MD->isVirtual())
-      if (const auto *TIV = Ctx.getVTableContext()->getThunkInfo(MD))
-        for (const auto &T : *TIV)
-          Manglings.emplace_back(getMangledThunk(M, DL, MD, T));
-  }
-
+  ASTContext &Ctx = D->getASTContext();
+  index::CodegenNameGenerator CGNameGen(Ctx);
+  std::vector<std::string> Manglings = CGNameGen.getAllManglings(D);
   return cxstring::createSet(Manglings);
 }
 




More information about the cfe-commits mailing list