[cfe-commits] r92127 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td include/clang/Lex/LiteralSupport.h lib/Lex/LiteralSupport.cpp lib/Sema/SemaExpr.cpp test/Lexer/constants.c
John McCall
rjmccall at apple.com
Thu Dec 24 01:08:05 PST 2009
Author: rjmccall
Date: Thu Dec 24 03:08:04 2009
New Revision: 92127
URL: http://llvm.org/viewvc/llvm-project?rev=92127&view=rev
Log:
Diagnose out-of-bounds floating-point constants. Fixes rdar://problem/6974641
Modified:
cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
cfe/trunk/include/clang/Lex/LiteralSupport.h
cfe/trunk/lib/Lex/LiteralSupport.cpp
cfe/trunk/lib/Sema/SemaExpr.cpp
cfe/trunk/test/Lexer/constants.c
Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=92127&r1=92126&r2=92127&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Thu Dec 24 03:08:04 2009
@@ -26,9 +26,13 @@
-// 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<
Modified: cfe/trunk/include/clang/Lex/LiteralSupport.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/LiteralSupport.h?rev=92127&r1=92126&r2=92127&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/LiteralSupport.h (original)
+++ cfe/trunk/include/clang/Lex/LiteralSupport.h Thu Dec 24 03:08:04 2009
@@ -16,15 +16,10 @@
#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;
@@ -82,8 +77,7 @@
/// 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:
Modified: cfe/trunk/lib/Lex/LiteralSupport.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/LiteralSupport.cpp?rev=92127&r1=92126&r2=92127&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/LiteralSupport.cpp (original)
+++ cfe/trunk/lib/Lex/LiteralSupport.cpp Thu Dec 24 03:08:04 2009
@@ -610,23 +610,14 @@
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);
}
Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=92127&r1=92126&r2=92127&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Thu Dec 24 03:08:04 2009
@@ -1585,9 +1585,27 @@
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()) {
Modified: cfe/trunk/test/Lexer/constants.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Lexer/constants.c?rev=92127&r1=92126&r2=92127&view=diff
==============================================================================
--- cfe/trunk/test/Lexer/constants.c (original)
+++ cfe/trunk/test/Lexer/constants.c Thu Dec 24 03:08:04 2009
@@ -33,3 +33,25 @@
#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}}
+};
More information about the cfe-commits
mailing list