[PATCH] D43547: [Indexing] Fixing inconsistencies between FUNCDNAME and generated code by improving ASTContext's API for MangleContext

2018-02-22 Thread Michael Haidl via Phabricator via cfe-commits
pacxx updated this revision to Diff 135372.
pacxx retitled this revision from "[NameMangling] Make ASTContext owning the 
ManglingContext during entire compilation" to "[Indexing] Fixing 
inconsistencies between FUNCDNAME and generated code by improving ASTContext's 
API for MangleContext".
pacxx edited the summary of this revision.
pacxx added a comment.

Refactored as suggested.


https://reviews.llvm.org/D43547

Files:
  include/clang/AST/ASTContext.h
  lib/AST/ASTContext.cpp
  lib/AST/Expr.cpp
  lib/CodeGen/CGCXXABI.h
  lib/Index/CodegenNameGenerator.cpp

Index: lib/Index/CodegenNameGenerator.cpp
===
--- lib/Index/CodegenNameGenerator.cpp
+++ lib/Index/CodegenNameGenerator.cpp
@@ -26,11 +26,11 @@
 using namespace clang::index;
 
 struct CodegenNameGenerator::Implementation {
-  std::unique_ptr MC;
+  MangleContext& MC;
   llvm::DataLayout DL;
 
   Implementation(ASTContext )
-: MC(Ctx.createMangleContext()),
+: MC(Ctx.getMangleContext()),
   DL(Ctx.getTargetInfo().getDataLayout()) {}
 
   bool writeName(const Decl *D, raw_ostream ) {
@@ -46,7 +46,7 @@
   if (writeFuncOrVarName(VD, FrontendBufOS))
 return true;
 } else if (auto *MD = dyn_cast(D)) {
-  MC->mangleObjCMethodNameWithoutSize(MD, OS);
+  MC.mangleObjCMethodNameWithoutSize(MD, OS);
   return false;
 } else if (auto *ID = dyn_cast(D)) {
   writeObjCClassName(ID, FrontendBufOS);
@@ -106,7 +106,6 @@
 const NamedDecl *ND = cast(D);
 
 ASTContext  = ND->getASTContext();
-std::unique_ptr M(Ctx.createMangleContext());
 
 std::vector Manglings;
 
@@ -148,13 +147,13 @@
 
 private:
   bool writeFuncOrVarName(const NamedDecl *D, raw_ostream ) {
-if (MC->shouldMangleDeclName(D)) {
+if (MC.shouldMangleDeclName(D)) {
   if (const auto *CtorD = dyn_cast(D))
-MC->mangleCXXCtor(CtorD, Ctor_Complete, OS);
+MC.mangleCXXCtor(CtorD, Ctor_Complete, OS);
   else if (const auto *DtorD = dyn_cast(D))
-MC->mangleCXXDtor(DtorD, Dtor_Complete, OS);
+MC.mangleCXXDtor(DtorD, Dtor_Complete, OS);
   else
-MC->mangleName(D, OS);
+MC.mangleName(D, OS);
   return false;
 } else {
   IdentifierInfo *II = D->getIdentifier();
@@ -181,9 +180,9 @@
 llvm::raw_string_ostream FOS(FrontendBuf);
 
 if (const auto *CD = dyn_cast_or_null(ND))
-  MC->mangleCXXCtor(CD, static_cast(StructorType), FOS);
+  MC.mangleCXXCtor(CD, static_cast(StructorType), FOS);
 else if (const auto *DD = dyn_cast_or_null(ND))
-  MC->mangleCXXDtor(DD, static_cast(StructorType), FOS);
+  MC.mangleCXXDtor(DD, static_cast(StructorType), FOS);
 
 std::string BackendBuf;
 llvm::raw_string_ostream BOS(BackendBuf);
@@ -197,7 +196,7 @@
 std::string FrontendBuf;
 llvm::raw_string_ostream FOS(FrontendBuf);
 
-MC->mangleThunk(MD, T, FOS);
+MC.mangleThunk(MD, T, FOS);
 
 std::string BackendBuf;
 llvm::raw_string_ostream BOS(BackendBuf);
Index: lib/CodeGen/CGCXXABI.h
===
--- lib/CodeGen/CGCXXABI.h
+++ lib/CodeGen/CGCXXABI.h
@@ -44,10 +44,10 @@
 class CGCXXABI {
 protected:
   CodeGenModule 
-  std::unique_ptr MangleCtx;
+  MangleContext 
 
   CGCXXABI(CodeGenModule )
-: CGM(CGM), MangleCtx(CGM.getContext().createMangleContext()) {}
+: CGM(CGM), MangleCtx(CGM.getContext().getMangleContext()) {}
 
 protected:
   ImplicitParamDecl *getThisDecl(CodeGenFunction ) {
@@ -95,7 +95,7 @@
 
   /// Gets the mangle context.
   MangleContext () {
-return *MangleCtx;
+return MangleCtx;
   }
 
   /// Returns true if the given constructor or destructor is one of the
Index: lib/AST/Expr.cpp
===
--- lib/AST/Expr.cpp
+++ lib/AST/Expr.cpp
@@ -497,18 +497,17 @@
 
   if (IT == PredefinedExpr::FuncDName) {
 if (const NamedDecl *ND = dyn_cast(CurrentDecl)) {
-  std::unique_ptr MC;
-  MC.reset(Context.createMangleContext());
+  MangleContext& MC = Context.getMangleContext();
 
-  if (MC->shouldMangleDeclName(ND)) {
+  if (MC.shouldMangleDeclName(ND)) {
 SmallString<256> Buffer;
 llvm::raw_svector_ostream Out(Buffer);
 if (const CXXConstructorDecl *CD = dyn_cast(ND))
-  MC->mangleCXXCtor(CD, Ctor_Base, Out);
+  MC.mangleCXXCtor(CD, Ctor_Base, Out);
 else if (const CXXDestructorDecl *DD = dyn_cast(ND))
-  MC->mangleCXXDtor(DD, Dtor_Base, Out);
+  MC.mangleCXXDtor(DD, Dtor_Base, Out);
 else
-  MC->mangleName(ND, Out);
+  MC.mangleName(ND, Out);
 
 if (!Buffer.empty() && Buffer.front() == '\01')
   return Buffer.substr(1);
Index: lib/AST/ASTContext.cpp
===
--- lib/AST/ASTContext.cpp
+++ lib/AST/ASTContext.cpp
@@ -9570,6 +9570,12 @@
   

[PATCH] D43547: [NameMangling] Make ASTContext owning the ManglingContext during entire compilation

2018-02-21 Thread Michael Haidl via Phabricator via cfe-commits
pacxx created this revision.
pacxx added reviewers: rnk, rsmith.
Herald added a subscriber: cfe-commits.

The ASTContext is only used to create a Mangling Context and forwards ownership 
to everyone who requests a ManglingContext.

The problem fixed by this commit is located in the handling of the 
__FUNCDNAME__ generation. Every time a __FUNCDNAME__ expression is handled a 
new ManglingContext is created. The MC and its cached manglings are thrown away 
every time the __FUNCDNAME__ was handled which results in wrong numbering of 
lambda expressions in name manglings (every lambda gets id 0) what again leads 
to inconsistencies between __FUNCDNAME__ and the name manglings in the 
generated code.


Repository:
  rC Clang

https://reviews.llvm.org/D43547

Files:
  include/clang/AST/ASTContext.h
  lib/AST/ASTContext.cpp
  lib/Index/CodegenNameGenerator.cpp


Index: lib/Index/CodegenNameGenerator.cpp
===
--- lib/Index/CodegenNameGenerator.cpp
+++ lib/Index/CodegenNameGenerator.cpp
@@ -26,11 +26,11 @@
 using namespace clang::index;
 
 struct CodegenNameGenerator::Implementation {
-  std::unique_ptr MC;
+  MangleContext& MC;
   llvm::DataLayout DL;
 
   Implementation(ASTContext )
-: MC(Ctx.createMangleContext()),
+: MC(Ctx.getMangleContext()),
   DL(Ctx.getTargetInfo().getDataLayout()) {}
 
   bool writeName(const Decl *D, raw_ostream ) {
@@ -106,7 +106,6 @@
 const NamedDecl *ND = cast(D);
 
 ASTContext  = ND->getASTContext();
-std::unique_ptr M(Ctx.createMangleContext());
 
 std::vector Manglings;
 
Index: lib/AST/ASTContext.cpp
===
--- lib/AST/ASTContext.cpp
+++ lib/AST/ASTContext.cpp
@@ -9570,6 +9570,12 @@
   llvm_unreachable("Unsupported ABI");
 }
 
+MangleContext ::getMangleContext() {
+  if (!MContext)
+MContext.reset(createMangleContext());
+  return *MContext.get();
+}
+
 CXXABI::~CXXABI() = default;
 
 size_t ASTContext::getSideTableAllocatedMemory() const {
Index: include/clang/AST/ASTContext.h
===
--- include/clang/AST/ASTContext.h
+++ include/clang/AST/ASTContext.h
@@ -2158,6 +2158,7 @@
   VTableContextBase *getVTableContext();
 
   MangleContext *createMangleContext();
+  MangleContext ();
 
   void DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, bool leafClass,
 SmallVectorImpl ) const;
@@ -2828,6 +2829,7 @@
   std::unique_ptr OtherParents;
 
   std::unique_ptr VTContext;
+  std::unique_ptr MContext;
 
   void ReleaseDeclContextMaps();
   void ReleaseParentMapEntries();


Index: lib/Index/CodegenNameGenerator.cpp
===
--- lib/Index/CodegenNameGenerator.cpp
+++ lib/Index/CodegenNameGenerator.cpp
@@ -26,11 +26,11 @@
 using namespace clang::index;
 
 struct CodegenNameGenerator::Implementation {
-  std::unique_ptr MC;
+  MangleContext& MC;
   llvm::DataLayout DL;
 
   Implementation(ASTContext )
-: MC(Ctx.createMangleContext()),
+: MC(Ctx.getMangleContext()),
   DL(Ctx.getTargetInfo().getDataLayout()) {}
 
   bool writeName(const Decl *D, raw_ostream ) {
@@ -106,7 +106,6 @@
 const NamedDecl *ND = cast(D);
 
 ASTContext  = ND->getASTContext();
-std::unique_ptr M(Ctx.createMangleContext());
 
 std::vector Manglings;
 
Index: lib/AST/ASTContext.cpp
===
--- lib/AST/ASTContext.cpp
+++ lib/AST/ASTContext.cpp
@@ -9570,6 +9570,12 @@
   llvm_unreachable("Unsupported ABI");
 }
 
+MangleContext ::getMangleContext() {
+  if (!MContext)
+MContext.reset(createMangleContext());
+  return *MContext.get();
+}
+
 CXXABI::~CXXABI() = default;
 
 size_t ASTContext::getSideTableAllocatedMemory() const {
Index: include/clang/AST/ASTContext.h
===
--- include/clang/AST/ASTContext.h
+++ include/clang/AST/ASTContext.h
@@ -2158,6 +2158,7 @@
   VTableContextBase *getVTableContext();
 
   MangleContext *createMangleContext();
+  MangleContext ();
 
   void DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, bool leafClass,
 SmallVectorImpl ) const;
@@ -2828,6 +2829,7 @@
   std::unique_ptr OtherParents;
 
   std::unique_ptr VTContext;
+  std::unique_ptr MContext;
 
   void ReleaseDeclContextMaps();
   void ReleaseParentMapEntries();
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits