[cfe-commits] r44339 - in /cfe/trunk: Lex/LiteralSupport.cpp include/clang/Lex/LiteralSupport.h

Ted Kremenek kremenek at apple.com
Mon Nov 26 15:12:30 PST 2007


Author: kremenek
Date: Mon Nov 26 17:12:30 2007
New Revision: 44339

URL: http://llvm.org/viewvc/llvm-project?rev=44339&view=rev
Log:
Added optional pass-by-reference argument "isExact" to
NumericLiteralParser::GetFloatValue(). Upon method return, this flag has the value
true if the returned APFloat can exactly represent the number in the parsed text,
and false otherwise.

Modified the implementation of GetFloatValue() to parse literals using APFloat's
convertFromString method (which allows us to set the value of isExact).

Modified:
    cfe/trunk/Lex/LiteralSupport.cpp
    cfe/trunk/include/clang/Lex/LiteralSupport.h

Modified: cfe/trunk/Lex/LiteralSupport.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Lex/LiteralSupport.cpp?rev=44339&r1=44338&r2=44339&view=diff

==============================================================================
--- cfe/trunk/Lex/LiteralSupport.cpp (original)
+++ cfe/trunk/Lex/LiteralSupport.cpp Mon Nov 26 17:12:30 2007
@@ -410,17 +410,27 @@
 }
 
 llvm::APFloat NumericLiteralParser::
-GetFloatValue(const llvm::fltSemantics &Format) {
+GetFloatValue(const llvm::fltSemantics &Format, bool* isExact) {
+  using llvm::APFloat;
+  
   char floatChars[256];
   strncpy(floatChars, ThisTokBegin, ThisTokEnd-ThisTokBegin);
   floatChars[ThisTokEnd-ThisTokBegin] = '\0';
-#if 0
-  // This doesn't work yet.
-  return llvm::APFloat(Format, floatChars);
+
+#if 1
+  APFloat V (Format, APFloat::fcZero, false);
+
+  APFloat::opStatus status;
+  status = V.convertFromString(floatChars,APFloat::rmTowardZero);
+  
+  if (isExact)
+    *isExact = status == APFloat::opOK;
+  
+  return V;
 #else
   // FIXME: this is horrible!
-  llvm::APFloat V(strtod(floatChars, 0));
-  V.convert(Format, llvm::APFloat::rmTowardZero);
+  APFloat V(strtod(floatChars, 0));
+  V.convert(Format, APFloat::rmTowardZero);
   return V;
 #endif
 }

Modified: cfe/trunk/include/clang/Lex/LiteralSupport.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/LiteralSupport.h?rev=44339&r1=44338&r2=44339&view=diff

==============================================================================
--- cfe/trunk/include/clang/Lex/LiteralSupport.h (original)
+++ cfe/trunk/include/clang/Lex/LiteralSupport.h Mon Nov 26 17:12:30 2007
@@ -77,8 +77,11 @@
   
   /// GetFloatValue - Convert this numeric literal to a floating value, using
   /// the specified APFloat fltSemantics (specifying float, double, etc).
-  ///
-  llvm::APFloat GetFloatValue(const llvm::fltSemantics &Format);
+  /// 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);
 
 private:  
   void Diag(SourceLocation Loc, unsigned DiagID, 





More information about the cfe-commits mailing list