Skip to content

Commit 77bab2a

Browse files
committed
[llvm][unittests] Strip unneeded use of raw_string_ostream::str() (NFC)
Avoid unneeded layer of indirection.
1 parent cfd0c4f commit 77bab2a

12 files changed

+78
-90
lines changed

llvm/unittests/IR/AsmWriterTest.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ TEST(AsmWriterTest, DebugPrintDetachedInstruction) {
3333
std::string S;
3434
raw_string_ostream OS(S);
3535
Add->print(OS);
36-
EXPECT_THAT(OS.str(),
37-
HasSubstr("<badref> = add i32 poison, poison, !<empty"));
36+
EXPECT_THAT(S, HasSubstr("<badref> = add i32 poison, poison, !<empty"));
3837
}
3938

4039
TEST(AsmWriterTest, DebugPrintDetachedArgument) {
@@ -60,8 +59,7 @@ TEST(AsmWriterTest, DumpDIExpression) {
6059
std::string S;
6160
raw_string_ostream OS(S);
6261
Expr->print(OS);
63-
EXPECT_EQ("!DIExpression(DW_OP_constu, 4, DW_OP_minus, DW_OP_deref)",
64-
OS.str());
62+
EXPECT_EQ("!DIExpression(DW_OP_constu, 4, DW_OP_minus, DW_OP_deref)", S);
6563
}
6664

6765
TEST(AsmWriterTest, PrintAddrspaceWithNullOperand) {
@@ -80,7 +78,7 @@ TEST(AsmWriterTest, PrintAddrspaceWithNullOperand) {
8078
std::string S;
8179
raw_string_ostream OS(S);
8280
Call->print(OS);
83-
EXPECT_THAT(OS.str(), HasSubstr("<cannot get addrspace!>"));
81+
EXPECT_THAT(S, HasSubstr("<cannot get addrspace!>"));
8482
}
8583

8684
TEST(AsmWriterTest, PrintNullOperandBundle) {
@@ -103,6 +101,6 @@ TEST(AsmWriterTest, PrintNullOperandBundle) {
103101
std::string S;
104102
raw_string_ostream OS(S);
105103
Invoke->print(OS);
106-
EXPECT_THAT(OS.str(), HasSubstr("<null operand bundle!>"));
104+
EXPECT_THAT(S, HasSubstr("<null operand bundle!>"));
107105
}
108106
}

llvm/unittests/IR/MetadataTest.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ TEST_F(MDStringTest, PrintingSimple) {
157157
std::string Str;
158158
raw_string_ostream oss(Str);
159159
s->print(oss);
160-
EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
160+
EXPECT_STREQ("!\"testing 1 2 3\"", Str.c_str());
161161
}
162162

163163
// Test printing of MDString with non-printable characters.
@@ -167,7 +167,7 @@ TEST_F(MDStringTest, PrintingComplex) {
167167
std::string Str;
168168
raw_string_ostream oss(Str);
169169
s->print(oss);
170-
EXPECT_STREQ("!\"\\00\\0A\\22\\\\\\FF\"", oss.str().c_str());
170+
EXPECT_STREQ("!\"\\00\\0A\\22\\\\\\FF\"", Str.c_str());
171171
}
172172

173173
typedef MetadataTest MDNodeTest;
@@ -4604,8 +4604,7 @@ TEST(NamedMDNodeTest, Search) {
46044604
std::string Str;
46054605
raw_string_ostream oss(Str);
46064606
NMD->print(oss);
4607-
EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
4608-
oss.str().c_str());
4607+
EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n", Str.c_str());
46094608
}
46104609

46114610
typedef MetadataTest FunctionAttachmentTest;

llvm/unittests/IR/ValueTest.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,13 @@ TEST(ValueTest, printSlots) {
157157
std::string S; \
158158
raw_string_ostream OS(S); \
159159
INST->print(OS); \
160-
EXPECT_EQ(STR, OS.str()); \
160+
EXPECT_EQ(STR, S); \
161161
} \
162162
{ \
163163
std::string S; \
164164
raw_string_ostream OS(S); \
165165
INST->print(OS, MST); \
166-
EXPECT_EQ(STR, OS.str()); \
166+
EXPECT_EQ(STR, S); \
167167
} \
168168
} while (false)
169169
CHECK_PRINT(I0, " %0 = add i32 %y, 1");
@@ -176,13 +176,13 @@ TEST(ValueTest, printSlots) {
176176
std::string S; \
177177
raw_string_ostream OS(S); \
178178
INST->printAsOperand(OS, TYPE); \
179-
EXPECT_EQ(StringRef(STR), StringRef(OS.str())); \
179+
EXPECT_EQ(StringRef(STR), StringRef(S)); \
180180
} \
181181
{ \
182182
std::string S; \
183183
raw_string_ostream OS(S); \
184184
INST->printAsOperand(OS, TYPE, MST); \
185-
EXPECT_EQ(StringRef(STR), StringRef(OS.str())); \
185+
EXPECT_EQ(StringRef(STR), StringRef(S)); \
186186
} \
187187
} while (false)
188188
CHECK_PRINT_AS_OPERAND(I0, false, "%0");

