]> granicus.if.org Git - clang/commitdiff
[CodeComplete] Do not complete self-initializations
authorIlya Biryukov <ibiryukov@google.com>
Wed, 7 Nov 2018 10:02:31 +0000 (10:02 +0000)
committerIlya Biryukov <ibiryukov@google.com>
Wed, 7 Nov 2018 10:02:31 +0000 (10:02 +0000)
Summary:
Removes references to initialized variable from the following completions:

    int x = ^;

Handles only the trivial cases where the variable name is completed
immediately at the start of initializer or assignment, more complicated
cases aren't covered, e.g. these completions still contain 'x':

    // More complicated expressions.
    int x = foo(^);
    int x = 10 + ^;
    // Other kinds of initialization.
    int x{^};
    int x(^);
    // Constructor initializers.
    struct Foo {
      Foo() : x(^) {}
      int x;
    };

We should address those in the future, but they are outside of the scope of
this initial change.

Reviewers: sammccall

Reviewed By: sammccall

Subscribers: arphaman, cfe-commits

Differential Revision: https://reviews.llvm.org/D54156

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@346301 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaCodeComplete.cpp
test/CodeCompletion/ordinary-name-cxx11.cpp
test/CodeCompletion/ordinary-name.cpp
test/CodeCompletion/self-inits.cpp [new file with mode: 0644]
test/Index/complete-type-factors.m

index 6c4c7e622d4ddc36ec77cd3e474355b2e2cbb8a1..95714852c16e70accf07d4536b7ee8236071d4d6 100644 (file)
@@ -4747,7 +4747,12 @@ void Sema::CodeCompleteInitializer(Scope *S, Decl *D) {
     return;
   }
 
-  CodeCompleteExpression(S, VD->getType());
+  CodeCompleteExpressionData Data;
+  Data.PreferredType = VD->getType();
+  // Ignore VD to avoid completing the variable itself, e.g. in 'int foo = ^'.
+  Data.IgnoreDecls.push_back(VD);
+
+  CodeCompleteExpression(S, Data);
 }
 
 void Sema::CodeCompleteReturn(Scope *S) {
index 34c3bf96a9d536b9b85f65cb03d72e65489f592c..0125ffbbb3cf0310b1fdeb06ac2cf620c411ccc0 100644 (file)
@@ -197,7 +197,6 @@ void foo() {
   // CHECK-CC4-NEXT: COMPLETION: volatile
   // CHECK-CC4-NEXT: COMPLETION: wchar_t
   // CHECK-CC4-NEXT: COMPLETION: X : X
-  // CHECK-CC4-NEXT: COMPLETION: y : [#int#]y
   // CHECK-CC4-NEXT: COMPLETION: z : [#void#]z(<#int#>)
 
   // RUN: %clang_cc1 -fsyntax-only -fno-rtti -code-completion-patterns -code-completion-at=%s:6:14 -std=gnu++11 %s -o - | FileCheck -check-prefix=CHECK-NO-RTTI %s
index 03dbbcaf74485ec6bddaa9e2d455fdb6be0b327f..ba613bc9152b951dc31ea5a9daa79b87099f1060 100644 (file)
@@ -171,7 +171,6 @@ void foo() {
   // CHECK-CC4-NEXT: COMPLETION: volatile
   // CHECK-CC4-NEXT: COMPLETION: wchar_t
   // CHECK-CC4-NEXT: COMPLETION: X : X
-  // CHECK-CC4-NEXT: COMPLETION: y : [#int#]y
   // CHECK-CC4-NEXT: COMPLETION: z : [#void#]z(<#int#>)
 
   // RUN: %clang_cc1 -fsyntax-only -fno-rtti -code-completion-patterns -code-completion-at=%s:6:14 -std=gnu++98 %s -o - | FileCheck -check-prefix=CHECK-NO-RTTI %s
diff --git a/test/CodeCompletion/self-inits.cpp b/test/CodeCompletion/self-inits.cpp
new file mode 100644 (file)
index 0000000..a642095
--- /dev/null
@@ -0,0 +1,3 @@
+int foo = 10;
+// RUN: %clang_cc1 -fsyntax-only -code-completion-at=%s:1:11 %s -o - | FileCheck --check-prefix=CC1 %s
+// CC1-NOT: foo
index f2588e6fe65ed31fe4ec62757557d7db1bbd5f1c..fcd51284bf0b4f84082fc0511f475bad9331bed3 100644 (file)
@@ -39,7 +39,6 @@ void test2(A *a) {
 // CHECK-CC1: FunctionDecl:{ResultType enum Priority}{TypedText func2}{LeftParen (}{Placeholder int}{RightParen )} (25)
 // CHECK-CC1: EnumConstantDecl:{ResultType enum Color}{TypedText Green} (32)
 // CHECK-CC1: EnumConstantDecl:{ResultType enum Priority}{TypedText High} (32)
-// CHECK-CC1: VarDecl:{ResultType int}{TypedText i} (8)
 // CHECK-CC1: ParmDecl:{ResultType int}{TypedText integer} (8)
 // CHECK-CC1: EnumConstantDecl:{ResultType enum Priority}{TypedText Low} (32)
 // CHECK-CC1: ParmDecl:{ResultType enum Priority}{TypedText priority} (17)
@@ -48,7 +47,6 @@ void test2(A *a) {
 // CHECK-CC1: FunctionDecl:{ResultType enum Priority}{TypedText test1}{LeftParen (}{Placeholder enum Priority priority}{Comma , }{Placeholder enum Color color}{Comma , }{Placeholder int integer}{RightParen )} (25)
 // RUN: c-index-test -code-completion-at=%s:17:18 -Xclang -code-completion-patterns %s | FileCheck -check-prefix=CHECK-CC2 %s
 // CHECK-CC2: EnumConstantDecl:{ResultType enum Color}{TypedText Blue} (16)
-// CHECK-CC2: VarDecl:{ResultType enum Color}{TypedText c} (8)
 // CHECK-CC2: ParmDecl:{ResultType enum Color}{TypedText color} (8)
 // CHECK-CC2: FunctionDecl:{ResultType int}{TypedText func1}{LeftParen (}{Placeholder enum Color}{RightParen )} (25)
 // CHECK-CC2: FunctionDecl:{ResultType enum Priority}{TypedText func2}{LeftParen (}{Placeholder int}{RightParen )} (50)