libclang: Add a function to libclang for retrieving the bit width value

Patch by Jyun-Yan You.

llvm-svn: 169276
This commit is contained in:
Dmitri Gribenko 2012-12-04 15:13:46 +00:00
parent 242b6305f0
commit b506ba1d79
5 changed files with 70 additions and 1 deletions

View File

@ -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) \
@ -2682,6 +2682,13 @@ CINDEX_LINKAGE long long clang_getEnumConstantDeclValue(CXCursor C);
*/
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.

View File

@ -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

View File

@ -1134,6 +1134,23 @@ static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
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 @@ static void print_usage(void) {
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 @@ int cindextest_main(int argc, const char **argv) {
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);

View File

@ -265,6 +265,21 @@ unsigned long long clang_getEnumConstantDeclUnsignedValue(CXCursor C) {
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;

View File

@ -160,6 +160,7 @@ clang_getElementType
clang_getEnumConstantDeclUnsignedValue
clang_getEnumConstantDeclValue
clang_getEnumDeclIntegerType
clang_getFieldDeclBitWidth
clang_getExpansionLocation
clang_getFile
clang_getFileName