From: Ilya Biryukov Date: Wed, 7 Nov 2018 10:02:31 +0000 (+0000) Subject: [CodeComplete] Do not complete self-initializations X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=04fd8b2a71f62bb832dd1da34ce09e0a3de202dc;p=clang [CodeComplete] Do not complete self-initializations 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 --- diff --git a/lib/Sema/SemaCodeComplete.cpp b/lib/Sema/SemaCodeComplete.cpp index 6c4c7e622d..95714852c1 100644 --- a/lib/Sema/SemaCodeComplete.cpp +++ b/lib/Sema/SemaCodeComplete.cpp @@ -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) { diff --git a/test/CodeCompletion/ordinary-name-cxx11.cpp b/test/CodeCompletion/ordinary-name-cxx11.cpp index 34c3bf96a9..0125ffbbb3 100644 --- a/test/CodeCompletion/ordinary-name-cxx11.cpp +++ b/test/CodeCompletion/ordinary-name-cxx11.cpp @@ -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 diff --git a/test/CodeCompletion/ordinary-name.cpp b/test/CodeCompletion/ordinary-name.cpp index 03dbbcaf74..ba613bc915 100644 --- a/test/CodeCompletion/ordinary-name.cpp +++ b/test/CodeCompletion/ordinary-name.cpp @@ -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 index 0000000000..a64209534a --- /dev/null +++ b/test/CodeCompletion/self-inits.cpp @@ -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 diff --git a/test/Index/complete-type-factors.m b/test/Index/complete-type-factors.m index f2588e6fe6..fcd51284bf 100644 --- a/test/Index/complete-type-factors.m +++ b/test/Index/complete-type-factors.m @@ -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)