[llvm] [AsmParser] Use cantFail for FloatLiteral string conversion (PR #197064)

Krut Patel via llvm-commits llvm-commits at lists.llvm.org
Mon May 11 17:50:42 PDT 2026


https://github.com/iamkroot updated https://github.com/llvm/llvm-project/pull/197064

>From 389031cca7a7496392499c52dd404da58cc5da52 Mon Sep 17 00:00:00 2001
From: Krut Patel <krutp at nvidia.com>
Date: Mon, 11 May 2026 22:03:16 +0000
Subject: [PATCH 1/2] [AsmParser] Use cantFail for FloatLiteral string
 conversion

The FloatLiteral handling in parseValID() relied on
`assert(Except && ...)` followed by `*Except` to read the opStatus
returned by APFloat::convertFromString. In NDEBUG builds the assert is
elided, so the Expected was never checked before being dereferenced; if
the lexer ever let an invalid float string through, the destructor
would abort with the unchecked-Expected message and the dereference
would be UB.

Switch to cantFail, which both consumes the error and aborts with the
intended diagnostic message in release builds. Behavior in the success
path is unchanged.

Assisted-by: Claude Opus
---
 llvm/lib/AsmParser/LLParser.cpp | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
index 820f64bf30ba7..5691d020e4d46 100644
--- a/llvm/lib/AsmParser/LLParser.cpp
+++ b/llvm/lib/AsmParser/LLParser.cpp
@@ -4162,15 +4162,18 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
     if (!ExpectedTy->isFloatingPointTy())
       return error(ID.Loc, "floating-point constant invalid for type");
     ID.APFloatVal = APFloat(ExpectedTy->getFltSemantics());
-    auto Except = ID.APFloatVal.convertFromString(
-        Lex.getStrVal(), RoundingMode::NearestTiesToEven);
-    assert(Except && "Invalid float strings should be caught by the lexer");
+    // The lexer is responsible for rejecting malformed floating-point
+    // literals, so any conversion failure here is a programmer bug.
+    APFloat::opStatus Except = cantFail(
+        ID.APFloatVal.convertFromString(Lex.getStrVal(),
+                                        RoundingMode::NearestTiesToEven),
+        "Invalid float strings should be caught by the lexer");
     // Forbid overflowing and underflowing literals, but permit inexact
     // literals. Underflow is thrown when the result is denormal, so to allow
     // denormals, only reject underflowing literals that resulted in a zero.
-    if (*Except & APFloat::opOverflow)
+    if (Except & APFloat::opOverflow)
       return error(ID.Loc, "floating-point constant overflowed type");
-    if ((*Except & APFloat::opUnderflow) && ID.APFloatVal.isZero())
+    if ((Except & APFloat::opUnderflow) && ID.APFloatVal.isZero())
       return error(ID.Loc, "floating-point constant underflowed type");
     ID.Kind = ValID::t_APFloat;
     break;

>From 4d6e9ccc9627927cf57c01787dd1e73c53907548 Mon Sep 17 00:00:00 2001
From: iamkroot <kroot.patel at gmail.com>
Date: Mon, 11 May 2026 17:50:29 -0700
Subject: [PATCH 2/2] Fix clang-format

---
 llvm/lib/AsmParser/LLParser.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
index 5691d020e4d46..59e4b45bf91c1 100644
--- a/llvm/lib/AsmParser/LLParser.cpp
+++ b/llvm/lib/AsmParser/LLParser.cpp
@@ -4164,10 +4164,10 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
     ID.APFloatVal = APFloat(ExpectedTy->getFltSemantics());
     // The lexer is responsible for rejecting malformed floating-point
     // literals, so any conversion failure here is a programmer bug.
-    APFloat::opStatus Except = cantFail(
-        ID.APFloatVal.convertFromString(Lex.getStrVal(),
-                                        RoundingMode::NearestTiesToEven),
-        "Invalid float strings should be caught by the lexer");
+    APFloat::opStatus Except =
+        cantFail(ID.APFloatVal.convertFromString(
+                     Lex.getStrVal(), RoundingMode::NearestTiesToEven),
+                 "Invalid float strings should be caught by the lexer");
     // Forbid overflowing and underflowing literals, but permit inexact
     // literals. Underflow is thrown when the result is denormal, so to allow
     // denormals, only reject underflowing literals that resulted in a zero.



More information about the llvm-commits mailing list