kadircet created this revision.
kadircet added a reviewer: ilya-biryukov.
Herald added subscribers: cfe-commits, arphaman, eraman.

Instead of providing generic "args" for member and bas class
initializations, tries to fetch relevant constructors and show their signatures.


Repository:
  rC Clang

https://reviews.llvm.org/D53654

Files:
  lib/Sema/SemaCodeComplete.cpp
  test/CodeCompletion/ctor-initializer.cpp
  test/Index/complete-ctor-inits.cpp
  test/Index/complete-cxx-inline-methods.cpp

Index: test/Index/complete-cxx-inline-methods.cpp
===================================================================
--- test/Index/complete-cxx-inline-methods.cpp
+++ test/Index/complete-cxx-inline-methods.cpp
@@ -35,10 +35,10 @@
 // CHECK-NEXT: Container Kind: StructDecl
 
 // RUN: c-index-test -code-completion-at=%s:18:41 %s | FileCheck -check-prefix=CHECK-CTOR-INIT %s
-// CHECK-CTOR-INIT: NotImplemented:{TypedText MyCls}{LeftParen (}{Placeholder args}{RightParen )} (7)
+// CHECK-CTOR-INIT: ClassDecl:{TypedText MyCls} (7)
 // CHECK-CTOR-INIT: MemberRef:{TypedText object}{LeftParen (}{Placeholder args}{RightParen )} (35)
 // CHECK-CTOR-INIT: MemberRef:{TypedText value}{LeftParen (}{Placeholder args}{RightParen )} (35)
 // RUN: c-index-test -code-completion-at=%s:18:55 %s | FileCheck -check-prefix=CHECK-CTOR-INIT-2 %s
-// CHECK-CTOR-INIT-2-NOT: NotImplemented:{TypedText MyCls}{LeftParen (}{Placeholder args}{RightParen )}
+// CHECK-CTOR-INIT-2-NOT: ClassDecl:{TypedText MyCls} (7)
 // CHECK-CTOR-INIT-2: MemberRef:{TypedText object}{LeftParen (}{Placeholder args}{RightParen )} (35)
 // CHECK-CTOR-INIT-2: MemberRef:{TypedText value}{LeftParen (}{Placeholder args}{RightParen )} (7)
Index: test/Index/complete-ctor-inits.cpp
===================================================================
--- test/Index/complete-ctor-inits.cpp
+++ test/Index/complete-ctor-inits.cpp
@@ -33,23 +33,29 @@
 // CHECK-CC1: MemberRef:{TypedText a}{LeftParen (}{Placeholder args}{RightParen )} (35)
 // CHECK-CC1: MemberRef:{TypedText b}{LeftParen (}{Placeholder args}{RightParen )} (35)
 // CHECK-CC1: MemberRef:{TypedText c}{LeftParen (}{Placeholder args}{RightParen )} (35)
-// CHECK-CC1: NotImplemented:{TypedText Virt}{LeftParen (}{Placeholder args}{RightParen )} (35)
+// CHECK-CC1: CXXConstructor:{TypedText Virt}{LeftParen (}{Placeholder const Virt &}{RightParen )} (35)
+// CHECK-CC1: CXXConstructor:{TypedText Virt}{LeftParen (}{Placeholder Virt &&}{RightParen )} (35)
 // CHECK-CC1: NotImplemented:{TypedText X<int>}{LeftParen (}{Placeholder args}{RightParen )} (7)
-// CHECK-CC1: NotImplemented:{TypedText Y}{LeftParen (}{Placeholder args}{RightParen )} (35)
+// CHECK-CC1: CXXConstructor:{TypedText Y}{LeftParen (}{Placeholder const Y &}{RightParen )} (35)
+// CHECK-CC1: CXXConstructor:{TypedText Y}{LeftParen (}{Placeholder Y &&}{RightParen )} (35)
 
 // RUN: c-index-test -code-completion-at=%s:18:23 %s | FileCheck -check-prefix=CHECK-CC2 %s
 // CHECK-CC2: MemberRef:{TypedText a}{LeftParen (}{Placeholder args}{RightParen )} (35)
 // CHECK-CC2: MemberRef:{TypedText b}{LeftParen (}{Placeholder args}{RightParen )} (35)
 // CHECK-CC2: MemberRef:{TypedText c}{LeftParen (}{Placeholder args}{RightParen )} (35)
