collinbaker updated this revision to Diff 503953. collinbaker retitled this revision from "Handle template parameter-dependent bit field widths in libclang" to "Fix include order in CXType.cpp". collinbaker edited the summary of this revision. collinbaker added a comment.
Re-add "Fixes" commit message footer Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D130303/new/ https://reviews.llvm.org/D130303 Files: clang/docs/ReleaseNotes.rst clang/include/clang-c/Index.h clang/tools/libclang/CXType.cpp clang/tools/libclang/libclang.map Index: clang/tools/libclang/libclang.map =================================================================== --- clang/tools/libclang/libclang.map +++ clang/tools/libclang/libclang.map @@ -421,6 +421,7 @@ LLVM_17 { global: clang_CXXMethod_isExplicit; + clang_isBitFieldDecl; }; # Example of how to add a new symbol version entry. If you do add a new symbol Index: clang/tools/libclang/CXType.cpp =================================================================== --- clang/tools/libclang/CXType.cpp +++ clang/tools/libclang/CXType.cpp @@ -10,11 +10,11 @@ // //===--------------------------------------------------------------------===// +#include "CXType.h" #include "CIndexer.h" #include "CXCursor.h" #include "CXString.h" #include "CXTranslationUnit.h" -#include "CXType.h" #include "clang/AST/Decl.h" #include "clang/AST/DeclObjC.h" #include "clang/AST/DeclTemplate.h" @@ -371,14 +371,27 @@ return ULLONG_MAX; } +unsigned clang_isBitFieldDecl(CXCursor C) { + using namespace cxcursor; + + if (clang_isDeclaration(C.kind)) { + const Decl *D = getCursorDecl(C); + + if (const auto *FD = dyn_cast_or_null<FieldDecl>(D)) + return FD->isBitField(); + } + + return 0; +} + int clang_getFieldDeclBitWidth(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()) + if (const auto *FD = dyn_cast_or_null<FieldDecl>(D)) { + 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 @@ -2887,10 +2887,18 @@ CINDEX_LINKAGE unsigned long long clang_getEnumConstantDeclUnsignedValue(CXCursor C); +/** + * Returns non-zero if a field declaration has a bit width expression. + * + * If the cursor does not reference a bit field declaration 0 is returned. + */ +CINDEX_LINKAGE unsigned clang_isBitFieldDecl(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 the cursor does not reference a bit field, or if the bit field's width + * expression cannot be evaluated, -1 is returned. */ CINDEX_LINKAGE int clang_getFieldDeclBitWidth(CXCursor C); Index: clang/docs/ReleaseNotes.rst =================================================================== --- clang/docs/ReleaseNotes.rst +++ clang/docs/ReleaseNotes.rst @@ -290,6 +290,11 @@ - Introduced the new function ``clang_CXXMethod_isExplicit``, which identifies whether a constructor or conversion function cursor was marked with the explicit identifier. +- Added check in ``clang_getFieldDeclBitWidth`` for whether a bit field + has an evaluable bit width. Fixes undefined behavior when called on a + bit field whose width depends on a template paramter. +- Added function ``clang_isBitFieldDecl`` to check if a struct/class field is a + bit field. Static Analyzer ---------------
Index: clang/tools/libclang/libclang.map =================================================================== --- clang/tools/libclang/libclang.map +++ clang/tools/libclang/libclang.map @@ -421,6 +421,7 @@ LLVM_17 { global: clang_CXXMethod_isExplicit; + clang_isBitFieldDecl; }; # Example of how to add a new symbol version entry. If you do add a new symbol Index: clang/tools/libclang/CXType.cpp =================================================================== --- clang/tools/libclang/CXType.cpp +++ clang/tools/libclang/CXType.cpp @@ -10,11 +10,11 @@ // //===--------------------------------------------------------------------===// +#include "CXType.h" #include "CIndexer.h" #include "CXCursor.h" #include "CXString.h" #include "CXTranslationUnit.h" -#include "CXType.h" #include "clang/AST/Decl.h" #include "clang/AST/DeclObjC.h" #include "clang/AST/DeclTemplate.h" @@ -371,14 +371,27 @@ return ULLONG_MAX; } +unsigned clang_isBitFieldDecl(CXCursor C) { + using namespace cxcursor; + + if (clang_isDeclaration(C.kind)) { + const Decl *D = getCursorDecl(C); + + if (const auto *FD = dyn_cast_or_null<FieldDecl>(D)) + return FD->isBitField(); + } + + return 0; +} + int clang_getFieldDeclBitWidth(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()) + if (const auto *FD = dyn_cast_or_null<FieldDecl>(D)) { + 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 @@ -2887,10 +2887,18 @@ CINDEX_LINKAGE unsigned long long clang_getEnumConstantDeclUnsignedValue(CXCursor C); +/** + * Returns non-zero if a field declaration has a bit width expression. + * + * If the cursor does not reference a bit field declaration 0 is returned. + */ +CINDEX_LINKAGE unsigned clang_isBitFieldDecl(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 the cursor does not reference a bit field, or if the bit field's width + * expression cannot be evaluated, -1 is returned. */ CINDEX_LINKAGE int clang_getFieldDeclBitWidth(CXCursor C); Index: clang/docs/ReleaseNotes.rst =================================================================== --- clang/docs/ReleaseNotes.rst +++ clang/docs/ReleaseNotes.rst @@ -290,6 +290,11 @@ - Introduced the new function ``clang_CXXMethod_isExplicit``, which identifies whether a constructor or conversion function cursor was marked with the explicit identifier. +- Added check in ``clang_getFieldDeclBitWidth`` for whether a bit field + has an evaluable bit width. Fixes undefined behavior when called on a + bit field whose width depends on a template paramter. +- Added function ``clang_isBitFieldDecl`` to check if a struct/class field is a + bit field. Static Analyzer ---------------
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits