From: Douglas Gregor Date: Tue, 24 Aug 2010 23:58:17 +0000 (+0000) Subject: Give a slight preference to functions returning "void" when we're X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=eb0d0145910f3024d9bdefe279a1e9b95495f2fd;p=clang Give a slight preference to functions returning "void" when we're performing code completion at the statement level (rather than in an arbitrary expression). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@112001 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Sema/CodeCompleteConsumer.h b/include/clang/Sema/CodeCompleteConsumer.h index 87974f1452..b4a0301dbf 100644 --- a/include/clang/Sema/CodeCompleteConsumer.h +++ b/include/clang/Sema/CodeCompleteConsumer.h @@ -59,7 +59,12 @@ enum { /// based on the context of the result. enum { /// \brief The result is in a base class. - CCD_InBaseClass = 2 + CCD_InBaseClass = 2, + /// \brief The result is a type match against void. + /// + /// Since everything converts to "void", we don't give as drastic an + /// adjustment for matching void. + CCD_VoidMatch = -5 }; /// \brief Priority value factors by which we will divide or multiply the diff --git a/lib/Sema/SemaCodeComplete.cpp b/lib/Sema/SemaCodeComplete.cpp index e25a47c2a5..9ed73dc8c8 100644 --- a/lib/Sema/SemaCodeComplete.cpp +++ b/lib/Sema/SemaCodeComplete.cpp @@ -593,9 +593,12 @@ void ResultBuilder::AdjustResultPriorityForPreferredType(Result &R) { CanQualType TC = SemaRef.Context.getCanonicalType(T); // Check for exactly-matching types (modulo qualifiers). - if (SemaRef.Context.hasSameUnqualifiedType(PreferredType, TC)) - R.Priority /= CCF_ExactTypeMatch; - // Check for nearly-matching types, based on classification of each. + if (SemaRef.Context.hasSameUnqualifiedType(PreferredType, TC)) { + if (PreferredType->isVoidType()) + R.Priority += CCD_VoidMatch; + else + R.Priority /= CCF_ExactTypeMatch; + } // Check for nearly-matching types, based on classification of each. else if ((getSimplifiedTypeClass(PreferredType) == getSimplifiedTypeClass(TC)) && !(PreferredType->isEnumeralType() && TC->isEnumeralType())) @@ -2400,8 +2403,14 @@ void Sema::CodeCompleteOrdinaryName(Scope *S, Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName); break; - case PCC_Expression: case PCC_Statement: + // For statements that are expressions, we prefer to call 'void' functions + // rather than functions that return a result, since then the result would + // be ignored. + Results.setPreferredType(Context.VoidTy); + // Fall through + + case PCC_Expression: case PCC_ForInit: case PCC_Condition: if (WantTypesInContext(CompletionContext, getLangOptions())) diff --git a/test/Index/complete-blocks.m b/test/Index/complete-blocks.m index fa76730ebf..7233efb8b0 100644 --- a/test/Index/complete-blocks.m +++ b/test/Index/complete-blocks.m @@ -17,8 +17,8 @@ void test_A(A *a) { [a method:0]; } // RUN: c-index-test -code-completion-at=%s:8:1 %s | FileCheck -check-prefix=CHECK-CC1 %s -// CHECK-CC1: FunctionDecl:{ResultType void}{TypedText f}{LeftParen (}{Placeholder int (^)(int x, int y)}{RightParen )} (50) -// CHECK-CC1: FunctionDecl:{ResultType void}{TypedText g}{LeftParen (}{Placeholder void (^)(float f, double d)}{RightParen )} (50) +// CHECK-CC1: FunctionDecl:{ResultType void}{TypedText f}{LeftParen (}{Placeholder int (^)(int x, int y)}{RightParen )} (45) +// CHECK-CC1: FunctionDecl:{ResultType void}{TypedText g}{LeftParen (}{Placeholder void (^)(float f, double d)}{RightParen )} (45) // RUN: c-index-test -code-completion-at=%s:17:6 %s | FileCheck -check-prefix=CHECK-CC2 %s // CHECK-CC2: ObjCInstanceMethodDecl:{ResultType id}{TypedText method2:}{Placeholder void (^)(float f, double d)} (20) // CHECK-CC2: ObjCInstanceMethodDecl:{ResultType id}{TypedText method:}{Placeholder int (^)(int x, int y)} (20) diff --git a/test/Index/complete-exprs.c b/test/Index/complete-exprs.c index f64706c866..170f6d549f 100644 --- a/test/Index/complete-exprs.c +++ b/test/Index/complete-exprs.c @@ -57,6 +57,6 @@ void f4(const char* str) { // CHECK-CC5: NotImplemented:{TypedText volatile} (65) // RUN: c-index-test -code-completion-at=%s:19:3 -Xclang -code-completion-patterns %s | FileCheck -check-prefix=CHECK-CC6 %s -// CHECK-CC6: FunctionDecl:{ResultType void}{TypedText f3}{LeftParen (}{Placeholder char const *}{Placeholder , ...}{Text , NULL}{RightParen )} (50) +// CHECK-CC6: FunctionDecl:{ResultType void}{TypedText f3}{LeftParen (}{Placeholder char const *}{Placeholder , ...}{Text , NULL}{RightParen )} (45) // CHECK-CC6: NotImplemented:{TypedText void} (65) // CHECK-CC6: NotImplemented:{TypedText volatile} (65) diff --git a/test/Index/preamble.c b/test/Index/preamble.c index 1d3e7e82cf..54abf99ed1 100644 --- a/test/Index/preamble.c +++ b/test/Index/preamble.c @@ -23,6 +23,6 @@ void f(int x) { // CHECK-DIAG: preamble.h:4:7:{4:9-4:13}: warning: incompatible pointer types assigning to 'int *' from 'float *' // RUN: env CINDEXTEST_EDITING=1 c-index-test -code-completion-at=%s:6:1 -I %S/Inputs -include %t %s 2> %t.stderr.txt | FileCheck -check-prefix CHECK-CC %s // CHECK-CC: FunctionDecl:{ResultType int}{TypedText bar}{LeftParen (}{Placeholder int i}{RightParen )} (50) -// CHECK-CC: FunctionDecl:{ResultType void}{TypedText f}{LeftParen (}{Placeholder int x}{RightParen )} (50) +// CHECK-CC: FunctionDecl:{ResultType void}{TypedText f}{LeftParen (}{Placeholder int x}{RightParen )} (45) // CHECK-CC: FunctionDecl:{ResultType int}{TypedText foo}{LeftParen (}{Placeholder int}{RightParen )} (50) // CHECK-CC: FunctionDecl:{ResultType int}{TypedText wibble}{LeftParen (}{Placeholder int}{RightParen )} (50)