[clang-tools-extra] 24b326c - [APFloat] Fix checked error assert failures
Ehud Katz via cfe-commits
cfe-commits at lists.llvm.org
Wed Jan 8 23:54:00 PST 2020
Author: Ehud Katz
Date: 2020-01-09T09:42:32+02:00
New Revision: 24b326cc610dfdccdd50bc78505ec228d96c8e7a
URL: https://github.com/llvm/llvm-project/commit/24b326cc610dfdccdd50bc78505ec228d96c8e7a
DIFF: https://github.com/llvm/llvm-project/commit/24b326cc610dfdccdd50bc78505ec228d96c8e7a.diff
LOG: [APFloat] Fix checked error assert failures
`APFLoat::convertFromString` returns `Expected` result, which must be
"checked" if the LLVM_ENABLE_ABI_BREAKING_CHECKS preprocessor flag is
set.
To mark an `Expected` result as "checked" we must consume the `Error`
within.
In many cases, we are only interested in knowing if an error occured,
without the need to examine the error info. This is achieved, easily,
with the `errorToBool()` API.
Added:
Modified:
clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp
clang/lib/Lex/LiteralSupport.cpp
llvm/include/llvm/ADT/StringRef.h
llvm/lib/MC/MCParser/AsmParser.cpp
llvm/lib/Support/APFloat.cpp
llvm/lib/Support/StringRef.cpp
llvm/lib/Target/AArch64/AsmParser/AArch64AsmParser.cpp
llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp
llvm/unittests/ADT/APFloatTest.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp b/clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp
index 231e565f27e5..86443a155069 100644
--- a/clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/MagicNumbersCheck.cpp
@@ -86,15 +86,17 @@ MagicNumbersCheck::MagicNumbersCheck(StringRef Name, ClangTidyContext *Context)
IgnoredDoublePointValues.reserve(IgnoredFloatingPointValuesInput.size());
for (const auto &InputValue : IgnoredFloatingPointValuesInput) {
llvm::APFloat FloatValue(llvm::APFloat::IEEEsingle());
- if (!FloatValue.convertFromString(InputValue, DefaultRoundingMode)) {
- assert(false && "Invalid floating point representation");
- }
+ auto StatusOrErr =
+ FloatValue.convertFromString(InputValue, DefaultRoundingMode);
+ assert(StatusOrErr && "Invalid floating point representation");
+ consumeError(StatusOrErr.takeError());
IgnoredFloatingPointValues.push_back(FloatValue.convertToFloat());
llvm::APFloat DoubleValue(llvm::APFloat::IEEEdouble());
- if (!DoubleValue.convertFromString(InputValue, DefaultRoundingMode)) {
- assert(false && "Invalid floating point representation");
- }
+ StatusOrErr =
+ DoubleValue.convertFromString(InputValue, DefaultRoundingMode);
+ assert(StatusOrErr && "Invalid floating point representation");
+ consumeError(StatusOrErr.takeError());
IgnoredDoublePointValues.push_back(DoubleValue.convertToDouble());
}
llvm::sort(IgnoredFloatingPointValues.begin(),
diff --git a/clang/lib/Lex/LiteralSupport.cpp b/clang/lib/Lex/LiteralSupport.cpp
index 5881852b1424..9a852141c6ee 100644
--- a/clang/lib/Lex/LiteralSupport.cpp
+++ b/clang/lib/Lex/LiteralSupport.cpp
@@ -1053,11 +1053,9 @@ NumericLiteralParser::GetFloatValue(llvm::APFloat &Result) {
auto StatusOrErr =
Result.convertFromString(Str, APFloat::rmNearestTiesToEven);
- if (!StatusOrErr) {
- assert(false && "Invalid floating point representation");
- return APFloat::opInvalidOp;
- }
- return *StatusOrErr;
+ assert(StatusOrErr && "Invalid floating point representation");
+ return !errorToBool(StatusOrErr.takeError()) ? *StatusOrErr
+ : APFloat::opInvalidOp;
}
static inline bool IsExponentPart(char c) {
diff --git a/llvm/include/llvm/ADT/StringRef.h b/llvm/include/llvm/ADT/StringRef.h
index e87a08f7efff..9bfaaccd953e 100644
--- a/llvm/include/llvm/ADT/StringRef.h
+++ b/llvm/include/llvm/ADT/StringRef.h
@@ -566,7 +566,8 @@ namespace llvm {
///
/// If \p AllowInexact is false, the function will fail if the string
/// cannot be represented exactly. Otherwise, the function only fails
- /// in case of an overflow or underflow.
+ /// in case of an overflow or underflow, or an invalid floating point
+ /// representation.
bool getAsDouble(double &Result, bool AllowInexact = true) const;
/// @}
diff --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp
index 0c4eb953aa4e..dc8132b627a6 100644
--- a/llvm/lib/MC/MCParser/AsmParser.cpp
+++ b/llvm/lib/MC/MCParser/AsmParser.cpp
@@ -3130,7 +3130,9 @@ 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))
+ } else if (errorToBool(
+ Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven)
+ .takeError()))
return TokError("invalid floating point literal");
if (IsNeg)
Value.changeSign();
diff --git a/llvm/lib/Support/APFloat.cpp b/llvm/lib/Support/APFloat.cpp
index f8a217d3535d..050c37baefb8 100644
--- a/llvm/lib/Support/APFloat.cpp
+++ b/llvm/lib/Support/APFloat.cpp
@@ -4518,9 +4518,8 @@ hash_code hash_value(const APFloat &Arg) {
APFloat::APFloat(const fltSemantics &Semantics, StringRef S)
: APFloat(Semantics) {
auto StatusOrErr = convertFromString(S, rmNearestTiesToEven);
- if (!StatusOrErr) {
- assert(false && "Invalid floating point representation");
- }
+ assert(StatusOrErr && "Invalid floating point representation");
+ consumeError(StatusOrErr.takeError());
}
APFloat::opStatus APFloat::convert(const fltSemantics &ToSemantics,
diff --git a/llvm/lib/Support/StringRef.cpp b/llvm/lib/Support/StringRef.cpp
index b5db172cc1a3..104482de4ad7 100644
--- a/llvm/lib/Support/StringRef.cpp
+++ b/llvm/lib/Support/StringRef.cpp
@@ -588,13 +588,11 @@ bool StringRef::getAsInteger(unsigned Radix, APInt &Result) const {
bool StringRef::getAsDouble(double &Result, bool AllowInexact) const {
APFloat F(0.0);
- auto ErrOrStatus = F.convertFromString(*this, APFloat::rmNearestTiesToEven);
- if (!ErrOrStatus) {
- assert(false && "Invalid floating point representation");
+ auto StatusOrErr = F.convertFromString(*this, APFloat::rmNearestTiesToEven);
+ if (errorToBool(StatusOrErr.takeError()))
return true;
- }
- APFloat::opStatus Status = *ErrOrStatus;
+ APFloat::opStatus Status = *StatusOrErr;
if (Status != APFloat::opOK) {
if (!AllowInexact || !(Status & APFloat::opInexact))
return true;
diff --git a/llvm/lib/Target/AArch64/AsmParser/AArch64AsmParser.cpp b/llvm/lib/Target/AArch64/AsmParser/AArch64AsmParser.cpp
index bd48e5d846af..70c9db13f139 100644
--- a/llvm/lib/Target/AArch64/AsmParser/AArch64AsmParser.cpp
+++ b/llvm/lib/Target/AArch64/AsmParser/AArch64AsmParser.cpp
@@ -1223,7 +1223,7 @@ class AArch64Operand : public MCParsedAsmOperand {
APFloat RealVal(APFloat::IEEEdouble());
auto StatusOrErr =
RealVal.convertFromString(Desc->Repr, APFloat::rmTowardZero);
- if (!StatusOrErr || *StatusOrErr != APFloat::opOK)
+ if (errorToBool(StatusOrErr.takeError()) || *StatusOrErr != APFloat::opOK)
llvm_unreachable("FP immediate is not exact");
if (getFPImm().bitwiseIsEqual(RealVal))
@@ -2580,7 +2580,7 @@ AArch64AsmParser::tryParseFPImm(OperandVector &Operands) {
APFloat RealVal(APFloat::IEEEdouble());
auto StatusOrErr =
RealVal.convertFromString(Tok.getString(), APFloat::rmTowardZero);
- if (!StatusOrErr) {
+ if (errorToBool(StatusOrErr.takeError())) {
TokError("invalid floating point representation");
return MatchOperand_ParseFail;
}
diff --git a/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp b/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp
index 326df6bc8fb2..d5834826fcd8 100644
--- a/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp
+++ b/llvm/lib/Target/AMDGPU/AsmParser/AMDGPUAsmParser.cpp
@@ -2363,7 +2363,7 @@ AMDGPUAsmParser::parseImm(OperandVector &Operands, bool HasSP3AbsModifier) {
APFloat RealVal(APFloat::IEEEdouble());
auto roundMode = APFloat::rmNearestTiesToEven;
- if (!RealVal.convertFromString(Num, roundMode)) {
+ if (errorToBool(RealVal.convertFromString(Num, roundMode).takeError())) {
return MatchOperand_ParseFail;
}
if (Negate)
diff --git a/llvm/unittests/ADT/APFloatTest.cpp b/llvm/unittests/ADT/APFloatTest.cpp
index adbf1b3b8c60..65b831c96e8f 100644
--- a/llvm/unittests/ADT/APFloatTest.cpp
+++ b/llvm/unittests/ADT/APFloatTest.cpp
@@ -22,15 +22,18 @@ using namespace llvm;
static std::string convertToErrorFromString(StringRef Str) {
llvm::APFloat F(0.0);
- auto ErrOrStatus =
+ auto StatusOrErr =
F.convertFromString(Str, llvm::APFloat::rmNearestTiesToEven);
- EXPECT_TRUE(!ErrOrStatus);
- return toString(ErrOrStatus.takeError());
+ EXPECT_TRUE(!StatusOrErr);
+ return toString(StatusOrErr.takeError());
}
static double convertToDoubleFromString(StringRef Str) {
llvm::APFloat F(0.0);
- EXPECT_FALSE(!F.convertFromString(Str, llvm::APFloat::rmNearestTiesToEven));
+ auto StatusOrErr =
+ F.convertFromString(Str, llvm::APFloat::rmNearestTiesToEven);
+ EXPECT_FALSE(!StatusOrErr);
+ consumeError(StatusOrErr.takeError());
return F.convertToDouble();
}
More information about the cfe-commits
mailing list