Skip to content

Commit

Permalink
Merge pull request #698 from andreasfertig/harmonize
Browse files Browse the repository at this point in the history
Harmonize floating point formatting.
  • Loading branch information
andreasfertig authored Jan 28, 2025
2 parents 5b73fd8 + 4df4724 commit be2c95f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 35 deletions.
3 changes: 1 addition & 2 deletions CodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -747,8 +747,7 @@ void CodeGenerator::InsertArg(const IntegerLiteral* stmt)

void CodeGenerator::InsertArg(const FloatingLiteral* stmt)
{
// FIXME: not working correctly
mOutputFormatHelper.Append(EvaluateAsFloat(*stmt));
mOutputFormatHelper.Append(stmt->getValue());
InsertSuffix(stmt->getType());
}
//-----------------------------------------------------------------------------
Expand Down
17 changes: 0 additions & 17 deletions InsightsHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,23 +309,6 @@ const QualType GetDesugarType(const QualType& QT)
}
//-----------------------------------------------------------------------------

const std::string EvaluateAsFloat(const FloatingLiteral& expr)
{
// return std::to_string(expr.getValueAsApproximateDouble());

SmallString<16> str{};
expr.getValue().toString(str);

if(std::string::npos == str.find('.')) {
/* in case it is a number like 10.0 toString() seems to leave out the .0. However, as this distinguished
* between an integer and a floating point literal we need that dot. */
str.append(".0"sv);
}

return std::string{str.str()};
}
//-----------------------------------------------------------------------------

static const VarDecl* GetVarDeclFromDeclRefExpr(const DeclRefExpr& declRefExpr)
{
const auto* valueDecl = declRefExpr.getDecl();
Expand Down
1 change: 0 additions & 1 deletion InsightsHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ std::string GetDeclContext(const DeclContext* ctx,
WithTemplateParameters withTemplateParameters = WithTemplateParameters::No);
//-----------------------------------------------------------------------------

const std::string EvaluateAsFloat(const FloatingLiteral& expr);
const std::string GetNoExcept(const FunctionDecl& decl);
const std::string_view GetConst(const FunctionDecl& decl);
//-----------------------------------------------------------------------------
Expand Down
33 changes: 18 additions & 15 deletions InsightsStrCat.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,29 @@ inline std::string Normalize(const llvm::APSInt& arg)
}
//-----------------------------------------------------------------------------

inline std::string Normalize(const APValue& arg)
inline std::string Normalize(const llvm::APFloat& arg)
{
switch(arg.getKind()) {
case APValue::Int: return Normalize(arg.getInt());
case APValue::Float: {
std::string str{};
::llvm::raw_string_ostream stream{str};
std::string str{};
::llvm::raw_string_ostream stream{str};

arg.getFloat().print(stream);
str.pop_back();
arg.print(stream);
str.pop_back();

if(std::string::npos == str.find('.')) {
/* in case it is a number like 10.0 toString() seems to leave out the .0. However, as this distinguished
* between an integer and a floating point literal we need that dot. */
str.append(".0");
}
if(std::string::npos == str.find('.')) {
/* in case it is a number like 10.0 toString() seems to leave out the .0. However, as this distinguished
* between an integer and a floating point literal we need that dot. */
str.append(".0");
}

return str;
}
return str;
}
//-----------------------------------------------------------------------------

inline std::string Normalize(const APValue& arg)
{
switch(arg.getKind()) {
case APValue::Int: return Normalize(arg.getInt());
case APValue::Float: return Normalize(arg.getFloat());
default: break;
}

Expand Down

0 comments on commit be2c95f

Please sign in to comment.