From e9466593c204b42db3a7df6ea3f2150355494ede Mon Sep 17 00:00:00 2001 From: ptrstr Date: Mon, 11 Dec 2023 16:48:01 -0500 Subject: [PATCH 1/4] Add bindings for Clang 17 --- Cargo.toml | 3 +- src/lib.rs | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++- src/link.rs | 7 ++- 3 files changed, 129 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3c6d3d624..a2b7bf738 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,6 +36,7 @@ clang_13_0 = ["clang_12_0"] clang_14_0 = ["clang_13_0"] clang_15_0 = ["clang_14_0"] clang_16_0 = ["clang_15_0"] +clang_17_0 = ["clang_16_0"] runtime = ["libloading"] static = [] @@ -58,4 +59,4 @@ tempfile = "3" [package.metadata.docs.rs] -features = ["clang_16_0", "runtime"] +features = ["clang_17_0", "runtime"] diff --git a/src/lib.rs b/src/lib.rs index edaf2d37a..97c54c6e6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -97,6 +97,47 @@ cenum! { } } +cenum! { + /// Only available on `libclang` 17.0 and later. + #[cfg(feature = "clang_17_0")] + enum CXBinaryOperatorKind { + const CXBinaryOperator_Invalid = 0, + const CXBinaryOperator_PtrMemD = 1, + const CXBinaryOperator_PtrMemI = 2, + const CXBinaryOperator_Mul = 3, + const CXBinaryOperator_Div = 4, + const CXBinaryOperator_Rem = 5, + const CXBinaryOperator_Add = 6, + const CXBinaryOperator_Sub = 7, + const CXBinaryOperator_Shl = 8, + const CXBinaryOperator_Shr = 9, + const CXBinaryOperator_Cmp = 10, + const CXBinaryOperator_LT = 11, + const CXBinaryOperator_GT = 12, + const CXBinaryOperator_LE = 13, + const CXBinaryOperator_GE = 14, + const CXBinaryOperator_EQ = 15, + const CXBinaryOperator_NE = 16, + const CXBinaryOperator_And = 17, + const CXBinaryOperator_Xor = 18, + const CXBinaryOperator_Or = 19, + const CXBinaryOperator_LAnd = 20, + const CXBinaryOperator_LOr = 21, + const CXBinaryOperator_Assign = 22, + const CXBinaryOperator_MulAssign = 23, + const CXBinaryOperator_DivAssign = 24, + const CXBinaryOperator_RemAssign = 25, + const CXBinaryOperator_AddAssign = 26, + const CXBinaryOperator_SubAssign = 27, + const CXBinaryOperator_ShlAssign = 28, + const CXBinaryOperator_ShrAssign = 29, + const CXBinaryOperator_AndAssign = 30, + const CXBinaryOperator_XorAssign = 31, + const CXBinaryOperator_OrAssign = 32, + const CXBinaryOperator_Comma = 33, + } +} + cenum! { enum CXCallingConv { const CXCallingConv_Default = 0, @@ -140,6 +181,16 @@ cenum! { } } +cenum! { + /// Only available on `libclang` 17.0 and later. + #[cfg(feature = "clang_17_0")] + enum CXChoice { + const CXChoice_Default = 0, + const CXChoice_Enabled = 1, + const CXChoice_Disabled = 2, + } +} + cenum! { enum CXCommentInlineCommandRenderKind { const CXCommentInlineCommandRenderKind_Normal = 0, @@ -714,6 +765,22 @@ cenum! { } } +/// Only available on `libclang` 17.0 and later. +#[cfg(feature = "clang_17_0")] +pub type CXIndexOptions_Flags = c_ushort; + +/// Only available on `libclang` 17.0 and later. +#[cfg(feature = "clang_17_0")] +pub const CXIndexOptions_ExcludeDeclarationsFromPCH: CXIndexOptions_Flags = 1; + +/// Only available on `libclang` 17.0 and later. +#[cfg(feature = "clang_17_0")] +pub const CXIndexOptions_DisplayDiagnostics: CXIndexOptions_Flags = 2; + +/// Only available on `libclang` 17.0 and later. +#[cfg(feature = "clang_17_0")] +pub const CXIndexOptions_StorePreamblesInMemory: CXIndexOptions_Flags = 4; + cenum! { enum CXLanguageKind { const CXLanguage_Invalid = 0, @@ -1049,7 +1116,7 @@ cenum! { /// Only produced by `libclang` 11.0 and later. const CXType_Atomic = 177, /// Only produced by `libclang` 15.0 and later. - const CXType_BTFTagAttributed = 178, + const CXType_BTFTagAttributed = 178, } } @@ -1089,6 +1156,27 @@ cenum! { } } +cenum! { + /// Only available on `libclang` 17.0 and later. + #[cfg(feature = "clang_17_0")] + enum CXUnaryOperatorKind { + const CXUnaryOperator_PostInc = 0, + const CXUnaryOperator_PostDec = 1, + const CXUnaryOperator_PreInc = 2, + const CXUnaryOperator_PreDec = 3, + const CXUnaryOperator_AddrOf = 4, + const CXUnaryOperator_Deref = 5, + const CXUnaryOperator_Plus = 6, + const CXUnaryOperator_Minus = 7, + const CXUnaryOperator_Not = 8, + const CXUnaryOperator_LNot = 9, + const CXUnaryOperator_Real = 10, + const CXUnaryOperator_Imag = 11, + const CXUnaryOperator_Extension = 12, + const CXUnaryOperator_Coawait = 13, + } +} + cenum! { enum CXVisitorResult { const CXVisit_Break = 0, @@ -1594,6 +1682,21 @@ pub struct CXIdxObjCProtocolRefListInfo { default!(CXIdxObjCProtocolRefListInfo); +#[cfg(feature = "clang_17_0")] +#[derive(Copy, Clone, Debug)] +#[repr(C)] +pub struct CXIndexOptions { + pub Size: c_uint, + pub ThreadBackgroundPriorityForIndexing: CXChoice, + pub ThreadBackgroundPriorityForEditing: CXChoice, + pub flags: CXIndexOptions_Flags, + pub PreambleStoragePath: *const c_char, + pub InvocationEmissionPath: *const c_char, +} + +#[cfg(feature = "clang_17_0")] +default!(CXIndexOptions); + #[derive(Copy, Clone, Debug)] #[repr(C)] pub struct CXPlatformAvailability { @@ -1771,6 +1874,9 @@ link! { pub fn clang_CXXMethod_isPureVirtual(cursor: CXCursor) -> c_uint; pub fn clang_CXXMethod_isStatic(cursor: CXCursor) -> c_uint; pub fn clang_CXXMethod_isVirtual(cursor: CXCursor) -> c_uint; + /// Only available on `libclang` 17.0 and later. + #[cfg(feature = "clang_17_0")] + pub fn clang_CXXMethod_isExplicit(cursor: CXCursor) -> c_uint; /// Only available on `libclang` 6.0 and later. #[cfg(feature = "clang_6_0")] pub fn clang_CXXRecord_isAbstract(cursor: CXCursor) -> c_uint; @@ -2005,6 +2111,9 @@ link! { pub fn clang_constructUSR_ObjCProtocol(protocol: *const c_char) -> CXString; pub fn clang_createCXCursorSet() -> CXCursorSet; pub fn clang_createIndex(exclude: c_int, display: c_int) -> CXIndex; + /// Only available on `libclang` 17.0 and later. + #[cfg(feature = "clang_17_0")] + pub fn clang_createIndexWithOptions(options: CXIndexOptions) -> CXIndex; pub fn clang_createTranslationUnit(index: CXIndex, file: *const c_char) -> CXTranslationUnit; pub fn clang_createTranslationUnit2(index: CXIndex, file: *const c_char, tu: *mut CXTranslationUnit) -> CXErrorCode; pub fn clang_createTranslationUnitFromSourceFile(index: CXIndex, file: *const c_char, n_arguments: c_int, arguments: *const *const c_char, n_unsaved: c_uint, unsaved: *mut CXUnsavedFile) -> CXTranslationUnit; @@ -2049,6 +2158,9 @@ link! { pub fn clang_getArgType(type_: CXType, index: c_uint) -> CXType; pub fn clang_getArrayElementType(type_: CXType) -> CXType; pub fn clang_getArraySize(type_: CXType) -> c_longlong; + /// Only available on `libclang` 17.0 and later. + #[cfg(feature = "clang_17_0")] + pub fn clang_getBinaryOperatorKindSpelling(kind: CXBinaryOperatorKind) -> CXString; pub fn clang_getCString(string: CXString) -> *const c_char; pub fn clang_getCXTUResourceUsage(tu: CXTranslationUnit) -> CXTUResourceUsage; pub fn clang_getCXXAccessSpecifier(cursor: CXCursor) -> CX_CXXAccessSpecifier; @@ -2073,6 +2185,9 @@ link! { pub fn clang_getCompletionPriority(string: CXCompletionString) -> c_uint; pub fn clang_getCursor(tu: CXTranslationUnit, location: CXSourceLocation) -> CXCursor; pub fn clang_getCursorAvailability(cursor: CXCursor) -> CXAvailabilityKind; + /// Only available on `libclang` 17.0 and later. + #[cfg(feature = "clang_17_0")] + pub fn clang_getCursorBinaryOperatorKind(cursor: CXCursor) -> CXBinaryOperatorKind; pub fn clang_getCursorCompletionString(cursor: CXCursor) -> CXCompletionString; pub fn clang_getCursorDefinition(cursor: CXCursor) -> CXCursor; pub fn clang_getCursorDisplayName(cursor: CXCursor) -> CXString; @@ -2102,6 +2217,9 @@ link! { #[cfg(feature = "clang_6_0")] pub fn clang_getCursorTLSKind(cursor: CXCursor) -> CXTLSKind; pub fn clang_getCursorType(cursor: CXCursor) -> CXType; + /// Only available on `libclang` 17.0 and later. + #[cfg(feature = "clang_17_0")] + pub fn clang_getCursorUnaryOperatorKind(cursor: CXCursor) -> CXUnaryOperatorKind; pub fn clang_getCursorUSR(cursor: CXCursor) -> CXString; /// Only available on `libclang` 3.8 and later. #[cfg(feature = "clang_3_8")] @@ -2184,6 +2302,9 @@ link! { /// Only available on `libclang` 5.0 and later. #[cfg(feature = "clang_5_0")] pub fn clang_getTranslationUnitTargetInfo(tu: CXTranslationUnit) -> CXTargetInfo; + /// Only available on `libclang` 17.0 and later. + #[cfg(feature = "clang_17_0")] + pub fn clang_getUnaryOperatorKindSpelling(kind: CXUnaryOperatorKind) -> CXString; /// Only available on `libclang` 16.0 and later. #[cfg(feature = "clang_16_0")] pub fn clang_getUnqualifiedType(type_: CXType) -> CXType; diff --git a/src/link.rs b/src/link.rs index 07c4be3b3..c0e08fa71 100644 --- a/src/link.rs +++ b/src/link.rs @@ -62,6 +62,7 @@ macro_rules! link { V11_0 = 110, V12_0 = 120, V16_0 = 160, + V17_0 = 170, } impl fmt::Display for Version { @@ -81,7 +82,8 @@ macro_rules! link { V9_0 => write!(f, "9.0.x - 10.0.x"), V11_0 => write!(f, "11.0.x"), V12_0 => write!(f, "12.0.x - 15.0.x"), - V16_0 => write!(f, "16.0.x or later"), + V16_0 => write!(f, "16.0.x"), + V17_0 => write!(f, "17.0.x or later"), } } } @@ -130,6 +132,7 @@ macro_rules! link { } unsafe { + check!(b"clang_CXXMethod_isExplicit", V17_0); check!(b"clang_CXXMethod_isCopyAssignmentOperator", V16_0); check!(b"clang_Cursor_getVarDeclInitializer", V12_0); check!(b"clang_Type_getValueType", V11_0); @@ -188,7 +191,7 @@ https://docs.rs/clang-sys/latest/clang_sys/{0}/index.html Instructions for installing `libclang` can be found here: https://rust-lang.github.io/rust-bindgen/requirements.html -"#, +"#, stringify!($name), library .version() From 2d2ac3160e0de28c4675eacddf2e987785550bed Mon Sep 17 00:00:00 2001 From: ptrstr Date: Mon, 11 Dec 2023 22:16:58 -0500 Subject: [PATCH 2/4] Add repr attribute handler for cenum. Fix types for CXIndexOptions --- src/lib.rs | 42 ++++++++++++++++++++++++++---------------- src/link.rs | 2 +- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 97c54c6e6..ec457bd71 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -48,6 +48,20 @@ pub type CXInclusionVisitor = extern "C" fn(CXFile, *mut CXSourceLocation, c_uin /// Defines a C enum as a series of constants. macro_rules! cenum { + (#[repr($ty:ty)] $(#[$meta:meta])* enum $name:ident { + $($(#[$vmeta:meta])* const $variant:ident = $value:expr), +, + }) => ( + pub type $name = $ty; + + $($(#[$vmeta])* pub const $variant: $name = $value;)+ + ); + (#[repr($ty:ty)] $(#[$meta:meta])* enum $name:ident { + $($(#[$vmeta:meta])* const $variant:ident = $value:expr); +; + }) => ( + pub type $name = $ty; + + $($(#[$vmeta])* pub const $variant: $name = $value;)+ + ); ($(#[$meta:meta])* enum $name:ident { $($(#[$vmeta:meta])* const $variant:ident = $value:expr), +, }) => ( @@ -182,6 +196,7 @@ cenum! { } cenum! { + #[repr(c_uchar)] /// Only available on `libclang` 17.0 and later. #[cfg(feature = "clang_17_0")] enum CXChoice { @@ -765,22 +780,6 @@ cenum! { } } -/// Only available on `libclang` 17.0 and later. -#[cfg(feature = "clang_17_0")] -pub type CXIndexOptions_Flags = c_ushort; - -/// Only available on `libclang` 17.0 and later. -#[cfg(feature = "clang_17_0")] -pub const CXIndexOptions_ExcludeDeclarationsFromPCH: CXIndexOptions_Flags = 1; - -/// Only available on `libclang` 17.0 and later. -#[cfg(feature = "clang_17_0")] -pub const CXIndexOptions_DisplayDiagnostics: CXIndexOptions_Flags = 2; - -/// Only available on `libclang` 17.0 and later. -#[cfg(feature = "clang_17_0")] -pub const CXIndexOptions_StorePreamblesInMemory: CXIndexOptions_Flags = 4; - cenum! { enum CXLanguageKind { const CXLanguage_Invalid = 0, @@ -1289,6 +1288,17 @@ cenum! { } } +cenum! { + #[repr(c_ushort)] + /// Only available on `libclang` 17.0 and later. + #[cfg(feature = "clang_17_0")] + enum CXIndexOptions_Flags { + const CXIndexOptions_ExcludeDeclarationsFromPCH = 0; + const CXIndexOptions_DisplayDiagnostics = 1; + const CXIndexOptions_StorePreamblesInMemory = 2; + } +} + cenum! { enum CXNameRefFlags { const CXNameRange_WantQualifier = 1; diff --git a/src/link.rs b/src/link.rs index c0e08fa71..79b3d49a3 100644 --- a/src/link.rs +++ b/src/link.rs @@ -191,7 +191,7 @@ https://docs.rs/clang-sys/latest/clang_sys/{0}/index.html Instructions for installing `libclang` can be found here: https://rust-lang.github.io/rust-bindgen/requirements.html -"#, +"#, stringify!($name), library .version() From aa9bfe4dcd543a0c6a857a9d75962f399fbf92e2 Mon Sep 17 00:00:00 2001 From: ptrstr Date: Tue, 12 Dec 2023 20:27:42 -0500 Subject: [PATCH 3/4] Fix CXIndexOptions values.Fix alignment on Windows --- src/lib.rs | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ec457bd71..a72d9a6ea 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1288,16 +1288,27 @@ cenum! { } } -cenum! { - #[repr(c_ushort)] - /// Only available on `libclang` 17.0 and later. - #[cfg(feature = "clang_17_0")] - enum CXIndexOptions_Flags { - const CXIndexOptions_ExcludeDeclarationsFromPCH = 0; - const CXIndexOptions_DisplayDiagnostics = 1; - const CXIndexOptions_StorePreamblesInMemory = 2; - } -} +/// Only available on `libclang` 17.0 and later. +#[cfg(feature = "clang_17_0")] +#[cfg(not(target_os = "windows"))] +pub type CXIndexOptions_Flags = c_ushort; + +/// Only available on `libclang` 17.0 and later. +#[cfg(feature = "clang_17_0")] +#[cfg(target_os = "windows")] +pub type CXIndexOptions_Flags = c_uint; + +/// Only available on `libclang` 17.0 and later. +#[cfg(feature = "clang_17_0")] +pub const CXIndexOptions_ExcludeDeclarationsFromPCH: CXIndexOptions_Flags = 1; + +/// Only available on `libclang` 17.0 and later. +#[cfg(feature = "clang_17_0")] +pub const CXIndexOptions_DisplayDiagnostics: CXIndexOptions_Flags = 2; + +/// Only available on `libclang` 17.0 and later. +#[cfg(feature = "clang_17_0")] +pub const CXIndexOptions_StorePreamblesInMemory: CXIndexOptions_Flags = 4; cenum! { enum CXNameRefFlags { From 47a8f4d833d4df5d721c6554840aa0fc4f38eac1 Mon Sep 17 00:00:00 2001 From: ptrstr Date: Wed, 13 Dec 2023 10:01:58 -0500 Subject: [PATCH 4/4] Fix CXUnaryOperatorKind --- src/lib.rs | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a72d9a6ea..31e2e9603 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1159,20 +1159,21 @@ cenum! { /// Only available on `libclang` 17.0 and later. #[cfg(feature = "clang_17_0")] enum CXUnaryOperatorKind { - const CXUnaryOperator_PostInc = 0, - const CXUnaryOperator_PostDec = 1, - const CXUnaryOperator_PreInc = 2, - const CXUnaryOperator_PreDec = 3, - const CXUnaryOperator_AddrOf = 4, - const CXUnaryOperator_Deref = 5, - const CXUnaryOperator_Plus = 6, - const CXUnaryOperator_Minus = 7, - const CXUnaryOperator_Not = 8, - const CXUnaryOperator_LNot = 9, - const CXUnaryOperator_Real = 10, - const CXUnaryOperator_Imag = 11, - const CXUnaryOperator_Extension = 12, - const CXUnaryOperator_Coawait = 13, + const CXUnaryOperator_Invalid = 0, + const CXUnaryOperator_PostInc = 1, + const CXUnaryOperator_PostDec = 2, + const CXUnaryOperator_PreInc = 3, + const CXUnaryOperator_PreDec = 4, + const CXUnaryOperator_AddrOf = 5, + const CXUnaryOperator_Deref = 6, + const CXUnaryOperator_Plus = 7, + const CXUnaryOperator_Minus = 8, + const CXUnaryOperator_Not = 9, + const CXUnaryOperator_LNot = 10, + const CXUnaryOperator_Real = 11, + const CXUnaryOperator_Imag = 12, + const CXUnaryOperator_Extension = 13, + const CXUnaryOperator_Coawait = 14, } }