[clang] [llvm] [HLSL][RootSignature] Add parsing of floats for StaticSampler (PR #140181)

Finn Plummer via cfe-commits cfe-commits at lists.llvm.org
Wed May 28 10:41:33 PDT 2025


================
@@ -821,22 +873,115 @@ std::optional<uint32_t> RootSignatureParser::handleUIntLiteral() {
                                       PP.getSourceManager(), PP.getLangOpts(),
                                       PP.getTargetInfo(), PP.getDiagnostics());
   if (Literal.hadError)
-    return true; // Error has already been reported so just return
+    return std::nullopt; // Error has already been reported so just return
 
-  assert(Literal.isIntegerLiteral() && "IsNumberChar will only support digits");
+  assert(Literal.isIntegerLiteral() &&
+         "NumSpelling can only consist of digits");
 
-  llvm::APSInt Val = llvm::APSInt(32, false);
+  llvm::APSInt Val = llvm::APSInt(32, /*IsUnsigned=*/true);
   if (Literal.GetIntegerValue(Val)) {
     // Report that the value has overflowed
     PP.getDiagnostics().Report(CurToken.TokLoc,
                                diag::err_hlsl_number_literal_overflow)
-        << 0 << CurToken.NumSpelling;
+        << /*integer type*/ 0 << /*is signed*/ 0;
     return std::nullopt;
   }
 
   return Val.getExtValue();
 }
 
+std::optional<int32_t> RootSignatureParser::handleIntLiteral(bool Negated) {
+  // Parse the numeric value and do semantic checks on its specification
+  clang::NumericLiteralParser Literal(CurToken.NumSpelling, CurToken.TokLoc,
+                                      PP.getSourceManager(), PP.getLangOpts(),
+                                      PP.getTargetInfo(), PP.getDiagnostics());
+  if (Literal.hadError)
+    return std::nullopt; // Error has already been reported so just return
+
+  assert(Literal.isIntegerLiteral() &&
+         "NumSpelling can only consist of digits");
+
+  llvm::APSInt Val = llvm::APSInt(32, /*IsUnsigned=*/true);
+  // GetIntegerValue will overwrite Val from the parsed Literal and return
+  // true if it overflows as a 32-bit unsigned int. Then check that it also
+  // doesn't overflow as a signed 32-bit int.
+  int64_t MaxMagnitude =
+      -static_cast<int64_t>(std::numeric_limits<int32_t>::min());
+  if (Literal.GetIntegerValue(Val) || MaxMagnitude < Val.getExtValue()) {
----------------
inbelic wrote:

Updated such that `handleIntLiteral` will correctly handle overflow for a positive signed integer `int32_t`.

The only caveat is that it doesn't have a test case as this can't currently be invoked through the current public parser api.

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


More information about the cfe-commits mailing list