================
@@ -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()) {
----------------
Icohedron wrote:

`2147483648` would be parsed by `handleUIntLiteral` instead. But yes, if 
`handleUIntLiteral` was called instead for some reason then it would fail to 
report an overflow.

https://github.com/llvm/llvm-project/pull/140181
_______________________________________________
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits

Reply via email to