[cfe-commits] r82264 - in /cfe/trunk: include/clang/Parse/Action.h include/clang/Sema/CodeCompleteConsumer.h lib/Parse/ParseExprCXX.cpp lib/Sema/CodeCompleteConsumer.cpp lib/Sema/Sema.h lib/Sema/SemaCodeComplete.cpp test/CodeCompletion/operator.cpp

Douglas Gregor dgregor at apple.com
Fri Sep 18 13:05:18 PDT 2009


Author: dgregor
Date: Fri Sep 18 15:05:18 2009
New Revision: 82264

URL: http://llvm.org/viewvc/llvm-project?rev=82264&view=rev
Log:
C++ code completion after the "operator" keyword. Provide overloaded
operators, type specifiers, type names, and nested-name-specifiers.


Added:
    cfe/trunk/test/CodeCompletion/operator.cpp   (with props)
Modified:
    cfe/trunk/include/clang/Parse/Action.h
    cfe/trunk/include/clang/Sema/CodeCompleteConsumer.h
    cfe/trunk/lib/Parse/ParseExprCXX.cpp
    cfe/trunk/lib/Sema/CodeCompleteConsumer.cpp
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaCodeComplete.cpp

Modified: cfe/trunk/include/clang/Parse/Action.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/Action.h?rev=82264&r1=82263&r2=82264&view=diff

==============================================================================
--- cfe/trunk/include/clang/Parse/Action.h (original)
+++ cfe/trunk/include/clang/Parse/Action.h Fri Sep 18 15:05:18 2009
@@ -2270,6 +2270,14 @@
   ///
   /// \param S the scope in which the namespace alias declaration occurs.
   virtual void CodeCompleteNamespaceAliasDecl(Scope *S) { }
+  
+  /// \brief Code completion for an operator name.
+  ///
+  /// This code completion action is invoked when the code-completion token is
+  /// found after the keyword "operator".
+  ///
+  /// \param S the scope in which the operator keyword occurs.
+  virtual void CodeCompleteOperatorName(Scope *S) { }
   //@}
 };
 

Modified: cfe/trunk/include/clang/Sema/CodeCompleteConsumer.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/CodeCompleteConsumer.h?rev=82264&r1=82263&r2=82264&view=diff

==============================================================================
--- cfe/trunk/include/clang/Sema/CodeCompleteConsumer.h (original)
+++ cfe/trunk/include/clang/Sema/CodeCompleteConsumer.h Fri Sep 18 15:05:18 2009
@@ -219,7 +219,15 @@
   /// found after "namespace identifier = ".
   ///
   /// \param S the scope in which the namespace alias declaration occurs.
-  virtual void CodeCompleteNamespaceAliasDecl(Scope *S);  
+  virtual void CodeCompleteNamespaceAliasDecl(Scope *S);
+  
+  /// \brief Code completion for an operator name.
+  ///
+  /// This code completion action is invoked when the code-completion token is
+  /// found after the keyword "operator".
+  ///
+  /// \param S the scope in which the operator keyword occurs.
+  virtual void CodeCompleteOperatorName(Scope *S);
   //@}
   
   /// \name Name lookup functions
@@ -248,14 +256,14 @@
   bool IsUnion(NamedDecl *ND) const;
   bool IsNamespace(NamedDecl *ND) const;
   bool IsNamespaceOrAlias(NamedDecl *ND) const;
+  bool IsType(NamedDecl *ND) const;
   //@}
   
   /// \name Utility functions
   ///
-  //@{
-  
+  //@{  
   bool canHiddenResultBeFound(NamedDecl *Hidden, NamedDecl *Visible);
-  
+  void AddTypeSpecifierResults(unsigned Rank, ResultSet &Results);
   //@}
 };
   

Modified: cfe/trunk/lib/Parse/ParseExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseExprCXX.cpp?rev=82264&r1=82263&r2=82264&view=diff

==============================================================================
--- cfe/trunk/lib/Parse/ParseExprCXX.cpp (original)
+++ cfe/trunk/lib/Parse/ParseExprCXX.cpp Fri Sep 18 15:05:18 2009
@@ -835,6 +835,18 @@
       *EndLoc = Loc;
     return OO_Subscript;
 
