[cfe-dev] [PATCH] rewrite floats more precisely

Olaf Krzikalla Olaf.Krzikalla at tu-dresden.de
Fri Jul 20 06:49:47 PDT 2012


Hi @clang,

attached there is a patch fixing some issues regarding rewriting of 
floats. First, if APFloat::toString yields a string which could be an 
integer, a trailing point is added. This fixes the rewriting of 
expressions like "1./3.".
Second the proper suffixes are appended. Maybe someone can add the 
proper suffix for half-floats too.

Best regards
Olaf Krzikalla
-------------- next part --------------
Index: lib/AST/StmtPrinter.cpp
===================================================================
--- lib/AST/StmtPrinter.cpp	(revision 160546)
+++ lib/AST/StmtPrinter.cpp	(working copy)
@@ -735,6 +740,19 @@
   SmallString<16> Str;
   Node->getValue().toString(Str);
   OS << Str;
+  if (Str.find_first_not_of("-0123456789") == std::string::npos)
+  {
+    OS << ".";  // trailing dot in order to separate from ints
+  } 
+
+  // Emit suffixes.  Float literals are always a builtin float type.
+  switch (Node->getType()->getAs<BuiltinType>()->getKind()) {
+  default: llvm_unreachable("Unexpected type for float literal!");
+  case BuiltinType::Half:       break; // FIXME: suffix?
+  case BuiltinType::Double:     break; // no suffix.
+  case BuiltinType::Float:      OS << 'F'; break;
+  case BuiltinType::LongDouble: OS << 'L'; break;
+  }
 }
 
 void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) {


More information about the cfe-dev mailing list