Skip to content

Commit d5dd7d2

Browse files
committed
[flang] Tidy uses of raw_string_ostream (NFC)
As specified in the docs, 1) raw_string_ostream is always unbuffered and 2) the underlying buffer may be used directly ( 65b1361 for further reference ) Avoid unneeded calls to raw_string_ostream::str(), to avoid excess indirection.
1 parent 6497283 commit d5dd7d2

File tree

11 files changed

+21
-24
lines changed

11 files changed

+21
-24
lines changed

flang/include/flang/Parser/dump-parse-tree.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ class ParseTreeDumper {
875875
ss << x;
876876
}
877877
if (ss.tell()) {
878-
return ss.str();
878+
return buf;
879879
}
880880
if constexpr (std::is_same_v<T, Name>) {
881881
return x.source.ToString();

flang/lib/Evaluate/formatting.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ std::string ExpressionBase<RESULT>::AsFortran() const {
563563
std::string buf;
564564
llvm::raw_string_ostream ss{buf};
565565
AsFortran(ss);
566-
return ss.str();
566+
return buf;
567567
}
568568

569569
template <typename RESULT>
@@ -604,7 +604,7 @@ static std::string DerivedTypeSpecAsFortran(
604604
if (ch != '(') {
605605
ss << ')';
606606
}
607-
return ss.str();
607+
return buf;
608608
}
609609

610610
llvm::raw_ostream &StructureConstructor::AsFortran(llvm::raw_ostream &o) const {

flang/lib/Parser/prescan.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,7 @@ void Prescanner::FortranInclude(const char *firstQuote) {
10981098
const SourceFile *included{
10991099
allSources_.Open(path, error, std::move(prependPath))};
11001100
if (!included) {
1101-
Say(provenance, "INCLUDE: %s"_err_en_US, error.str());
1101+
Say(provenance, "INCLUDE: %s"_err_en_US, buf);
11021102
} else if (included->bytes() > 0) {
11031103
ProvenanceRange includeLineRange{
11041104
provenance, static_cast<std::size_t>(p - nextLine_)};

flang/lib/Semantics/expression.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3223,7 +3223,7 @@ void ExpressionAnalyzer::Analyze(const parser::CallStmt &callStmt) {
32233223
llvm::raw_string_ostream dump{buf};
32243224
parser::DumpTree(dump, callStmt);
32253225
Say("Internal error: Expression analysis failed on CALL statement: %s"_err_en_US,
3226-
dump.str());
3226+
buf);
32273227
}
32283228
}
32293229
}
@@ -3847,8 +3847,7 @@ MaybeExpr ExpressionAnalyzer::ExprOrVariable(
38473847
std::string buf;
38483848
llvm::raw_string_ostream dump{buf};
38493849
parser::DumpTree(dump, x);
3850-
Say("Internal error: Expression analysis failed on: %s"_err_en_US,
3851-
dump.str());
3850+
Say("Internal error: Expression analysis failed on: %s"_err_en_US, buf);
38523851
}
38533852
return std::nullopt;
38543853
}

flang/lib/Semantics/pointer-assignment.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ bool PointerAssignmentChecker::Check(const evaluate::Designator<T> &d) {
353353
std::string buf;
354354
llvm::raw_string_ostream ss{buf};
355355
d.AsFortran(ss);
356-
Say(*m, description_, ss.str());
356+
Say(*m, description_, buf);
357357
} else {
358358
Say(std::get<MessageFormattedText>(*msg));
359359
}

flang/lib/Semantics/scope.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ std::string EquivalenceObject::AsFortran() const {
4747
if (substringStart) {
4848
ss << '(' << *substringStart << ":)";
4949
}
50-
return ss.str();
50+
return buf;
5151
}
5252

5353
Scope &Scope::MakeScope(Kind kind, Symbol *symbol) {

flang/lib/Semantics/tools.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ static void CheckMissingAnalysis(
440440
llvm::raw_string_ostream ss{buf};
441441
ss << "node has not been analyzed:\n";
442442
parser::DumpTree(ss, x);
443-
common::die(ss.str().c_str());
443+
common::die(buf.c_str());
444444
}
445445
}
446446

flang/lib/Semantics/type.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ std::string DerivedTypeSpec::VectorTypeAsFortran() const {
658658
case (Fortran::semantics::DerivedTypeSpec::Category::DerivedType):
659659
Fortran::common::die("Vector element type not implemented");
660660
}
661-
return ss.str();
661+
return buf;
662662
}
663663

