r219809 - [libclang] Add function to retrieve storage class in libclang.
Argyrios Kyrtzidis
akyrtzi at gmail.com
Wed Oct 15 10:05:31 PDT 2014
Author: akirtzidis
Date: Wed Oct 15 12:05:31 2014
New Revision: 219809
URL: http://llvm.org/viewvc/llvm-project?rev=219809&view=rev
Log:
[libclang] Add function to retrieve storage class in libclang.
Patch by guibufolo!
Modified:
cfe/trunk/bindings/python/clang/cindex.py
cfe/trunk/include/clang-c/Index.h
cfe/trunk/tools/libclang/CIndex.cpp
cfe/trunk/tools/libclang/libclang.exports
Modified: cfe/trunk/bindings/python/clang/cindex.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/clang/cindex.py?rev=219809&r1=219808&r2=219809&view=diff
==============================================================================
--- cfe/trunk/bindings/python/clang/cindex.py (original)
+++ cfe/trunk/bindings/python/clang/cindex.py Wed Oct 15 12:05:31 2014
@@ -1247,6 +1247,17 @@ class Cursor(Structure):
return self._extent
@property
+ def storage_class(self):
+ """
+ Retrieves the storage class (if any) of the entity pointed at by the
+ cursor.
+ """
+ if not hasattr(self, '_storage_class'):
+ self._storage_class = conf.lib.clang_Cursor_getStorageClass(self)
+
+ return StorageClass.from_id(self._storage_class)
+
+ @property
def access_specifier(self):
"""
Retrieves the access specifier (if any) of the entity pointed at by the
@@ -1510,6 +1521,56 @@ class Cursor(Structure):
res._tu = args[0]._tu
return res
+class StorageClass(object):
+ """
+ Describes the storage class of a declaration
+ """
+
+ # The unique kind objects, index by id.
+ _kinds = []
+ _name_map = None
+
+ def __init__(self, value):
+ if value >= len(StorageClass._kinds):
+ StorageClass._kinds += [None] * (value - len(StorageClass._kinds) + 1)
+ if StorageClass._kinds[value] is not None:
+ raise ValueError,'StorageClass already loaded'
+ self.value = value
+ StorageClass._kinds[value] = self
+ StorageClass._name_map = None
+
+ def from_param(self):
+ return self.value
+
+ @property
+ def name(self):
+ """Get the enumeration name of this storage class."""
+ if self._name_map is None:
+ self._name_map = {}
+ for key,value in StorageClass.__dict__.items():
+ if isinstance(value,StorageClass):
+ self._name_map[value] = key
+ return self._name_map[self]
+
+ @staticmethod
+ def from_id(id):
+ if id >= len(StorageClass._kinds) or not StorageClass._kinds[id]:
+ raise ValueError,'Unknown storage class %d' % id
+ return StorageClass._kinds[id]
+
+ def __repr__(self):
+ return 'StorageClass.%s' % (self.name,)
+
+StorageClass.INVALID = StorageClass(0)
+StorageClass.NONE = StorageClass(1)
+StorageClass.EXTERN = StorageClass(2)
+StorageClass.STATIC = StorageClass(3)
+StorageClass.PRIVATEEXTERN = StorageClass(4)
+StorageClass.OPENCLWORKGROUPLOCAL = StorageClass(5)
+StorageClass.AUTO = StorageClass(6)
+StorageClass.REGISTER = StorageClass(7)
+
+
### C++ access specifiers ###
class AccessSpecifier(BaseEnumeration):
Modified: cfe/trunk/include/clang-c/Index.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=219809&r1=219808&r2=219809&view=diff
==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Wed Oct 15 12:05:31 2014
@@ -32,7 +32,7 @@
* compatible, thus CINDEX_VERSION_MAJOR is expected to remain stable.
*/
#define CINDEX_VERSION_MAJOR 0
-#define CINDEX_VERSION_MINOR 28
+#define CINDEX_VERSION_MINOR 29
#define CINDEX_VERSION_ENCODE(major, minor) ( \
((major) * 10000) \
@@ -3343,6 +3343,29 @@ enum CX_CXXAccessSpecifier {
CINDEX_LINKAGE enum CX_CXXAccessSpecifier clang_getCXXAccessSpecifier(CXCursor);
/**
+ * \brief Represents the storage classes as declared in the source. CX_SC_Invalid
+ * was added for the clase that the passed cursor in not a declaration.
+ */
+enum CX_StorageClass {
+ CX_SC_Invalid,
+ CX_SC_None,
+ CX_SC_Extern,
+ CX_SC_Static,
+ CX_SC_PrivateExtern,
+ CX_SC_OpenCLWorkGroupLocal,
+ CX_SC_Auto,
+ CX_SC_Register
+};
+
+/**
+ * \brief Returns the storage class for a function or variable declaration.
+ *
+ * If the passed in Cursor is not a function or variable declaration,
+ * CX_SC_Invalid is returned else the storage class.
+ */
+CINDEX_LINKAGE enum CX_StorageClass clang_Cursor_getStorageClass(CXCursor);
+
+/**
* \brief Determine the number of overloaded declarations referenced by a
* \c CXCursor_OverloadedDeclRef cursor.
*
Modified: cfe/trunk/tools/libclang/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndex.cpp?rev=219809&r1=219808&r2=219809&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndex.cpp (original)
+++ cfe/trunk/tools/libclang/CIndex.cpp Wed Oct 15 12:05:31 2014
@@ -6418,6 +6418,41 @@ static const Decl *maybeGetTemplateCurso
return D;
}
+
+enum CX_StorageClass clang_Cursor_getStorageClass(CXCursor C) {
+ StorageClass sc = SC_None;
+ const Decl *D = getCursorDecl(C);
+ if (D) {
+ if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
+ sc = FD->getStorageClass();
+ } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
+ sc = VD->getStorageClass();
+ } else {
+ return CX_SC_Invalid;
+ }
+ } else {
+ return CX_SC_Invalid;
+ }
+ switch (sc) {
+ case SC_None:
+ return CX_SC_None;
+ case SC_Extern:
+ return CX_SC_Extern;
+ case SC_Static:
+ return CX_SC_Static;
+ case SC_PrivateExtern:
+ return CX_SC_PrivateExtern;
+ case SC_OpenCLWorkGroupLocal:
+ return CX_SC_OpenCLWorkGroupLocal;
+ case SC_Auto:
+ return CX_SC_Auto;
+ case SC_Register:
+ return CX_SC_Register;
+ default:
+ return CX_SC_Invalid;
+ }
+}
+
CXCursor clang_getCursorSemanticParent(CXCursor cursor) {
if (clang_isDeclaration(cursor.kind)) {
if (const Decl *D = getCursorDecl(cursor)) {
Modified: cfe/trunk/tools/libclang/libclang.exports
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/libclang.exports?rev=219809&r1=219808&r2=219809&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/libclang.exports (original)
+++ cfe/trunk/tools/libclang/libclang.exports Wed Oct 15 12:05:31 2014
@@ -30,6 +30,7 @@ clang_Cursor_isNull
clang_Cursor_isObjCOptional
clang_Cursor_isVariadic
clang_Cursor_getModule
+clang_Cursor_getStorageClass
clang_File_isEqual
clang_Module_getASTFile
clang_Module_getParent
More information about the cfe-commits
mailing list