[cfe-commits] r109677 - in /cfe/trunk: lib/Sema/Sema.h lib/Sema/SemaCodeComplete.cpp test/CodeCompletion/enum-switch-case.c

Douglas Gregor dgregor at apple.com
Wed Jul 28 14:50:18 PDT 2010


Author: dgregor
Date: Wed Jul 28 16:50:18 2010
New Revision: 109677

URL: http://llvm.org/viewvc/llvm-project?rev=109677&view=rev
Log:
When performing code completion for a case statement in a switch whose
condition is not of enumeration type, provide code-completion results
containing all values of integral or enumeral type.

Modified:
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaCodeComplete.cpp
    cfe/trunk/test/CodeCompletion/enum-switch-case.c

Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=109677&r1=109676&r2=109677&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Wed Jul 28 16:50:18 2010
@@ -4526,7 +4526,8 @@
   //@{
   virtual void CodeCompleteOrdinaryName(Scope *S,
                                      CodeCompletionContext CompletionContext);
-  virtual void CodeCompleteExpression(Scope *S, QualType T);
+  virtual void CodeCompleteExpression(Scope *S, QualType T,
+                                      bool IntegralConstantExpression = false);
   virtual void CodeCompleteMemberReferenceExpr(Scope *S, ExprTy *Base,
                                                SourceLocation OpLoc,
                                                bool IsArrow);

Modified: cfe/trunk/lib/Sema/SemaCodeComplete.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaCodeComplete.cpp?rev=109677&r1=109676&r2=109677&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaCodeComplete.cpp (original)
+++ cfe/trunk/lib/Sema/SemaCodeComplete.cpp Wed Jul 28 16:50:18 2010
@@ -227,6 +227,7 @@
     //@{
     bool IsOrdinaryName(NamedDecl *ND) const;
     bool IsOrdinaryNonTypeName(NamedDecl *ND) const;
+    bool IsIntegralConstantValue(NamedDecl *ND) const;
     bool IsOrdinaryNonValueName(NamedDecl *ND) const;
     bool IsNestedNameSpecifier(NamedDecl *ND) const;
     bool IsEnum(NamedDecl *ND) const;
@@ -821,6 +822,17 @@
   return ND->getIdentifierNamespace() & IDNS;
 }
 
+bool ResultBuilder::IsIntegralConstantValue(NamedDecl *ND) const {
+  if (!IsOrdinaryNonTypeName(ND))
+    return 0;
+  
+  if (ValueDecl *VD = dyn_cast<ValueDecl>(ND->getUnderlyingDecl()))
+    if (VD->getType()->isIntegralOrEnumerationType())
+      return true;
+        
+  return false;
+}
+
 /// \brief Determines whether this given declaration will be found by
 /// ordinary name lookup.
 bool ResultBuilder::IsOrdinaryNonValueName(NamedDecl *ND) const {
@@ -2271,11 +2283,17 @@
 
 /// \brief Perform code-completion in an expression context when we know what
 /// type we're looking for.
-void Sema::CodeCompleteExpression(Scope *S, QualType T) {
+///
+/// \param IntegralConstantExpression Only permit integral constant 
+/// expressions.
+void Sema::CodeCompleteExpression(Scope *S, QualType T,
+                                  bool IntegralConstantExpression) {
   typedef CodeCompleteConsumer::Result Result;
   ResultBuilder Results(*this);
   
-  if (WantTypesInContext(CCC_Expression, getLangOptions()))
+  if (IntegralConstantExpression)
+    Results.setFilter(&ResultBuilder::IsIntegralConstantValue);
+  else if (WantTypesInContext(CCC_Expression, getLangOptions()))
     Results.setFilter(&ResultBuilder::IsOrdinaryName);
   else
     Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName);
@@ -2476,8 +2494,10 @@
     return;
   
   SwitchStmt *Switch = getSwitchStack().back();
-  if (!Switch->getCond()->getType()->isEnumeralType())
+  if (!Switch->getCond()->getType()->isEnumeralType()) {
+    CodeCompleteExpression(S, Switch->getCond()->getType(), true);
     return;
+  }
   
   // Code-complete the cases of a switch statement over an enumeration type
   // by providing the list of 

Modified: cfe/trunk/test/CodeCompletion/enum-switch-case.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeCompletion/enum-switch-case.c?rev=109677&r1=109676&r2=109677&view=diff
==============================================================================
--- cfe/trunk/test/CodeCompletion/enum-switch-case.c (original)
+++ cfe/trunk/test/CodeCompletion/enum-switch-case.c Wed Jul 28 16:50:18 2010
@@ -18,11 +18,28 @@
 
     case Green:
       break;
-      
+  }
+
+  unsigned c2;
+  switch (c2) {
+    case 
+  }
+
     // RUN: %clang_cc1 -fsyntax-only -code-completion-at=%s:19:10 %s -o - | FileCheck -check-prefix=CC1 %s
     // CHECK-CC1: Blue
     // CHECK-CC1-NEXT: Green
     // CHECK-CC1-NEXT: Indigo
     // CHECK-CC1-NEXT: Orange
     // CHECK-CC1-NEXT: Violet
-      
+
+    // RUN: %clang_cc1 -fsyntax-only -code-completion-at=%s:25:10 %s -o - | FileCheck -check-prefix=CC2 %s      
+  // CHECK-CC2: COMPLETION: Blue : [#enum Color#]Blue
+  // CHECK-CC2-NEXT: COMPLETION: c2 : [#unsigned int#]c2
+  // CHECK-CC2-NEXT: COMPLETION: color : [#enum Color#]color
+  // CHECK-CC2-NEXT: COMPLETION: Green : [#enum Color#]Green
+  // CHECK-CC2-NEXT: COMPLETION: Indigo : [#enum Color#]Indigo
+  // CHECK-CC2-NEXT: COMPLETION: Orange : [#enum Color#]Orange
+  // CHECK-CC2-NEXT: COMPLETION: Red : [#enum Color#]Red
+  // CHECK-CC2-NEXT: COMPLETION: Pattern : sizeof(<#expression-or-type#>)
+  // CHECK-CC2-NEXT: COMPLETION: Violet : [#enum Color#]Violet
+  // CHECK-CC2-NEXT: COMPLETION: Yellow : [#enum Color#]Yellow





More information about the cfe-commits mailing list