[clang] [HLSL] Rewrite semantics parsing (PR #152537)

Chris B via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 8 10:41:07 PDT 2025


Nathan =?utf-8?q?Gauër?= <brioche at google.com>,
Nathan =?utf-8?q?Gauër?= <brioche at google.com>,
Nathan =?utf-8?q?Gauër?= <brioche at google.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/152537 at github.com>


================
@@ -118,6 +118,47 @@ static void fixSeparateAttrArgAndNumber(StringRef ArgStr, SourceLocation ArgLoc,
   Slot = new (Ctx) IdentifierLoc(ArgLoc, PP.getIdentifierInfo(FixedArg));
 }
 
+Parser::ParsedSemantic Parser::ParseHLSLSemantic() {
+  assert(Tok.is(tok::identifier) && "Not a HLSL Annotation");
+
+  // Semantic pattern: [A-Za-z_]([A-Za-z_0-9]*[A-Za-z_])?[0-9]*
+  // The first part is the semantic name, the second is the optional
+  // semantic index. The semantic index is the number at the end of
+  // the semantic, including leading zeroes. Digits located before
+  // the last letter are part of the semantic name.
+  bool Invalid = false;
+  SmallString<256> Buffer;
+  Buffer.resize(Tok.getLength() + 1);
+  StringRef Identifier = PP.getSpelling(Tok, Buffer);
+  if (Invalid) {
+    Diag(Tok.getLocation(), diag::err_expected_semantic_identifier);
+    return {};
+  }
+
+  assert(Identifier.size() > 0);
+  unsigned I = Identifier.size();
+  for (; I > 0 && isDigit(Identifier[I - 1]); --I)
----------------
llvm-beanz wrote:

Also as a follow-up it would be nice if `llvm::StringRef` had a utility for this. Currently it has a bunch of find and reverse-find methods, but it doesn't have an ideal fit. `find_if_not` is probably the right pattern, but you'd want an `rfind_if_not` so that this could be something like:

```c++
size_t I = Identifier.rfind_if_not(isDigit);
```

https://github.com/llvm/llvm-project/pull/152537


More information about the cfe-commits mailing list