llvm/unittests/IR/VerifierTest.cpp

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,8 @@ TEST(VerifierTest, InvalidRetAttribute) {
105105
std::string Error;
106106
raw_string_ostream ErrorOS(Error);
107107
EXPECT_TRUE(verifyModule(M, &ErrorOS));
108-
EXPECT_TRUE(
109-
StringRef(ErrorOS.str())
110-
.starts_with(
111-
"Attribute 'uwtable' does not apply to function return values"));
108+
EXPECT_TRUE(StringRef(Error).starts_with(
109+
"Attribute 'uwtable' does not apply to function return values"));
112110
}
113111

114112
/// Test the verifier rejects invalid nofpclass values that the assembler may
@@ -133,7 +131,7 @@ TEST(VerifierTest, InvalidNoFPClassAttribute) {
133131
raw_string_ostream ErrorOS(Error);
134132
EXPECT_TRUE(verifyModule(M, &ErrorOS));
135133

136-
StringRef ErrMsg(ErrorOS.str());
134+
StringRef ErrMsg(Error);
137135

138136
if (InvalidMask == 0) {
139137
EXPECT_TRUE(ErrMsg.starts_with(
@@ -173,33 +171,30 @@ TEST(VerifierTest, CrossModuleRef) {
173171
std::string Error;
174172
raw_string_ostream ErrorOS(Error);
175173
EXPECT_TRUE(verifyModule(M2, &ErrorOS));
176-
EXPECT_TRUE(StringRef(ErrorOS.str()) ==
177-
"Global is referenced in a different module!\n"
178-
"ptr @foo2\n"
179-
"; ModuleID = 'M2'\n"
180-
" %call = call i32 @foo2()\n"
181-
"ptr @foo1\n"
182-
"; ModuleID = 'M1'\n"
183-
"Global is used by function in a different module\n"
184-
"ptr @foo2\n"
185-
"; ModuleID = 'M2'\n"
186-
"ptr @foo3\n"
187-
"; ModuleID = 'M3'\n");
174+
EXPECT_TRUE(Error == "Global is referenced in a different module!\n"
175+
"ptr @foo2\n"
176+
"; ModuleID = 'M2'\n"
177+
" %call = call i32 @foo2()\n"
178+
"ptr @foo1\n"
179+
"; ModuleID = 'M1'\n"
180+
"Global is used by function in a different module\n"
181+
"ptr @foo2\n"
182+
"; ModuleID = 'M2'\n"
183+
"ptr @foo3\n"
184+
"; ModuleID = 'M3'\n");
188185

189186
Error.clear();
190187
EXPECT_TRUE(verifyModule(M1, &ErrorOS));
191-
EXPECT_TRUE(StringRef(ErrorOS.str()) ==
192-
"Referencing function in another module!\n"
193-
" %call = call i32 @foo2()\n"
194-
"; ModuleID = 'M1'\n"
195-
"ptr @foo2\n"
196-
"; ModuleID = 'M2'\n");
188+
EXPECT_TRUE(StringRef(Error) == "Referencing function in another module!\n"
189+
" %call = call i32 @foo2()\n"
190+
"; ModuleID = 'M1'\n"
191+
"ptr @foo2\n"
192+
"; ModuleID = 'M2'\n");
197193

198194
Error.clear();
199195
EXPECT_TRUE(verifyModule(M3, &ErrorOS));
200-
EXPECT_TRUE(
201-
StringRef(ErrorOS.str())
202-
.starts_with("Referencing personality function in another module!"));
196+
EXPECT_TRUE(StringRef(Error).starts_with(
197+
"Referencing personality function in another module!"));
203198

204199
// Erase bad methods to avoid triggering an assertion failure on destruction
205200
F1->eraseFromParent();
@@ -214,9 +209,8 @@ TEST(VerifierTest, InvalidVariableLinkage) {
214209
std::string Error;
215210
raw_string_ostream ErrorOS(Error);
216211
EXPECT_TRUE(verifyModule(M, &ErrorOS));
217-
EXPECT_TRUE(StringRef(ErrorOS.str())
218-
.starts_with("Global is external, but doesn't "
219-
"have external or weak linkage!"));
212+
EXPECT_TRUE(StringRef(Error).starts_with("Global is external, but doesn't "
213+
"have external or weak linkage!"));
220214
}
221215

222216
TEST(VerifierTest, InvalidFunctionLinkage) {
@@ -228,9 +222,8 @@ TEST(VerifierTest, InvalidFunctionLinkage) {
228222
std::string Error;
229223
raw_string_ostream ErrorOS(Error);
230224
EXPECT_TRUE(verifyModule(M, &ErrorOS));
231-
EXPECT_TRUE(StringRef(ErrorOS.str())
232-
.starts_with("Global is external, but doesn't "
233-
"have external or weak linkage!"));
225+
EXPECT_TRUE(StringRef(Error).starts_with("Global is external, but doesn't "
226+
"have external or weak linkage!"));
234227
}
235228

236229
TEST(VerifierTest, DetectInvalidDebugInfo) {
@@ -287,9 +280,8 @@ TEST(VerifierTest, MDNodeWrongContext) {
287280
std::string Error;
288281
raw_string_ostream ErrorOS(Error);
289282
EXPECT_TRUE(verifyModule(M, &ErrorOS));
290-
EXPECT_TRUE(
291-
StringRef(ErrorOS.str())
292-
.starts_with("MDNode context does not match Module context!"));
283+
EXPECT_TRUE(StringRef(Error).starts_with(
284+
"MDNode context does not match Module context!"));
293285
}
294286

295287
TEST(VerifierTest, AttributesWrongContext) {
@@ -358,9 +350,8 @@ TEST(VerifierTest, CrossFunctionRef) {
358350
std::string Error;
359351
raw_string_ostream ErrorOS(Error);
360352
EXPECT_TRUE(verifyModule(M, &ErrorOS));
361-
EXPECT_TRUE(
362-
StringRef(ErrorOS.str())
363-
.starts_with("Referring to an instruction in another function!"));
353+
EXPECT_TRUE(StringRef(Error).starts_with(
354+
"Referring to an instruction in another function!"));
364355

365356
// Explicitly erase the store to avoid a use-after-free when the module is
366357
// destroyed.
@@ -388,11 +379,10 @@ TEST(VerifierTest, AtomicRMW) {
388379
std::string Error;
389380
raw_string_ostream ErrorOS(Error);
390381
EXPECT_TRUE(verifyFunction(*F, &ErrorOS));
391-
EXPECT_TRUE(
392-
StringRef(ErrorOS.str())
393-
.starts_with("atomicrmw fadd operand must have floating-point or "
394-
"fixed vector of floating-point type!"))
395-
<< ErrorOS.str();
382+
EXPECT_TRUE(StringRef(Error).starts_with(
383+
"atomicrmw fadd operand must have floating-point or "
384+
"fixed vector of floating-point type!"))
385+
<< Error;
396386
}
397387

398388
} // end anonymous namespace

llvm/unittests/Support/ARMAttributeParser.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ bool testBuildAttr(unsigned Tag, unsigned Value,
3131
raw_string_ostream OS(buffer);
3232
AttributeSection Section(Tag, Value);
3333
Section.write(OS);
34-
ArrayRef<uint8_t> Bytes(
35-
reinterpret_cast<const uint8_t*>(OS.str().c_str()), OS.str().size());
34+
ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(buffer.c_str()),
35+
buffer.size());
3636

3737
ARMAttributeParser Parser;
3838
cantFail(Parser.parse(Bytes, llvm::endianness::little));

llvm/unittests/Support/CSKYAttributeParserTest.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ static bool testAttributeInt(unsigned Tag, unsigned Value, unsigned ExpectedTag,
7777
raw_string_ostream OS(buffer);
7878
CSKYAttributeSection Section(Tag, Value);
7979
Section.writeInt(OS);
80-
ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(OS.str().c_str()),
81-
OS.str().size());
80+
ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(buffer.c_str()),
81+
buffer.size());
8282

8383
CSKYAttributeParser Parser;
8484
cantFail(Parser.parse(Bytes, llvm::endianness::little));
@@ -94,8 +94,8 @@ static bool testAttributeString(unsigned Tag, const char *Value,
9494
raw_string_ostream OS(buffer);
9595
CSKYAttributeSection Section(Tag, Value);
9696
Section.writeString(OS);
97-
ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(OS.str().c_str()),
98-
OS.str().size());
97+
ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(buffer.c_str()),
98+
buffer.size());
9999

100100
CSKYAttributeParser Parser;
101101
cantFail(Parser.parse(Bytes, llvm::endianness::little));
@@ -109,8 +109,8 @@ static void testParseError(unsigned Tag, unsigned Value, const char *msg) {
109109
raw_string_ostream OS(buffer);
110110
CSKYAttributeSection Section(Tag, Value);
111111
Section.writeInt(OS);
112-
ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(OS.str().c_str()),
113-
OS.str().size());
112+
ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(buffer.c_str()),
113+
buffer.size());
114114

115115
CSKYAttributeParser Parser;
116116
Error e = Parser.parse(Bytes, llvm::endianness::little);

llvm/unittests/Support/Chrono.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ TEST(Chrono, TimePointFormat) {
4848
std::string S;
4949
raw_string_ostream OS(S);
5050
OS << T;
51-
EXPECT_EQ("2006-01-02 15:04:05.123456789", OS.str());
51+
EXPECT_EQ("2006-01-02 15:04:05.123456789", S);
5252
S.clear();
5353
OS << T2;
54-
EXPECT_EQ("2006-01-02 15:04:05.023456789", OS.str());
54+
EXPECT_EQ("2006-01-02 15:04:05.023456789", S);
5555

5656
// formatv default style matches operator<<.
5757
EXPECT_EQ("2006-01-02 15:04:05.123456789", formatv("{0}", T).str());

llvm/unittests/Support/ErrorTest.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ TEST(Error, StringError) {
442442
raw_string_ostream S(Msg);
443443
logAllUnhandledErrors(
444444
make_error<StringError>("foo" + Twine(42), inconvertibleErrorCode()), S);
445-
EXPECT_EQ(S.str(), "foo42\n") << "Unexpected StringError log result";
445+
EXPECT_EQ(Msg, "foo42\n") << "Unexpected StringError log result";
446446

447447
auto EC =
448448
errorToErrorCode(make_error<StringError>("", errc::invalid_argument));
@@ -457,14 +457,14 @@ TEST(Error, createStringError) {
457457
raw_string_ostream S(Msg);
458458
logAllUnhandledErrors(createStringError(EC, "foo%s%d0x%" PRIx8, Bar, 1, 0xff),
459459
S);
460-
EXPECT_EQ(S.str(), "foobar10xff\n")
461-
<< "Unexpected createStringError() log result";
460+
EXPECT_EQ(Msg, "foobar10xff\n")
461+
<< "Unexpected createStringError() log result";
462462

463463
S.flush();
464464
Msg.clear();
465465
logAllUnhandledErrors(createStringError(EC, Bar), S);
466-
EXPECT_EQ(S.str(), "bar\n")
467-
<< "Unexpected createStringError() (overloaded) log result";
466+
EXPECT_EQ(Msg, "bar\n")
467+
<< "Unexpected createStringError() (overloaded) log result";
468468

469469
S.flush();
470470
Msg.clear();
@@ -769,15 +769,15 @@ TEST(Error, Stream) {
769769
std::string Buf;
770770
llvm::raw_string_ostream S(Buf);
771771
S << OK;
772-
EXPECT_EQ("success", S.str());
772+
EXPECT_EQ("success", Buf);
773773
consumeError(std::move(OK));
774774
}
775775
{
776776
Error E1 = make_error<CustomError>(0);
777777
std::string Buf;
778778
llvm::raw_string_ostream S(Buf);
779779
S << E1;
780-
EXPECT_EQ("CustomError {0}", S.str());
780+
EXPECT_EQ("CustomError {0}", Buf);
781781
consumeError(std::move(E1));
782782
}
783783
}

llvm/unittests/Support/JSONTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ static std::string errorContext(const Value &V, const Path::Root &R) {
502502
std::string Context;
503503
llvm::raw_string_ostream OS(Context);
504504
R.printErrorContext(V, OS);
505-
return OS.str();
505+
return Context;
506506
}
507507

508508
TEST(JSONTest, Deserialize) {
@@ -603,7 +603,7 @@ TEST(JSONTest, Stream) {
603603
J.attributeEnd();
604604
J.attribute("baz", "xyz");
605605
});
606-
return OS.str();
606+
return S;
607607
};
608608

609609
const char *Plain =

0 commit comments

Comments
 (0)