collinbaker created this revision. collinbaker added a reviewer: rnk. Herald added a subscriber: arphaman. Herald added a project: All. collinbaker requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
In a class template, a bit field's width may depend on a template parameter. In this case the width expression cannot be evaluated. Previously clang_getFieldDeclBitWidth() would assert, or cause memory unsafety and return an invalid result if assertions are disabled. This adds a check for this case which returns an error code. An additional function clang_isFieldDeclBitWidthDependent() as added for users to check for this case. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D130303 Files: clang/include/clang-c/Index.h clang/tools/libclang/CXType.cpp Index: clang/tools/libclang/CXType.cpp =================================================================== --- clang/tools/libclang/CXType.cpp +++ clang/tools/libclang/CXType.cpp @@ -15,6 +15,7 @@ #include "CXString.h" #include "CXTranslationUnit.h" #include "CXType.h" +#include "clang-c/Index.h" #include "clang/AST/Decl.h" #include "clang/AST/DeclObjC.h" #include "clang/AST/DeclTemplate.h" @@ -377,6 +378,21 @@ return ULLONG_MAX; } +unsigned clang_isFieldDeclBitWidthDependent(CXCursor C) { + using namespace cxcursor; + + if (clang_isDeclaration(C.kind)) { + const Decl *D = getCursorDecl(C); + + if (const FieldDecl *FD = dyn_cast_or_null<FieldDecl>(D)) { + if (FD->isBitField() && FD->getBitWidth()->isValueDependent()) + return 1; + } + } + + return 0; +} + int clang_getFieldDeclBitWidth(CXCursor C) { using namespace cxcursor; @@ -384,7 +400,7 @@ const Decl *D = getCursorDecl(C); if (const FieldDecl *FD = dyn_cast_or_null<FieldDecl>(D)) { - if (FD->isBitField()) + if (FD->isBitField() && !FD->getBitWidth()->isValueDependent()) return FD->getBitWidthValue(getCursorContext(C)); } } Index: clang/include/clang-c/Index.h =================================================================== --- clang/include/clang-c/Index.h +++ clang/include/clang-c/Index.h @@ -3545,10 +3545,19 @@ CINDEX_LINKAGE unsigned long long clang_getEnumConstantDeclUnsignedValue(CXCursor C); +/** + * Returns non-zero if a bit field's width depends on template parameters. + * + * If the cursor does not reference a bit field declaration or if the bit + * field's width does not depend on template parameters, 0 is returned. + */ +CINDEX_LINKAGE unsigned clang_isFieldDeclBitWidthDependent(CXCursor C); + /** * 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. + * If a cursor that is not a bit field declaration is passed in, or if the bit + * field's width expression cannot be evaluated, -1 is returned. */ CINDEX_LINKAGE int clang_getFieldDeclBitWidth(CXCursor C);
Index: clang/tools/libclang/CXType.cpp =================================================================== --- clang/tools/libclang/CXType.cpp +++ clang/tools/libclang/CXType.cpp @@ -15,6 +15,7 @@ #include "CXString.h" #include "CXTranslationUnit.h" #include "CXType.h" +#include "clang-c/Index.h" #include "clang/AST/Decl.h" #include "clang/AST/DeclObjC.h" #include "clang/AST/DeclTemplate.h" @@ -377,6 +378,21 @@ return ULLONG_MAX; } +unsigned clang_isFieldDeclBitWidthDependent(CXCursor C) { + using namespace cxcursor; + + if (clang_isDeclaration(C.kind)) { + const Decl *D = getCursorDecl(C); + + if (const FieldDecl *FD = dyn_cast_or_null<FieldDecl>(D)) { + if (FD->isBitField() && FD->getBitWidth()->isValueDependent()) + return 1; + } + } + + return 0; +} + int clang_getFieldDeclBitWidth(CXCursor C) { using namespace cxcursor; @@ -384,7 +400,7 @@ const Decl *D = getCursorDecl(C); if (const FieldDecl *FD = dyn_cast_or_null<FieldDecl>(D)) { - if (FD->isBitField()) + if (FD->isBitField() && !FD->getBitWidth()->isValueDependent()) return FD->getBitWidthValue(getCursorContext(C)); } } Index: clang/include/clang-c/Index.h =================================================================== --- clang/include/clang-c/Index.h +++ clang/include/clang-c/Index.h @@ -3545,10 +3545,19 @@ CINDEX_LINKAGE unsigned long long clang_getEnumConstantDeclUnsignedValue(CXCursor C); +/** + * Returns non-zero if a bit field's width depends on template parameters. + * + * If the cursor does not reference a bit field declaration or if the bit + * field's width does not depend on template parameters, 0 is returned. + */ +CINDEX_LINKAGE unsigned clang_isFieldDeclBitWidthDependent(CXCursor C); + /** * 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. + * If a cursor that is not a bit field declaration is passed in, or if the bit + * field's width expression cannot be evaluated, -1 is returned. */ CINDEX_LINKAGE int clang_getFieldDeclBitWidth(CXCursor C);
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits