Add a new libclang API to return a CXCompletionString for an arbitrary
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
diff --git a/tools/libclang/CXCursor.cpp b/tools/libclang/CXCursor.cpp
index b3c57dc..bbc9343 100644
--- a/tools/libclang/CXCursor.cpp
+++ b/tools/libclang/CXCursor.cpp
@@ -577,4 +577,40 @@
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"