]> granicus.if.org Git - clang/commitdiff
Add a new libclang API to return a CXCompletionString for an arbitrary
authorDouglas Gregor <dgregor@apple.com>
Thu, 4 Aug 2011 20:04:59 +0000 (20:04 +0000)
committerDouglas Gregor <dgregor@apple.com>
Thu, 4 Aug 2011 20:04:59 +0000 (20:04 +0000)
cursor, from Connor Wakamo! Addresses <rdar://problem/9087798>.

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

include/clang-c/Index.h
include/clang/Frontend/ASTUnit.h
lib/Frontend/ASTUnit.cpp
test/Index/get-cursor.cpp
tools/c-index-test/c-index-test.c
tools/libclang/CXCursor.cpp
tools/libclang/libclang.darwin.exports
tools/libclang/libclang.exports

index e52ed46131665ca4f3a3c31eda575ca3adec8804..e16a21ef3593a932cf850799abe12502f0fe1e68 100644 (file)
@@ -2898,6 +2898,18 @@ clang_getCompletionPriority(CXCompletionString completion_string);
 CINDEX_LINKAGE enum CXAvailabilityKind 
 clang_getCompletionAvailability(CXCompletionString completion_string);
 
+/**
+ * \brief Retrieve a completion string for an arbitrary declaration or macro
+ * definition cursor.
+ *
+ * \param cursor The cursor to query.
+ *
+ * \returns A non-context-sensitive completion string for declaration and macro
+ * definition cursors, or NULL for other kinds of cursors.
+ */
+CINDEX_LINKAGE CXCompletionString
+clang_getCursorCompletionString(CXCursor cursor);
+  
 /**
  * \brief Contains the results of code-completion.
  *
index 6a3f9d5d4228860c209f0c0f428aa6754904e1fd..4f81de53be20ea26b50af74b5c311980f488f778 100644 (file)
@@ -310,10 +310,24 @@ public:
     return CachedCompletionAllocator;
   }
   
+  /// \brief Retrieve the allocator used to cache global code completions.
+  /// Creates the allocator if it doesn't already exist.
+  llvm::IntrusiveRefCntPtr<GlobalCodeCompletionAllocator>
+  getCursorCompletionAllocator() {
+    if (!CursorCompletionAllocator.getPtr()) {
+      CursorCompletionAllocator = new GlobalCodeCompletionAllocator;
+    }
+    return CursorCompletionAllocator;
+  }
+  
 private:
   /// \brief Allocator used to store cached code completions.
   llvm::IntrusiveRefCntPtr<GlobalCodeCompletionAllocator>
     CachedCompletionAllocator;
+  
+  /// \brief Allocator used to store code completions for arbitrary cursors.
+  llvm::IntrusiveRefCntPtr<GlobalCodeCompletionAllocator>
+    CursorCompletionAllocator;
 
   /// \brief The set of cached code-completion results.
   std::vector<CachedCodeCompletionResult> CachedCompletionResults;
index f19acd093a6e9f5de62945ff32689f144a7f4469..d9635c5de459573225b40d0a67c8ddd7d410efbe 100644 (file)
@@ -1903,6 +1903,10 @@ bool ASTUnit::Reparse(RemappedFile *RemappedFiles, unsigned NumRemappedFiles) {
       CurrentTopLevelHashValue != CompletionCacheTopLevelHashValue)
     CacheCodeCompletionResults();
 
+  // We now need to clear out the completion allocator for
+  // clang_getCursorCompletionString; it'll be recreated if necessary.
+  CursorCompletionAllocator = 0;
+  
   return Result;
 }
 
index 2aa76c4725695ea2292fb8d8aa9587c70b5542ab..441ed1cdabe616888ec33f78a2daa4de232cfaec 100644 (file)
@@ -35,6 +35,14 @@ void test() {
   X foo;
 }
 
+// RUN: c-index-test -cursor-at=%s:6:4 %s | FileCheck -check-prefix=CHECK-COMPLETION-1 %s
+// CHECK-COMPLETION-1: CXXConstructor=X:6:3
+// CHECK-COMPLETION-1-NEXT: Completion string: {TypedText X}{LeftParen (}{Placeholder int}{Comma , }{Placeholder int}{RightParen )}
+
+// RUN: c-index-test -cursor-at=%s:31:16 %s | FileCheck -check-prefix=CHECK-COMPLETION-2 %s
+// CHECK-COMPLETION-2: CXXMethod=getAnotherX:31:5 (Definition)
+// CHECK-COMPLETION-2-NEXT: Completion string: {ResultType X}{TypedText getAnotherX}{LeftParen (}{RightParen )}
+
 // RUN: c-index-test -cursor-at=%s:12:20 %s | FileCheck -check-prefix=CHECK-VALUE-REF %s
 // RUN: c-index-test -cursor-at=%s:13:21 %s | FileCheck -check-prefix=CHECK-VALUE-REF %s
 // RUN: c-index-test -cursor-at=%s:13:28 %s | FileCheck -check-prefix=CHECK-VALUE-REF %s
@@ -68,3 +76,7 @@ void test() {
 
 // RUN: c-index-test -cursor-at=%s:35:5 %s | FileCheck -check-prefix=CHECK-DECL %s
 // CHECK-DECL: VarDecl=foo:35:5
+
+// RUN: c-index-test -cursor-at=%s:21:3 %s | FileCheck -check-prefix=CHECK-MEMBER %s
+// CHECK-MEMBER: FieldDecl=member:21:7 (Definition)
+// CHECK-MEMBER-NEXT: Completion string: {ResultType int}{TypedText member}
index b737a38229e965cae901ec0d015dd346f7ef761a..0fec79848cc3fae1e269b9ef50298486730f2e1a 100644 (file)
@@ -1333,7 +1333,13 @@ int inspect_cursor_at(int argc, const char **argv) {
                                clang_getLocation(TU, file, Locations[Loc].line,
                                                  Locations[Loc].column));
       if (I + 1 == Repeats) {
+        CXCompletionString completionString = clang_getCursorCompletionString(
+                                                                        Cursor);
         PrintCursor(TU, Cursor);
+        if (completionString != NULL) {
+          printf("\nCompletion string: ");
+          print_completion_string(completionString, stdout);
+        }
         printf("\n");
         free(Locations[Loc].filename);
       }
index b3c57dc9344f7cb0623e1edda8127c1fea2500ac..bbc93438a5bc8c02c79dd0db71ba2a22e549d1d7 100644 (file)
@@ -577,4 +577,40 @@ unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
   entry = 1;
   return flag;
 }
+  
+CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
+  enum CXCursorKind kind = clang_getCursorKind(cursor);
+  if (clang_isDeclaration(kind)) {
+    Decl *decl = getCursorDecl(cursor);
+    if (isa<NamedDecl>(decl)) {
+      NamedDecl *namedDecl = (NamedDecl *)decl;
+      ASTUnit *unit = getCursorASTUnit(cursor);
+      if (unit->hasSema()) {
+        Sema &S = unit->getSema();
+        CodeCompletionAllocator *Allocator 
+          = unit->getCursorCompletionAllocator().getPtr();
+        CodeCompletionResult Result(namedDecl);
+        CodeCompletionString *String 
+          = Result.CreateCodeCompletionString(S, *Allocator);
+        return String;
+      }
+    }
+  }
+  else if (kind == CXCursor_MacroDefinition) {
+    MacroDefinition *definition = getCursorMacroDefinition(cursor);
+    const IdentifierInfo *MacroInfo = definition->getName();
+    ASTUnit *unit = getCursorASTUnit(cursor);
+    if (unit->hasSema()) {
+      Sema &S = unit->getSema();
+      CodeCompletionAllocator *Allocator
+        = unit->getCursorCompletionAllocator().getPtr();
+      CodeCompletionResult Result((IdentifierInfo *)MacroInfo);
+      CodeCompletionString *String 
+        = Result.CreateCodeCompletionString(S, *Allocator);
+      return String;
+    }
+  }
+  return NULL;
+}
+  
 } // end: extern "C"
index 59905364d8dd07b576421d9ab22e89f430abe07e..a858f624c88ec08e2eda2664fc96f4fee4ef9c1f 100644 (file)
@@ -55,6 +55,7 @@ _clang_getCompletionChunkText
 _clang_getCompletionPriority
 _clang_getCursor
 _clang_getCursorAvailability
+_clang_getCursorCompletionString
 _clang_getCursorDefinition
 _clang_getCursorDisplayName
 _clang_getCursorExtent
index ab9face5ec38c9f7f1c66d175a1d197b27fb1736..85e9f0269504b49c8ebaa98e7939261de74160fa 100644 (file)
@@ -55,6 +55,7 @@ clang_getCompletionChunkText
 clang_getCompletionPriority
 clang_getCursor
 clang_getCursorAvailability
+clang_getCursorCompletionString
 clang_getCursorDefinition
 clang_getCursorDisplayName
 clang_getCursorExtent