Skip to content

Commit

Permalink
[APFloat] Add recoverable string parsing errors to APFloat
Browse files Browse the repository at this point in the history
Implementing the APFloat part in PR4745.

Differential Revision: https://reviews.llvm.org/D69770
  • Loading branch information
ekatz committed Jan 6, 2020
1 parent a792953 commit c5fb73c
Show file tree
Hide file tree
Showing 8 changed files with 256 additions and 196 deletions.
5 changes: 4 additions & 1 deletion clang/lib/Lex/LiteralSupport.cpp
Expand Up @@ -1051,7 +1051,10 @@ NumericLiteralParser::GetFloatValue(llvm::APFloat &Result) {
Str = Buffer;
}

return Result.convertFromString(Str, APFloat::rmNearestTiesToEven);
auto StatusOrErr =
Result.convertFromString(Str, APFloat::rmNearestTiesToEven);
assert(StatusOrErr && "Invalid floating point representation");
return StatusOrErr ? *StatusOrErr : APFloat::opInvalidOp;
}

static inline bool IsExponentPart(char c) {
Expand Down
11 changes: 6 additions & 5 deletions llvm/include/llvm/ADT/APFloat.h
Expand Up @@ -38,6 +38,7 @@ class StringRef;
class APFloat;
class raw_ostream;

template <typename T> class Expected;
template <typename T> class SmallVectorImpl;

/// Enum that represents what fraction of the LSB truncated bits of an fp number
Expand Down Expand Up @@ -299,7 +300,7 @@ class IEEEFloat final : public APFloatBase {
bool, roundingMode);
opStatus convertFromZeroExtendedInteger(const integerPart *, unsigned int,
bool, roundingMode);
opStatus convertFromString(StringRef, roundingMode);
Expected<opStatus> convertFromString(StringRef, roundingMode);
APInt bitcastToAPInt() const;
double convertToDouble() const;
float convertToFloat() const;
Expand Down Expand Up @@ -525,8 +526,8 @@ class IEEEFloat final : public APFloatBase {
bool *) const;
opStatus convertFromUnsignedParts(const integerPart *, unsigned int,
roundingMode);
opStatus convertFromHexadecimalString(StringRef, roundingMode);
opStatus convertFromDecimalString(StringRef, roundingMode);
Expected<opStatus> convertFromHexadecimalString(StringRef, roundingMode);
Expected<opStatus> convertFromDecimalString(StringRef, roundingMode);
char *convertNormalToHexString(char *, unsigned int, bool,
roundingMode) const;
opStatus roundSignificandWithExponent(const integerPart *, unsigned int, int,
Expand Down Expand Up @@ -648,7 +649,7 @@ class DoubleAPFloat final : public APFloatBase {
cmpResult compare(const DoubleAPFloat &RHS) const;
bool bitwiseIsEqual(const DoubleAPFloat &RHS) const;
APInt bitcastToAPInt() const;
opStatus convertFromString(StringRef, roundingMode);
Expected<opStatus> convertFromString(StringRef, roundingMode);
opStatus next(bool nextDown);

opStatus convertToInteger(MutableArrayRef<integerPart> Input,
Expand Down Expand Up @@ -1108,7 +1109,7 @@ class APFloat : public APFloatBase {
APFLOAT_DISPATCH_ON_SEMANTICS(
convertFromZeroExtendedInteger(Input, InputSize, IsSigned, RM));
}
opStatus convertFromString(StringRef, roundingMode);
Expected<opStatus> convertFromString(StringRef, roundingMode);
APInt bitcastToAPInt() const {
APFLOAT_DISPATCH_ON_SEMANTICS(bitcastToAPInt());
}
Expand Down
3 changes: 1 addition & 2 deletions llvm/lib/MC/MCParser/AsmParser.cpp
Expand Up @@ -3130,8 +3130,7 @@ bool AsmParser::parseRealValue(const fltSemantics &Semantics, APInt &Res) {
Value = APFloat::getNaN(Semantics, false, ~0);
else
return TokError("invalid floating point literal");
} else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) ==
APFloat::opInvalidOp)
} else if (!Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven))
return TokError("invalid floating point literal");
if (IsNeg)
Value.changeSign();
Expand Down

0 comments on commit c5fb73c

Please sign in to comment.