Skip to content

[llvm][utils] Avoid 'raw_string_ostream::str()' (NFC) #97160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 29, 2024

Conversation

JOE1994
Copy link
Member

@JOE1994 JOE1994 commented Jun 29, 2024

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().

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()`.
@llvmbot
Copy link
Member

llvmbot commented Jun 29, 2024

@llvm/pr-subscribers-testing-tools

Author: Youngsuk Kim (JOE1994)

Changes

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().


Full diff: https://github.com/llvm/llvm-project/pull/97160.diff

4 Files Affected:

  • (modified) llvm/utils/FileCheck/FileCheck.cpp (+2-2)
  • (modified) llvm/utils/TableGen/AsmMatcherEmitter.cpp (+2-2)
  • (modified) llvm/utils/TableGen/AsmWriterEmitter.cpp (+6-6)
  • (modified) llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp (+1-1)
diff --git a/llvm/utils/FileCheck/FileCheck.cpp b/llvm/utils/FileCheck/FileCheck.cpp
index a96251867c084..9cf3a3164dfec 100644
--- a/llvm/utils/FileCheck/FileCheck.cpp
+++ b/llvm/utils/FileCheck/FileCheck.cpp
@@ -641,7 +641,7 @@ static void DumpAnnotatedInput(raw_ostream &OS, const FileCheckRequest &Req,
       LineOS = &ElidedLinesOS;
     else {
       LineOS = &OS;
-      DumpEllipsisOrElidedLines(OS, ElidedLinesOS.str(), LabelWidth);
+      DumpEllipsisOrElidedLines(OS, ElidedLines, LabelWidth);
     }
 
     // Print right-aligned line number.
@@ -723,7 +723,7 @@ static void DumpAnnotatedInput(raw_ostream &OS, const FileCheckRequest &Req,
       ++AnnotationItr;
     }
   }
-  DumpEllipsisOrElidedLines(OS, ElidedLinesOS.str(), LabelWidth);
+  DumpEllipsisOrElidedLines(OS, ElidedLines, LabelWidth);
 
   OS << ">>>>>>\n";
 }
diff --git a/llvm/utils/TableGen/AsmMatcherEmitter.cpp b/llvm/utils/TableGen/AsmMatcherEmitter.cpp
index 29f15f0e57333..5035ef52707f4 100644
--- a/llvm/utils/TableGen/AsmMatcherEmitter.cpp
+++ b/llvm/utils/TableGen/AsmMatcherEmitter.cpp
@@ -2367,10 +2367,10 @@ emitConvertFuncs(CodeGenTarget &Target, StringRef ClassName,
   OS << "};\n\n";
 
   // Spit out the conversion driver function.
-  OS << CvtOS.str();
+  OS << ConvertFnBody;
 
   // Spit out the operand number lookup function.
-  OS << OpOS.str();
+  OS << OperandFnBody;
 
   return ConversionTable.size();
 }
diff --git a/llvm/utils/TableGen/AsmWriterEmitter.cpp b/llvm/utils/TableGen/AsmWriterEmitter.cpp
index 16661cd29edc5..79a1c245f0fd9 100644
--- a/llvm/utils/TableGen/AsmWriterEmitter.cpp
+++ b/llvm/utils/TableGen/AsmWriterEmitter.cpp
@@ -1162,8 +1162,8 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
                        PatternCount - PatternStart);
   }
 
-  if (OpcodeO.str().empty()) {
-    O << HeaderO.str();
+  if (PatternsForOpcode.empty()) {
+    O << Header;
     O << "  return false;\n";
     O << "}\n\n";
     O << "#endif // PRINT_ALIAS_INSTR\n";
@@ -1177,15 +1177,15 @@ void AsmWriterEmitter::EmitPrintAliasInstruction(raw_ostream &O) {
       << "                  const MCSubtargetInfo &STI,\n"
       << "                  unsigned PredicateIndex);\n";
 
-  O << HeaderO.str();
+  O << Header;
   O.indent(2) << "static const PatternsForOpcode OpToPatterns[] = {\n";
-  O << OpcodeO.str();
+  O << PatternsForOpcode;
   O.indent(2) << "};\n\n";
   O.indent(2) << "static const AliasPattern Patterns[] = {\n";
-  O << PatternO.str();
+  O << Patterns;
   O.indent(2) << "};\n\n";
   O.indent(2) << "static const AliasPatternCond Conds[] = {\n";
-  O << CondO.str();
+  O << Conds;
   O.indent(2) << "};\n\n";
   O.indent(2) << "static const char AsmStrings[] =\n";
   for (const auto &P : AsmStrings) {
diff --git a/llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp b/llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp
index 4263d8f41715f..e633dc36e2343 100644
--- a/llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp
+++ b/llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp
@@ -405,7 +405,7 @@ void VarLenCodeEmitterGen::emitInstructionBaseValues(
   IS.indent(4) << "{/*NumBits*/0, /*Index*/0}\n  };\n";
   SS.indent(4) << "UINT64_C(0)\n  };\n";
 
-  OS << IS.str() << SS.str();
+  OS << IndexArray << StorageArray;
 }
 
 std::string VarLenCodeEmitterGen::getInstructionCases(Record *R,

@JOE1994
Copy link
Member Author

JOE1994 commented Jun 29, 2024

Below is the TODO comment I'm referring to:

/// Returns the string's reference. In most cases it is better to simply use
/// the underlying std::string directly.
/// TODO: Consider removing this API.
std::string &str() { return OS; }

@JOE1994 JOE1994 merged commit caf26b9 into llvm:main Jun 29, 2024
7 of 9 checks passed
@JOE1994 JOE1994 deleted the move_away_from_str branch June 29, 2024 22:50
lravenclaw pushed a commit to lravenclaw/llvm-project that referenced this pull request Jul 3, 2024
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()`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants