llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Add recoverable string parsing errors to APInt and APFloat

Open llvmbot opened this issue 17 years ago • 4 comments

Bugzilla Link 4745
Version trunk
OS All
Attachments Patch to add recoverable string parsing errors to APFloat., An updated patch for APFloat., A patch to implement recoverable string parsing errors to APInt
Reporter LLVM Bugzilla Contributor
CC @ekatz

Extended Description

This is a bug to track the discussion we had on llvm-dev and irc about adding recoverable string parsing to APInt and APFloat. I've attached the first version to implementing this for APFloat. It converts most of the asserts in the string parsing code to return "opInvalidString" if it finds an invalid character.

Two open questions. First, in order to be efficient, I created a constructor that can create an empty APFloat. Then, you can call "convertFromString" to parse the string. This is a little ugly, however. Does anyone have a better suggestion?

Second, it only has one error code, opInvalidString. Should we expose more details, like "does not have exponent", or "too many dots in number", or is opInvalidString sufficient?

Fortunately, it looks like most of the exposure of this patch is pretty small, as only ConstantFP::get actually creates a APFloat from a string in the codebase.

If this looks good, I'll do the same with APInt, which I expect will be a bit simpler to do.

llvmbot avatar Aug 20 '09 11:08 llvmbot

Implemented the APFloat part in rGc5fb73c5d1b3.

ekatz avatar Jan 06 '20 11:01 ekatz

It works, but I haven't committed it. I was hoping for someone to check over my approach and see if anyone had better suggestions. For instance, I had to add an opInvalidString to APFloat::opStatus. I'm not sure that's the most appropriate location to put that information, since it's only needed for APFloat::convertFromString.

llvmbot avatar Aug 23 '09 01:08 llvmbot

Is this done?

lattner avatar Aug 22 '09 23:08 lattner

Both halves are covered now. APFloat::convertFromString returns Expected<opStatus> (c5fb73c5d1b3), so malformed input is reported as an Error and doesn't assert. For integers, StringRef::getAsInteger(unsigned Radix, APInt &) and StringRef::consumeInteger(unsigned Radix, APInt &) provide recoverable parsing into an APInt. The APInt(numBits, str, radix) constructor intentionally keeps its well-formedness precondition and documents getAsInteger as the alternative. Closing as completed.

isanbard avatar Oct 23 '26 09:10 isanbard