]> granicus.if.org Git - clang/commitdiff
Diagnose out-of-bounds floating-point constants. Fixes rdar://problem/6974641
authorJohn McCall <rjmccall@apple.com>
Thu, 24 Dec 2009 09:08:04 +0000 (09:08 +0000)
committerJohn McCall <rjmccall@apple.com>
Thu, 24 Dec 2009 09:08:04 +0000 (09:08 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@92127 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
include/clang/Lex/LiteralSupport.h
lib/Lex/LiteralSupport.cpp
lib/Sema/SemaExpr.cpp
test/Lexer/constants.c

index 6f1039e9786eff5eb43c8944d84301f85d272cc2..bb507d17a74168c3b5a62e47d71e98a059fe46b9 100644 (file)
@@ -26,9 +26,13 @@ def ext_null_pointer_expr_not_ice : Extension<
 
 
 
-// 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<
index c4ab5aebf7217d48f0c3c36dfa28b1cae2ecf1fe..2334d728f6e3df4aeec7e823f3cb7279926df5b5 100644 (file)
 #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 @@ public:
   /// 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:
 
index f1993d226439cbdc262bb201ac215ce125785fd9..9aaa82d6263ce65f58fffbeced44d31060823622 100644 (file)
@@ -610,23 +610,14 @@ bool NumericLiteralParser::GetIntegerValue(llvm::APInt &Val) {
   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);
 }
 
 
index 24bc29560d4850fe49ce5fb1d229a5fa08c81804..0951677db240c676697a2034d3f52a6216d4780f 100644 (file)
@@ -1585,9 +1585,27 @@ Action::OwningExprResult Sema::ActOnNumericConstant(const Token &Tok) {
 
     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()) {
index 72e0dc4231de1cce448ea95a7df634b6296d88c1..104a3a2a2b27d5ebd7554112b73fc0f2895d2d5c 100644 (file)
@@ -33,3 +33,25 @@ char e = 'abcd';  // still warn: expected-warning {{multi-character character co
 #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}}
+};