[llvm] 82f9a5b - [llvm] Avoid 'raw_string_ostream::str' (NFC)

Youngsuk Kim via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 3 04:38:03 PDT 2024


Author: Youngsuk Kim
Date: 2024-07-03T06:37:48-05:00
New Revision: 82f9a5ba965dc1a40fe955b8205dd863bf6385cf

URL: https://github.com/llvm/llvm-project/commit/82f9a5ba965dc1a40fe955b8205dd863bf6385cf
DIFF: https://github.com/llvm/llvm-project/commit/82f9a5ba965dc1a40fe955b8205dd863bf6385cf.diff

LOG: [llvm] Avoid 'raw_string_ostream::str' (NFC)

Since `raw_string_ostream` doesn't own the string buffer, it is
desirable (in terms of memory safety) for users to directly reference
the string buffer rather than use `raw_string_ostream::str()`.

Work towards TODO comment to remove `raw_string_ostream::str()`.

Added: 
    

Modified: 
    llvm/lib/MC/MCParser/AsmParser.cpp
    llvm/lib/MC/MCParser/MasmParser.cpp
    llvm/lib/Support/JSON.cpp
    llvm/lib/Support/LockFileManager.cpp
    llvm/lib/TableGen/Main.cpp
    llvm/lib/TableGen/SetTheory.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp
index 72cf59f7f583b..13879220a25e7 100644
--- a/llvm/lib/MC/MCParser/AsmParser.cpp
+++ b/llvm/lib/MC/MCParser/AsmParser.cpp
@@ -6238,7 +6238,7 @@ bool AsmParser::parseMSInlineAsm(
   if (AsmStart != AsmEnd)
     OS << StringRef(AsmStart, AsmEnd - AsmStart);
 
-  AsmString = OS.str();
+  AsmString = AsmStringIR;
   return false;
 }
 

diff  --git a/llvm/lib/MC/MCParser/MasmParser.cpp b/llvm/lib/MC/MCParser/MasmParser.cpp
index 67f35409244f0..653cc64b4c36a 100644
--- a/llvm/lib/MC/MCParser/MasmParser.cpp
+++ b/llvm/lib/MC/MCParser/MasmParser.cpp
@@ -2843,7 +2843,7 @@ bool MasmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
     raw_string_ostream LocalName(Name);
     LocalName << "??"
               << format_hex_no_prefix(LocalCounter++, 4, /*Upper=*/true);
-    LocalSymbols.insert({Local, LocalName.str()});
+    LocalSymbols.insert({Local, Name});
     Name.clear();
   }
 

diff  --git a/llvm/lib/Support/JSON.cpp b/llvm/lib/Support/JSON.cpp
index 17779b58f81b7..a5c617bb4a076 100644
--- a/llvm/lib/Support/JSON.cpp
+++ b/llvm/lib/Support/JSON.cpp
@@ -239,7 +239,7 @@ Error Path::Root::getError() const {
         OS << '[' << S.index() << ']';
     }
   }
-  return createStringError(llvm::inconvertibleErrorCode(), OS.str());
+  return createStringError(llvm::inconvertibleErrorCode(), S);
 }
 
 std::vector<const Object::value_type *> sortedElements(const Object &O) {

diff  --git a/llvm/lib/Support/LockFileManager.cpp b/llvm/lib/Support/LockFileManager.cpp
index 3169aa25ec0d9..ea040ccf22b99 100644
--- a/llvm/lib/Support/LockFileManager.cpp
+++ b/llvm/lib/Support/LockFileManager.cpp
@@ -228,7 +228,7 @@ LockFileManager::LockFileManager(StringRef FileName)
       std::string S("failed to create link ");
       raw_string_ostream OSS(S);
       OSS << LockFileName.str() << " to " << UniqueLockFileName.str();
-      setError(EC, OSS.str());
+      setError(EC, S);
       return;
     }
 
@@ -274,7 +274,7 @@ std::string LockFileManager::getErrorMessage() const {
     raw_string_ostream OSS(Str);
     if (!ErrCodeMsg.empty())
       OSS << ": " << ErrCodeMsg;
-    return OSS.str();
+    return Str;
   }
   return "";
 }

diff  --git a/llvm/lib/TableGen/Main.cpp b/llvm/lib/TableGen/Main.cpp
index c6e2e6c89fd2a..841fa7c3f3690 100644
--- a/llvm/lib/TableGen/Main.cpp
+++ b/llvm/lib/TableGen/Main.cpp
@@ -159,7 +159,7 @@ int llvm::TableGenMain(const char *argv0,
     // aren't any.
     if (auto ExistingOrErr =
             MemoryBuffer::getFile(OutputFilename, /*IsText=*/true))
-      if (std::move(ExistingOrErr.get())->getBuffer() == Out.str())
+      if (std::move(ExistingOrErr.get())->getBuffer() == OutString)
         WriteFile = false;
   }
   if (WriteFile) {
@@ -168,7 +168,7 @@ int llvm::TableGenMain(const char *argv0,
     if (EC)
       return reportError(argv0, "error opening " + OutputFilename + ": " +
                                     EC.message() + "\n");
-    OutFile.os() << Out.str();
+    OutFile.os() << OutString;
     if (ErrorsPrinted == 0)
       OutFile.keep();
   }

diff  --git a/llvm/lib/TableGen/SetTheory.cpp b/llvm/lib/TableGen/SetTheory.cpp
index 3db46aae6d967..f4e3e3d4ce473 100644
--- a/llvm/lib/TableGen/SetTheory.cpp
+++ b/llvm/lib/TableGen/SetTheory.cpp
@@ -221,7 +221,7 @@ struct SequenceOp : public SetTheory::Operator {
       std::string Name;
       raw_string_ostream OS(Name);
       OS << format(Format.c_str(), unsigned(From));
-      Record *Rec = Records.getDef(OS.str());
+      Record *Rec = Records.getDef(Name);
       if (!Rec)
         PrintFatalError(Loc, "No def named '" + Name + "': " +
           Expr->getAsString());


        


More information about the llvm-commits mailing list