+  case tok::code_completion: {
+    // Code completion for the operator name.
+    Actions.CodeCompleteOperatorName(CurScope);
+    
+    // Consume the 'operator' token, then replace the code-completion token
+    // with an 'operator' token and try again.
+    SourceLocation OperatorLoc = ConsumeToken();
+    Tok.setLocation(OperatorLoc);
+    Tok.setKind(tok::kw_operator);
+    return TryParseOperatorFunctionId(EndLoc);
+  }
+      
   default:
     return OO_None;
   }

Modified: cfe/trunk/lib/Sema/CodeCompleteConsumer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/CodeCompleteConsumer.cpp?rev=82264&r1=82263&r2=82264&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/CodeCompleteConsumer.cpp (original)
+++ cfe/trunk/lib/Sema/CodeCompleteConsumer.cpp Fri Sep 18 15:05:18 2009
@@ -175,6 +175,28 @@
   ProcessCodeCompleteResults(Results.data(), Results.size());  
 }
 
+void CodeCompleteConsumer::CodeCompleteOperatorName(Scope *S) {
+  ResultSet Results(*this, &CodeCompleteConsumer::IsType);
+  
+  // Add the names of overloadable operators.
+#define OVERLOADED_OPERATOR(Name,Spelling,Token,Unary,Binary,MemberOnly)      \
+  if (strcmp(Spelling, "?"))                                                  \
+    Results.MaybeAddResult(Result(Spelling, 0));
+#include "clang/Basic/OperatorKinds.def"
+  
+  // Add any type names visible from the current scope
+  unsigned NextRank = CollectLookupResults(S, 0, Results);
+  
+  // Add any type specifiers
+  AddTypeSpecifierResults(0, Results);
+  
+  // Add any nested-name-specifiers
+  Results.setFilter(&CodeCompleteConsumer::IsNestedNameSpecifier);
+  CollectLookupResults(S, NextRank + 1, Results);
+
+  ProcessCodeCompleteResults(Results.data(), Results.size());  
+}
+
 void CodeCompleteConsumer::ResultSet::MaybeAddResult(Result R) {
   if (R.Kind != Result::RK_Declaration) {
     // For non-declaration results, just add the result.
@@ -208,7 +230,7 @@
 
   if (const IdentifierInfo *Id = R.Declaration->getIdentifier()) {
     // __va_list_tag is a freak of nature. Find it and skip it.
-    if (Id->isStr("__va_list_tag"))
+    if (Id->isStr("__va_list_tag") || Id->isStr("__builtin_va_list"))
       return;
 
     // FIXME: Should we filter out other names in the implementation's
@@ -522,6 +544,12 @@
   return isa<NamespaceDecl>(ND) || isa<NamespaceAliasDecl>(ND);
 }
 
+/// \brief Brief determines whether the given declaration is a namespace or
+/// namespace alias.
+bool CodeCompleteConsumer::IsType(NamedDecl *ND) const {
+  return isa<TypeDecl>(ND);
+}
+
 namespace {
   struct VISIBILITY_HIDDEN SortCodeCompleteResult {
     typedef CodeCompleteConsumer::Result Result;
@@ -588,6 +616,53 @@
   return HiddenCtx != Visible->getDeclContext()->getLookupContext();
 }
 
+/// \brief Add type specifiers for the current language as keyword results.
+void CodeCompleteConsumer::AddTypeSpecifierResults(unsigned Rank, 
+                                                   ResultSet &Results) {
+  Results.MaybeAddResult(Result("short", Rank));
+  Results.MaybeAddResult(Result("long", Rank));
+  Results.MaybeAddResult(Result("signed", Rank));
+  Results.MaybeAddResult(Result("unsigned", Rank));
+  Results.MaybeAddResult(Result("void", Rank));
+  Results.MaybeAddResult(Result("char", Rank));
+  Results.MaybeAddResult(Result("int", Rank));
+  Results.MaybeAddResult(Result("float", Rank));
+  Results.MaybeAddResult(Result("double", Rank));
+  Results.MaybeAddResult(Result("enum", Rank));
+  Results.MaybeAddResult(Result("struct", Rank));
+  Results.MaybeAddResult(Result("union", Rank));
+
+  if (getSema().getLangOptions().C99) {
+    // C99-specific
+    Results.MaybeAddResult(Result("_Complex", Rank));
+    Results.MaybeAddResult(Result("_Imaginary", Rank));
+    Results.MaybeAddResult(Result("_Bool", Rank));
+  }
+  
+  if (getSema().getLangOptions().CPlusPlus) {
+    // C++-specific
+    Results.MaybeAddResult(Result("bool", Rank));
+    Results.MaybeAddResult(Result("class", Rank));
+    Results.MaybeAddResult(Result("typename", Rank));
+    Results.MaybeAddResult(Result("wchar_t", Rank));
+    
+    if (getSema().getLangOptions().CPlusPlus0x) {
+      Results.MaybeAddResult(Result("char16_t", Rank));
+      Results.MaybeAddResult(Result("char32_t", Rank));
+      Results.MaybeAddResult(Result("decltype", Rank));
+    }
+  }
+  
+  // GNU extensions
+  if (getSema().getLangOptions().GNUMode) {
+    // FIXME: Enable when we actually support decimal floating point.
+    //    Results.MaybeAddResult(Result("_Decimal32", Rank));
+    //    Results.MaybeAddResult(Result("_Decimal64", Rank));
+    //    Results.MaybeAddResult(Result("_Decimal128", Rank));
+    Results.MaybeAddResult(Result("typeof", Rank));
+  }
+}
+
 void 
 PrintingCodeCompleteConsumer::ProcessCodeCompleteResults(Result *Results, 
                                                          unsigned NumResults) {

Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=82264&r1=82263&r2=82264&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Fri Sep 18 15:05:18 2009
@@ -3642,6 +3642,7 @@
   virtual void CodeCompleteUsingDirective(Scope *S);
   virtual void CodeCompleteNamespaceDecl(Scope *S);
   virtual void CodeCompleteNamespaceAliasDecl(Scope *S);
+  virtual void CodeCompleteOperatorName(Scope *S);
   //@}
   
   //===--------------------------------------------------------------------===//

Modified: cfe/trunk/lib/Sema/SemaCodeComplete.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaCodeComplete.cpp?rev=82264&r1=82263&r2=82264&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaCodeComplete.cpp (original)
+++ cfe/trunk/lib/Sema/SemaCodeComplete.cpp Fri Sep 18 15:05:18 2009
@@ -101,4 +101,10 @@
   CodeCompleter->CodeCompleteNamespaceAliasDecl(S);
 }
 
+void Sema::CodeCompleteOperatorName(Scope *S) {
+  if (!CodeCompleter)
+    return;
+  
+  CodeCompleter->CodeCompleteOperatorName(S);
+}
 

Added: cfe/trunk/test/CodeCompletion/operator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeCompletion/operator.cpp?rev=82264&view=auto

==============================================================================
--- cfe/trunk/test/CodeCompletion/operator.cpp (added)
+++ cfe/trunk/test/CodeCompletion/operator.cpp Fri Sep 18 15:05:18 2009
@@ -0,0 +1,19 @@
+// RUN: clang-cc -fsyntax-only -code-completion-dump=1 %s -o - | FileCheck -check-prefix=CC1 %s &&
+// RUN: true
+
+class T { };
+
+typedef int Integer;
+
+namespace N { }
+
+void f() {
+  typedef float Float;
+  
+  // CHECK-CC1: Float : 0
+  // CHECK-CC1: + : 0
+  // CHECK-CC1: short : 0
+  // CHECK-CC1: Integer : 2
+  // CHECK-CC1: T : 2
+  // CHECK-CC1: N : 5
+  operator

Propchange: cfe/trunk/test/CodeCompletion/operator.cpp

------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/trunk/test/CodeCompletion/operator.cpp

------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cfe/trunk/test/CodeCompletion/operator.cpp

------------------------------------------------------------------------------
    svn:mime-type = text/plain





More information about the cfe-commits mailing list