]> granicus.if.org Git - clang/commitdiff
Give a slight preference to functions returning "void" when we're
authorDouglas Gregor <dgregor@apple.com>
Tue, 24 Aug 2010 23:58:17 +0000 (23:58 +0000)
committerDouglas Gregor <dgregor@apple.com>
Tue, 24 Aug 2010 23:58:17 +0000 (23:58 +0000)
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

include/clang/Sema/CodeCompleteConsumer.h
lib/Sema/SemaCodeComplete.cpp
test/Index/complete-blocks.m
test/Index/complete-exprs.c
test/Index/preamble.c

index 87974f1452178deb5630d90caec7f312a0c0f5c9..b4a0301dbf61da6b859364888603e57da87b7674 100644 (file)
@@ -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
index e25a47c2a553e10cabcea0ed7ecbd23fbaf6b0ca..9ed73dc8c844744f83ee0529db9029a0b80441a7 100644 (file)
@@ -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()))
index fa76730ebf7acda4296962bc0888e2c2d40f9fa3..7233efb8b00f0edbe09c2fb6e9598e20dee925b4 100644 (file)
@@ -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)
index f64706c866dfe9de29734345b4a08f079b276679..170f6d549fa6b01f610fe1d598ec43d4153c2eaf 100644 (file)
@@ -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)
index 1d3e7e82cf2244470b53c459625c15ccc5765751..54abf99ed149adfffa46f6b5a39d16bcb977df73 100644 (file)
@@ -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)