Replace std::to_string with operator<<(llvm::raw_ostream&) #208
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes #180 by replacing our overloads of
std::to_string
, which is UB, with correspondingllvm::raw_ostream& operator<<(llvm::raw_ostream&, <thing>)
overloads.This was less trivial than thought, because we were using a mix of
std::ostream
,std::stringstream
andllvm::raw_ostream
in the codebase. Furthermore,raw_ostream
cannot output to files, whilestd::ostream
can, so the replacement wasn't a simple find+replace. I think the best way forward is:operator<<(raw_ostream&)
,llvm::outs()
andllvm::errs()
as the stream source,llvm::raw_string_ostream
orllvm::raw_svector_stream
and then call its.str()
method,raw_ostream
can't do), do (3) and to output it to astd::ofstream
.I think this PR also fixes a bug where we were calling
std::to_string
on a string literal (inexamples/ptb.cpp
), which was actually creating a string with the textual representation of the literal's memory address (because of thevoid*
overload we had forstd::to_string
), rather than constructing a string.I also recommend using one of my favorite LLVM data structures ever, llvm::Twine, for simple string concatenation instead of
std::string::operator+
.It's a lot of code, so please do look at it carefully. Also, please tell me what would be a good test to make sure the dotty output for the IR looks good (@artemrakhov)?