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).
This commit is contained in:
Marijn Suijten 2021-03-07 16:21:05 +01:00 committed by GitHub
parent c554da6321
commit 8605a34273
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 17 deletions

View file

@ -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"),

View file

@ -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(),

View file

@ -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)]

View file

@ -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 {

View file

@ -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()