]> granicus.if.org Git - clang/commitdiff
When code-completing a case statement for a switch on a value of
authorDouglas Gregor <dgregor@apple.com>
Fri, 18 Feb 2011 23:30:37 +0000 (23:30 +0000)
committerDouglas Gregor <dgregor@apple.com>
Fri, 18 Feb 2011 23:30:37 +0000 (23:30 +0000)
enumeration type, prioritize the enumeration constants and don't
provide completions for any other expressions. Fixes <rdar://problem/7283668>.

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

include/clang/Sema/CodeCompleteConsumer.h
lib/Frontend/ASTUnit.cpp
lib/Sema/CodeCompleteConsumer.cpp
lib/Sema/SemaCodeComplete.cpp
test/Index/complete-enums.c

index 2838c19b723231273c703f30aba4ab88717f4e82..882440b78c7c7c41b596d6e839213c81e07cc2e5 100644 (file)
@@ -36,6 +36,9 @@ enum {
   /// \brief Priority for the next initialization in a constructor initializer
   /// list.
   CCP_NextInitializer = 7,
+  /// \brief Priority for an enumeration constant inside a switch whose 
+  /// condition is of the enumeration type.
+  CCP_EnumInCase = 7,
   /// \brief Priority for a send-to-super completion.
   CCP_SuperCompletion = 20,
   /// \brief Priority for a declaration that is in the local scope.
@@ -153,6 +156,9 @@ public:
   enum Kind {
     /// \brief An unspecified code-completion context.
     CCC_Other,
+    /// \brief An unspecified code-completion context where we should also add
+    /// macro completions.
+    CCC_OtherWithMacros,
     /// \brief Code completion occurred within a "top-level" completion context,
     /// e.g., at namespace or global scope.
     CCC_TopLevel,
index 46f5b5a48e749d693f033e51b22ac5d760d013d3..4a5a51d9f1dc32c791b086f5946019d3209dede9 100644 (file)
@@ -333,8 +333,8 @@ void ASTUnit::CacheCodeCompletionResults() {
         | (1 << (CodeCompletionContext::CCC_ObjCMessageReceiver - 1))
         | (1 << (CodeCompletionContext::CCC_MacroNameUse - 1))
         | (1 << (CodeCompletionContext::CCC_PreprocessorExpression - 1))
-        | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1));
-
+        | (1 << (CodeCompletionContext::CCC_ParenthesizedExpression - 1))
+        | (1 << (CodeCompletionContext::CCC_OtherWithMacros - 1));
       
       CachedResult.Priority = Results[I].Priority;
       CachedResult.Kind = Results[I].CursorKind;
@@ -1790,6 +1790,7 @@ static void CalculateHiddenNames(const CodeCompletionContext &Context,
   case CodeCompletionContext::CCC_SelectorName:
   case CodeCompletionContext::CCC_TypeQualifiers:
   case CodeCompletionContext::CCC_Other:
+  case CodeCompletionContext::CCC_OtherWithMacros:
     // We're looking for nothing, or we're looking for names that cannot
     // be hidden.
     return;
index 253c10e7d5e98164db08acaa095b3636724fc0e6..b7037ce83e7fdbfe8da2a56ae691b98911aa3628 100644 (file)
@@ -63,6 +63,7 @@ bool CodeCompletionContext::wantConstructorResults() const {
   case CCC_SelectorName:
   case CCC_TypeQualifiers:
   case CCC_Other:
+  case CCC_OtherWithMacros:
     return false;
   }
   
index a65d5fd6fc705e0514948c019d67f2660438d797..aef5cab98acffd507a58a4554e1dbec6ea04003f 100644 (file)
@@ -3331,15 +3331,16 @@ void Sema::CodeCompleteCase(Scope *S) {
     if (EnumeratorsSeen.count(*E))
       continue;
     
-    Results.AddResult(CodeCompletionResult(*E, Qualifier),
-                      CurContext, 0, false);
+    CodeCompletionResult R(*E, Qualifier);
+    R.Priority = CCP_EnumInCase;
+    Results.AddResult(R, CurContext, 0, false);
   }
   Results.ExitScope();
 
   if (CodeCompleter->includeMacros())
     AddMacroResults(PP, Results);
   HandleCodeCompleteResults(this, CodeCompleter, 
-                            CodeCompletionContext::CCC_Expression,
+                            CodeCompletionContext::CCC_OtherWithMacros,
                             Results.data(),Results.size());
 }
 
index 5e712a11227f14e9342334ca706637ea2e0d1642..33a4cd42811c72933fdeaa08c0b6a5b83c4932c0 100644 (file)
@@ -1,15 +1,22 @@
 // Note: the run lines follow their respective tests, since line/column
 // matter in this test.
 
-enum {
-  Red = 17,
-  Green,
-  Blue
+enum Color {
+  Color_Red = 17,
+  Color_Green,
+  Color_Blue
 };
-
-void f() {
-  
+int Greeby();
+void f(Color color) {
+  switch (color) {
+  case Red:
+  }
 }
 
 // RUN: c-index-test -code-completion-at=%s:11:1 %s | FileCheck -check-prefix=CHECK-CC1 %s
-// CHECK-CC1: EnumConstantDecl:{ResultType enum <anonymous>}{TypedText Red}
+// CHECK-CC1: EnumConstantDecl:{ResultType enum Color}{TypedText Color_Red}
+
+// RUN: c-index-test -code-completion-at=%s:12:8 %s | FileCheck -check-prefix=CHECK-CC2 %s
+// CHECK-CC2: EnumConstantDecl:{ResultType enum Color}{TypedText Color_Blue} (7)
+// CHECK-CC2-NEXT: EnumConstantDecl:{ResultType enum Color}{TypedText Color_Green} (7)
+// CHECK-CC2-NEXT: EnumConstantDecl:{ResultType enum Color}{TypedText Color_Red} (7)