Skip to content

Commit 18c7d14

Browse files
JOE1994lravenclaw
authored andcommitted
[llvm] Avoid 'raw_string_ostream::str()' (NFC) (llvm#96995)
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()`.
1 parent c18bb59 commit 18c7d14

File tree

6 files changed

+13
-15
lines changed

6 files changed

+13
-15
lines changed

llvm/include/llvm/ADT/FloatingPointMode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ struct DenormalMode {
176176
std::string storage;
177177
raw_string_ostream OS(storage);
178178
print(OS);
179-
return OS.str();
179+
return storage;
180180
}
181181
};
182182

llvm/include/llvm/Analysis/CFGPrinter.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ std::string SimpleNodeLabelString(const BasicBlockT *Node) {
133133
raw_string_ostream OS(Str);
134134

135135
Node->printAsOperand(OS, false);
136-
return OS.str();
136+
return Str;
137137
}
138138

139139
template <typename BasicBlockT>
@@ -145,10 +145,9 @@ std::string CompleteNodeLabelString(
145145
HandleComment) {
146146

147147
enum { MaxColumns = 80 };
148-
std::string Str;
149-
raw_string_ostream OS(Str);
148+
std::string OutStr;
149+
raw_string_ostream OS(OutStr);
150150
HandleBasicBlock(OS, *Node);
151-
std::string OutStr = OS.str();
152151
// Remove "%" from BB name
153152
if (OutStr[0] == '%') {
154153
OutStr.erase(OutStr.begin());
@@ -247,7 +246,7 @@ struct DOTGraphTraits<DOTFuncInfo *> : public DefaultDOTGraphTraits {
247246
raw_string_ostream OS(Str);
248247
auto Case = *SwitchInst::ConstCaseIt::fromSuccessorIndex(SI, SuccNo);
249248
OS << Case.getCaseValue()->getValue();
250-
return OS.str();
249+
return Str;
251250
}
252251
return "";
253252
}
@@ -257,7 +256,6 @@ struct DOTGraphTraits<DOTFuncInfo *> : public DefaultDOTGraphTraits {
257256
if (NodeName.empty()) {
258257
raw_string_ostream NodeOS(NodeName);
259258
Node->printAsOperand(NodeOS, false);
260-
NodeName = NodeOS.str();
261259
// Removing %
262260
NodeName.erase(NodeName.begin());
263261
}

llvm/include/llvm/Analysis/DDG.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -467,16 +467,16 @@ DependenceGraphInfo<NodeType>::getDependenceString(const NodeType &Src,
467467
raw_string_ostream OS(Str);
468468
DependenceList Deps;
469469
if (!getDependencies(Src, Dst, Deps))
470-
return OS.str();
470+
return Str;
471471
interleaveComma(Deps, OS, [&](const std::unique_ptr<Dependence> &D) {
472472
D->dump(OS);
473473
// Remove the extra new-line character printed by the dump
474474
// method
475-
if (OS.str().back() == '\n')
476-
OS.str().pop_back();
475+
if (Str.back() == '\n')
476+
Str.pop_back();
477477
});
478478

479-
return OS.str();
479+
return Str;
480480
}
481481

482482
//===--------------------------------------------------------------------===//

llvm/include/llvm/DebugInfo/LogicalView/Core/LVSupport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ inline std::string hexString(uint64_t Value, size_t Width = HEX_WIDTH) {
110110
std::string String;
111111
raw_string_ostream Stream(String);
112112
Stream << hexValue(Value, Width, false);
113-
return Stream.str();
113+
return String;
114114
}
115115

116116
// Get a hexadecimal string representation for the given value.

llvm/include/llvm/IR/ModuleSummaryIndex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ class FunctionSummary : public GlobalValueSummary {
793793
OS << ", hasUnknownCall: " << this->HasUnknownCall;
794794
OS << ", mustBeUnreachable: " << this->MustBeUnreachable;
795795
OS << ")";
796-
return OS.str();
796+
return Output;
797797
}
798798
};
799799

llvm/include/llvm/Object/MachO.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ class MachOObjectFile : public ObjectFile {
799799
std::string ret;
800800
raw_string_ostream ss(ret);
801801
ss << format_hex(platform, 8, true);
802-
return ss.str();
802+
return ret;
803803
}
804804
}
805805

@@ -814,7 +814,7 @@ class MachOObjectFile : public ObjectFile {
814814
std::string ret;
815815
raw_string_ostream ss(ret);
816816
ss << format_hex(tools, 8, true);
817-
return ss.str();
817+
return ret;
818818
}
819819
}
820820

0 commit comments

Comments
 (0)