-// CHECK-CC2: NotImplemented:{TypedText Virt}{LeftParen (}{Placeholder args}{RightParen )} (35)
-// CHECK-CC2: NotImplemented:{TypedText Y}{LeftParen (}{Placeholder args}{RightParen )} (7)
+// CHECK-CC2: CXXConstructor:{TypedText Virt}{LeftParen (}{Placeholder const Virt &}{RightParen )} (35)
+// CHECK-CC2: CXXConstructor:{TypedText Virt}{LeftParen (}{Placeholder Virt &&}{RightParen )} (35)
+// CHECK-CC2: CXXConstructor:{TypedText Y}{LeftParen (}{Placeholder const Y &}{RightParen )} (7)
+// CHECK-CC2: CXXConstructor:{TypedText Y}{LeftParen (}{Placeholder Y &&}{RightParen )} (7)
 
 // RUN: c-index-test -code-completion-at=%s:18:36 %s | FileCheck -check-prefix=CHECK-CC3 %s
 // CHECK-CC3: MemberRef:{TypedText a}{LeftParen (}{Placeholder args}{RightParen )} (35)
 // CHECK-CC3-NOT: MemberRef:{TypedText b}{LeftParen (}{Placeholder args}{RightParen )}
 // CHECK-CC3: MemberRef:{TypedText c}{LeftParen (}{Placeholder args}{RightParen )} (7)
-// CHECK-CC3-NOT: NotImplemented:{TypedText Virt}{LeftParen (}{Placeholder args}{RightParen )}
-// CHECK-CC3: NotImplemented:{TypedText Y}{LeftParen (}{Placeholder args}{RightParen )} (35)
+// CHECK-CC3-NOT: CXXConstructor:{TypedText Virt}{LeftParen (}{Placeholder const Virt &}{RightParen )} (35)
+// CHECK-CC3-NOT: CXXConstructor:{TypedText Virt}{LeftParen (}{Placeholder Virt &&}{RightParen )} (35)
+// CHECK-CC3: CXXConstructor:{TypedText Y}{LeftParen (}{Placeholder const Y &}{RightParen )} (35)
+// CHECK-CC3: CXXConstructor:{TypedText Y}{LeftParen (}{Placeholder Y &&}{RightParen )} (35)
 
 // RUN: c-index-test -code-completion-at=%s:22:10 -target i386-apple-darwin %s | FileCheck -check-prefix=CHECK-CC4 %s
 // CHECK-CC4: MemberRef:{TypedText a}{LeftParen (}{Placeholder args}{RightParen )} (7)
Index: test/CodeCompletion/ctor-initializer.cpp
===================================================================
--- test/CodeCompletion/ctor-initializer.cpp
+++ test/CodeCompletion/ctor-initializer.cpp
@@ -3,13 +3,13 @@
   // RUN: %clang_cc1 -fsyntax-only -std=c++98 -code-completion-at=%s:2:12 %s -o - | FileCheck -check-prefix=CHECK-CC1 %s
   // RUN: %clang_cc1 -fsyntax-only -std=c++14 -code-completion-at=%s:2:12 %s -o - | FileCheck -check-prefix=CHECK-CC1 %s
   // CHECK-CC1: COMPLETION: Pattern : member1(<#args#>)
