[llvm] [Support] Check the key position in getTypeName() and guard the MSVC path (PR #216528)
Larry Meadows via llvm-commits
llvm-commits at lists.llvm.org
Sat Aug 15 20:48:50 PDT 2026
https://github.com/lfmeadow updated https://github.com/llvm/llvm-project/pull/216528
>From 3ea8dd389c8bf705a405484668d84f7d9318be88 Mon Sep 17 00:00:00 2001
From: Larry Meadows <Lawrence.Meadows at amd.com>
Date: Sat, 15 Aug 2026 21:06:41 -0500
Subject: [PATCH] [Support] Check the key position in getTypeName() and guard
the MSVC path
getTypeName() searches its own signature for a key and asserts that it
found it, but it does so after taking the substring:
LLVM_GET_TYPE_NAME_CONSTEXPR std::string_view TemplateParamsStart =
Name.substr(Name.find(Key));
static_assert(!TemplateParamsStart.empty(),
"Unable to find the template parameter!");
A failed find() returns npos and substr(npos) throws, so the compile
fails on the initializer and the message is never reported; when the
assertion is evaluable it is also tautological, since the substring
begins with Key. Check the position before using it instead, so the
check is the one the message describes.
The MSVC branch was left out when 45ca39d8e1a2 introduced
LLVM_GET_TYPE_NAME_STATIC_ASSERT, and still uses bare static_asserts.
Gate those on the same macro, and spell its condition _MSC_VER >= 1910,
the version in which __FUNCSIG__ became usable in a constant
expression. <cassert> is now needed on both paths, so include it
unconditionally.
LLVM_GET_TYPE_NAME_STATIC_ASSERT remains 1 for every compiler LLVM
supports, so the checks stay compile-time checks.
Co-authored-by: Cursor <cursoragent at cursor.com>
---
llvm/include/llvm/Support/TypeName.h | 42 ++++++++++++++++++++--------
1 file changed, 31 insertions(+), 11 deletions(-)
diff --git a/llvm/include/llvm/Support/TypeName.h b/llvm/include/llvm/Support/TypeName.h
index 85612650ce897..55e8c602701fc 100644
--- a/llvm/include/llvm/Support/TypeName.h
+++ b/llvm/include/llvm/Support/TypeName.h
@@ -9,19 +9,21 @@
#ifndef LLVM_SUPPORT_TYPENAME_H
#define LLVM_SUPPORT_TYPENAME_H
+#include <cassert>
#include <string_view>
#include "llvm/ADT/StringRef.h"
-// Versions of GCC prior to GCC 9 don't declare __PRETTY_FUNCTION__ as constexpr
-#if defined(__clang__) || defined(_MSC_VER) || \
- (defined(__GNUC__) && __GNUC__ >= 9)
+// Versions of GCC prior to GCC 9 don't declare __PRETTY_FUNCTION__ as
+// constexpr, and versions of MSVC prior to VS2017 15.3 (_MSC_VER 1910) don't
+// declare __FUNCSIG__ as constexpr.
+#if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 9) || \
+ (defined(_MSC_VER) && _MSC_VER >= 1910)
#define LLVM_GET_TYPE_NAME_CONSTEXPR constexpr
#define LLVM_GET_TYPE_NAME_STATIC_ASSERT 1
#else
#define LLVM_GET_TYPE_NAME_CONSTEXPR
#define LLVM_GET_TYPE_NAME_STATIC_ASSERT 0
-#include <cassert>
#endif
namespace llvm {
@@ -42,16 +44,19 @@ inline LLVM_GET_TYPE_NAME_CONSTEXPR StringRef getTypeName() {
LLVM_GET_TYPE_NAME_CONSTEXPR std::string_view Name = __PRETTY_FUNCTION__;
LLVM_GET_TYPE_NAME_CONSTEXPR std::string_view Key = "DesiredTypeName = ";
- LLVM_GET_TYPE_NAME_CONSTEXPR std::string_view TemplateParamsStart =
- Name.substr(Name.find(Key));
+ LLVM_GET_TYPE_NAME_CONSTEXPR std::string_view::size_type KeyPos =
+ Name.find(Key);
#if LLVM_GET_TYPE_NAME_STATIC_ASSERT
- static_assert(!TemplateParamsStart.empty(),
+ static_assert(KeyPos != std::string_view::npos,
"Unable to find the template parameter!");
#else
- assert(!TemplateParamsStart.empty() &&
+ assert(KeyPos != std::string_view::npos &&
"Unable to find the template parameter!");
#endif
+ LLVM_GET_TYPE_NAME_CONSTEXPR std::string_view TemplateParamsStart =
+ Name.substr(KeyPos);
+
LLVM_GET_TYPE_NAME_CONSTEXPR std::string_view SubstitutionKey =
TemplateParamsStart.substr(Key.size());
@@ -69,10 +74,19 @@ inline LLVM_GET_TYPE_NAME_CONSTEXPR StringRef getTypeName() {
LLVM_GET_TYPE_NAME_CONSTEXPR std::string_view Name = __FUNCSIG__;
LLVM_GET_TYPE_NAME_CONSTEXPR std::string_view Key = "getTypeName<";
- LLVM_GET_TYPE_NAME_CONSTEXPR std::string_view GetTypeNameStart =
- Name.substr(Name.find(Key));
- static_assert(!GetTypeNameStart.empty(),
+ LLVM_GET_TYPE_NAME_CONSTEXPR std::string_view::size_type KeyPos =
+ Name.find(Key);
+#if LLVM_GET_TYPE_NAME_STATIC_ASSERT
+ static_assert(KeyPos != std::string_view::npos,
"Unable to find the template parameter!");
+#else
+ assert(KeyPos != std::string_view::npos &&
+ "Unable to find the template parameter!");
+#endif
+
+ LLVM_GET_TYPE_NAME_CONSTEXPR std::string_view GetTypeNameStart =
+ Name.substr(KeyPos);
+
LLVM_GET_TYPE_NAME_CONSTEXPR std::string_view SubstitutionKey =
GetTypeNameStart.substr(Key.size());
@@ -95,8 +109,14 @@ inline LLVM_GET_TYPE_NAME_CONSTEXPR StringRef getTypeName() {
: RmPrefixUnion;
LLVM_GET_TYPE_NAME_CONSTEXPR auto AnglePos = RmPrefixEnum.rfind('>');
+#if LLVM_GET_TYPE_NAME_STATIC_ASSERT
static_assert(AnglePos != std::string_view::npos,
"Unable to find the closing '>'!");
+#else
+ assert(AnglePos != std::string_view::npos &&
+ "Unable to find the closing '>'!");
+#endif
+
return RmPrefixEnum.substr(0, AnglePos);
#else
// No known technique for statically extracting a type name on this compiler.
More information about the llvm-commits
mailing list