From 8605a3427324aa54a0c55bd38a9a5934f9b559b3 Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Sun, 7 Mar 2021 16:21:05 +0100 Subject: [PATCH] generator: Only replace "VK_" at the start of enum variant names (#384) * generator: Only replace "VK_" at the start of enum variant names Replacing the occurence of "VK" is dangerous as shown by this diff; MVK shorthands for MoltenVK are accidentally mangled. Using strip_prefix/suffix is not only more correct and performs more runtime checks, it is also more performant by returning a slice into the string. * generator: Correct duplicate suffix stripping The original `.replace(vendor, "")` operation and `.trim_end_matches` replace every (trailing) occurence of the vendor, causing DEBUG_REPORT_CALLBACK_EXT_EXT to end up without `_EXT` at all. This name is also provided by vk.xml as fallback alias because it is used but technically incorrect; the current generator can however not generate it (limitation in vkxml representation). --- ash/src/vk/const_debugs.rs | 10 +++++----- ash/src/vk/definitions.rs | 4 ++-- ash/src/vk/enums.rs | 6 +++--- ash/src/vk/extensions.rs | 4 ++-- generator/src/lib.rs | 16 +++++++++++----- 5 files changed, 23 insertions(+), 17 deletions(-) diff --git a/ash/src/vk/const_debugs.rs b/ash/src/vk/const_debugs.rs index f05090b..2298522 100644 --- a/ash/src/vk/const_debugs.rs +++ b/ash/src/vk/const_debugs.rs @@ -828,10 +828,10 @@ impl fmt::Debug for DebugReportObjectTypeEXT { Self::COMMAND_POOL => Some("COMMAND_POOL"), Self::SURFACE_KHR => Some("SURFACE_KHR"), Self::SWAPCHAIN_KHR => Some("SWAPCHAIN_KHR"), - Self::DEBUG_REPORT_CALLBACK => Some("DEBUG_REPORT_CALLBACK"), + Self::DEBUG_REPORT_CALLBACK_EXT => Some("DEBUG_REPORT_CALLBACK_EXT"), Self::DISPLAY_KHR => Some("DISPLAY_KHR"), Self::DISPLAY_MODE_KHR => Some("DISPLAY_MODE_KHR"), - Self::VALIDATION_CACHE => Some("VALIDATION_CACHE"), + Self::VALIDATION_CACHE_EXT => Some("VALIDATION_CACHE_EXT"), Self::SAMPLER_YCBCR_CONVERSION => Some("SAMPLER_YCBCR_CONVERSION"), Self::DESCRIPTOR_UPDATE_TEMPLATE => Some("DESCRIPTOR_UPDATE_TEMPLATE"), Self::ACCELERATION_STRUCTURE_KHR => Some("ACCELERATION_STRUCTURE_KHR"), @@ -1171,7 +1171,7 @@ impl fmt::Debug for DriverId { Self::GGP_PROPRIETARY => Some("GGP_PROPRIETARY"), Self::BROADCOM_PROPRIETARY => Some("BROADCOM_PROPRIETARY"), Self::MESA_LLVMPIPE => Some("MESA_LLVMPIPE"), - Self::MOLTEN => Some("MOLTEN"), + Self::MOLTENVK => Some("MOLTENVK"), _ => None, }; if let Some(x) = name { @@ -3780,8 +3780,8 @@ impl fmt::Debug for StructureType { Self::DISPLAY_MODE_PROPERTIES_2_KHR => Some("DISPLAY_MODE_PROPERTIES_2_KHR"), Self::DISPLAY_PLANE_INFO_2_KHR => Some("DISPLAY_PLANE_INFO_2_KHR"), Self::DISPLAY_PLANE_CAPABILITIES_2_KHR => Some("DISPLAY_PLANE_CAPABILITIES_2_KHR"), - Self::IOS_SURFACE_CREATE_INFO_M => Some("IOS_SURFACE_CREATE_INFO_M"), - Self::MACOS_SURFACE_CREATE_INFO_M => Some("MACOS_SURFACE_CREATE_INFO_M"), + Self::IOS_SURFACE_CREATE_INFO_MVK => Some("IOS_SURFACE_CREATE_INFO_MVK"), + Self::MACOS_SURFACE_CREATE_INFO_MVK => Some("MACOS_SURFACE_CREATE_INFO_MVK"), Self::DEBUG_UTILS_OBJECT_NAME_INFO_EXT => Some("DEBUG_UTILS_OBJECT_NAME_INFO_EXT"), Self::DEBUG_UTILS_OBJECT_TAG_INFO_EXT => Some("DEBUG_UTILS_OBJECT_TAG_INFO_EXT"), Self::DEBUG_UTILS_LABEL_EXT => Some("DEBUG_UTILS_LABEL_EXT"), diff --git a/ash/src/vk/definitions.rs b/ash/src/vk/definitions.rs index 627a157..0038dd1 100644 --- a/ash/src/vk/definitions.rs +++ b/ash/src/vk/definitions.rs @@ -21173,7 +21173,7 @@ pub struct IOSSurfaceCreateInfoMVK { impl ::std::default::Default for IOSSurfaceCreateInfoMVK { fn default() -> IOSSurfaceCreateInfoMVK { IOSSurfaceCreateInfoMVK { - s_type: StructureType::IOS_SURFACE_CREATE_INFO_M, + s_type: StructureType::IOS_SURFACE_CREATE_INFO_MVK, p_next: ::std::ptr::null(), flags: IOSSurfaceCreateFlagsMVK::default(), p_view: ::std::ptr::null(), @@ -21250,7 +21250,7 @@ pub struct MacOSSurfaceCreateInfoMVK { impl ::std::default::Default for MacOSSurfaceCreateInfoMVK { fn default() -> MacOSSurfaceCreateInfoMVK { MacOSSurfaceCreateInfoMVK { - s_type: StructureType::MACOS_SURFACE_CREATE_INFO_M, + s_type: StructureType::MACOS_SURFACE_CREATE_INFO_MVK, p_next: ::std::ptr::null(), flags: MacOSSurfaceCreateFlagsMVK::default(), p_view: ::std::ptr::null(), diff --git a/ash/src/vk/enums.rs b/ash/src/vk/enums.rs index b755c25..f3922a9 100644 --- a/ash/src/vk/enums.rs +++ b/ash/src/vk/enums.rs @@ -1188,10 +1188,10 @@ impl DebugReportObjectTypeEXT { pub const COMMAND_POOL: Self = Self(25); pub const SURFACE_KHR: Self = Self(26); pub const SWAPCHAIN_KHR: Self = Self(27); - pub const DEBUG_REPORT_CALLBACK: Self = Self(28); + pub const DEBUG_REPORT_CALLBACK_EXT: Self = Self(28); pub const DISPLAY_KHR: Self = Self(29); pub const DISPLAY_MODE_KHR: Self = Self(30); - pub const VALIDATION_CACHE: Self = Self(33); + pub const VALIDATION_CACHE_EXT: Self = Self(33); } #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] #[repr(transparent)] @@ -1681,7 +1681,7 @@ impl DriverId { #[doc = "Mesa"] pub const MESA_LLVMPIPE: Self = Self(13); #[doc = "MoltenVK"] - pub const MOLTEN: Self = Self(14); + pub const MOLTENVK: Self = Self(14); } #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)] #[repr(transparent)] diff --git a/ash/src/vk/extensions.rs b/ash/src/vk/extensions.rs index 8435edc..7403bd5 100644 --- a/ash/src/vk/extensions.rs +++ b/ash/src/vk/extensions.rs @@ -10020,7 +10020,7 @@ impl MvkIosSurfaceFn { } #[doc = "Generated from 'VK_MVK_ios_surface'"] impl StructureType { - pub const IOS_SURFACE_CREATE_INFO_M: Self = Self(1_000_122_000); + pub const IOS_SURFACE_CREATE_INFO_MVK: Self = Self(1_000_122_000); } impl MvkMacosSurfaceFn { pub fn name() -> &'static ::std::ffi::CStr { @@ -10095,7 +10095,7 @@ impl MvkMacosSurfaceFn { } #[doc = "Generated from 'VK_MVK_macos_surface'"] impl StructureType { - pub const MACOS_SURFACE_CREATE_INFO_M: Self = Self(1_000_123_000); + pub const MACOS_SURFACE_CREATE_INFO_MVK: Self = Self(1_000_123_000); } impl MvkMoltenvkFn { pub fn name() -> &'static ::std::ffi::CStr { diff --git a/generator/src/lib.rs b/generator/src/lib.rs index 61b6195..ba17b32 100644 --- a/generator/src/lib.rs +++ b/generator/src/lib.rs @@ -1290,6 +1290,7 @@ pub enum EnumType { } pub fn variant_ident(enum_name: &str, variant_name: &str) -> Ident { + let variant_name = variant_name.to_uppercase(); let _name = enum_name.replace("FlagBits", ""); // TODO: Should be read from vk.xml id:2 // TODO: Also needs to be more robust, vendor names can be substrings from itself, id:4 @@ -1302,12 +1303,17 @@ pub fn variant_ident(enum_name: &str, variant_name: &str) -> Ident { .cloned() .unwrap_or(""); let struct_name = struct_name.strip_suffix(vendor).unwrap(); - let new_variant_name = variant_name.replace(&struct_name, "").replace("VK", ""); + let new_variant_name = variant_name + .strip_prefix(struct_name) + .unwrap_or_else(|| variant_name.strip_prefix("VK").unwrap()); + // Both of the above strip_prefix leave a leading `_`: + let new_variant_name = new_variant_name.strip_prefix("_").unwrap(); let new_variant_name = new_variant_name - .trim_matches('_') - .to_shouty_snake_case() - .replace("_BIT", "") - .replace(vendor, ""); + .strip_suffix(vendor) + .unwrap_or(new_variant_name); + // Replace _BIT anywhere in the string, also works when there's a trailing + // vendor extension in the variant name that's not in the enum/type name: + let new_variant_name = new_variant_name.replace("_BIT", ""); let is_digit = new_variant_name .chars() .next()