libclang: Add a function to libclang for retrieving the bit width value
Patch by Jyun-Yan You.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@169276 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang-c/Index.h b/include/clang-c/Index.h
index 24c754d..ac8f4df 100644
--- a/include/clang-c/Index.h
+++ b/include/clang-c/Index.h
@@ -32,7 +32,7 @@
* compatible, thus CINDEX_VERSION_MAJOR is expected to remain stable.
*/
#define CINDEX_VERSION_MAJOR 0
-#define CINDEX_VERSION_MINOR 7
+#define CINDEX_VERSION_MINOR 8
#define CINDEX_VERSION_ENCODE(major, minor) ( \
((major) * 10000) \
@@ -2683,6 +2683,13 @@
CINDEX_LINKAGE unsigned long long clang_getEnumConstantDeclUnsignedValue(CXCursor C);
/**
+ * \brief Retrieve the bit width of a bit field declaration as an integer.
+ *
+ * If a cursor that is not a bit field declaration is passed in, -1 is returned.
+ */
+CINDEX_LINKAGE int clang_getFieldDeclBitWidth(CXCursor C);
+
+/**
* \brief Retrieve the number of non-variadic arguments associated with a given
* cursor.
*
diff --git a/test/Index/print-bitwidth.c b/test/Index/print-bitwidth.c
new file mode 100644
index 0000000..e9e330a
--- /dev/null
+++ b/test/Index/print-bitwidth.c
@@ -0,0 +1,25 @@
+union S {
+ unsigned ac : 4;
+ unsigned : 4;
+ unsigned clock : 1;
+ unsigned : 0;
+ unsigned flag : 1;
+};
+
+struct X {
+ unsigned light : 1;
+ unsigned toaster : 1;
+ int count;
+ union S stat;
+};
+
+// RUN: c-index-test -test-print-bitwidth %s | FileCheck %s
+// CHECK: FieldDecl=ac:2:12 (Definition) bitwidth=4
+// CHECK: FieldDecl=:3:3 (Definition) bitwidth=4
+// CHECK: FieldDecl=clock:4:12 (Definition) bitwidth=1
+// CHECK: FieldDecl=:5:3 (Definition) bitwidth=0
+// CHECK: FieldDecl=flag:6:12 (Definition) bitwidth=1
+// CHECK: FieldDecl=light:10:12 (Definition) bitwidth=1
+// CHECK: FieldDecl=toaster:11:12 (Definition) bitwidth=1
+// CHECK-NOT: count
+// CHECK-NOT: stat
diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c
index 3e4404c..5ca148f 100644
--- a/tools/c-index-test/c-index-test.c
+++ b/tools/c-index-test/c-index-test.c
@@ -1134,6 +1134,23 @@
return CXChildVisit_Recurse;
}
+/******************************************************************************/
+/* Bitwidth testing. */
+/******************************************************************************/
+
+static enum CXChildVisitResult PrintBitWidth(CXCursor cursor, CXCursor p,
+ CXClientData d) {
+ if (clang_getCursorKind(cursor) != CXCursor_FieldDecl)
+ return CXChildVisit_Recurse;
+
+ int Bitwidth = clang_getFieldDeclBitWidth(cursor);
+ if (Bitwidth >= 0) {
+ PrintCursor(cursor, NULL);
+ printf(" bitwidth=%d\n", Bitwidth);
+ }
+
+ return CXChildVisit_Recurse;
+}
/******************************************************************************/
/* Loading ASTs/source. */
@@ -3382,6 +3399,7 @@
fprintf(stderr,
" c-index-test -test-print-linkage-source {<args>}*\n"
" c-index-test -test-print-typekind {<args>}*\n"
+ " c-index-test -test-print-bitwidth {<args>}*\n"
" c-index-test -print-usr [<CursorKind> {<args>}]*\n"
" c-index-test -print-usr-file <file>\n"
" c-index-test -write-pch <file> <compiler arguments>\n");
@@ -3463,6 +3481,9 @@
else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
return perform_test_load_source(argc - 2, argv + 2, "all",
PrintTypeKind, 0);
+ else if (argc > 2 && strcmp(argv[1], "-test-print-bitwidth") == 0)
+ return perform_test_load_source(argc - 2, argv + 2, "all",
+ PrintBitWidth, 0);
else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
if (argc > 2)
return print_usrs(argv + 2, argv + argc);
diff --git a/tools/libclang/CXType.cpp b/tools/libclang/CXType.cpp
index c4b8a09..e2fbc0b 100644
--- a/tools/libclang/CXType.cpp
+++ b/tools/libclang/CXType.cpp
@@ -265,6 +265,21 @@
return ULLONG_MAX;
}
+int clang_getFieldDeclBitWidth(CXCursor C) {
+ using namespace cxcursor;
+
+ if (clang_isDeclaration(C.kind)) {
+ Decl *D = getCursorDecl(C);
+
+ if (FieldDecl *FD = dyn_cast_or_null<FieldDecl>(D)) {
+ if (FD->isBitField())
+ return FD->getBitWidthValue(getCursorContext(C));
+ }
+ }
+
+ return -1;
+}
+
CXType clang_getCanonicalType(CXType CT) {
if (CT.kind == CXType_Invalid)
return CT;
diff --git a/tools/libclang/libclang.exports b/tools/libclang/libclang.exports
index ee34edc..68bfb71 100644
--- a/tools/libclang/libclang.exports
+++ b/tools/libclang/libclang.exports
@@ -160,6 +160,7 @@
clang_getEnumConstantDeclUnsignedValue
clang_getEnumConstantDeclValue
clang_getEnumDeclIntegerType
+clang_getFieldDeclBitWidth
clang_getExpansionLocation
clang_getFile
clang_getFileName