mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
This adds a RawStringOstream. Versus TestRawOstream, which is
consolidated over to RawStringOstream, it uses a string for storage
instead of a vector, mainly to support move-to-string semantics. Versus
llvm::raw_string_ostream, it owns the string and supports pwrite (which
is needed for driver and its fd_ostream compatibility requirement).
This converts most uses of llvm::raw_string_ostream, leaving behind a
few in InstNamer that explicitly cannot own the string, such as:
```
llvm::raw_string_ostream(name)
<< "_" << tree.tokens().GetColumnNumber(token);
```
I have this as its own library so that it can use CHECK.
Yes this doesn't save much code, but it's code we repeatedly write.
---------
Co-authored-by: Geoff Romer <gromer@google.com>
62 lines
1.4 KiB
C++
62 lines
1.4 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#include "common/vlog.h"
|
|
|
|
#include <gmock/gmock.h>
|
|
#include <gtest/gtest.h>
|
|
|
|
#include "common/raw_string_ostream.h"
|
|
|
|
namespace Carbon::Testing {
|
|
namespace {
|
|
|
|
using ::testing::IsEmpty;
|
|
using ::testing::StrEq;
|
|
|
|
// Helper class with a vlog_stream_ member for CARBON_VLOG.
|
|
class VLogger {
|
|
public:
|
|
explicit VLogger(bool enable) {
|
|
if (enable) {
|
|
vlog_stream_ = &buffer_;
|
|
}
|
|
}
|
|
|
|
void VLog() { CARBON_VLOG("Test\n"); }
|
|
void VLogFormatArgs() { CARBON_VLOG("Test {0} {1} {2}\n", 1, 2, 3); }
|
|
|
|
auto TakeStr() -> std::string { return buffer_.TakeStr(); }
|
|
|
|
private:
|
|
RawStringOstream buffer_;
|
|
|
|
llvm::raw_ostream* vlog_stream_ = nullptr;
|
|
};
|
|
|
|
TEST(VLogTest, Enabled) {
|
|
VLogger vlog(/*enable=*/true);
|
|
vlog.VLog();
|
|
EXPECT_THAT(vlog.TakeStr(), StrEq("Test\n"));
|
|
vlog.VLogFormatArgs();
|
|
EXPECT_THAT(vlog.TakeStr(), StrEq("Test 1 2 3\n"));
|
|
}
|
|
|
|
TEST(VLogTest, Disabled) {
|
|
VLogger vlog(/*enable=*/false);
|
|
vlog.VLog();
|
|
EXPECT_THAT(vlog.TakeStr(), IsEmpty());
|
|
}
|
|
|
|
TEST(VLogTest, To) {
|
|
RawStringOstream buffer;
|
|
CARBON_VLOG_TO(&buffer, "Test");
|
|
EXPECT_THAT(buffer.TakeStr(), "Test");
|
|
}
|
|
|
|
TEST(VLogTest, ToNull) { CARBON_VLOG_TO(nullptr, "Unused"); }
|
|
|
|
} // namespace
|
|
} // namespace Carbon::Testing
|