-// Semantic analysis of string and character constant literals.
+// Semantic analysis of constant literals.
def ext_predef_outside_function : Warning<
"predefined identifier is only valid inside function">;
+def err_float_overflow : Error<
+ "magnitude of floating-point constant too large for type %0; maximum is %1">;
+def err_float_underflow : Error<
+ "magnitude of floating-point constant too small for type %0; minimum is %1">;
// C99 Designated Initializers
def err_array_designator_negative : Error<
#define CLANG_LITERALSUPPORT_H
#include <string>
+#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/System/DataTypes.h"
-namespace llvm {
- class APInt;
- class APFloat;
- struct fltSemantics;
-}
-
namespace clang {
class Diagnostic;
/// The optional bool isExact (passed-by-reference) has its value
/// set to true if the returned APFloat can represent the number in the
/// literal exactly, and false otherwise.
- llvm::APFloat GetFloatValue(const llvm::fltSemantics &Format,
- bool* isExact = NULL);
+ llvm::APFloat::opStatus GetFloatValue(llvm::APFloat &Result);
private:
return OverflowOccurred;
}
-llvm::APFloat NumericLiteralParser::
-GetFloatValue(const llvm::fltSemantics &Format, bool* isExact) {
+llvm::APFloat::opStatus
+NumericLiteralParser::GetFloatValue(llvm::APFloat &Result) {
using llvm::APFloat;
using llvm::StringRef;
unsigned n = std::min(SuffixBegin - ThisTokBegin, ThisTokEnd - ThisTokBegin);
-
- APFloat V (Format, APFloat::fcZero, false);
- APFloat::opStatus status;
-
- status = V.convertFromString(StringRef(ThisTokBegin, n),
- APFloat::rmNearestTiesToEven);
-
- if (isExact)
- *isExact = status == APFloat::opOK;
-
- return V;
+ return Result.convertFromString(StringRef(ThisTokBegin, n),
+ APFloat::rmNearestTiesToEven);
}
const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty);
- // isExact will be set by GetFloatValue().
- bool isExact = false;
- llvm::APFloat Val = Literal.GetFloatValue(Format, &isExact);
+ using llvm::APFloat;
+ APFloat Val(Format);
+
+ APFloat::opStatus result = Literal.GetFloatValue(Val);
+ if (result & (APFloat::opOverflow | APFloat::opUnderflow)) {
+ unsigned diagnostic;
+ llvm::SmallVector<char, 20> buffer;
+ if (result & APFloat::opOverflow) {
+ diagnostic = diag::err_float_overflow;
+ APFloat::getLargest(Format).toString(buffer);
+ } else {
+ diagnostic = diag::err_float_underflow;
+ APFloat::getSmallest(Format).toString(buffer);
+ }
+
+ Diag(Tok.getLocation(), diagnostic)
+ << Ty
+ << llvm::StringRef(buffer.data(), buffer.size());
+ }
+
+ bool isExact = (result == APFloat::opOK);
Res = new (Context) FloatingLiteral(Val, isExact, Ty, Tok.getLocation());
} else if (!Literal.isIntegerLiteral()) {
#pragma clang diagnostic ignored "-Wfour-char-constants"
char f = 'abcd'; // ignored.
+
+// rdar://problem/6974641
+float t0[] = {
+ 1.9e20f,
+ 1.9e-20f,
+ 1.9e50f, // expected-error {{too large}}
+ 1.9e-50f, // expected-error {{too small}}
+ -1.9e20f,
+ -1.9e-20f,
+ -1.9e50f, // expected-error {{too large}}
+ -1.9e-50f // expected-error {{too small}}
+};
+double t1[] = {
+ 1.9e50,
+ 1.9e-50,
+ 1.9e500, // expected-error {{too large}}
+ 1.9e-500, // expected-error {{too small}}
+ -1.9e50,
+ -1.9e-50,
+ -1.9e500, // expected-error {{too large}}
+ -1.9e-500 // expected-error {{too small}}
+};