Skip to content

Commit 0856064

Browse files
committed
[clang][StaticAnalyzer] 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()`.
1 parent 7c94a22 commit 0856064

File tree

5 files changed

+18
-18
lines changed

5 files changed

+18
-18
lines changed

clang/lib/StaticAnalyzer/Checkers/CheckObjCDealloc.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,8 @@ void ObjCDeallocChecker::checkASTDecl(const ObjCImplementationDecl *D,
247247
PathDiagnosticLocation DLoc =
248248
PathDiagnosticLocation::createBegin(D, BR.getSourceManager());
249249

250-
BR.EmitBasicReport(D, this, Name, categories::CoreFoundationObjectiveC,
251-
OS.str(), DLoc);
250+
BR.EmitBasicReport(D, this, Name, categories::CoreFoundationObjectiveC, Buf,
251+
DLoc);
252252
return;
253253
}
254254
}
@@ -585,7 +585,7 @@ void ObjCDeallocChecker::diagnoseMissingReleases(CheckerContext &C) const {
585585
" before '[super dealloc]'";
586586

587587
auto BR = std::make_unique<PathSensitiveBugReport>(MissingReleaseBugType,
588-
OS.str(), ErrNode);
588+
Buf, ErrNode);
589589
C.emitReport(std::move(BR));
590590
}
591591

@@ -706,8 +706,8 @@ bool ObjCDeallocChecker::diagnoseExtraRelease(SymbolRef ReleasedValue,
706706
OS << " property but was released in 'dealloc'";
707707
}
708708

709-
auto BR = std::make_unique<PathSensitiveBugReport>(ExtraReleaseBugType,
710-
OS.str(), ErrNode);
709+
auto BR = std::make_unique<PathSensitiveBugReport>(ExtraReleaseBugType, Buf,
710+
ErrNode);
711711
BR->addRange(M.getOriginExpr()->getSourceRange());
712712

713713
C.emitReport(std::move(BR));
@@ -749,7 +749,7 @@ bool ObjCDeallocChecker::diagnoseMistakenDealloc(SymbolRef DeallocedValue,
749749
<< "' should be released rather than deallocated";
750750

751751
auto BR = std::make_unique<PathSensitiveBugReport>(MistakenDeallocBugType,
752-
OS.str(), ErrNode);
752+
Buf, ErrNode);
753753
BR->addRange(M.getOriginExpr()->getSourceRange());
754754

755755
C.emitReport(std::move(BR));

clang/lib/StaticAnalyzer/Checkers/RetainCountChecker/RetainCountDiagnostics.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -411,11 +411,11 @@ annotateConsumedSummaryMismatch(const ExplodedNode *N,
411411
}
412412
}
413413

414-
if (os.str().empty())
414+
if (sbuf.empty())
415415
return nullptr;
416416

417417
PathDiagnosticLocation L = PathDiagnosticLocation::create(CallExitLoc, SM);
418-
return std::make_shared<PathDiagnosticEventPiece>(L, os.str());
418+
return std::make_shared<PathDiagnosticEventPiece>(L, sbuf);
419419
}
420420

421421
/// Annotate the parameter at the analysis entry point.
@@ -446,7 +446,7 @@ annotateStartParameter(const ExplodedNode *N, SymbolRef Sym,
446446
assert(CurrT->getCount() == 0);
447447
os << "0";
448448
}
449-
return std::make_shared<PathDiagnosticEventPiece>(L, os.str());
449+
return std::make_shared<PathDiagnosticEventPiece>(L, s);
450450
}
451451

452452
PathDiagnosticPieceRef
@@ -493,7 +493,7 @@ RefCountReportVisitor::VisitNode(const ExplodedNode *N, BugReporterContext &BRC,
493493
if (PrevT && IsFreeUnowned && CurrV.isNotOwned() && PrevT->isOwned()) {
494494
os << "Object is now not exclusively owned";
495495
auto Pos = PathDiagnosticLocation::create(N->getLocation(), SM);
496-
return std::make_shared<PathDiagnosticEventPiece>(Pos, os.str());
496+
return std::make_shared<PathDiagnosticEventPiece>(Pos, sbuf);
497497
}
498498

499499
// This is the allocation site since the previous node had no bindings
@@ -535,7 +535,7 @@ RefCountReportVisitor::VisitNode(const ExplodedNode *N, BugReporterContext &BRC,
535535
}
536536

537537
PathDiagnosticLocation Pos(S, SM, N->getLocationContext());
538-
return std::make_shared<PathDiagnosticEventPiece>(Pos, os.str());
538+
return std::make_shared<PathDiagnosticEventPiece>(Pos, sbuf);
539539
}
540540

541541
// Gather up the effects that were performed on the object at this
@@ -582,13 +582,13 @@ RefCountReportVisitor::VisitNode(const ExplodedNode *N, BugReporterContext &BRC,
582582
if (!shouldGenerateNote(os, PrevT, CurrV, DeallocSent))
583583
return nullptr;
584584

585-
if (os.str().empty())
585+
if (sbuf.empty())
586586
return nullptr; // We have nothing to say!
587587

588588
const Stmt *S = N->getLocation().castAs<StmtPoint>().getStmt();
589589
PathDiagnosticLocation Pos(S, BRC.getSourceManager(),
590590
N->getLocationContext());
591-
auto P = std::make_shared<PathDiagnosticEventPiece>(Pos, os.str());
591+
auto P = std::make_shared<PathDiagnosticEventPiece>(Pos, sbuf);
592592

593593
// Add the range by scanning the children of the statement for any bindings
594594
// to Sym.
@@ -831,7 +831,7 @@ RefLeakReportVisitor::getEndPath(BugReporterContext &BRC,
831831
<< RV->getCount();
832832
}
833833

834-
return std::make_shared<PathDiagnosticEventPiece>(L, os.str());
834+
return std::make_shared<PathDiagnosticEventPiece>(L, sbuf);
835835
}
836836

837837
RefCountReport::RefCountReport(const RefCountBug &D, const LangOptions &LOpts,

clang/lib/StaticAnalyzer/Core/ExprEngine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3900,7 +3900,7 @@ struct DOTGraphTraits<ExplodedGraph*> : public DefaultDOTGraphTraits {
39003900
State->printDOT(Out, N->getLocationContext(), Space);
39013901

39023902
Out << "\\l}\\l";
3903-
return Out.str();
3903+
return Buf;
39043904
}
39053905
};
39063906

clang/lib/StaticAnalyzer/Core/RangeConstraintManager.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3283,7 +3283,7 @@ static std::string toString(const SymbolRef &Sym) {
32833283
std::string S;
32843284
llvm::raw_string_ostream O(S);
32853285
Sym->dumpToStream(O);
3286-
return O.str();
3286+
return S;
32873287
}
32883288

32893289
void RangeConstraintManager::printConstraints(raw_ostream &Out,
@@ -3354,7 +3354,7 @@ static std::string toString(ProgramStateRef State, EquivalenceClass Class) {
33543354
Out << "\"" << ClassMember << "\"";
33553355
}
33563356
Out << " ]";
3357-
return Out.str();
3357+
return Str;
33583358
}
33593359

33603360
void RangeConstraintManager::printEquivalenceClasses(raw_ostream &Out,

clang/lib/StaticAnalyzer/Core/SVals.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ void SVal::printJson(raw_ostream &Out, bool AddQuotes) const {
271271

272272
dumpToStream(TempOut);
273273

274-
Out << JsonFormat(TempOut.str(), AddQuotes);
274+
Out << JsonFormat(Buf, AddQuotes);
275275
}
276276

277277
void SVal::dumpToStream(raw_ostream &os) const {

0 commit comments

Comments
 (0)