mirror of
https://github.com/KhronosGroup/Vulkan-Utility-Libraries.git
synced 2025-05-14 16:58:43 +00:00
tests: Ensure vk_enum_string_helper.h is C compatible
Added ifdef checks for backcompat Intended to properly address these issue(s): - https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/3233 - https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/1211
This commit is contained in:
parent
d05c872b51
commit
02d552db0c
6 changed files with 505 additions and 427 deletions
File diff suppressed because it is too large
Load diff
|
@ -27,7 +27,9 @@ class EnumStringHelperOutputGenerator(BaseGenerator):
|
|||
|
||||
out.append('''
|
||||
#pragma once
|
||||
#ifdef __cplusplus
|
||||
#include <string>
|
||||
#endif
|
||||
#include <vulkan/vulkan.h>
|
||||
''')
|
||||
|
||||
|
@ -53,8 +55,16 @@ class EnumStringHelperOutputGenerator(BaseGenerator):
|
|||
# If there are no flags (empty bitmask) ignore
|
||||
for bitmask in [x for x in self.vk.bitmasks.values() if len(x.flags) > 0]:
|
||||
groupType = bitmask.name if bitmask.bitWidth == 32 else 'uint64_t'
|
||||
|
||||
# switch labels must be constant expressions. In C a const-qualified variable is not a constant expression.
|
||||
use_switch_statement = True
|
||||
if groupType == 'uint64_t':
|
||||
use_switch_statement = False
|
||||
|
||||
out.extend([f'#ifdef {bitmask.protect}\n'] if bitmask.protect else [])
|
||||
out.append(f'static inline const char* string_{bitmask.name}({groupType} input_value) {{\n')
|
||||
|
||||
if use_switch_statement:
|
||||
out.append(' switch (input_value) {\n')
|
||||
for flag in [x for x in bitmask.flags if not x.multiBit]:
|
||||
out.extend([f'#ifdef {flag.protect}\n'] if flag.protect else [])
|
||||
|
@ -64,6 +74,14 @@ class EnumStringHelperOutputGenerator(BaseGenerator):
|
|||
out.append(' default:\n')
|
||||
out.append(f' return "Unhandled {bitmask.name}";\n')
|
||||
out.append(' }\n')
|
||||
else:
|
||||
# We need to use if statements
|
||||
for flag in [x for x in bitmask.flags if not x.multiBit]:
|
||||
out.extend([f'#ifdef {flag.protect}\n'] if flag.protect else [])
|
||||
out.append(f' if (input_value == {flag.name}) return "{flag.name}";\n')
|
||||
out.extend([f'#endif //{flag.protect}\n'] if flag.protect else [])
|
||||
out.append(f' return "Unhandled {bitmask.name}";\n')
|
||||
|
||||
out.append('}\n')
|
||||
|
||||
mulitBitChecks = ''
|
||||
|
@ -71,6 +89,7 @@ class EnumStringHelperOutputGenerator(BaseGenerator):
|
|||
mulitBitChecks += f' if (input_value == {flag.name}) {{ return "{flag.name}"; }}\n'
|
||||
intSuffix = 'U' if bitmask.bitWidth == 32 else 'ULL'
|
||||
|
||||
out.append('\n#ifdef __cplusplus')
|
||||
out.append(f'''
|
||||
static inline std::string string_{bitmask.flagName}({bitmask.flagName} input_value) {{
|
||||
{mulitBitChecks} std::string ret;
|
||||
|
@ -87,6 +106,7 @@ static inline std::string string_{bitmask.flagName}({bitmask.flagName} input_val
|
|||
return ret;
|
||||
}}\n''')
|
||||
out.extend([f'#endif //{bitmask.protect}\n'] if bitmask.protect else [])
|
||||
out.append('#endif // __cplusplus\n')
|
||||
|
||||
out.append('// NOLINTEND') # Wrap for clang-tidy to ignore
|
||||
self.write("".join(out))
|
||||
|
|
|
@ -9,10 +9,13 @@ project(TEST_ADD_SUBDIRECTORY LANGUAGES C)
|
|||
|
||||
add_library(add_subdirectory_example STATIC)
|
||||
|
||||
# Test c99 support as well as find_package support
|
||||
# Test c99 support
|
||||
target_compile_features(add_subdirectory_example PRIVATE c_std_99)
|
||||
|
||||
target_sources(add_subdirectory_example PRIVATE client.c)
|
||||
target_sources(add_subdirectory_example PRIVATE
|
||||
client.c
|
||||
vk_enum_string_helper.c
|
||||
)
|
||||
|
||||
# NOTE: Because VulkanHeaders is a PUBLIC dependency it needs to be found prior to VulkanUtilityLibraries
|
||||
add_subdirectory(${GITHUB_VULKAN_HEADER_SOURCE_DIR} ${PROJECT_BINARY_DIR}/headers)
|
||||
|
|
14
tests/add_subdirectory/vk_enum_string_helper.c
Normal file
14
tests/add_subdirectory/vk_enum_string_helper.c
Normal file
|
@ -0,0 +1,14 @@
|
|||
// Copyright 2023 The Khronos Group Inc.
|
||||
// Copyright 2023 Valve Corporation
|
||||
// Copyright 2023 LunarG, Inc.
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include <vulkan/vk_enum_string_helper.h>
|
||||
|
||||
// Ensure vk_enum_string_helper.h can be compiled with a C compiler
|
||||
const char* foobar() { return string_VkResult(VK_SUCCESS); }
|
||||
|
||||
// Ensure string_VkPipelineStageFlagBits2 is callable by C users
|
||||
const char* vk_format_feature_2_sampled_image_bit() {
|
||||
return string_VkPipelineStageFlagBits2(VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT);
|
||||
}
|
|
@ -14,6 +14,7 @@ target_compile_features(find_package_example PRIVATE c_std_99)
|
|||
|
||||
target_sources(find_package_example PRIVATE
|
||||
${CMAKE_CURRENT_LIST_DIR}/../add_subdirectory/client.c
|
||||
${CMAKE_CURRENT_LIST_DIR}/../add_subdirectory/vk_enum_string_helper.c
|
||||
)
|
||||
|
||||
# NOTE: Because VulkanHeaders is a PUBLIC dependency it needs to be found prior to VulkanUtilityLibraries
|
||||
|
|
|
@ -74,3 +74,12 @@ TEST(vk_enum_string_helper, string_VkFormat) {
|
|||
EXPECT_STREQ(magic_str.data(), str);
|
||||
}
|
||||
}
|
||||
|
||||
// string_VkPipelineStageFlagBits2 can't use a switch statement due to C not treating const-qualified variables as constant
|
||||
// expressions. As a result test the codegen for this function explicitly.
|
||||
TEST(vk_enum_string_helper, string_VkFormatFeatureFlagBits2) {
|
||||
EXPECT_STREQ(string_VkFormatFeatureFlagBits2(VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT), "VK_FORMAT_FEATURE_2_SAMPLED_IMAGE_BIT");
|
||||
EXPECT_STREQ(string_VkFormatFeatureFlagBits2(VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT), "VK_FORMAT_FEATURE_2_STORAGE_IMAGE_BIT");
|
||||
EXPECT_STREQ(string_VkFormatFeatureFlagBits2(VK_FORMAT_FEATURE_2_VERTEX_BUFFER_BIT), "VK_FORMAT_FEATURE_2_VERTEX_BUFFER_BIT");
|
||||
EXPECT_STREQ(string_VkFormatFeatureFlagBits2(3), "Unhandled VkFormatFeatureFlagBits2");
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue