Replace existing uses of `stringstream` (#258)
Inspired by https://github.com/apple/swift-llbuild/pull/255#discussion_r177496068
I went ahead and replaced existing uses, some of which I previously
introduced unnoticed.
diff --git a/lib/Commands/CommandUtil.cpp b/lib/Commands/CommandUtil.cpp
index ff61d2a..f46869f 100644
--- a/lib/Commands/CommandUtil.cpp
+++ b/lib/Commands/CommandUtil.cpp
@@ -18,9 +18,9 @@
#include "llbuild/Ninja/Parser.h"
#include "llvm/ADT/STLExtras.h"
+#include "llvm/Support/raw_ostream.h"
#include <iostream>
-#include <sstream>
using namespace llbuild;
using namespace llbuild::commands;
@@ -44,22 +44,24 @@
}
std::string util::escapedString(const char* start, unsigned length) {
- std::stringstream result;
+ std::string result;
+ llvm::raw_string_ostream resultStream(result);
for (unsigned i = 0; i != length; ++i) {
char c = start[i];
if (c == '"') {
- result << "\\\"";
+ resultStream << "\\\"";
} else if (isprint(c)) {
- result << c;
+ resultStream << c;
} else if (c == '\n') {
- result << "\\n";
+ resultStream << "\\n";
} else {
- result << "\\x"
+ resultStream << "\\x"
<< hexdigit(((unsigned char) c >> 4) & 0xF)
<< hexdigit((unsigned char) c & 0xF);
}
}
- return result.str();
+ resultStream.flush();
+ return result;
}
std::string util::escapedString(const std::string& string) {
return escapedString(string.data(), string.size());
diff --git a/lib/Commands/NinjaBuildCommand.cpp b/lib/Commands/NinjaBuildCommand.cpp
index 2cd1b71..b9a4925 100644
--- a/lib/Commands/NinjaBuildCommand.cpp
+++ b/lib/Commands/NinjaBuildCommand.cpp
@@ -25,6 +25,7 @@
#include "llbuild/Ninja/ManifestLoader.h"
#include "llvm/ADT/SmallString.h"
+#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/TimeValue.h"
#include "CommandLineStatusOutput.h"
@@ -38,7 +39,6 @@
#include <cstdlib>
#include <deque>
#include <mutex>
-#include <sstream>
#include <thread>
#include <unordered_set>
@@ -1686,17 +1686,19 @@
void NinjaBuildEngineDelegate::cycleDetected(
const std::vector<core::Rule*>& cycle) {
// Report the cycle.
- std::stringstream message;
- message << "cycle detected among targets:";
+ std::string message;
+ llvm::raw_string_ostream messageStream(message);
+ messageStream << "cycle detected among targets:";
bool first = true;
for (const auto* rule: cycle) {
if (!first)
- message << " ->";
- message << " \"" << rule->key << '"';
+ messageStream << " ->";
+ messageStream << " \"" << rule->key << '"';
first = false;
}
- context->emitError(message.str());
+ messageStream.flush();
+ context->emitError(message.c_str());
// Cancel the build.
context->isCancelled = true;
diff --git a/lib/Core/SQLiteBuildDB.cpp b/lib/Core/SQLiteBuildDB.cpp
index 51b4d97..ea6bddf 100644
--- a/lib/Core/SQLiteBuildDB.cpp
+++ b/lib/Core/SQLiteBuildDB.cpp
@@ -17,12 +17,12 @@
#include "llbuild/Core/BuildEngine.h"
#include "llvm/ADT/STLExtras.h"
+#include "llvm/Support/raw_ostream.h"
#include <cassert>
#include <cerrno>
#include <cstring>
#include <mutex>
-#include <sstream>
#include <sqlite3.h>
@@ -51,14 +51,16 @@
const char* err_message = sqlite3_errmsg(db);
const char* filename = sqlite3_db_filename(db, "main");
- std::stringstream out;
- out << "error: accessing build database \"" << filename << "\": " << err_message;
+ std::string out;
+ llvm::raw_string_ostream outStream(out);
+ outStream << "error: accessing build database \"" << filename << "\": " << err_message;
if (err_code == SQLITE_BUSY || err_code == SQLITE_LOCKED) {
- out << " Possibly there are two concurrent builds running in the same filesystem location.";
+ outStream << " Possibly there are two concurrent builds running in the same filesystem location.";
}
- return out.str();
+ outStream.flush();
+ return out;
}
public:
diff --git a/unittests/Core/SQLiteBuildDBTest.cpp b/unittests/Core/SQLiteBuildDBTest.cpp
index 6010543..310da90 100644
--- a/unittests/Core/SQLiteBuildDBTest.cpp
+++ b/unittests/Core/SQLiteBuildDBTest.cpp
@@ -17,8 +17,6 @@
#include "gtest/gtest.h"
-#include <sstream>
-
#include <sqlite3.h>
using namespace llbuild;