From: Ted Kremenek Date: Wed, 8 Dec 2010 23:43:14 +0000 (+0000) Subject: Add new libclang hooks for CXCursorSet, a X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=eca099bdb0178d408d4f717c2e9627e0d0e673c6;p=clang Add new libclang hooks for CXCursorSet, a DenseMap-backed hashtable for doing client-side management of CXCursors within a set. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@121318 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang-c/Index.h b/include/clang-c/Index.h index d7a4b602bb..0be4d4a02c 100644 --- a/include/clang-c/Index.h +++ b/include/clang-c/Index.h @@ -1460,7 +1460,38 @@ CINDEX_LINKAGE enum CXLanguageKind { */ CINDEX_LINKAGE enum CXLanguageKind clang_getCursorLanguage(CXCursor cursor); - + +/** + * \brief A fast container representing a set of CXCursors. + */ +typedef struct CXCursorSetImpl *CXCursorSet; + +/** + * \brief Creates an empty CXCursorSet. + */ +CINDEX_LINKAGE CXCursorSet clang_createCXCursorSet(); + +/** + * \brief Disposes a CXCursorSet and releases its associated memory. + */ +CINDEX_LINKAGE void clang_disposeCXCursorSet(CXCursorSet cset); + +/** + * \brief Queries a CXCursorSet to see if it contains a specific CXCursor. + * + * \returns non-zero if the set contains the specified cursor. +*/ +CINDEX_LINKAGE unsigned clang_CXCursorSet_contains(CXCursorSet cset, + CXCursor cursor); + +/** + * \brief Inserts a CXCursor into a CXCursorSet. + * + * \returns zero if the CXCursor was already in the set, and non-zero otherwise. +*/ +CINDEX_LINKAGE unsigned clang_CXCursorSet_insert(CXCursorSet cset, + CXCursor cursor); + /** * \brief Determine the semantic parent of the given cursor. * diff --git a/tools/libclang/CXCursor.cpp b/tools/libclang/CXCursor.cpp index 9d485f8acf..26cb2d20b2 100644 --- a/tools/libclang/CXCursor.cpp +++ b/tools/libclang/CXCursor.cpp @@ -493,3 +493,68 @@ bool cxcursor::isFirstInDeclGroup(CXCursor C) { return ((uintptr_t) (C.data[1])) != 0; } +//===----------------------------------------------------------------------===// +// CXCursorSet. +//===----------------------------------------------------------------------===// + +typedef llvm::DenseMap CXCursorSet_Impl; + +static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) { + return (CXCursorSet) setImpl; +} +static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) { + return (CXCursorSet_Impl*) set; +} +namespace llvm { +template<> class llvm::DenseMapInfo { +public: + static inline CXCursor getEmptyKey() { + return MakeCXCursorInvalid(CXCursor_InvalidFile); + } + static inline CXCursor getTombstoneKey() { + return MakeCXCursorInvalid(CXCursor_NoDeclFound); + } + static inline unsigned getHashValue(const CXCursor &cursor) { + return llvm::DenseMapInfo > + ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1])); + } + static inline bool isEqual(const CXCursor &x, const CXCursor &y) { + return x.kind == y.kind && + x.data[0] == y.data[0] && + x.data[1] == y.data[1]; + } +}; +} + +extern "C" { +CXCursorSet clang_createCXCursorSet() { + return packCXCursorSet(new CXCursorSet_Impl()); +} + +void clang_disposeCXCursorSet(CXCursorSet set) { + delete unpackCXCursorSet(set); +} + +unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) { + CXCursorSet_Impl *setImpl = unpackCXCursorSet(set); + if (!setImpl) + return 0; + return setImpl->find(cursor) == setImpl->end(); +} + +unsigned clang_CXCurorSet_insert(CXCursorSet set, CXCursor cursor) { + // Do not insert invalid cursors into the set. + if (cursor.kind >= CXCursor_FirstInvalid && + cursor.kind <= CXCursor_LastInvalid) + return 1; + + CXCursorSet_Impl *setImpl = unpackCXCursorSet(set); + if (!setImpl) + return 1; + unsigned &entry = (*setImpl)[cursor]; + unsigned flag = entry == 0 ? 1 : 0; + entry = 1; + return flag; +} +} // end: extern "C" + diff --git a/tools/libclang/libclang.darwin.exports b/tools/libclang/libclang.darwin.exports index 936ad46451..33a0f8382f 100644 --- a/tools/libclang/libclang.darwin.exports +++ b/tools/libclang/libclang.darwin.exports @@ -1,3 +1,5 @@ +_clang_CXCursorSet_contains +_clang_CXCursorSet_insert _clang_CXXMethod_isStatic _clang_annotateTokens _clang_codeCompleteAt @@ -9,6 +11,7 @@ _clang_constructUSR_ObjCIvar _clang_constructUSR_ObjCMethod _clang_constructUSR_ObjCProperty _clang_constructUSR_ObjCProtocol +_clang_createCXCursorSet _clang_createIndex _clang_createTranslationUnit _clang_createTranslationUnitFromSourceFile @@ -17,6 +20,7 @@ _clang_defaultDiagnosticDisplayOptions _clang_defaultEditingTranslationUnitOptions _clang_defaultReparseOptions _clang_defaultSaveOptions +_clang_disposeCXCursorSet _clang_disposeCodeCompleteResults _clang_disposeDiagnostic _clang_disposeIndex @@ -25,10 +29,10 @@ _clang_disposeString _clang_disposeTokens _clang_disposeTranslationUnit _clang_enableStackTraces -_clang_executeOnThread _clang_equalCursors _clang_equalLocations _clang_equalTypes +_clang_executeOnThread _clang_formatDiagnostic _clang_getCString _clang_getCXXAccessSpecifier @@ -120,3 +124,4 @@ _clang_saveTranslationUnit _clang_sortCodeCompletionResults _clang_tokenize _clang_visitChildren +_clang_visitChildrenWithBlock diff --git a/tools/libclang/libclang.exports b/tools/libclang/libclang.exports index de770eea45..d966b6654e 100644 --- a/tools/libclang/libclang.exports +++ b/tools/libclang/libclang.exports @@ -1,3 +1,5 @@ +clang_CXCursorSet_contains +clang_CXCursorSet_insert clang_CXXMethod_isStatic clang_annotateTokens clang_codeCompleteAt @@ -9,6 +11,7 @@ clang_constructUSR_ObjCIvar clang_constructUSR_ObjCMethod clang_constructUSR_ObjCProperty clang_constructUSR_ObjCProtocol +clang_createCXCursorSet clang_createIndex clang_createTranslationUnit clang_createTranslationUnitFromSourceFile @@ -17,6 +20,7 @@ clang_defaultDiagnosticDisplayOptions clang_defaultEditingTranslationUnitOptions clang_defaultReparseOptions clang_defaultSaveOptions +clang_disposeCXCursorSet clang_disposeCodeCompleteResults clang_disposeDiagnostic clang_disposeIndex @@ -25,10 +29,10 @@ clang_disposeString clang_disposeTokens clang_disposeTranslationUnit clang_enableStackTraces -clang_executeOnThread clang_equalCursors clang_equalLocations clang_equalTypes +clang_executeOnThread clang_formatDiagnostic clang_getCString clang_getCXXAccessSpecifier