664664
std::string DerivedTypeSpec::AsFortran() const {
@@ -694,7 +694,7 @@ std::string DerivedTypeSpec::AsFortran() const {
694694
}
695695
ss << ')';
696696
}
697-
return ss.str();
697+
return buf;
698698
}
699699

700700
llvm::raw_ostream &operator<<(llvm::raw_ostream &o, const DerivedTypeSpec &x) {
@@ -771,7 +771,7 @@ std::string ParamValue::AsFortran() const {
771771
std::string buf;
772772
llvm::raw_string_ostream ss{buf};
773773
expr_->AsFortran(ss);
774-
return ss.str();
774+
return buf;
775775
} else {
776776
return "";
777777
}
@@ -795,7 +795,7 @@ static std::string KindAsFortran(const KindExpr &kind) {
795795
} else {
796796
kind.AsFortran(ss);
797797
}
798-
return ss.str();
798+
return buf;
799799
}
800800

801801
std::string IntrinsicTypeSpec::AsFortran() const {

flang/unittests/Evaluate/real.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,9 @@ template <typename R> void basicTests(int rm, Rounding rounding) {
158158
TEST(ivf.flags.empty())(ldesc);
159159
MATCH(x, ivf.value.ToUInt64())(ldesc);
160160
if (rounding.mode == RoundingMode::TiesToEven) { // to match stold()
161-
std::string buf;
162-
llvm::raw_string_ostream ss{buf};
161+
std::string decimal;
162+
llvm::raw_string_ostream ss{decimal};
163163
vr.value.AsFortran(ss, kind, false /*exact*/);
164-
std::string decimal{ss.str()};
165164
const char *p{decimal.data()};
166165
MATCH(x, static_cast<std::uint64_t>(std::stold(decimal)))
167166
("%s %s", ldesc, p);
@@ -424,22 +423,21 @@ void subsetTests(int pass, Rounding rounding, std::uint32_t opds) {
424423
("%d IsInfinite(0x%jx)", pass, static_cast<std::intmax_t>(rj));
425424

426425
static constexpr int kind{REAL::bits / 8};
427-
std::string ssBuf, cssBuf;
428-
llvm::raw_string_ostream ss{ssBuf};
426+
std::string s, cssBuf;
427+
llvm::raw_string_ostream ss{s};
429428
llvm::raw_string_ostream css{cssBuf};
430429
x.AsFortran(ss, kind, false /*exact*/);
431-
std::string s{ss.str()};
432430
if (IsNaN(rj)) {
433431
css << "(0._" << kind << "/0.)";
434-
MATCH(css.str(), s)
432+
MATCH(cssBuf, s)
435433
("%d invalid(0x%jx)", pass, static_cast<std::intmax_t>(rj));
436434
} else if (IsInfinite(rj)) {
437435
css << '(';
438436
if (IsNegative(rj)) {
439437
css << '-';
440438
}
441439
css << "1._" << kind << "/0.)";
442-
MATCH(css.str(), s)
440+
MATCH(cssBuf, s)
443441
("%d overflow(0x%jx)", pass, static_cast<std::intmax_t>(rj));
444442
} else {
445443
const char *p = s.data();

flang/unittests/Frontend/CodeGenActionTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ TEST(CodeGenAction, GracefullyHandleLLVMConversionFailure) {
103103
action.setCurrentInput(file);
104104

105105
consumeError(action.execute());
106-
ASSERT_EQ(diagnosticsOS.str(),
106+
ASSERT_EQ(diagnosticOutput,
107107
"error: Lowering to LLVM IR failed\n"
108108
"error: failed to create the LLVM module\n");
109109
}

flang/unittests/Frontend/CompilerInstanceTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,6 @@ TEST(CompilerInstance, AllowDiagnosticLogWithUnownedDiagnosticConsumer) {
9090

9191
// 6. Verify that the reported diagnostic wasn't lost and did end up in the
9292
// output stream
93-
ASSERT_EQ(diagnosticsOS.str(), "error: expected no crash\n");
93+
ASSERT_EQ(diagnosticOutput, "error: expected no crash\n");
9494
}
9595
} // namespace

0 commit comments

Comments
 (0)