[lld] 4b27b58 - [lld] Nits on uses of raw_string_ostream (NFC)

via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 15 01:26:28 PDT 2024


Author: JOE1994
Date: 2024-09-15T04:23:11-04:00
New Revision: 4b27b5800f8c94ec010213e2a4ddf552cc282bce

URL: https://github.com/llvm/llvm-project/commit/4b27b5800f8c94ec010213e2a4ddf552cc282bce
DIFF: https://github.com/llvm/llvm-project/commit/4b27b5800f8c94ec010213e2a4ddf552cc282bce.diff

LOG: [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.

Added: 
    

Modified: 
    lld/COFF/Chunks.cpp
    lld/COFF/DriverUtils.cpp
    lld/COFF/SymbolTable.cpp
    lld/ELF/Driver.cpp
    lld/ELF/Thunks.cpp
    lld/ELF/Writer.cpp
    lld/MachO/Driver.cpp
    lld/wasm/OutputSections.cpp
    lld/wasm/SyntheticSections.cpp
    lld/wasm/SyntheticSections.h
    lld/wasm/Writer.cpp

Removed: 
    


################################################################################
diff  --git a/lld/COFF/Chunks.cpp b/lld/COFF/Chunks.cpp
index ee54fa39fc3d6a..6510c637ae8fe6 100644
--- a/lld/COFF/Chunks.cpp
+++ b/lld/COFF/Chunks.cpp
@@ -385,7 +385,7 @@ static void maybeReportRelocationToDiscarded(const SectionChunk *fromChunk,
   os << "relocation against symbol in discarded section: " + name;
   for (const std::string &s : symbolLocations)
     os << s;
-  error(os.str());
+  error(out);
 }
 
 void SectionChunk::writeTo(uint8_t *buf) const {

diff  --git a/lld/COFF/DriverUtils.cpp b/lld/COFF/DriverUtils.cpp
index 6e8f74c83be4a4..39af621cec85ca 100644
--- a/lld/COFF/DriverUtils.cpp
+++ b/lld/COFF/DriverUtils.cpp
@@ -415,7 +415,7 @@ std::string LinkerDriver::createDefaultXml() {
        << "  </dependency>\n";
   }
   os << "</assembly>\n";
-  return os.str();
+  return ret;
 }
 
 std::string

diff  --git a/lld/COFF/SymbolTable.cpp b/lld/COFF/SymbolTable.cpp
index 582a856213cbc8..fa40335c26943e 100644
--- a/lld/COFF/SymbolTable.cpp
+++ b/lld/COFF/SymbolTable.cpp
@@ -275,7 +275,7 @@ static void reportUndefinedSymbol(const COFFLinkerContext &ctx,
   }
   if (numDisplayedRefs < numRefs)
     os << "\n>>> referenced " << numRefs - numDisplayedRefs << " more times";
-  errorOrWarn(os.str(), ctx.config.forceUnresolved);
+  errorOrWarn(out, ctx.config.forceUnresolved);
 }
 
 void SymbolTable::loadMinGWSymbols() {
@@ -667,7 +667,7 @@ static std::string getSourceLocationObj(ObjFile *file, SectionChunk *sc,
   if (fileLine)
     os << fileLine->first << ":" << fileLine->second << "\n>>>            ";
   os << toString(file);
-  return os.str();
+  return res;
 }
 
 static std::string getSourceLocation(InputFile *file, SectionChunk *sc,
@@ -706,9 +706,9 @@ void SymbolTable::reportDuplicate(Symbol *existing, InputFile *newFile,
                           existing->getName());
 
   if (ctx.config.forceMultiple)
-    warn(os.str());
+    warn(msg);
   else
-    error(os.str());
+    error(msg);
 }
 
 Symbol *SymbolTable::addAbsolute(StringRef n, COFFSymbolRef sym) {

diff  --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 37460a7a6c8eb4..34d9964407a5f8 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -1216,7 +1216,6 @@ static void parseClangOption(StringRef opt, const Twine &msg) {
   const char *argv[] = {config->progName.data(), opt.data()};
   if (cl::ParseCommandLineOptions(2, argv, "", &os))
     return;
-  os.flush();
   error(msg + ": " + StringRef(err).trim());
 }
 

diff  --git a/lld/ELF/Thunks.cpp b/lld/ELF/Thunks.cpp
index fe83c086d84322..1b814039cf10da 100644
--- a/lld/ELF/Thunks.cpp
+++ b/lld/ELF/Thunks.cpp
@@ -1088,7 +1088,7 @@ void PPC32PltCallStub::addSymbols(ThunkSection &isec) {
   else
     os << ".plt_pic32.";
   os << destination.getName();
-  addSymbol(saver().save(os.str()), STT_FUNC, 0, isec);
+  addSymbol(saver().save(buf), STT_FUNC, 0, isec);
 }
 
 bool PPC32PltCallStub::isCompatibleWith(const InputSection &isec,

diff  --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 0165253551714c..bb0ae5f073ce5c 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -2772,7 +2772,7 @@ template <class ELFT> void Writer<ELFT>::openFile() {
       << "section sizes:\n";
     for (OutputSection *os : ctx.outputSections)
       s << os->name << ' ' << os->size << "\n";
-    error(s.str());
+    error(msg);
     return;
   }
 

diff  --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index 73b83123f49b4d..9efb5bcf01d04b 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -949,7 +949,6 @@ static void parseClangOption(StringRef opt, const Twine &msg) {
   const char *argv[] = {"lld", opt.data()};
   if (cl::ParseCommandLineOptions(2, argv, "", &os))
     return;
-  os.flush();
   error(msg + ": " + StringRef(err).trim());
 }
 

diff  --git a/lld/wasm/OutputSections.cpp b/lld/wasm/OutputSections.cpp
index 3974146ab7270b..b0b2446cb56bfc 100644
--- a/lld/wasm/OutputSections.cpp
+++ b/lld/wasm/OutputSections.cpp
@@ -42,7 +42,6 @@ void OutputSection::createHeader(size_t bodySize) {
   debugWrite(os.tell(), "section type [" + getSectionName() + "]");
   encodeULEB128(type, os);
   writeUleb128(os, bodySize, "section size");
-  os.flush();
   log("createHeader: " + toString(*this) + " body=" + Twine(bodySize) +
       " total=" + Twine(getSize()));
 }
@@ -50,7 +49,6 @@ void OutputSection::createHeader(size_t bodySize) {
 void CodeSection::finalizeContents() {
   raw_string_ostream os(codeSectionHeader);
   writeUleb128(os, functions.size(), "function count");
-  os.flush();
   bodySize = codeSectionHeader.size();
 
   for (InputFunction *func : functions) {
@@ -112,7 +110,6 @@ void DataSection::finalizeContents() {
          "output segments should have been combined by now");
 
   writeUleb128(os, segmentCount, "data segment count");
-  os.flush();
   bodySize = dataSectionHeader.size();
   bool is64 = config->is64.value_or(false);
 
@@ -147,7 +144,6 @@ void DataSection::finalizeContents() {
       }
     }
     writeUleb128(os, segment->size, "segment size");
-    os.flush();
 
     segment->sectionOffset = bodySize;
     bodySize += segment->header.size() + segment->size;
@@ -245,7 +241,6 @@ void CustomSection::finalizeContents() {
   raw_string_ostream os(nameData);
   encodeULEB128(name.size(), os);
   os << name;
-  os.flush();
 
   for (InputChunk *section : inputSections) {
     assert(!section->discarded);

diff  --git a/lld/wasm/SyntheticSections.cpp b/lld/wasm/SyntheticSections.cpp
index 72d08b849d8e86..a3bc90cfe759ca 100644
--- a/lld/wasm/SyntheticSections.cpp
+++ b/lld/wasm/SyntheticSections.cpp
@@ -38,7 +38,6 @@ class SubSection {
   explicit SubSection(uint32_t type) : type(type) {}
 
   void writeTo(raw_ostream &to) {
-    os.flush();
     writeUleb128(to, type, "subsection type");
     writeUleb128(to, body.size(), "subsection size");
     to.write(body.data(), body.size());

diff  --git a/lld/wasm/SyntheticSections.h b/lld/wasm/SyntheticSections.h
index 11479962956aaa..10183e93d2a28d 100644
--- a/lld/wasm/SyntheticSections.h
+++ b/lld/wasm/SyntheticSections.h
@@ -56,7 +56,6 @@ class SyntheticSection : public OutputSection {
 
   void finalizeContents() override {
     writeBody();
-    bodyOutputStream.flush();
     createHeader(body.size());
   }
 

diff  --git a/lld/wasm/Writer.cpp b/lld/wasm/Writer.cpp
index 6beef81d39a381..681f6a137ceac4 100644
--- a/lld/wasm/Writer.cpp
+++ b/lld/wasm/Writer.cpp
@@ -1875,7 +1875,6 @@ void Writer::createHeader() {
   raw_string_ostream os(header);
   writeBytes(os, WasmMagic, sizeof(WasmMagic), "wasm magic");
   writeU32(os, WasmVersion, "wasm version");
-  os.flush();
   fileSize += header.size();
 }
 


        


More information about the llvm-commits mailing list