Skip to content

Commit 4b27b58

Browse files
committed
[lld] Nits on uses of raw_string_ostream (NFC)
* Don't call raw_string_ostream::flush(), which is essentially a no-op. * Strip calls to raw_string_ostream::str(), to avoid excess layer of indirection.
1 parent ba7a0b7 commit 4b27b58

11 files changed

+8
-18
lines changed

lld/COFF/Chunks.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ static void maybeReportRelocationToDiscarded(const SectionChunk *fromChunk,
385385
os << "relocation against symbol in discarded section: " + name;
386386
for (const std::string &s : symbolLocations)
387387
os << s;
388-
error(os.str());
388+
error(out);
389389
}
390390

391391
void SectionChunk::writeTo(uint8_t *buf) const {

lld/COFF/DriverUtils.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ std::string LinkerDriver::createDefaultXml() {
415415
<< " </dependency>\n";
416416
}
417417
os << "</assembly>\n";
418-
return os.str();
418+
return ret;
419419
}
420420

421421
std::string

lld/COFF/SymbolTable.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ static void reportUndefinedSymbol(const COFFLinkerContext &ctx,
275275
}
276276
if (numDisplayedRefs < numRefs)
277277
os << "\n>>> referenced " << numRefs - numDisplayedRefs << " more times";
278-
errorOrWarn(os.str(), ctx.config.forceUnresolved);
278+
errorOrWarn(out, ctx.config.forceUnresolved);
279279
}
280280

281281
void SymbolTable::loadMinGWSymbols() {
@@ -667,7 +667,7 @@ static std::string getSourceLocationObj(ObjFile *file, SectionChunk *sc,
667667
if (fileLine)
668668
os << fileLine->first << ":" << fileLine->second << "\n>>> ";
669669
os << toString(file);
670-
return os.str();
670+
return res;
671671
}
672672

673673
static std::string getSourceLocation(InputFile *file, SectionChunk *sc,
@@ -706,9 +706,9 @@ void SymbolTable::reportDuplicate(Symbol *existing, InputFile *newFile,
706706
existing->getName());
707707

708708
if (ctx.config.forceMultiple)
709-
warn(os.str());
709+
warn(msg);
710710
else
711-
error(os.str());
711+
error(msg);
712712
}
713713

714714
Symbol *SymbolTable::addAbsolute(StringRef n, COFFSymbolRef sym) {

lld/ELF/Driver.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -1216,7 +1216,6 @@ static void parseClangOption(StringRef opt, const Twine &msg) {
12161216
const char *argv[] = {config->progName.data(), opt.data()};
12171217
if (cl::ParseCommandLineOptions(2, argv, "", &os))
12181218
return;
1219-
os.flush();
12201219
error(msg + ": " + StringRef(err).trim());
12211220
}
12221221

lld/ELF/Thunks.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1088,7 +1088,7 @@ void PPC32PltCallStub::addSymbols(ThunkSection &isec) {
10881088
else
10891089
os << ".plt_pic32.";
10901090
os << destination.getName();
1091-
addSymbol(saver().save(os.str()), STT_FUNC, 0, isec);
1091+
addSymbol(saver().save(buf), STT_FUNC, 0, isec);
10921092
}
10931093

10941094
bool PPC32PltCallStub::isCompatibleWith(const InputSection &isec,

lld/ELF/Writer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2772,7 +2772,7 @@ template <class ELFT> void Writer<ELFT>::openFile() {
27722772
<< "section sizes:\n";
27732773
for (OutputSection *os : ctx.outputSections)
27742774
s << os->name << ' ' << os->size << "\n";
2775-
error(s.str());
2775+
error(msg);
27762776
return;
27772777
}
27782778

lld/MachO/Driver.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,6 @@ static void parseClangOption(StringRef opt, const Twine &msg) {
949949
const char *argv[] = {"lld", opt.data()};
950950
if (cl::ParseCommandLineOptions(2, argv, "", &os))
951951
return;
952-
os.flush();
953952
error(msg + ": " + StringRef(err).trim());
954953
}
955954

lld/wasm/OutputSections.cpp

-5
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,13 @@ void OutputSection::createHeader(size_t bodySize) {
4242
debugWrite(os.tell(), "section type [" + getSectionName() + "]");
4343
encodeULEB128(type, os);
4444
writeUleb128(os, bodySize, "section size");
45-
os.flush();
4645
log("createHeader: " + toString(*this) + " body=" + Twine(bodySize) +
4746
" total=" + Twine(getSize()));
4847
}
4948

5049
void CodeSection::finalizeContents() {
5150
raw_string_ostream os(codeSectionHeader);
5251
writeUleb128(os, functions.size(), "function count");
53-
os.flush();
5452
bodySize = codeSectionHeader.size();
5553

5654
for (InputFunction *func : functions) {
@@ -112,7 +110,6 @@ void DataSection::finalizeContents() {
112110
"output segments should have been combined by now");
113111

114112
writeUleb128(os, segmentCount, "data segment count");
115-
os.flush();
116113
bodySize = dataSectionHeader.size();
117114
bool is64 = config->is64.value_or(false);
118115

@@ -147,7 +144,6 @@ void DataSection::finalizeContents() {
147144
}
148145
}
149146
writeUleb128(os, segment->size, "segment size");
150-
os.flush();
151147

152148
segment->sectionOffset = bodySize;
153149
bodySize += segment->header.size() + segment->size;
@@ -245,7 +241,6 @@ void CustomSection::finalizeContents() {
245241
raw_string_ostream os(nameData);
246242
encodeULEB128(name.size(), os);
247243
os << name;
248-
os.flush();
249244

250245
for (InputChunk *section : inputSections) {
251246
assert(!section->discarded);

lld/wasm/SyntheticSections.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ class SubSection {
3838
explicit SubSection(uint32_t type) : type(type) {}
3939

4040
void writeTo(raw_ostream &to) {
41-
os.flush();
4241
writeUleb128(to, type, "subsection type");
4342
writeUleb128(to, body.size(), "subsection size");
4443
to.write(body.data(), body.size());

lld/wasm/SyntheticSections.h

-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ class SyntheticSection : public OutputSection {
5656

5757
void finalizeContents() override {
5858
writeBody();
59-
bodyOutputStream.flush();
6059
createHeader(body.size());
6160
}
6261

lld/wasm/Writer.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -1875,7 +1875,6 @@ void Writer::createHeader() {
18751875
raw_string_ostream os(header);
18761876
writeBytes(os, WasmMagic, sizeof(WasmMagic), "wasm magic");
18771877
writeU32(os, WasmVersion, "wasm version");
1878-
os.flush();
18791878
fileSize += header.size();
18801879
}
18811880

0 commit comments

Comments
 (0)