[clang] [clang][HLSL] Fix dangling StringRef in ParsedSemantic::Name (PR #200450)
Roberto Turrado Camblor via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 15 02:24:45 PDT 2026
https://github.com/rturrado updated https://github.com/llvm/llvm-project/pull/200450
>From 2012e9704af9ef230a8271d2f1d9743fe2893613 Mon Sep 17 00:00:00 2001
From: rturrado <rturrado at gmail.com>
Date: Fri, 29 May 2026 18:41:35 +0200
Subject: [PATCH 1/3] [clang][HLSL] Fix dangling StringRef in
ParsedSemantic::Name
Change `ParsedSemantic::Name` from `StringRef` to `std::string`.
Also mark `ParseHLSLSemantic()` as `const` and fix a typo in a nearby comment.
Fixes #194223
---
clang/include/clang/Parse/Parser.h | 5 +++--
clang/lib/Parse/ParseHLSL.cpp | 6 +++---
2 files changed, 6 insertions(+), 5 deletions(-)
diff --git a/clang/include/clang/Parse/Parser.h b/clang/include/clang/Parse/Parser.h
index c6c492b4980af..a034f7e369283 100644
--- a/clang/include/clang/Parse/Parser.h
+++ b/clang/include/clang/Parse/Parser.h
@@ -27,6 +27,7 @@
#include "llvm/Support/SaveAndRestore.h"
#include <optional>
#include <stack>
+#include <string>
namespace clang {
class PragmaHandler;
@@ -5271,12 +5272,12 @@ class Parser : public CodeCompletionHandler {
}
struct ParsedSemantic {
- StringRef Name = "";
+ std::string Name = "";
unsigned Index = 0;
bool Explicit = false;
};
- ParsedSemantic ParseHLSLSemantic();
+ ParsedSemantic ParseHLSLSemantic() const;
void ParseHLSLAnnotations(ParsedAttributes &Attrs,
SourceLocation *EndLoc = nullptr,
diff --git a/clang/lib/Parse/ParseHLSL.cpp b/clang/lib/Parse/ParseHLSL.cpp
index c727ee3a1f1a6..f6de0cb4afb50 100644
--- a/clang/lib/Parse/ParseHLSL.cpp
+++ b/clang/lib/Parse/ParseHLSL.cpp
@@ -118,7 +118,7 @@ static void fixSeparateAttrArgAndNumber(StringRef ArgStr, SourceLocation ArgLoc,
Slot = new (Ctx) IdentifierLoc(ArgLoc, PP.getIdentifierInfo(FixedArg));
}
-Parser::ParsedSemantic Parser::ParseHLSLSemantic() {
+Parser::ParsedSemantic Parser::ParseHLSLSemantic() const {
assert(Tok.is(tok::identifier) && "Not a HLSL Annotation");
// Semantic pattern: [A-Za-z_]([A-Za-z_0-9]*[A-Za-z_])?[0-9]*
@@ -133,10 +133,10 @@ Parser::ParsedSemantic Parser::ParseHLSLSemantic() {
// Determine the start of the semantic index.
unsigned IndexIndex = Identifier.find_last_not_of("0123456789") + 1;
- // ParseHLSLSemantic being called on an indentifier, the first
+ // ParseHLSLSemantic being called on an identifier, the first
// character cannot be a digit. This error should be handled by
// the caller. We can assert here.
- StringRef SemanticName = Identifier.take_front(IndexIndex);
+ std::string SemanticName = Identifier.take_front(IndexIndex).str();
assert(SemanticName.size() > 0);
unsigned Index = 0;
>From 605657174a3ff37ffddeba75f45021c410aa0995 Mon Sep 17 00:00:00 2001
From: rturrado <rturrado at gmail.com>
Date: Sat, 27 Jun 2026 00:38:46 +0200
Subject: [PATCH 2/3] [clang][HLSL] Store ParsedSemantic::Name as
IdentifierInfo *
Replace `std::string` with `IdentifierInfo *`.
`ParseHLSLSemantic` now stores the name in the shared identifier table via `PP.getIdentifierInfo()`, and `ParseHLSLAnnotations` uses that pointer directly.
The pointer returned by `getIdentifierInfo()` stays valid for the whole compilation, because the `Preprocessor` owns the identifier table and never removes entries from it.
This matches how `Token`, `NamedDecl`, `ParsedAttr`, and other clang classes store identifier-like data.
Assisted-by: Claude Opus 4.7 via Claude Code
---
clang/include/clang/Parse/Parser.h | 2 +-
clang/lib/Parse/ParseHLSL.cpp | 11 +++++++----
2 files changed, 8 insertions(+), 5 deletions(-)
diff --git a/clang/include/clang/Parse/Parser.h b/clang/include/clang/Parse/Parser.h
index a034f7e369283..65ad29f772867 100644
--- a/clang/include/clang/Parse/Parser.h
+++ b/clang/include/clang/Parse/Parser.h
@@ -5272,7 +5272,7 @@ class Parser : public CodeCompletionHandler {
}
struct ParsedSemantic {
- std::string Name = "";
+ IdentifierInfo *Name = nullptr;
unsigned Index = 0;
bool Explicit = false;
};
diff --git a/clang/lib/Parse/ParseHLSL.cpp b/clang/lib/Parse/ParseHLSL.cpp
index f6de0cb4afb50..afa8f54160601 100644
--- a/clang/lib/Parse/ParseHLSL.cpp
+++ b/clang/lib/Parse/ParseHLSL.cpp
@@ -136,8 +136,11 @@ Parser::ParsedSemantic Parser::ParseHLSLSemantic() const {
// ParseHLSLSemantic being called on an identifier, the first
// character cannot be a digit. This error should be handled by
// the caller. We can assert here.
- std::string SemanticName = Identifier.take_front(IndexIndex).str();
- assert(SemanticName.size() > 0);
+ // getIdentifierInfo copies the bytes into the identifier table,
+ // so Buffer can safely die when this function returns.
+ IdentifierInfo *Name =
+ PP.getIdentifierInfo(Identifier.take_front(IndexIndex));
+ assert(Name->getLength() > 0);
unsigned Index = 0;
bool Explicit = false;
@@ -149,7 +152,7 @@ Parser::ParsedSemantic Parser::ParseHLSLSemantic() const {
assert(!Failure);
}
- return {SemanticName, Index, Explicit};
+ return {Name, Index, Explicit};
}
void Parser::ParseHLSLAnnotations(ParsedAttributes &Attrs,
@@ -327,7 +330,7 @@ void Parser::ParseHLSLAnnotations(ParsedAttributes &Attrs,
SourceLocation()));
ArgExprs.push_back(IntegerLiteral::Create(
Ctx, llvm::APInt(1, Semantic.Explicit), Ctx.BoolTy, SourceLocation()));
- II = PP.getIdentifierInfo(Semantic.Name);
+ II = Semantic.Name;
break;
}
case ParsedAttr::UnknownAttribute: // FIXME: maybe this is obsolete?
>From 4dc3376b709c5d9b60a62bcfa2ef6fbb4a598d02 Mon Sep 17 00:00:00 2001
From: Roberto Turrado Camblor <rturrado at gmail.com>
Date: Wed, 15 Jul 2026 11:13:43 +0200
Subject: [PATCH 3/3] Remove unnecessary header
---
clang/include/clang/Parse/Parser.h | 1 -
1 file changed, 1 deletion(-)
diff --git a/clang/include/clang/Parse/Parser.h b/clang/include/clang/Parse/Parser.h
index 65ad29f772867..2ec7f206ca6e6 100644
--- a/clang/include/clang/Parse/Parser.h
+++ b/clang/include/clang/Parse/Parser.h
@@ -27,7 +27,6 @@
#include "llvm/Support/SaveAndRestore.h"
#include <optional>
#include <stack>
-#include <string>
namespace clang {
class PragmaHandler;
More information about the cfe-commits
mailing list