From da8249e57f3badecf925571881fe57243935c6c1 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sat, 7 Jun 2008 22:13:43 +0000 Subject: [PATCH] Fix ast dumping to work with long double literals, e.g. we dump: long double X() { return 1.0L; } as: long double X() (CompoundStmt 0xb06a00 (ReturnStmt 0xb068d0 (FloatingLiteral 0xb02cf0 'long double' 1.000000))) git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@52080 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/Expr.h | 16 +++++----------- lib/AST/Expr.cpp | 10 ++++++++++ lib/AST/StmtDumper.cpp | 2 +- lib/AST/StmtPrinter.cpp | 2 +- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/include/clang/AST/Expr.h b/include/clang/AST/Expr.h index 3813455864..9a76823ff2 100644 --- a/include/clang/AST/Expr.h +++ b/include/clang/AST/Expr.h @@ -271,17 +271,11 @@ public: bool isExact() const { return IsExact; } - /// getValueAsDouble - This returns the value as an inaccurate double. Note - /// that this may cause loss of precision, but is useful for debugging dumps - /// etc. - double getValueAsDouble() const { - // FIXME: We need something for long double here. - if (cast(getType())->getKind() == BuiltinType::Float) - return Value.convertToFloat(); - else - return Value.convertToDouble(); - } - + /// getValueAsApproximateDouble - This returns the value as an inaccurate + /// double. Note that this may cause loss of precision, but is useful for + /// debugging dumps, etc. + double getValueAsApproximateDouble() const; + virtual SourceRange getSourceRange() const { return SourceRange(Loc); } static bool classof(const Stmt *T) { diff --git a/lib/AST/Expr.cpp b/lib/AST/Expr.cpp index a89fbd621a..78c305051e 100644 --- a/lib/AST/Expr.cpp +++ b/lib/AST/Expr.cpp @@ -22,6 +22,16 @@ using namespace clang; // Primary Expressions. //===----------------------------------------------------------------------===// +/// getValueAsApproximateDouble - This returns the value as an inaccurate +/// double. Note that this may cause loss of precision, but is useful for +/// debugging dumps, etc. +double FloatingLiteral::getValueAsApproximateDouble() const { + llvm::APFloat V = getValue(); + V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven); + return V.convertToDouble(); +} + + StringLiteral::StringLiteral(const char *strData, unsigned byteLength, bool Wide, QualType t, SourceLocation firstLoc, SourceLocation lastLoc) : diff --git a/lib/AST/StmtDumper.cpp b/lib/AST/StmtDumper.cpp index 082d2a2259..ae6ca40460 100644 --- a/lib/AST/StmtDumper.cpp +++ b/lib/AST/StmtDumper.cpp @@ -335,7 +335,7 @@ void StmtDumper::VisitIntegerLiteral(IntegerLiteral *Node) { } void StmtDumper::VisitFloatingLiteral(FloatingLiteral *Node) { DumpExpr(Node); - fprintf(F, " %f", Node->getValueAsDouble()); + fprintf(F, " %f", Node->getValueAsApproximateDouble()); } void StmtDumper::VisitStringLiteral(StringLiteral *Str) { diff --git a/lib/AST/StmtPrinter.cpp b/lib/AST/StmtPrinter.cpp index a740facf76..5e347435b6 100644 --- a/lib/AST/StmtPrinter.cpp +++ b/lib/AST/StmtPrinter.cpp @@ -577,7 +577,7 @@ void StmtPrinter::VisitIntegerLiteral(IntegerLiteral *Node) { } void StmtPrinter::VisitFloatingLiteral(FloatingLiteral *Node) { // FIXME: print value more precisely. - OS << Node->getValueAsDouble(); + OS << Node->getValueAsApproximateDouble(); } void StmtPrinter::VisitImaginaryLiteral(ImaginaryLiteral *Node) { -- 2.40.0