-  // CHECK-CC1: COMPLETION: Pattern : member2(<#args#>
+  // CHECK-CC1: COMPLETION: Pattern : member2(<#args#>)
 
   Base1(int) : member1(123), {}
   // RUN: %clang_cc1 -fsyntax-only -std=c++98 -code-completion-at=%s:8:30 %s -o - | FileCheck -check-prefix=CHECK-CC2 %s
   // RUN: %clang_cc1 -fsyntax-only -std=c++14 -code-completion-at=%s:8:30 %s -o - | FileCheck -check-prefix=CHECK-CC2 %s
   // CHECK-CC2-NOT: COMPLETION: Pattern : member1(<#args#>)
-  // CHECK-CC2: COMPLETION: Pattern : member2(<#args#>
+  // CHECK-CC2: COMPLETION: Pattern : member2(<#args#>)
 
   int member1;
   float member2;
@@ -25,52 +25,52 @@
 Derived::Derived() : {}
 // RUN: %clang_cc1 -fsyntax-only -std=c++98 -code-completion-at=%s:25:22 %s -o - | FileCheck -check-prefix=CHECK-CC3 %s
 // RUN: %clang_cc1 -fsyntax-only -std=c++14 -code-completion-at=%s:25:22 %s -o - | FileCheck -check-prefix=CHECK-CC3 %s
-// CHECK-CC3: COMPLETION: Pattern : Base1(<#args#>)
+// CHECK-CC3: COMPLETION: Base1 : Base1()
+// CHECK-CC3: COMPLETION: Base1 : Base1(<#int#>)
 // CHECK-CC3: COMPLETION: Pattern : deriv1(<#args#>)
 
 Derived::Derived(int) try : {
 } catch (...) {
 }
-// RUN: %clang_cc1 -fsyntax-only -std=c++98 -code-completion-at=%s:31:29 %s -o - | FileCheck -check-prefix=CHECK-CC4 %s
-// RUN: %clang_cc1 -fsyntax-only -std=c++14 -code-completion-at=%s:31:29 %s -o - | FileCheck -check-prefix=CHECK-CC4 %s
-// CHECK-CC4: COMPLETION: Pattern : Base1(<#args#>)
-// CHECK-CC4: COMPLETION: Pattern : deriv1(<#args#>)
+// RUN: %clang_cc1 -fsyntax-only -std=c++98 -code-completion-at=%s:32:29 %s -o - | FileCheck -check-prefix=CHECK-CC3 %s
+// RUN: %clang_cc1 -fsyntax-only -std=c++14 -code-completion-at=%s:32:29 %s -o - | FileCheck -check-prefix=CHECK-CC3 %s
 
 Derived::Derived(float) try : Base1(),
 {
 } catch (...) {
 }
