Skip to content

Commit

Permalink
Merge pull request #164 from ptrstr/master
Browse files Browse the repository at this point in the history
Add bindings for Clang 17
  • Loading branch information
KyleMayes authored Dec 31, 2023
2 parents a307f83 + 47a8f4d commit 20882f9
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -58,4 +59,4 @@ tempfile = "3"

[package.metadata.docs.rs]

features = ["clang_16_0", "runtime"]
features = ["clang_17_0", "runtime"]
145 changes: 144 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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), +,
}) => (
Expand Down Expand Up @@ -97,6 +111,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,
Expand Down Expand Up @@ -140,6 +195,17 @@ cenum! {
}
}

cenum! {
#[repr(c_uchar)]
/// 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,
Expand Down Expand Up @@ -1049,7 +1115,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,
}
}

Expand Down Expand Up @@ -1089,6 +1155,28 @@ cenum! {
}
}

cenum! {
/// Only available on `libclang` 17.0 and later.
#[cfg(feature = "clang_17_0")]
enum CXUnaryOperatorKind {
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,
}
}

cenum! {
enum CXVisitorResult {
const CXVisit_Break = 0,
Expand Down Expand Up @@ -1201,6 +1289,28 @@ cenum! {
}
}

/// 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 {
const CXNameRange_WantQualifier = 1;
Expand Down Expand Up @@ -1594,6 +1704,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 {
Expand Down Expand Up @@ -1771,6 +1896,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;
Expand Down Expand Up @@ -2005,6 +2133,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;
Expand Down Expand Up @@ -2049,6 +2180,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;
Expand All @@ -2073,6 +2207,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;
Expand Down Expand Up @@ -2102,6 +2239,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")]
Expand Down Expand Up @@ -2184,6 +2324,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;
Expand Down
5 changes: 4 additions & 1 deletion src/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ macro_rules! link {
V11_0 = 110,
V12_0 = 120,
V16_0 = 160,
V17_0 = 170,
}

impl fmt::Display for Version {
Expand All @@ -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"),
}
}
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 20882f9

Please sign in to comment.