From: Dmitri Gribenko Date: Tue, 4 Dec 2012 15:13:46 +0000 (+0000) Subject: libclang: Add a function to libclang for retrieving the bit width value X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1eb60825f0b858a4568c1a9497cc61b0d90c9b3a;p=clang 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 24c754d995..ac8f4dfa10 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) \ @@ -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. diff --git a/test/Index/print-bitwidth.c b/test/Index/print-bitwidth.c new file mode 100644 index 0000000000..e9e330aaa0 --- /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 3e4404cbaa..5ca148f1e4 100644 --- a/tools/c-index-test/c-index-test.c +++ b/tools/c-index-test/c-index-test.c @@ -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 {}*\n" " c-index-test -test-print-typekind {}*\n" + " c-index-test -test-print-bitwidth {}*\n" " c-index-test -print-usr [ {}]*\n" " c-index-test -print-usr-file \n" " c-index-test -write-pch \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); diff --git a/tools/libclang/CXType.cpp b/tools/libclang/CXType.cpp index c4b8a09a54..e2fbc0be82 100644 --- a/tools/libclang/CXType.cpp +++ b/tools/libclang/CXType.cpp @@ -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(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 ee34edcbb2..68bfb71f18 100644 --- a/tools/libclang/libclang.exports +++ b/tools/libclang/libclang.exports @@ -160,6 +160,7 @@ clang_getElementType clang_getEnumConstantDeclUnsignedValue clang_getEnumConstantDeclValue clang_getEnumDeclIntegerType +clang_getFieldDeclBitWidth clang_getExpansionLocation clang_getFile clang_getFileName