-// RUN: %clang_cc1 -fsyntax-only -std=c++98 -code-completion-at=%s:39:39 %s -o - | FileCheck -check-prefix=CHECK-CC5 %s
-// RUN: %clang_cc1 -fsyntax-only -std=c++14 -code-completion-at=%s:39:39 %s -o - | FileCheck -check-prefix=CHECK-CC5 %s
-// CHECK-CC5-NOT: COMPLETION: Pattern : Base1(<#args#>)
+// RUN: %clang_cc1 -fsyntax-only -std=c++98 -code-completion-at=%s:38:39 %s -o - | FileCheck -check-prefix=CHECK-CC5 %s
+// RUN: %clang_cc1 -fsyntax-only -std=c++14 -code-completion-at=%s:38:39 %s -o - | FileCheck -check-prefix=CHECK-CC5 %s
+// CHECK-CC5-NOT: COMPLETION: Base1 : Base1()
+// CHECK-CC5-NOT: COMPLETION: Base1 : Base1(<#int#>)
 // CHECK-CC5: COMPLETION: Pattern : deriv1(<#args#>)
 
 struct A {
   A() : , member2() {}
   // RUN: %clang_cc1 -fsyntax-only -std=c++98 -code-completion-at=%s:49:9 %s -o - | FileCheck -check-prefix=CHECK-CC6 %s
   // RUN: %clang_cc1 -fsyntax-only -std=c++14 -code-completion-at=%s:49:9 %s -o - | FileCheck -check-prefix=CHECK-CC6 %s
-  // CHECK-CC6: COMPLETION: Pattern : member1(<#args#>
+  // CHECK-CC6: COMPLETION: Pattern : member1(<#args#>)
   int member1, member2;
 };
 
 struct B {
   B() : member2() {}
   // RUN: %clang_cc1 -fsyntax-only -std=c++98 -code-completion-at=%s:57:9 %s -o - | FileCheck -check-prefix=CHECK-CC7 %s
   // RUN: %clang_cc1 -fsyntax-only -std=c++14 -code-completion-at=%s:57:9 %s -o - | FileCheck -check-prefix=CHECK-CC7 %s
-  // CHECK-CC7: COMPLETION: Pattern : member1(<#args#>
+  // CHECK-CC7: COMPLETION: Pattern : member1(<#args#>)
   // Check in the middle and at the end of identifier too.
   // RUN: %clang_cc1 -fsyntax-only -std=c++98 -code-completion-at=%s:57:13 %s -o - | FileCheck -check-prefix=CHECK-CC8 %s
   // RUN: %clang_cc1 -fsyntax-only -std=c++98 -code-completion-at=%s:57:16 %s -o - | FileCheck -check-prefix=CHECK-CC8 %s
-  // CHECK-CC8: COMPLETION: Pattern : member2(<#args#>
+  // CHECK-CC8: COMPLETION: Pattern : member2(<#args#>)
   int member1, member2;
 };
 
 struct Base2 {
-  Base2(int);
+  Base2(int, float x = 3);
 };
 
 struct Composition1 {
-  Composition1() : b2_elem() {}
+  Composition1() : b2_elem(2) {}
   // RUN: %clang_cc1 -fsyntax-only -std=c++98 -code-completion-at=%s:73:28 %s -o - | FileCheck -check-prefix=CHECK-CC9 %s
   // RUN: %clang_cc1 -fsyntax-only -std=c++14 -code-completion-at=%s:73:28 %s -o - | FileCheck -check-prefix=CHECK-CC9 %s
   // CHECK-CC9: OVERLOAD: Base2(<#int#>)
@@ -88,3 +88,7 @@
   // RUN: %clang_cc1 -fsyntax-only -std=c++14 -code-completion-at=%s:84:35 %s -o - | FileCheck -check-prefix=CHECK-CC9 %s
   Composition1 c1_elem;
 };
+// RUN: %clang_cc1 -fsyntax-only -std=c++98 -code-completion-at=%s:84:20 %s -o - | FileCheck -check-prefix=CHECK-CC10 %s
+// RUN: %clang_cc1 -fsyntax-only -std=c++14 -code-completion-at=%s:84:20 %s -o - | FileCheck -check-prefix=CHECK-CC10 %s
+// CHECK-CC10: Pattern : c1_elem()
+// CHECK-CC10: Pattern : c1_elem(<#Base2#>)
Index: lib/Sema/SemaCodeComplete.cpp
===================================================================
--- lib/Sema/SemaCodeComplete.cpp
+++ lib/Sema/SemaCodeComplete.cpp
@@ -173,6 +173,9 @@
     void MaybeAddConstructorResults(Result R);
 
   public:
+    std::vector<CodeCompletionResult>
+    GetConstructorResults(const CXXRecordDecl *Record, Result R);
+
     explicit ResultBuilder(Sema &SemaRef, CodeCompletionAllocator &Allocator,
                            CodeCompletionTUInfo &CCTUInfo,
                            const CodeCompletionContext &CompletionContext,
@@ -808,12 +811,29 @@
   }
 }
 
