diff options
author | Petr Kraus <petr_kraus@email.cz> | 2019-08-18 16:22:59 +0200 |
---|---|---|
committer | Mark Lobodzinski <mark@lunarg.com> | 2019-08-22 09:42:27 -0600 |
commit | 956e9716a0a1ef423c36532a7bdcecbb668ef583 (patch) | |
tree | 1b0d7e0f56636be76f1f41c9b8f56f5ecbf10f8d | |
parent | 08d510661c5c9a141dfc7b01e2c5484b3165a8f2 (diff) | |
download | vulkan-validation-layers-956e9716a0a1ef423c36532a7bdcecbb668ef583.tar.gz |
layers: Eliminate const_cast in safe_struct string
-rw-r--r-- | layers/generated/vk_safe_struct.cpp | 102 | ||||
-rw-r--r-- | layers/generated/vk_safe_struct.h | 2 | ||||
-rw-r--r-- | scripts/helper_file_generator.py | 9 |
3 files changed, 67 insertions, 46 deletions
diff --git a/layers/generated/vk_safe_struct.cpp b/layers/generated/vk_safe_struct.cpp index 147484f94..3b95490ff 100644 --- a/layers/generated/vk_safe_struct.cpp +++ b/layers/generated/vk_safe_struct.cpp @@ -120,14 +120,16 @@ safe_VkInstanceCreateInfo::safe_VkInstanceCreateInfo(const VkInstanceCreateInfo* enabledExtensionCount(in_struct->enabledExtensionCount) { pNext = SafePnextCopy(in_struct->pNext); - ppEnabledLayerNames = new char *[in_struct->enabledLayerCount]; + char **tmp_ppEnabledLayerNames = new char *[in_struct->enabledLayerCount]; for (uint32_t i = 0; i < enabledLayerCount; ++i) { - (const_cast<const char **>(ppEnabledLayerNames))[i] = SafeStringCopy(in_struct->ppEnabledLayerNames[i]); + tmp_ppEnabledLayerNames[i] = SafeStringCopy(in_struct->ppEnabledLayerNames[i]); } - ppEnabledExtensionNames = new char *[in_struct->enabledExtensionCount]; + ppEnabledLayerNames = tmp_ppEnabledLayerNames; + char **tmp_ppEnabledExtensionNames = new char *[in_struct->enabledExtensionCount]; for (uint32_t i = 0; i < enabledExtensionCount; ++i) { - (const_cast<const char **>(ppEnabledExtensionNames))[i] = SafeStringCopy(in_struct->ppEnabledExtensionNames[i]); + tmp_ppEnabledExtensionNames[i] = SafeStringCopy(in_struct->ppEnabledExtensionNames[i]); } + ppEnabledExtensionNames = tmp_ppEnabledExtensionNames; if (in_struct->pApplicationInfo) pApplicationInfo = new safe_VkApplicationInfo(in_struct->pApplicationInfo); else @@ -147,14 +149,16 @@ safe_VkInstanceCreateInfo::safe_VkInstanceCreateInfo(const safe_VkInstanceCreate enabledLayerCount = src.enabledLayerCount; enabledExtensionCount = src.enabledExtensionCount; pNext = SafePnextCopy(src.pNext); - ppEnabledLayerNames = new char *[src.enabledLayerCount]; + char **tmp_ppEnabledLayerNames = new char *[src.enabledLayerCount]; for (uint32_t i = 0; i < enabledLayerCount; ++i) { - (const_cast<const char **>(ppEnabledLayerNames))[i] = SafeStringCopy(src.ppEnabledLayerNames[i]); + tmp_ppEnabledLayerNames[i] = SafeStringCopy(src.ppEnabledLayerNames[i]); } - ppEnabledExtensionNames = new char *[src.enabledExtensionCount]; + ppEnabledLayerNames = tmp_ppEnabledLayerNames; + char **tmp_ppEnabledExtensionNames = new char *[src.enabledExtensionCount]; for (uint32_t i = 0; i < enabledExtensionCount; ++i) { - (const_cast<const char **>(ppEnabledExtensionNames))[i] = SafeStringCopy(src.ppEnabledExtensionNames[i]); + tmp_ppEnabledExtensionNames[i] = SafeStringCopy(src.ppEnabledExtensionNames[i]); } + ppEnabledExtensionNames = tmp_ppEnabledExtensionNames; if (src.pApplicationInfo) pApplicationInfo = new safe_VkApplicationInfo(*src.pApplicationInfo); else @@ -187,14 +191,16 @@ safe_VkInstanceCreateInfo& safe_VkInstanceCreateInfo::operator=(const safe_VkIns enabledLayerCount = src.enabledLayerCount; enabledExtensionCount = src.enabledExtensionCount; pNext = SafePnextCopy(src.pNext); - ppEnabledLayerNames = new char *[src.enabledLayerCount]; + char **tmp_ppEnabledLayerNames = new char *[src.enabledLayerCount]; for (uint32_t i = 0; i < enabledLayerCount; ++i) { - (const_cast<const char **>(ppEnabledLayerNames))[i] = SafeStringCopy(src.ppEnabledLayerNames[i]); + tmp_ppEnabledLayerNames[i] = SafeStringCopy(src.ppEnabledLayerNames[i]); } - ppEnabledExtensionNames = new char *[src.enabledExtensionCount]; + ppEnabledLayerNames = tmp_ppEnabledLayerNames; + char **tmp_ppEnabledExtensionNames = new char *[src.enabledExtensionCount]; for (uint32_t i = 0; i < enabledExtensionCount; ++i) { - (const_cast<const char **>(ppEnabledExtensionNames))[i] = SafeStringCopy(src.ppEnabledExtensionNames[i]); + tmp_ppEnabledExtensionNames[i] = SafeStringCopy(src.ppEnabledExtensionNames[i]); } + ppEnabledExtensionNames = tmp_ppEnabledExtensionNames; if (src.pApplicationInfo) pApplicationInfo = new safe_VkApplicationInfo(*src.pApplicationInfo); else @@ -230,14 +236,16 @@ void safe_VkInstanceCreateInfo::initialize(const VkInstanceCreateInfo* in_struct enabledLayerCount = in_struct->enabledLayerCount; enabledExtensionCount = in_struct->enabledExtensionCount; pNext = SafePnextCopy(in_struct->pNext); - ppEnabledLayerNames = new char *[in_struct->enabledLayerCount]; + char **tmp_ppEnabledLayerNames = new char *[in_struct->enabledLayerCount]; for (uint32_t i = 0; i < enabledLayerCount; ++i) { - (const_cast<const char **>(ppEnabledLayerNames))[i] = SafeStringCopy(in_struct->ppEnabledLayerNames[i]); + tmp_ppEnabledLayerNames[i] = SafeStringCopy(in_struct->ppEnabledLayerNames[i]); } - ppEnabledExtensionNames = new char *[in_struct->enabledExtensionCount]; + ppEnabledLayerNames = tmp_ppEnabledLayerNames; + char **tmp_ppEnabledExtensionNames = new char *[in_struct->enabledExtensionCount]; for (uint32_t i = 0; i < enabledExtensionCount; ++i) { - (const_cast<const char **>(ppEnabledExtensionNames))[i] = SafeStringCopy(in_struct->ppEnabledExtensionNames[i]); + tmp_ppEnabledExtensionNames[i] = SafeStringCopy(in_struct->ppEnabledExtensionNames[i]); } + ppEnabledExtensionNames = tmp_ppEnabledExtensionNames; if (in_struct->pApplicationInfo) pApplicationInfo = new safe_VkApplicationInfo(in_struct->pApplicationInfo); else @@ -251,14 +259,16 @@ void safe_VkInstanceCreateInfo::initialize(const safe_VkInstanceCreateInfo* src) enabledLayerCount = src->enabledLayerCount; enabledExtensionCount = src->enabledExtensionCount; pNext = SafePnextCopy(src->pNext); - ppEnabledLayerNames = new char *[src->enabledLayerCount]; + char **tmp_ppEnabledLayerNames = new char *[src->enabledLayerCount]; for (uint32_t i = 0; i < enabledLayerCount; ++i) { - (const_cast<const char **>(ppEnabledLayerNames))[i] = SafeStringCopy(src->ppEnabledLayerNames[i]); + tmp_ppEnabledLayerNames[i] = SafeStringCopy(src->ppEnabledLayerNames[i]); } - ppEnabledExtensionNames = new char *[src->enabledExtensionCount]; + ppEnabledLayerNames = tmp_ppEnabledLayerNames; + char **tmp_ppEnabledExtensionNames = new char *[src->enabledExtensionCount]; for (uint32_t i = 0; i < enabledExtensionCount; ++i) { - (const_cast<const char **>(ppEnabledExtensionNames))[i] = SafeStringCopy(src->ppEnabledExtensionNames[i]); + tmp_ppEnabledExtensionNames[i] = SafeStringCopy(src->ppEnabledExtensionNames[i]); } + ppEnabledExtensionNames = tmp_ppEnabledExtensionNames; if (src->pApplicationInfo) pApplicationInfo = new safe_VkApplicationInfo(*src->pApplicationInfo); else @@ -430,14 +440,16 @@ safe_VkDeviceCreateInfo::safe_VkDeviceCreateInfo(const VkDeviceCreateInfo* in_st pEnabledFeatures(nullptr) { pNext = SafePnextCopy(in_struct->pNext); - ppEnabledLayerNames = new char *[in_struct->enabledLayerCount]; + char **tmp_ppEnabledLayerNames = new char *[in_struct->enabledLayerCount]; for (uint32_t i = 0; i < enabledLayerCount; ++i) { - (const_cast<const char **>(ppEnabledLayerNames))[i] = SafeStringCopy(in_struct->ppEnabledLayerNames[i]); + tmp_ppEnabledLayerNames[i] = SafeStringCopy(in_struct->ppEnabledLayerNames[i]); } - ppEnabledExtensionNames = new char *[in_struct->enabledExtensionCount]; + ppEnabledLayerNames = tmp_ppEnabledLayerNames; + char **tmp_ppEnabledExtensionNames = new char *[in_struct->enabledExtensionCount]; for (uint32_t i = 0; i < enabledExtensionCount; ++i) { - (const_cast<const char **>(ppEnabledExtensionNames))[i] = SafeStringCopy(in_struct->ppEnabledExtensionNames[i]); + tmp_ppEnabledExtensionNames[i] = SafeStringCopy(in_struct->ppEnabledExtensionNames[i]); } + ppEnabledExtensionNames = tmp_ppEnabledExtensionNames; if (queueCreateInfoCount && in_struct->pQueueCreateInfos) { pQueueCreateInfos = new safe_VkDeviceQueueCreateInfo[queueCreateInfoCount]; for (uint32_t i = 0; i < queueCreateInfoCount; ++i) { @@ -467,14 +479,16 @@ safe_VkDeviceCreateInfo::safe_VkDeviceCreateInfo(const safe_VkDeviceCreateInfo& enabledExtensionCount = src.enabledExtensionCount; pEnabledFeatures = nullptr; pNext = SafePnextCopy(src.pNext); - ppEnabledLayerNames = new char *[src.enabledLayerCount]; + char **tmp_ppEnabledLayerNames = new char *[src.enabledLayerCount]; for (uint32_t i = 0; i < enabledLayerCount; ++i) { - (const_cast<const char **>(ppEnabledLayerNames))[i] = SafeStringCopy(src.ppEnabledLayerNames[i]); + tmp_ppEnabledLayerNames[i] = SafeStringCopy(src.ppEnabledLayerNames[i]); } - ppEnabledExtensionNames = new char *[src.enabledExtensionCount]; + ppEnabledLayerNames = tmp_ppEnabledLayerNames; + char **tmp_ppEnabledExtensionNames = new char *[src.enabledExtensionCount]; for (uint32_t i = 0; i < enabledExtensionCount; ++i) { - (const_cast<const char **>(ppEnabledExtensionNames))[i] = SafeStringCopy(src.ppEnabledExtensionNames[i]); + tmp_ppEnabledExtensionNames[i] = SafeStringCopy(src.ppEnabledExtensionNames[i]); } + ppEnabledExtensionNames = tmp_ppEnabledExtensionNames; if (queueCreateInfoCount && src.pQueueCreateInfos) { pQueueCreateInfos = new safe_VkDeviceQueueCreateInfo[queueCreateInfoCount]; for (uint32_t i = 0; i < queueCreateInfoCount; ++i) { @@ -517,14 +531,16 @@ safe_VkDeviceCreateInfo& safe_VkDeviceCreateInfo::operator=(const safe_VkDeviceC enabledExtensionCount = src.enabledExtensionCount; pEnabledFeatures = nullptr; pNext = SafePnextCopy(src.pNext); - ppEnabledLayerNames = new char *[src.enabledLayerCount]; + char **tmp_ppEnabledLayerNames = new char *[src.enabledLayerCount]; for (uint32_t i = 0; i < enabledLayerCount; ++i) { - (const_cast<const char **>(ppEnabledLayerNames))[i] = SafeStringCopy(src.ppEnabledLayerNames[i]); + tmp_ppEnabledLayerNames[i] = SafeStringCopy(src.ppEnabledLayerNames[i]); } - ppEnabledExtensionNames = new char *[src.enabledExtensionCount]; + ppEnabledLayerNames = tmp_ppEnabledLayerNames; + char **tmp_ppEnabledExtensionNames = new char *[src.enabledExtensionCount]; for (uint32_t i = 0; i < enabledExtensionCount; ++i) { - (const_cast<const char **>(ppEnabledExtensionNames))[i] = SafeStringCopy(src.ppEnabledExtensionNames[i]); + tmp_ppEnabledExtensionNames[i] = SafeStringCopy(src.ppEnabledExtensionNames[i]); } + ppEnabledExtensionNames = tmp_ppEnabledExtensionNames; if (queueCreateInfoCount && src.pQueueCreateInfos) { pQueueCreateInfos = new safe_VkDeviceQueueCreateInfo[queueCreateInfoCount]; for (uint32_t i = 0; i < queueCreateInfoCount; ++i) { @@ -570,14 +586,16 @@ void safe_VkDeviceCreateInfo::initialize(const VkDeviceCreateInfo* in_struct) enabledExtensionCount = in_struct->enabledExtensionCount; pEnabledFeatures = nullptr; pNext = SafePnextCopy(in_struct->pNext); - ppEnabledLayerNames = new char *[in_struct->enabledLayerCount]; + char **tmp_ppEnabledLayerNames = new char *[in_struct->enabledLayerCount]; for (uint32_t i = 0; i < enabledLayerCount; ++i) { - (const_cast<const char **>(ppEnabledLayerNames))[i] = SafeStringCopy(in_struct->ppEnabledLayerNames[i]); + tmp_ppEnabledLayerNames[i] = SafeStringCopy(in_struct->ppEnabledLayerNames[i]); } - ppEnabledExtensionNames = new char *[in_struct->enabledExtensionCount]; + ppEnabledLayerNames = tmp_ppEnabledLayerNames; + char **tmp_ppEnabledExtensionNames = new char *[in_struct->enabledExtensionCount]; for (uint32_t i = 0; i < enabledExtensionCount; ++i) { - (const_cast<const char **>(ppEnabledExtensionNames))[i] = SafeStringCopy(in_struct->ppEnabledExtensionNames[i]); + tmp_ppEnabledExtensionNames[i] = SafeStringCopy(in_struct->ppEnabledExtensionNames[i]); } + ppEnabledExtensionNames = tmp_ppEnabledExtensionNames; if (queueCreateInfoCount && in_struct->pQueueCreateInfos) { pQueueCreateInfos = new safe_VkDeviceQueueCreateInfo[queueCreateInfoCount]; for (uint32_t i = 0; i < queueCreateInfoCount; ++i) { @@ -599,14 +617,16 @@ void safe_VkDeviceCreateInfo::initialize(const safe_VkDeviceCreateInfo* src) enabledExtensionCount = src->enabledExtensionCount; pEnabledFeatures = nullptr; pNext = SafePnextCopy(src->pNext); - ppEnabledLayerNames = new char *[src->enabledLayerCount]; + char **tmp_ppEnabledLayerNames = new char *[src->enabledLayerCount]; for (uint32_t i = 0; i < enabledLayerCount; ++i) { - (const_cast<const char **>(ppEnabledLayerNames))[i] = SafeStringCopy(src->ppEnabledLayerNames[i]); + tmp_ppEnabledLayerNames[i] = SafeStringCopy(src->ppEnabledLayerNames[i]); } - ppEnabledExtensionNames = new char *[src->enabledExtensionCount]; + ppEnabledLayerNames = tmp_ppEnabledLayerNames; + char **tmp_ppEnabledExtensionNames = new char *[src->enabledExtensionCount]; for (uint32_t i = 0; i < enabledExtensionCount; ++i) { - (const_cast<const char **>(ppEnabledExtensionNames))[i] = SafeStringCopy(src->ppEnabledExtensionNames[i]); + tmp_ppEnabledExtensionNames[i] = SafeStringCopy(src->ppEnabledExtensionNames[i]); } + ppEnabledExtensionNames = tmp_ppEnabledExtensionNames; if (queueCreateInfoCount && src->pQueueCreateInfos) { pQueueCreateInfos = new safe_VkDeviceQueueCreateInfo[queueCreateInfoCount]; for (uint32_t i = 0; i < queueCreateInfoCount; ++i) { @@ -28299,7 +28319,7 @@ void safe_VkPhysicalDeviceTexelBufferAlignmentPropertiesEXT::initialize(const sa pNext = SafePnextCopy(src->pNext); } -const char *SafeStringCopy(const char *in_string) { +char *SafeStringCopy(const char *in_string) { if (nullptr == in_string) return nullptr; char* dest = new char[std::strlen(in_string) + 1]; return std::strcpy(dest, in_string); diff --git a/layers/generated/vk_safe_struct.h b/layers/generated/vk_safe_struct.h index f3e35f4d2..6a8831849 100644 --- a/layers/generated/vk_safe_struct.h +++ b/layers/generated/vk_safe_struct.h @@ -37,7 +37,7 @@ void *SafePnextCopy(const void *pNext); void FreePnextChain(const void *head); void FreePnextChain(void *head); -const char *SafeStringCopy(const char *in_string); +char *SafeStringCopy(const char *in_string); struct safe_VkApplicationInfo { diff --git a/scripts/helper_file_generator.py b/scripts/helper_file_generator.py index 288cc0c0a..638a51765 100644 --- a/scripts/helper_file_generator.py +++ b/scripts/helper_file_generator.py @@ -460,7 +460,7 @@ class HelperFileOutputGenerator(OutputGenerator): safe_struct_helper_header += 'void *SafePnextCopy(const void *pNext);\n' safe_struct_helper_header += 'void FreePnextChain(const void *head);\n' safe_struct_helper_header += 'void FreePnextChain(void *head);\n' - safe_struct_helper_header += 'const char *SafeStringCopy(const char *in_string);\n' + safe_struct_helper_header += 'char *SafeStringCopy(const char *in_string);\n' safe_struct_helper_header += '\n' safe_struct_helper_header += self.GenerateSafeStructHeader() return safe_struct_helper_header @@ -877,7 +877,7 @@ class HelperFileOutputGenerator(OutputGenerator): # Construct Safe-struct helper functions string_copy_proc = '\n\n' - string_copy_proc += 'const char *SafeStringCopy(const char *in_string) {\n' + string_copy_proc += 'char *SafeStringCopy(const char *in_string) {\n' string_copy_proc += ' if (nullptr == in_string) return nullptr;\n' string_copy_proc += ' char* dest = new char[std::strlen(in_string) + 1];\n' string_copy_proc += ' return std::strcpy(dest, in_string);\n' @@ -1250,10 +1250,11 @@ class HelperFileOutputGenerator(OutputGenerator): if m_type == 'char': # Create deep copies of strings if member.len: - copy_strings += ' %s = new char *[in_struct->%s];\n' % (member.name, member.len) + copy_strings += ' char **tmp_%s = new char *[in_struct->%s];\n' % (member.name, member.len) copy_strings += ' for (uint32_t i = 0; i < %s; ++i) {\n' % member.len - copy_strings += ' (const_cast<const char **>(%s))[i] = SafeStringCopy(in_struct->%s[i]);\n' % (member.name, member.name) + copy_strings += ' tmp_%s[i] = SafeStringCopy(in_struct->%s[i]);\n' % (member.name, member.name) copy_strings += ' }\n' + copy_strings += ' %s = tmp_%s;\n' % (member.name, member.name) destruct_txt += ' if (%s) {\n' % member.name destruct_txt += ' for (uint32_t i = 0; i < %s; ++i) {\n' % member.len |