+std::vector<CodeCompletionResult>
+ResultBuilder::GetConstructorResults(const CXXRecordDecl *Record, Result R) {
+  ASTContext &Context = SemaRef.Context;
+  std::vector<CodeCompletionResult> Results;
+  QualType RecordTy = Context.getTypeDeclType(Record);
+  DeclarationName ConstructorName
+    = Context.DeclarationNames.getCXXConstructorName(
+                                           Context.getCanonicalType(RecordTy));
+  DeclContext::lookup_result Ctors = Record->lookup(ConstructorName);
+  for (DeclContext::lookup_iterator I = Ctors.begin(), E = Ctors.end(); I != E;
+       ++I) {
+    R.Declaration = *I;
+    R.CursorKind = getCursorKindForDecl(R.Declaration);
+    Results.push_back(R);
+  }
+  return Results;
+}
+
 void ResultBuilder::MaybeAddConstructorResults(Result R) {
   if (!SemaRef.getLangOpts().CPlusPlus || !R.Declaration ||
       !CompletionContext.wantConstructorResults())
     return;
 
-  ASTContext &Context = SemaRef.Context;
   const NamedDecl *D = R.Declaration;
   const CXXRecordDecl *Record = nullptr;
   if (const ClassTemplateDecl *ClassTemplate = dyn_cast<ClassTemplateDecl>(D))
@@ -831,19 +851,10 @@
   if (!Record)
     return;
 
-
-  QualType RecordTy = Context.getTypeDeclType(Record);
-  DeclarationName ConstructorName
-    = Context.DeclarationNames.getCXXConstructorName(
-                                           Context.getCanonicalType(RecordTy));
-  DeclContext::lookup_result Ctors = Record->lookup(ConstructorName);
-  for (DeclContext::lookup_iterator I = Ctors.begin(),
-                                          E = Ctors.end();
-       I != E; ++I) {
-    R.Declaration = *I;
-    R.CursorKind = getCursorKindForDecl(R.Declaration);
-    Results.push_back(R);
-  }
+  auto ConstructorResults = GetConstructorResults(Record, R);
+  Results.insert(Results.end(),
+                 std::make_move_iterator(ConstructorResults.begin()),
+                 std::make_move_iterator(ConstructorResults.end()));
 }
 
 static bool isConstructor(const Decl *ND) {
@@ -5073,11 +5084,39 @@
   }
 
   // Add completions for base classes.
+  PrintingPolicy Policy = getCompletionPrintingPolicy(*this);
   CodeCompletionBuilder Builder(Results.getAllocator(),
                                 Results.getCodeCompletionTUInfo());
-  PrintingPolicy Policy = getCompletionPrintingPolicy(*this);
   bool SawLastInitializer = Initializers.empty();
   CXXRecordDecl *ClassDecl = Constructor->getParent();
+
+  auto CreatePrimitiveConstructor = [&](StringRef Name) {
+    Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(Name));
+    Builder.AddChunk(CodeCompletionString::CK_LeftParen);
+    Builder.AddPlaceholderChunk("args");
+    Builder.AddChunk(CodeCompletionString::CK_RightParen);
+  };
+  auto AddBase = [&](const CXXBaseSpecifier &Base) {
+    const auto *ND = Base.getType()->getAsCXXRecordDecl();
+    if (isa<ClassTemplateSpecializationDecl>(ND) ||
+        isa<ClassTemplatePartialSpecializationDecl>(ND)) {
+      CreatePrimitiveConstructor(Base.getType().getAsString(Policy));
+      Results.AddResult(CodeCompletionResult(
+          Builder.TakeString(),
+          SawLastInitializer ? CCP_NextInitializer : CCP_MemberDeclaration));
+    } else {
+      Results.MaybeAddResult(
+          CodeCompletionResult(ND, SawLastInitializer ? CCP_NextInitializer
+                                                      : CCP_MemberDeclaration));
+    }
+  };
+  auto AddField = [&](const FieldDecl *FD) {
+    Results.AddResult(CodeCompletionResult(
+        Builder.TakeString(),
+        SawLastInitializer ? CCP_NextInitializer : CCP_MemberDeclaration,
+        CXCursor_MemberRef, CXAvailability_Available, FD));
+  };
+
   for (const auto &Base : ClassDecl->bases()) {
     if (!InitializedBases.insert(Context.getCanonicalType(Base.getType()))
              .second) {
@@ -5089,15 +5128,7 @@
       continue;
     }
 
-    Builder.AddTypedTextChunk(
-               Results.getAllocator().CopyString(
-                          Base.getType().getAsString(Policy)));
-    Builder.AddChunk(CodeCompletionString::CK_LeftParen);
-    Builder.AddPlaceholderChunk("args");
-    Builder.AddChunk(CodeCompletionString::CK_RightParen);
-    Results.AddResult(CodeCompletionResult(Builder.TakeString(),
-                                   SawLastInitializer? CCP_NextInitializer
-                                                     : CCP_MemberDeclaration));
+    AddBase(Base);
     SawLastInitializer = false;
   }
 
@@ -5113,15 +5144,7 @@
       continue;
     }
 
-    Builder.AddTypedTextChunk(
-               Builder.getAllocator().CopyString(
-                          Base.getType().getAsString(Policy)));
-    Builder.AddChunk(CodeCompletionString::CK_LeftParen);
-    Builder.AddPlaceholderChunk("args");
-    Builder.AddChunk(CodeCompletionString::CK_RightParen);
-    Results.AddResult(CodeCompletionResult(Builder.TakeString(),
-                                   SawLastInitializer? CCP_NextInitializer
-                                                     : CCP_MemberDeclaration));
+    AddBase(Base);
     SawLastInitializer = false;
   }
 
@@ -5139,17 +5162,38 @@
     if (!Field->getDeclName())
       continue;
 
-    Builder.AddTypedTextChunk(Builder.getAllocator().CopyString(
-                                         Field->getIdentifier()->getName()));
-    Builder.AddChunk(CodeCompletionString::CK_LeftParen);
-    Builder.AddPlaceholderChunk("args");
-    Builder.AddChunk(CodeCompletionString::CK_RightParen);
-    Results.AddResult(CodeCompletionResult(Builder.TakeString(),
-                                   SawLastInitializer? CCP_NextInitializer
-                                                     : CCP_MemberDeclaration,
-                                           CXCursor_MemberRef,
-                                           CXAvailability_Available,
-                                           Field));
+    StringRef FieldName = Field->getIdentifier()->getName();
+    // Check if field is a cxx class/struct/union, if not it can't have a
+    // constructor.
+    if (const CXXRecordDecl *RD = Field->getType()->getAsCXXRecordDecl()) {
+      auto ConstResults = Results.GetConstructorResults(
+          RD, CodeCompletionResult(Field, SawLastInitializer
+                                              ? CCP_NextInitializer
+                                              : CCP_MemberDeclaration));
+      for (auto &ConstResult : ConstResults) {
+        auto *CCS = ConstResult.createCodeCompletionStringForDecl(
+            getPreprocessor(), Context, Builder, /*IncludeBriefComments=*/false,
+            CodeCompletionContext::CCC_Symbol, Policy);
+        for (auto C : *CCS) {
+          switch (C.Kind) {
+          case CodeCompletionString::CK_TypedText:
+            Builder.AddTypedTextChunk(
+                Builder.getAllocator().CopyString(FieldName));
+            break;
+          case CodeCompletionString::CK_Optional:
+            Builder.AddOptionalChunk(C.Optional);
+            break;
+          default:
+            Builder.AddChunk(C.Kind, C.Text);
+            break;
+          }
+        }
+        AddField(Field);
+      }
+    } else {
+      CreatePrimitiveConstructor(Builder.getAllocator().CopyString(FieldName));
+      AddField(Field);
+    }
     SawLastInitializer = false;
   }
   Results.ExitScope();
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to